bindings
6 months ago
commands
1 week ago
pro
1 week ago
_acf-compatibility.js
1 year ago
_acf-condition-types.js
7 months ago
_acf-condition.js
1 year ago
_acf-conditions.js
1 year ago
_acf-field-accordion.js
6 months ago
_acf-field-button-group.js
7 months ago
_acf-field-checkbox.js
1 month ago
_acf-field-color-picker.js
7 months ago
_acf-field-date-picker.js
1 month ago
_acf-field-date-time-picker.js
1 month ago
_acf-field-file.js
1 month ago
_acf-field-google-map.js
6 months ago
_acf-field-icon-picker.js
1 month ago
_acf-field-image.js
1 month ago
_acf-field-link.js
1 year ago
_acf-field-oembed.js
1 month ago
_acf-field-page-link.js
1 year ago
_acf-field-post-object.js
1 year ago
_acf-field-radio.js
1 month ago
_acf-field-range.js
1 year ago
_acf-field-relationship.js
1 month ago
_acf-field-select.js
1 month ago
_acf-field-tab.js
3 weeks ago
_acf-field-taxonomy.js
7 months ago
_acf-field-time-picker.js
1 month ago
_acf-field-true-false.js
1 month ago
_acf-field-url.js
1 year ago
_acf-field-user.js
1 year ago
_acf-field-wysiwyg.js
1 month ago
_acf-field.js
1 year ago
_acf-fields.js
1 year ago
_acf-helpers.js
1 year ago
_acf-hooks.js
1 year ago
_acf-internal-post-type.js
7 months ago
_acf-media.js
1 week ago
_acf-modal.js
1 year ago
_acf-model.js
1 year ago
_acf-notice.js
7 months ago
_acf-panel.js
1 year ago
_acf-popup.js
7 months ago
_acf-postbox.js
1 year ago
_acf-screen.js
10 months ago
_acf-select2.js
1 year ago
_acf-tinymce.js
6 months ago
_acf-tooltip.js
10 months ago
_acf-unload.js
1 year ago
_acf-validation.js
1 month ago
_acf.js
7 months ago
_browse-fields-modal.js
7 months ago
_field-group-compatibility.js
1 year ago
_field-group-conditions.js
1 year ago
_field-group-field.js
3 months ago
_field-group-fields.js
10 months ago
_field-group-locations.js
1 year ago
_field-group-settings.js
1 year ago
_field-group.js
10 months ago
acf-escaped-html-notice.js
1 year ago
acf-field-group.js
1 year ago
acf-input.js
1 year ago
acf-internal-post-type.js
1 year ago
acf.js
1 year ago
_acf-hooks.js
263 lines
| 1 | ( function ( window, undefined ) { |
| 2 | 'use strict'; |
| 3 | |
| 4 | /** |
| 5 | * Handles managing all events for whatever you plug it into. Priorities for hooks are based on lowest to highest in |
| 6 | * that, lowest priority hooks are fired first. |
| 7 | */ |
| 8 | var EventManager = function () { |
| 9 | /** |
| 10 | * Maintain a reference to the object scope so our public methods never get confusing. |
| 11 | */ |
| 12 | var MethodsAvailable = { |
| 13 | removeFilter: removeFilter, |
| 14 | applyFilters: applyFilters, |
| 15 | addFilter: addFilter, |
| 16 | removeAction: removeAction, |
| 17 | doAction: doAction, |
| 18 | addAction: addAction, |
| 19 | storage: getStorage, |
| 20 | }; |
| 21 | |
| 22 | /** |
| 23 | * Contains the hooks that get registered with this EventManager. The array for storage utilizes a "flat" |
| 24 | * object literal such that looking up the hook utilizes the native object literal hash. |
| 25 | */ |
| 26 | var STORAGE = { |
| 27 | actions: {}, |
| 28 | filters: {}, |
| 29 | }; |
| 30 | |
| 31 | function getStorage() { |
| 32 | return STORAGE; |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Adds an action to the event manager. |
| 37 | * |
| 38 | * @param action Must contain namespace.identifier |
| 39 | * @param callback Must be a valid callback function before this action is added |
| 40 | * @param [priority=10] Used to control when the function is executed in relation to other callbacks bound to the same hook |
| 41 | * @param [context] Supply a value to be used for this |
| 42 | */ |
| 43 | function addAction( action, callback, priority, context ) { |
| 44 | if ( |
| 45 | typeof action === 'string' && |
| 46 | typeof callback === 'function' |
| 47 | ) { |
| 48 | priority = parseInt( priority || 10, 10 ); |
| 49 | _addHook( 'actions', action, callback, priority, context ); |
| 50 | } |
| 51 | |
| 52 | return MethodsAvailable; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Performs an action if it exists. You can pass as many arguments as you want to this function; the only rule is |
| 57 | * that the first argument must always be the action. |
| 58 | */ |
| 59 | function doAction(/* action, arg1, arg2, ... */) { |
| 60 | var args = Array.prototype.slice.call( arguments ); |
| 61 | var action = args.shift(); |
| 62 | |
| 63 | if ( typeof action === 'string' ) { |
| 64 | _runHook( 'actions', action, args ); |
| 65 | } |
| 66 | |
| 67 | return MethodsAvailable; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Removes the specified action if it contains a namespace.identifier & exists. |
| 72 | * |
| 73 | * @param action The action to remove |
| 74 | * @param [callback] Callback function to remove |
| 75 | */ |
| 76 | function removeAction( action, callback ) { |
| 77 | if ( typeof action === 'string' ) { |
| 78 | _removeHook( 'actions', action, callback ); |
| 79 | } |
| 80 | |
| 81 | return MethodsAvailable; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Adds a filter to the event manager. |
| 86 | * |
| 87 | * @param filter Must contain namespace.identifier |
| 88 | * @param callback Must be a valid callback function before this action is added |
| 89 | * @param [priority=10] Used to control when the function is executed in relation to other callbacks bound to the same hook |
| 90 | * @param [context] Supply a value to be used for this |
| 91 | */ |
| 92 | function addFilter( filter, callback, priority, context ) { |
| 93 | if ( |
| 94 | typeof filter === 'string' && |
| 95 | typeof callback === 'function' |
| 96 | ) { |
| 97 | priority = parseInt( priority || 10, 10 ); |
| 98 | _addHook( 'filters', filter, callback, priority, context ); |
| 99 | } |
| 100 | |
| 101 | return MethodsAvailable; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Performs a filter if it exists. You should only ever pass 1 argument to be filtered. The only rule is that |
| 106 | * the first argument must always be the filter. |
| 107 | */ |
| 108 | function applyFilters(/* filter, filtered arg, arg2, ... */) { |
| 109 | var args = Array.prototype.slice.call( arguments ); |
| 110 | var filter = args.shift(); |
| 111 | |
| 112 | if ( typeof filter === 'string' ) { |
| 113 | return _runHook( 'filters', filter, args ); |
| 114 | } |
| 115 | |
| 116 | return MethodsAvailable; |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Removes the specified filter if it contains a namespace.identifier & exists. |
| 121 | * |
| 122 | * @param filter The action to remove |
| 123 | * @param [callback] Callback function to remove |
| 124 | */ |
| 125 | function removeFilter( filter, callback ) { |
| 126 | if ( typeof filter === 'string' ) { |
| 127 | _removeHook( 'filters', filter, callback ); |
| 128 | } |
| 129 | |
| 130 | return MethodsAvailable; |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Removes the specified hook by resetting the value of it. |
| 135 | * |
| 136 | * @param type Type of hook, either 'actions' or 'filters' |
| 137 | * @param hook The hook (namespace.identifier) to remove |
| 138 | * @private |
| 139 | */ |
| 140 | function _removeHook( type, hook, callback, context ) { |
| 141 | if ( ! STORAGE[ type ][ hook ] ) { |
| 142 | return; |
| 143 | } |
| 144 | if ( ! callback ) { |
| 145 | STORAGE[ type ][ hook ] = []; |
| 146 | } else { |
| 147 | var handlers = STORAGE[ type ][ hook ]; |
| 148 | var i; |
| 149 | if ( ! context ) { |
| 150 | for ( i = handlers.length; i--; ) { |
| 151 | if ( handlers[ i ].callback === callback ) { |
| 152 | handlers.splice( i, 1 ); |
| 153 | } |
| 154 | } |
| 155 | } else { |
| 156 | for ( i = handlers.length; i--; ) { |
| 157 | var handler = handlers[ i ]; |
| 158 | if ( |
| 159 | handler.callback === callback && |
| 160 | handler.context === context |
| 161 | ) { |
| 162 | handlers.splice( i, 1 ); |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Adds the hook to the appropriate storage container |
| 171 | * |
| 172 | * @param type 'actions' or 'filters' |
| 173 | * @param hook The hook (namespace.identifier) to add to our event manager |
| 174 | * @param callback The function that will be called when the hook is executed. |
| 175 | * @param priority The priority of this hook. Must be an integer. |
| 176 | * @param [context] A value to be used for this |
| 177 | * @private |
| 178 | */ |
| 179 | function _addHook( type, hook, callback, priority, context ) { |
| 180 | var hookObject = { |
| 181 | callback: callback, |
| 182 | priority: priority, |
| 183 | context: context, |
| 184 | }; |
| 185 | |
| 186 | // Utilize 'prop itself' : http://jsperf.com/hasownproperty-vs-in-vs-undefined/19 |
| 187 | var hooks = STORAGE[ type ][ hook ]; |
| 188 | if ( hooks ) { |
| 189 | hooks.push( hookObject ); |
| 190 | hooks = _hookInsertSort( hooks ); |
| 191 | } else { |
| 192 | hooks = [ hookObject ]; |
| 193 | } |
| 194 | |
| 195 | STORAGE[ type ][ hook ] = hooks; |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * Use an insert sort for keeping our hooks organized based on priority. This function is ridiculously faster |
| 200 | * than bubble sort, etc: http://jsperf.com/javascript-sort |
| 201 | * |
| 202 | * @param hooks The custom array containing all of the appropriate hooks to perform an insert sort on. |
| 203 | * @private |
| 204 | */ |
| 205 | function _hookInsertSort( hooks ) { |
| 206 | var tmpHook, j, prevHook; |
| 207 | for ( var i = 1, len = hooks.length; i < len; i++ ) { |
| 208 | tmpHook = hooks[ i ]; |
| 209 | j = i; |
| 210 | while ( |
| 211 | ( prevHook = hooks[ j - 1 ] ) && |
| 212 | prevHook.priority > tmpHook.priority |
| 213 | ) { |
| 214 | hooks[ j ] = hooks[ j - 1 ]; |
| 215 | --j; |
| 216 | } |
| 217 | hooks[ j ] = tmpHook; |
| 218 | } |
| 219 | |
| 220 | return hooks; |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Runs the specified hook. If it is an action, the value is not modified but if it is a filter, it is. |
| 225 | * |
| 226 | * @param type 'actions' or 'filters' |
| 227 | * @param hook The hook ( namespace.identifier ) to be ran. |
| 228 | * @param args Arguments to pass to the action/filter. If it's a filter, args is actually a single parameter. |
| 229 | * @private |
| 230 | */ |
| 231 | function _runHook( type, hook, args ) { |
| 232 | var handlers = STORAGE[ type ][ hook ]; |
| 233 | |
| 234 | if ( ! handlers ) { |
| 235 | return type === 'filters' ? args[ 0 ] : false; |
| 236 | } |
| 237 | |
| 238 | var i = 0, |
| 239 | len = handlers.length; |
| 240 | if ( type === 'filters' ) { |
| 241 | for ( ; i < len; i++ ) { |
| 242 | args[ 0 ] = handlers[ i ].callback.apply( |
| 243 | handlers[ i ].context, |
| 244 | args |
| 245 | ); |
| 246 | } |
| 247 | } else { |
| 248 | for ( ; i < len; i++ ) { |
| 249 | handlers[ i ].callback.apply( handlers[ i ].context, args ); |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | return type === 'filters' ? args[ 0 ] : true; |
| 254 | } |
| 255 | |
| 256 | // return all of the publicly available methods |
| 257 | return MethodsAvailable; |
| 258 | }; |
| 259 | |
| 260 | // instantiate |
| 261 | acf.hooks = new EventManager(); |
| 262 | } )( window ); |
| 263 |