Advertisement
hqt

Helper.js

hqt
Jul 31st, 2012
888
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. window.py = {}
  2.  
  3.  
  4. var Helper = {
  5.     extend:function (obj1, obj2) {
  6.         if (!obj1) {
  7.             obj1 = {}
  8.         }
  9.  
  10.         if (obj2) {
  11.             for (var prop in obj2) {
  12.                 obj1[prop] = obj2[prop];
  13.             }
  14.         }
  15.  
  16.         return obj1;
  17.     },
  18.  
  19.     // move to math.js
  20. //        distance:function (p1, p2) {
  21. //            return {x:p2.x - p1.x, y:p2.y - p1.y};
  22. //        },
  23.  
  24.     isFunction:function (object) {
  25.         return object && Object.prototype.toString.call(object) == '[object Function]';
  26.     },
  27.  
  28.     isArray:function (obj) {
  29.         return Object.prototype.toString.call(obj) === '[object Array]'
  30.     },
  31.  
  32.     inArray:function (obj, array) {
  33.         if (!Helper.isArray(array)) {
  34.             return false;
  35.         }
  36.         return array.indexOf(obj) >= 0;
  37.     },
  38.  
  39.     removeFromArray:function (obj, array) {
  40.         var index;
  41.         if (!Helper.isArray(array)) {
  42.             return;
  43.         }
  44.  
  45.         index = array.indexOf(obj);
  46.         return array.splice(index, 1);
  47.     },
  48.  
  49.     foreach:function (array, callback) {
  50.         var length = 0, i = 0;
  51.         if (!Helper.isArray(array) || !Helper.isFunction(callback)) {
  52.             return;
  53.         }
  54.  
  55.         length = array.length;
  56.         for (; i < length; ++i) {
  57.             callback(array[i], i);
  58.         }
  59.     },
  60.  
  61.     // move to math.js
  62. //        addVector:function (a, b) {
  63. //            return {x:a.x + b.x, y:a.y + b.y};
  64. //        },
  65.  
  66.     toProper:function (str, truncate) {
  67.         var temp = (" " + str).toLowerCase().replace(/\s(\S)/g,
  68.             function ($1) {
  69.                 return $1.toUpperCase()
  70.             }).substr(1);
  71.         return truncate ? temp.replace('\s', '') : temp;
  72.     },
  73.  
  74.     isBoolean:function (b) {
  75.         return b === true || b === false;
  76.     },
  77.  
  78.     Logger:(new function () {
  79.  
  80.         var self = this;
  81.         this.debug = true;
  82.         this.showStack = false;
  83.  
  84.         this.log = function () {
  85.             return;
  86.             var stack;
  87.             if (self.debug) {
  88.                 if (self.showStack) {
  89.                     try {
  90.                         throw new Error('stack trace:');
  91.                     } catch (e) {
  92.                         return console.log(arguments, e.stack);
  93.                     }
  94.                 }
  95.                 console.log(arguments);
  96.             }
  97.         }
  98.  
  99.         window.py.log = this.log;
  100.     }),
  101.  
  102.     abstractMethod:function () {
  103.         return function () {
  104.             throw new Error('this method is not implemented');
  105.         };
  106.     },
  107.  
  108.     using:function (ns, code) {
  109.  
  110.         var array, currentContext = window;
  111.  
  112.         if (typeof ns != 'string') {
  113.             throw new Error('namespace must be a string');
  114.         }
  115.  
  116.         array = ns.split('.');
  117.         array.forEach(function (context) {
  118.             var c = currentContext[context];
  119.             if (c) {
  120.                 currentContext = c;
  121.             } else {
  122.                 currentContext = currentContext[context] = {};
  123.             }
  124.         });
  125.  
  126.         if (code && Helper.isFunction(code)) {
  127.             code.call(currentContext, currentContext);
  128.  
  129.         }
  130.  
  131.     },
  132.  
  133.     throttle:function (fn, delay) {
  134.         var timer = null;
  135.         return function () {
  136.             var context = this, args = arguments;
  137.             clearTimeout(timer);
  138.             timer = setTimeout(function () {
  139.                 fn.apply(context, args);
  140.             }, delay);
  141.         };
  142.     },
  143.  
  144.     guid:function () {
  145.         var s = [], itoh = '0123456789ABCDEF';
  146.  
  147.         // Make array of random hex digits. The UUID only has 32 digits in it, but we
  148.         // allocate an extra items to make room for the '-'s we'll be inserting.
  149.         for (var i = 0; i < 36; i++) s[i] = Math.floor(Math.random() * 0x10);
  150.  
  151.         // Conform to RFC-4122, section 4.4
  152.         s[14] = 4;  // Set 4 high bits of time_high field to version
  153.         s[19] = (s[19] & 0x3) | 0x8;  // Specify 2 high bits of clock sequence
  154.  
  155.         // Convert to hex chars
  156.         for (var i = 0; i < 36; i++) s[i] = itoh[s[i]];
  157.  
  158.         // Insert '-'s
  159.         s[8] = s[13] = s[18] = s[23] = '-';
  160.  
  161.         return s.join('');
  162.     }
  163. };
  164.  
  165. Array.prototype.remove = Array.prototype.remove || function (obj) {
  166.     return Helper.removeFromArray.call(this, obj, this);
  167. }
  168.  
  169. Array.prototype.forEach = Array.prototype.forEach || function (callback) {
  170.     return Helper.foreach(this, callback);
  171. }
  172.  
  173. String.prototype.toProperCase = function () {
  174.     return Helper.toProper(this, arguments);
  175. }
  176.  
  177. Function.prototype.throttle = function (delay) {
  178.     return Helper.throttle(this, delay);
  179. }
  180.  
  181. Kinetic.Stage.prototype.getCenter = function () {
  182.     return {
  183.         x:this.getWidth() / 2, y:this.getHeight() / 2
  184.     };
  185. }
  186.  
  187.  
  188. py.logTime = function () {
  189.     var time = new Date().getTime()
  190.     py.log('Time: ' + time);
  191.     return time;
  192. }
  193.  
  194.  
  195. window._ = window._ || Helper;
  196.  
  197.  
  198. //Copyright (c) 2010 Nicholas C. Zakas. All rights reserved.
  199. //MIT License
  200. function EventTarget() {
  201.     this._listeners = {};
  202.     this.on = function (type, listener) {
  203.         if (typeof this._listeners[type] == "undefined") {
  204.             this._listeners[type] = [];
  205.         }
  206.  
  207.         this._listeners[type].push(listener);
  208.         return this;
  209.     };
  210.  
  211.     this.fire = function (event) {
  212.         if (typeof event == "string") {
  213.             event = { type:event };
  214.         }
  215.         if (!event.target) {
  216.             event.target = this;
  217.         }
  218.  
  219.         if (!event.type) {  //falsy
  220.             throw new Error("Event object missing 'type' property.");
  221.         }
  222.  
  223.         if (this._listeners[event.type] instanceof Array) {
  224.             var listeners = this._listeners[event.type];
  225.             for (var i = 0, len = listeners.length; i < len; i++) {
  226.                 listeners[i].call(this, event);
  227.             }
  228.         }
  229.     }
  230.  
  231.     this.off = function (type, listener) {
  232.         if (this._listeners[type] instanceof Array) {
  233.  
  234.             if (listener === true) {
  235.                 this._listeners[type] = [];
  236.                 return;
  237.             }
  238.  
  239.             var listeners = this._listeners[type];
  240.             for (var i = 0, len = listeners.length; i < len; i++) {
  241.                 if (listeners[i] === listener) {
  242.                     listeners.splice(i, 1);
  243.                     break;
  244.                 }
  245.             }
  246.         }
  247.     }
  248. }
  249.  
  250.  
  251. var ObservableArray = function (initArray) {
  252.     var _array = initArray || [];
  253.     EventTarget.call(_array);
  254.  
  255.     _array.push = function () {
  256.         this.fire({type:"pushing", data:arguments });
  257.         Array.prototype.push.apply(_array, arguments);
  258.         this.fire({type:"pushed", data:arguments });
  259.     }
  260.  
  261.     _array.remove = function () {
  262.         this.fire({type:"removing", data:arguments });
  263.         Array.prototype.remove.apply(_array, arguments);
  264.         this.fire({type:"removed", data:arguments });
  265.     }
  266.  
  267.     return _array;
  268. }
  269.  
  270. var Bound = function (params) {
  271.     var n = NaN;
  272.     params = Helper.extend({
  273.         x:n, y:n, w:n, h:n
  274.     }, params);
  275.  
  276.     this.getX = function () {
  277.         return params.x;
  278.     };
  279.     this.getY = function () {
  280.         return params.y;
  281.     };
  282.     this.getW = function () {
  283.         return params.w;
  284.     };
  285.     this.getH = function () {
  286.         return params.h;
  287.     };
  288.     this.getBounds = function () {
  289.         return [
  290.             {x:params.x, y:params.y},
  291.             {x:params.x + params.w, y:params.y + params.h}
  292.         ];
  293.     };
  294. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement