Code Viewer

./Classes/__action_hook.js File Size: 6.67 KB

  1. /**
  2. *      __action_hook.js
  3. *      Desc: Class for easily handling callback functions
  4. */
  5.  
  6. var ActionHook;
  7.  
  8.  (function () {
  9.         /* Private object properties */
  10.         var ActionHooks = {};
  11.        
  12.         /* Private functions */
  13.         //Binds a function to an object - based on Function.bind from Prototype(www.prototypejs.org)
  14.         var BindFunction = function () {
  15.                 var args = toArray(arguments);
  16.                 var func = args.shift();
  17.                 var obj = args.shift();
  18.                 return function () {
  19.                         return func.apply(obj, args.concat(toArray(arguments) ) );
  20.                 }
  21.         }
  22.  
  23.         //Converts any iterable object to an array - based on $A from Prototype(www.prototypejs.org)
  24.         var toArray = function (iterable) {
  25.                 if (!iterable) return[];
  26.                 if (iterable.toArray) return iterable.toArray();
  27.                 var length = iterable.length, results = new Array(length);
  28.                 while (length--) results[length] = iterable[length];
  29.                 return results;
  30.         }
  31.  
  32.         /* ActionHook class */
  33.         ActionHook = function (Id) {
  34.                 /* Private class properties */
  35.                 var Actions = {};
  36.  
  37.                 var Instance = {
  38.                         /* Public class functions */
  39.                         //Adds a function to a specified action
  40.                         Add: function (Action, fCallback, oScope) {
  41.                                 if (typeof Action === "undefined" || typeof fCallback !== "function") return false;
  42.                                 var fFunc = (typeof oScope === "undefined") ? fCallback : BindFunction (fCallback, this);
  43.  
  44.                                 if (typeof Action === "object") {
  45.                                         var Result = true;
  46.                                         if (typeof Action.length === "number") {
  47.                                                 for (var i=0; i<Action.length; i++) {
  48.                                                         Result &= this.Add(Action[i], fFunc);
  49.                                                 }
  50.                                         } else {
  51.                                                 for (var i in Action) {
  52.                                                         Result &= this.Add(Action[i], fFunc);
  53.                                                 }
  54.                                         }
  55.                                         return(Result == true);
  56.                                 } else if (typeof Action === "string") {
  57.                                         if (typeof Actions[Action] === "object") Actions[Action].push(fFunc);
  58.                                         else Actions[Action] =[fFunc];
  59.                                         return true;
  60.                                 }
  61.                                 return false;
  62.                         },
  63.                         //Removes an action and all hooked functions
  64.                         Remove: function (Action) {
  65.                                 if (typeof Action === "object") {
  66.                                         var Result = true;
  67.                                         if (typeof Action.length === "number") {
  68.                                                 for (var i=0; i<Action.length; i++) {
  69.                                                         Result &= this.Remove(Action[i]);
  70.                                                 }
  71.                                         } else {
  72.                                                 for (var i in Action) {
  73.                                                         Result &= this.Remove(Action[i]);
  74.                                                 }
  75.                                         }
  76.                                         return(Result == true);
  77.                                 } else if (typeof Action === "string") {
  78.                                         if (typeof Actions[Action] === "object") {
  79.                                                 delete Actions[Action];
  80.                                                 return true;
  81.                                         }
  82.                                 }
  83.                                 return false;
  84.                         },
  85.                         //Executes an action calling all hooked functions
  86.                         Do: function () {
  87.                                 var args = toArray(arguments);
  88.                                 var Action = args.shift();
  89.                                 if (typeof Action === "object") {
  90.                                         var Result = true;
  91.                                         if (typeof Action.length === "number") {
  92.                                                 for (var i=0; i<Action.length; i++) {
  93.                                                         Result &= this.Do(Action[i]);
  94.                                                 }
  95.                                         } else {
  96.                                                 for (var i in Action) {
  97.                                                         Result &= this.Do(Action[i]);
  98.                                                 }
  99.                                         }
  100.                                         return(Result == true);
  101.                                 } else if (typeof Action === "string") {
  102.                                         if (typeof Actions[Action] === "object") {
  103.                                                 for (var i=0; i<Actions[Action].length; i++) {
  104.                                                         Actions[Action][i].apply(null, args);
  105.                                                 }
  106.                                                 return true;
  107.                                         }
  108.                                 }
  109.                                 return false;
  110.                         },
  111.                         //Destroys this action hook instance
  112.                         Destroy: function () {
  113.                                 delete ActionHooks[Id];
  114.                         }
  115.                 };
  116.                 ActionHooks[Id] = Instance;
  117.                 return Instance;
  118.         }
  119. })();

Version

  • 5.0.0070_20100325_publicbeta1

Developers

Project Details

  • Folders8
  • Files122
  • Total Lines11,867
  • Repository Version70

User Count

  • 162