PageSourceSearch

https://www.braysoaksmd.org/wp-content/plugins/hydrogen-calendar-embeds/public/js/hooks.js

js braysoaksmd.org collected 2026-09-24 10:12:15 UTC 4,084 bytes, 156 lines download raw bytes

1/**
2 * Hydrogen Calendar Embeds - Hooks System
3 *
4 * A lightweight WordPress-style hooks system for JavaScript.
5 * Allows extensions to modify behavior via filters and actions.
6 *
7 * @since 1.0.0
8 *
9 * Usage:
10 *   // Add a filter
11 *   hycalHooks.addFilter('hycal.tooltipContent', function(content, event) {
12 *     return content + '<p>Custom content</p>';
13 *   });
14 *
15 *   // Add an action
16 *   hycalHooks.addAction('hycal.afterRender', function(calendar, settings) {
17 *     console.log('Calendar rendered!');
18 *   });
19 */
20const hycalHooks = (function () {
21  // Storage for registered hooks
22  const filters = {};
23  const actions = {};
24
25  /**
26   * Add a filter callback.
27   *
28   * Filters modify and return a value. Multiple callbacks can be added
29   * to the same filter name; they run in order of priority.
30   *
31   * @param {string}   name     The filter name.
32   * @param {function} callback The callback function.
33   * @param {number}   priority Priority (default 10, lower runs first).
34   */
35  function addFilter(name, callback, priority = 10) {
36    if (!filters[name]) {
37      filters[name] = [];
38    }
39    filters[name].push({ callback, priority });
40    filters[name].sort((a, b) => a.priority - b.priority);
41  }
42
43  /**
44   * Apply filters to a value.
45   *
46   * Runs all registered filter callbacks for the given name,
47   * passing the value through each one.
48   *
49   * @param {string} name  The filter name.
50   * @param {*}      value The value to filter.
51   * @param {...*}   args  Additional arguments passed to callbacks.
52   * @returns {*} The filtered value.
53   */
54  function applyFilters(name, value, ...args) {
55    if (!filters[name]) {
56      return value;
57    }
58    return filters[name].reduce((val, hook) => {
59      return hook.callback(val, ...args);
60    }, value);
61  }
62
63  /**
64   * Add an action callback.
65   *
66   * Actions perform side effects and don't return a value.
67   * Multiple callbacks can be added to the same action name.
68   *
69   * @param {string}   name     The action name.
70   * @param {function} callback The callback function.
71   * @param {number}   priority Priority (default 10, lower runs first).
72   */
73  function addAction(name, callback, priority = 10) {
74    if (!actions[name]) {
75      actions[name] = [];
76    }
77    actions[name].push({ callback, priority });
78    actions[name].sort((a, b) => a.priority - b.priority);
79  }
80
81  /**
82   * Execute all callbacks for an action.
83   *
84   * @param {string} name The action name.
85   * @param {...*}   args Arguments passed to callbacks.
86   */
87  function doAction(name, ...args) {
88    if (!actions[name]) {
89      return;
90    }
91    actions[name].forEach((hook) => {
92      hook.callback(...args);
93    });
94  }
95
96  /**
97   * Remove a filter callback.
98   *
99   * @param {string}   name     The filter name.
100   * @param {function} callback The callback to remove.
101   */
102  function removeFilter(name, callback) {
103    if (!filters[name]) {
104      return;
105    }
106    filters[name] = filters[name].filter((hook) => hook.callback !== callback);
107  }
108
109  /**
110   * Remove an action callback.
111   *
112   * @param {string}   name     The action name.
113   * @param {function} callback The callback to remove.
114   */
115  function removeAction(name, callback) {
116    if (!actions[name]) {
117      return;
118    }
119    actions[name] = actions[name].filter((hook) => hook.callback !== callback);
120  }
121
122  /**
123   * Check if a filter has any callbacks registered.
124   *
125   * @param {string} name The filter name.
126   * @returns {boolean} True if callbacks exist.
127   */
128  function hasFilter(name) {
129    return filters[name] && filters[name].length > 0;
130  }
131
132  /**
133   * Check if an action has any callbacks registered.
134   *
135   * @param {string} name The action name.
136   * @returns {boolean} True if callbacks exist.
137   */
138  function hasAction(name) {
139    return actions[name] && actions[name].length > 0;
140  }
141
142  // Public API
143  return {
144    addFilter,
145    applyFilters,
146    addAction,
147    doAction,
148    removeFilter,
149    removeAction,
150    hasFilter,
151    hasAction,
152  };
153})();
154
155// Make hooks available globally for extensions
156window.hycalHooks = hycalHooks;

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.