Guest User

Untitled

a guest
May 22nd, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.90 KB | None | 0 0
  1. /**
  2. *
  3. * Wordpress like hook system for javascript.
  4. *
  5. * The purpose of this library is to make code extensible by using hooks like in
  6. * the Wordpress CMS.
  7. *
  8. * The functions in this library shall help other developers hook their code
  9. * into a generic code using their callbacks.
  10. *
  11. * USAGE ------------------------------------------
  12. *
  13. * Add filter
  14. *
  15. * add_filter(tag, callback, priority);
  16. *
  17. * Apply filters
  18. *
  19. * apply_filtes(tag, value, options);
  20. *
  21. * ------------------------------------------------
  22. *
  23. * Add action
  24. *
  25. * add_action(tag, callback, priority);
  26. *
  27. * Apply action
  28. *
  29. * apply_filters(tag, options);
  30. *
  31. * ------------------------------------------------
  32. *
  33. * @author IAmRDhar
  34. * @ver 1.0
  35. */
  36.  
  37. // functional class for hooks
  38. function _jsHooks() {
  39.  
  40. // instance
  41. var _jsH = this;
  42.  
  43. // will contain data structure for actions
  44. this.actions = [];
  45.  
  46. // will contain data structure for filters
  47. this.filters = [];
  48.  
  49. // object that contains functions that can
  50. // be used for adding callbacks to actions
  51. // and filters with a certain priority
  52. this.add = {
  53.  
  54. action : function(tag, callback, priority) {
  55. if (typeof priority === "undefined") {
  56. priority = 10; // default priority
  57. }
  58.  
  59. // empty array if not already initialized
  60. _jsH.actions[tag] = _jsH.actions[tag] || [];
  61. // empty array if not already initialized
  62. _jsh.actions[tag][priority] = _jsH.actions[tag][priority] || [];
  63.  
  64. // add callback at the right place in ds
  65. // with respect to tag and priority
  66. _jsH.actions[tag][priority].push(callback);
  67. },
  68.  
  69. filter : function(tag, callback, priority) {
  70. if (typeof priority === "undefined") {
  71. priority = 10;
  72. }
  73.  
  74. // empty array if not already initialized
  75. _jsH.filters[tag] = _jsH.filters[tag] || [];
  76. // empty array if not already initialized
  77. _jsH.filters[tag][priority] = _jsH.filters[tag][priority] || [];
  78.  
  79. // add callback at the right place in ds
  80. // with respect to tag and priority
  81. _jsH.filters[tag][priority].push(callback);
  82. }
  83. };
  84.  
  85. // object that contains functions that can
  86. // be used for removing callbacks from actions
  87. // and filters
  88. this.remove = {
  89.  
  90. action : function(tag, callback) {
  91. // empty array if not already initialized
  92. _jsH.actions[tag] = _jsH.actions[tag] || [];
  93.  
  94. // looping over all the priorities for the specified tag
  95. _jsH.actions[tag].forEach(function(priority, i) {
  96. // looping over all the callback
  97. priority.forEach(function(_callback, j) {
  98. // checking callback to remove
  99. if (_callback === callback) {
  100. // removing callback from ds
  101. _jsH.actions[tag][i].splice(j, 1);
  102. }
  103. });
  104. });
  105. },
  106. filter : function(tag, callback) {
  107. // empty array if not already initialized
  108. _jsH.filters[tag] = _jsH.filters[tag] || [];
  109.  
  110. // looping over all the priorities for the specified tag
  111. _jsH.filters[tag].forEach(function(priority, i) {
  112. // looping over all the callback
  113. priority.forEach(function(_callback, j) {
  114. // checking callback to remove
  115. if (_callback === callback) {
  116. // removing callback from ds
  117. _jsH.filters[tag][i].splice(j, 1);
  118. }
  119. });
  120. });
  121. }
  122. };
  123.  
  124. // object that contains functions that can
  125. // be used for applying or processing callbacks
  126. // from actions and filters
  127. this.process = {
  128.  
  129. action : function(tag, options) {
  130. // check if tag is valid
  131. if (typeof _jsH.actions[tag] !== "undefined"
  132. && _jsH.actions[tag].length > 0) {
  133.  
  134. // looping over all priorities
  135. _jsH.actions[tag].forEach(function(priorities) {
  136. // looping over all callbacks
  137. priorities.forEach(function(callback) {
  138. // check if callback is a valid function
  139. if (typeof callback === "function") {
  140. callback(options); // call the callback
  141. }
  142. });
  143. });
  144. }
  145. },
  146.  
  147. filter : function(tag, value, options) {
  148. // check if tag is valid
  149. if (typeof _jsH.filters[tag] !== "undefined"
  150. && _jsH.filters[tag].length > 0) {
  151.  
  152. // looping over all priorities
  153. _jsH.filters[tag].forEach(function(priorities) {
  154. // looping over all callbacks
  155. priorities.forEach(function(callback) {
  156. // check if callback is a valid function
  157. if (typeof callback === "function") {
  158. // callback should return a value
  159. // call the callback and save value
  160. value = callback(value, options);
  161. }
  162. });
  163. });
  164. }
  165.  
  166. // return final value after being
  167. // processed by all the callback
  168. return value;
  169. }
  170. };
  171. }
  172.  
  173. // this needs to be defined very early in the code
  174. var _hooks;
  175.  
  176. // returns singleton _jsHooks object
  177. function hooks() {
  178. if (typeof _hooks === 'undefined') {
  179. _hooks = new _jsHooks();
  180. }
  181. return _hooks;
  182. }
  183.  
  184. // wrapper for adding filter
  185. function add_filter(tag, callback, priority) {
  186. var _h = hooks();
  187. _h.add.filter(tag, callback, priority);
  188. }
  189.  
  190. // wrapper for applying all the filters
  191. function apply_filters(tag, value, options) {
  192. var _h = hooks();
  193. return _h.process.filter(tag, value, options);
  194. }
  195.  
  196. // wrapper for removing filter with a certain callback
  197. function remove_filter(tag, callback) {
  198. var _h = hooks();
  199. _h.remove.filter(tag, callback);
  200. }
  201.  
  202. // --------------------------------------------------------------------------------------------------------------------------------
  203. // EXAMPLE
  204. // -------------------------------------------------------------------------------------------------------------------------------
  205.  
  206. function appendSalutation(name) {
  207. console.log('[Appending Salutation] Name: ' + name);
  208. return 'Hello ' + name + '!';
  209. }
  210. var name = "Rahul";
  211. console.log("Original Name: " + name);
  212.  
  213. // FILTER 1
  214. add_filter('name_changer', function(name) {
  215. console.log('[Appending Last Name] Name: ' + name);
  216. return name + ' Dhar';
  217. });
  218.  
  219. // FILTER 2
  220. add_filter('name_changer', appendSalutation);
  221.  
  222. // PROCESSED
  223. console.log("After Filter: " + apply_filters('name_changer', name));
  224.  
  225. // RESULT
  226. /**
  227. * Original Name: Rahul
  228. * [Appending Last Name] Name: Rahul
  229. * [Appending Salutation] Name: Rahul Dhar
  230. * After Filter: Hello Rahul Dhar!
  231. */
Add Comment
Please, Sign In to add comment