Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- *
- * Wordpress like hook system for javascript.
- *
- * The purpose of this library is to make code extensible by using hooks like in
- * the Wordpress CMS.
- *
- * The functions in this library shall help other developers hook their code
- * into a generic code using their callbacks.
- *
- * USAGE ------------------------------------------
- *
- * Add filter
- *
- * add_filter(tag, callback, priority);
- *
- * Apply filters
- *
- * apply_filtes(tag, value, options);
- *
- * ------------------------------------------------
- *
- * Add action
- *
- * add_action(tag, callback, priority);
- *
- * Apply action
- *
- * apply_filters(tag, options);
- *
- * ------------------------------------------------
- *
- * @author IAmRDhar
- * @ver 1.0
- */
- // functional class for hooks
- function _jsHooks() {
- // instance
- var _jsH = this;
- // will contain data structure for actions
- this.actions = [];
- // will contain data structure for filters
- this.filters = [];
- // object that contains functions that can
- // be used for adding callbacks to actions
- // and filters with a certain priority
- this.add = {
- action : function(tag, callback, priority) {
- if (typeof priority === "undefined") {
- priority = 10; // default priority
- }
- // empty array if not already initialized
- _jsH.actions[tag] = _jsH.actions[tag] || [];
- // empty array if not already initialized
- _jsh.actions[tag][priority] = _jsH.actions[tag][priority] || [];
- // add callback at the right place in ds
- // with respect to tag and priority
- _jsH.actions[tag][priority].push(callback);
- },
- filter : function(tag, callback, priority) {
- if (typeof priority === "undefined") {
- priority = 10;
- }
- // empty array if not already initialized
- _jsH.filters[tag] = _jsH.filters[tag] || [];
- // empty array if not already initialized
- _jsH.filters[tag][priority] = _jsH.filters[tag][priority] || [];
- // add callback at the right place in ds
- // with respect to tag and priority
- _jsH.filters[tag][priority].push(callback);
- }
- };
- // object that contains functions that can
- // be used for removing callbacks from actions
- // and filters
- this.remove = {
- action : function(tag, callback) {
- // empty array if not already initialized
- _jsH.actions[tag] = _jsH.actions[tag] || [];
- // looping over all the priorities for the specified tag
- _jsH.actions[tag].forEach(function(priority, i) {
- // looping over all the callback
- priority.forEach(function(_callback, j) {
- // checking callback to remove
- if (_callback === callback) {
- // removing callback from ds
- _jsH.actions[tag][i].splice(j, 1);
- }
- });
- });
- },
- filter : function(tag, callback) {
- // empty array if not already initialized
- _jsH.filters[tag] = _jsH.filters[tag] || [];
- // looping over all the priorities for the specified tag
- _jsH.filters[tag].forEach(function(priority, i) {
- // looping over all the callback
- priority.forEach(function(_callback, j) {
- // checking callback to remove
- if (_callback === callback) {
- // removing callback from ds
- _jsH.filters[tag][i].splice(j, 1);
- }
- });
- });
- }
- };
- // object that contains functions that can
- // be used for applying or processing callbacks
- // from actions and filters
- this.process = {
- action : function(tag, options) {
- // check if tag is valid
- if (typeof _jsH.actions[tag] !== "undefined"
- && _jsH.actions[tag].length > 0) {
- // looping over all priorities
- _jsH.actions[tag].forEach(function(priorities) {
- // looping over all callbacks
- priorities.forEach(function(callback) {
- // check if callback is a valid function
- if (typeof callback === "function") {
- callback(options); // call the callback
- }
- });
- });
- }
- },
- filter : function(tag, value, options) {
- // check if tag is valid
- if (typeof _jsH.filters[tag] !== "undefined"
- && _jsH.filters[tag].length > 0) {
- // looping over all priorities
- _jsH.filters[tag].forEach(function(priorities) {
- // looping over all callbacks
- priorities.forEach(function(callback) {
- // check if callback is a valid function
- if (typeof callback === "function") {
- // callback should return a value
- // call the callback and save value
- value = callback(value, options);
- }
- });
- });
- }
- // return final value after being
- // processed by all the callback
- return value;
- }
- };
- }
- // this needs to be defined very early in the code
- var _hooks;
- // returns singleton _jsHooks object
- function hooks() {
- if (typeof _hooks === 'undefined') {
- _hooks = new _jsHooks();
- }
- return _hooks;
- }
- // wrapper for adding filter
- function add_filter(tag, callback, priority) {
- var _h = hooks();
- _h.add.filter(tag, callback, priority);
- }
- // wrapper for applying all the filters
- function apply_filters(tag, value, options) {
- var _h = hooks();
- return _h.process.filter(tag, value, options);
- }
- // wrapper for removing filter with a certain callback
- function remove_filter(tag, callback) {
- var _h = hooks();
- _h.remove.filter(tag, callback);
- }
- // --------------------------------------------------------------------------------------------------------------------------------
- // EXAMPLE
- // -------------------------------------------------------------------------------------------------------------------------------
- function appendSalutation(name) {
- console.log('[Appending Salutation] Name: ' + name);
- return 'Hello ' + name + '!';
- }
- var name = "Rahul";
- console.log("Original Name: " + name);
- // FILTER 1
- add_filter('name_changer', function(name) {
- console.log('[Appending Last Name] Name: ' + name);
- return name + ' Dhar';
- });
- // FILTER 2
- add_filter('name_changer', appendSalutation);
- // PROCESSED
- console.log("After Filter: " + apply_filters('name_changer', name));
- // RESULT
- /**
- * Original Name: Rahul
- * [Appending Last Name] Name: Rahul
- * [Appending Salutation] Name: Rahul Dhar
- * After Filter: Hello Rahul Dhar!
- */
Add Comment
Please, Sign In to add comment