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-fields.js
435 lines
| 1 | ( function ( $, undefined ) { |
| 2 | /** |
| 3 | * findFields |
| 4 | * |
| 5 | * Returns a jQuery selection object of acf fields. |
| 6 | * |
| 7 | * @date 14/12/17 |
| 8 | * @since ACF 5.6.5 |
| 9 | * |
| 10 | * @param object $args { |
| 11 | * Optional. Arguments to find fields. |
| 12 | * |
| 13 | * @type string key The field's key (data-attribute). |
| 14 | * @type string name The field's name (data-attribute). |
| 15 | * @type string type The field's type (data-attribute). |
| 16 | * @type string is jQuery selector to compare against. |
| 17 | * @type jQuery parent jQuery element to search within. |
| 18 | * @type jQuery sibling jQuery element to search alongside. |
| 19 | * @type limit int The number of fields to find. |
| 20 | * @type suppressFilters bool Whether to allow filters to add/remove results. Default behaviour will ignore clone fields. |
| 21 | * } |
| 22 | * @return jQuery |
| 23 | */ |
| 24 | |
| 25 | acf.findFields = function ( args ) { |
| 26 | // vars |
| 27 | var selector = '.acf-field'; |
| 28 | var $fields = false; |
| 29 | |
| 30 | // args |
| 31 | args = acf.parseArgs( args, { |
| 32 | key: '', |
| 33 | name: '', |
| 34 | type: '', |
| 35 | is: '', |
| 36 | parent: false, |
| 37 | sibling: false, |
| 38 | limit: false, |
| 39 | visible: false, |
| 40 | suppressFilters: false, |
| 41 | excludeSubFields: false, |
| 42 | } ); |
| 43 | |
| 44 | // filter args |
| 45 | if ( ! args.suppressFilters ) { |
| 46 | args = acf.applyFilters( 'find_fields_args', args ); |
| 47 | } |
| 48 | |
| 49 | // key |
| 50 | if ( args.key ) { |
| 51 | selector += '[data-key="' + args.key + '"]'; |
| 52 | } |
| 53 | |
| 54 | // type |
| 55 | if ( args.type ) { |
| 56 | selector += '[data-type="' + args.type + '"]'; |
| 57 | } |
| 58 | |
| 59 | // name |
| 60 | if ( args.name ) { |
| 61 | selector += '[data-name="' + args.name + '"]'; |
| 62 | } |
| 63 | |
| 64 | // is |
| 65 | if ( args.is ) { |
| 66 | selector += args.is; |
| 67 | } |
| 68 | |
| 69 | // visibility |
| 70 | if ( args.visible ) { |
| 71 | selector += ':visible'; |
| 72 | } |
| 73 | |
| 74 | if ( ! args.suppressFilters ) { |
| 75 | selector = acf.applyFilters( |
| 76 | 'find_fields_selector', |
| 77 | selector, |
| 78 | args |
| 79 | ); |
| 80 | } |
| 81 | |
| 82 | // query |
| 83 | if ( args.parent ) { |
| 84 | $fields = args.parent.find( selector ); |
| 85 | // exclude sub fields if required (only if a parent is provided) |
| 86 | if ( args.excludeSubFields ) { |
| 87 | $fields = $fields.not( args.parent.find( '.acf-is-subfields .acf-field' ) ); |
| 88 | } |
| 89 | } else if ( args.sibling ) { |
| 90 | $fields = args.sibling.siblings( selector ); |
| 91 | } else { |
| 92 | $fields = $( selector ); |
| 93 | } |
| 94 | |
| 95 | // filter |
| 96 | if ( ! args.suppressFilters ) { |
| 97 | $fields = $fields.not( '.acf-clone .acf-field' ); |
| 98 | $fields = acf.applyFilters( 'find_fields', $fields ); |
| 99 | } |
| 100 | |
| 101 | // limit |
| 102 | if ( args.limit ) { |
| 103 | $fields = $fields.slice( 0, args.limit ); |
| 104 | } |
| 105 | |
| 106 | // return |
| 107 | return $fields; |
| 108 | }; |
| 109 | |
| 110 | /** |
| 111 | * findField |
| 112 | * |
| 113 | * Finds a specific field with jQuery |
| 114 | * |
| 115 | * @date 14/12/17 |
| 116 | * @since ACF 5.6.5 |
| 117 | * |
| 118 | * @param string key The field's key. |
| 119 | * @param jQuery $parent jQuery element to search within. |
| 120 | * @return jQuery |
| 121 | */ |
| 122 | |
| 123 | acf.findField = function ( key, $parent ) { |
| 124 | return acf.findFields( { |
| 125 | key: key, |
| 126 | limit: 1, |
| 127 | parent: $parent, |
| 128 | suppressFilters: true, |
| 129 | } ); |
| 130 | }; |
| 131 | |
| 132 | /** |
| 133 | * getField |
| 134 | * |
| 135 | * Returns a field instance |
| 136 | * |
| 137 | * @date 14/12/17 |
| 138 | * @since ACF 5.6.5 |
| 139 | * |
| 140 | * @param jQuery|string $field jQuery element or field key. |
| 141 | * @return object |
| 142 | */ |
| 143 | |
| 144 | acf.getField = function ( $field ) { |
| 145 | // allow jQuery |
| 146 | if ( $field instanceof jQuery ) { |
| 147 | // find fields |
| 148 | } else { |
| 149 | $field = acf.findField( $field ); |
| 150 | } |
| 151 | |
| 152 | // instantiate |
| 153 | var field = $field.data( 'acf' ); |
| 154 | if ( ! field ) { |
| 155 | field = acf.newField( $field ); |
| 156 | } |
| 157 | |
| 158 | // return |
| 159 | return field; |
| 160 | }; |
| 161 | |
| 162 | /** |
| 163 | * getFields |
| 164 | * |
| 165 | * Returns multiple field instances |
| 166 | * |
| 167 | * @date 14/12/17 |
| 168 | * @since ACF 5.6.5 |
| 169 | * |
| 170 | * @param jQuery|object $fields jQuery elements or query args. |
| 171 | * @return array |
| 172 | */ |
| 173 | |
| 174 | acf.getFields = function ( $fields ) { |
| 175 | // allow jQuery |
| 176 | if ( $fields instanceof jQuery ) { |
| 177 | // find fields |
| 178 | } else { |
| 179 | $fields = acf.findFields( $fields ); |
| 180 | } |
| 181 | |
| 182 | // loop |
| 183 | var fields = []; |
| 184 | $fields.each( function () { |
| 185 | var field = acf.getField( $( this ) ); |
| 186 | fields.push( field ); |
| 187 | } ); |
| 188 | |
| 189 | // return |
| 190 | return fields; |
| 191 | }; |
| 192 | |
| 193 | /** |
| 194 | * findClosestField |
| 195 | * |
| 196 | * Returns the closest jQuery field element |
| 197 | * |
| 198 | * @date 9/4/18 |
| 199 | * @since ACF 5.6.9 |
| 200 | * |
| 201 | * @param jQuery $el |
| 202 | * @return jQuery |
| 203 | */ |
| 204 | |
| 205 | acf.findClosestField = function ( $el ) { |
| 206 | return $el.closest( '.acf-field' ); |
| 207 | }; |
| 208 | |
| 209 | /** |
| 210 | * getClosestField |
| 211 | * |
| 212 | * Returns the closest field instance |
| 213 | * |
| 214 | * @date 22/1/18 |
| 215 | * @since ACF 5.6.5 |
| 216 | * |
| 217 | * @param jQuery $el |
| 218 | * @return object |
| 219 | */ |
| 220 | |
| 221 | acf.getClosestField = function ( $el ) { |
| 222 | var $field = acf.findClosestField( $el ); |
| 223 | return this.getField( $field ); |
| 224 | }; |
| 225 | |
| 226 | /** |
| 227 | * addGlobalFieldAction |
| 228 | * |
| 229 | * Sets up callback logic for global field actions |
| 230 | * |
| 231 | * @date 15/6/18 |
| 232 | * @since ACF 5.6.9 |
| 233 | * |
| 234 | * @param string action |
| 235 | * @return void |
| 236 | */ |
| 237 | |
| 238 | var addGlobalFieldAction = function ( action ) { |
| 239 | // vars |
| 240 | var globalAction = action; |
| 241 | var pluralAction = action + '_fields'; // ready_fields |
| 242 | var singleAction = action + '_field'; // ready_field |
| 243 | |
| 244 | // global action |
| 245 | var globalCallback = function ( $el /*, arg1, arg2, etc*/ ) { |
| 246 | //console.log( action, arguments ); |
| 247 | |
| 248 | // get args [$el, ...] |
| 249 | var args = acf.arrayArgs( arguments ); |
| 250 | var extraArgs = args.slice( 1 ); |
| 251 | |
| 252 | // find fields |
| 253 | var fields = acf.getFields( { parent: $el } ); |
| 254 | |
| 255 | // check |
| 256 | if ( fields.length ) { |
| 257 | // pluralAction |
| 258 | var pluralArgs = [ pluralAction, fields ].concat( extraArgs ); |
| 259 | acf.doAction.apply( null, pluralArgs ); |
| 260 | } |
| 261 | }; |
| 262 | |
| 263 | // plural action |
| 264 | var pluralCallback = function ( fields /*, arg1, arg2, etc*/ ) { |
| 265 | //console.log( pluralAction, arguments ); |
| 266 | |
| 267 | // get args [fields, ...] |
| 268 | var args = acf.arrayArgs( arguments ); |
| 269 | var extraArgs = args.slice( 1 ); |
| 270 | |
| 271 | // loop |
| 272 | fields.map( function ( field, i ) { |
| 273 | //setTimeout(function(){ |
| 274 | // singleAction |
| 275 | var singleArgs = [ singleAction, field ].concat( extraArgs ); |
| 276 | acf.doAction.apply( null, singleArgs ); |
| 277 | //}, i * 100); |
| 278 | } ); |
| 279 | }; |
| 280 | |
| 281 | // add actions |
| 282 | acf.addAction( globalAction, globalCallback ); |
| 283 | acf.addAction( pluralAction, pluralCallback ); |
| 284 | |
| 285 | // also add single action |
| 286 | addSingleFieldAction( action ); |
| 287 | }; |
| 288 | |
| 289 | /** |
| 290 | * addSingleFieldAction |
| 291 | * |
| 292 | * Sets up callback logic for single field actions |
| 293 | * |
| 294 | * @date 15/6/18 |
| 295 | * @since ACF 5.6.9 |
| 296 | * |
| 297 | * @param string action |
| 298 | * @return void |
| 299 | */ |
| 300 | |
| 301 | var addSingleFieldAction = function ( action ) { |
| 302 | // vars |
| 303 | var singleAction = action + '_field'; // ready_field |
| 304 | var singleEvent = action + 'Field'; // readyField |
| 305 | |
| 306 | // single action |
| 307 | var singleCallback = function ( field /*, arg1, arg2, etc*/ ) { |
| 308 | //console.log( singleAction, arguments ); |
| 309 | |
| 310 | // get args [field, ...] |
| 311 | var args = acf.arrayArgs( arguments ); |
| 312 | var extraArgs = args.slice( 1 ); |
| 313 | |
| 314 | // action variations (ready_field/type=image) |
| 315 | var variations = [ 'type', 'name', 'key' ]; |
| 316 | variations.map( function ( variation ) { |
| 317 | // vars |
| 318 | var prefix = '/' + variation + '=' + field.get( variation ); |
| 319 | |
| 320 | // singleAction |
| 321 | args = [ singleAction + prefix, field ].concat( extraArgs ); |
| 322 | acf.doAction.apply( null, args ); |
| 323 | } ); |
| 324 | |
| 325 | // event |
| 326 | if ( singleFieldEvents.indexOf( action ) > -1 ) { |
| 327 | field.trigger( singleEvent, extraArgs ); |
| 328 | } |
| 329 | }; |
| 330 | |
| 331 | // add actions |
| 332 | acf.addAction( singleAction, singleCallback ); |
| 333 | }; |
| 334 | |
| 335 | // vars |
| 336 | var globalFieldActions = [ |
| 337 | 'prepare', |
| 338 | 'ready', |
| 339 | 'load', |
| 340 | 'append', |
| 341 | 'remove', |
| 342 | 'unmount', |
| 343 | 'remount', |
| 344 | 'sortstart', |
| 345 | 'sortstop', |
| 346 | 'show', |
| 347 | 'hide', |
| 348 | 'unload', |
| 349 | ]; |
| 350 | var singleFieldActions = [ |
| 351 | 'valid', |
| 352 | 'invalid', |
| 353 | 'enable', |
| 354 | 'disable', |
| 355 | 'new', |
| 356 | 'duplicate', |
| 357 | ]; |
| 358 | var singleFieldEvents = [ |
| 359 | 'remove', |
| 360 | 'unmount', |
| 361 | 'remount', |
| 362 | 'sortstart', |
| 363 | 'sortstop', |
| 364 | 'show', |
| 365 | 'hide', |
| 366 | 'unload', |
| 367 | 'valid', |
| 368 | 'invalid', |
| 369 | 'enable', |
| 370 | 'disable', |
| 371 | 'duplicate', |
| 372 | ]; |
| 373 | |
| 374 | // add |
| 375 | globalFieldActions.map( addGlobalFieldAction ); |
| 376 | singleFieldActions.map( addSingleFieldAction ); |
| 377 | |
| 378 | /** |
| 379 | * fieldsEventManager |
| 380 | * |
| 381 | * Manages field actions and events |
| 382 | * |
| 383 | * @date 15/12/17 |
| 384 | * @since ACF 5.6.5 |
| 385 | * |
| 386 | * @param void |
| 387 | * @param void |
| 388 | */ |
| 389 | |
| 390 | var fieldsEventManager = new acf.Model( { |
| 391 | id: 'fieldsEventManager', |
| 392 | events: { |
| 393 | 'click .acf-field a[href="#"]': 'onClick', |
| 394 | 'change .acf-field': 'onChange', |
| 395 | }, |
| 396 | onClick: function ( e ) { |
| 397 | // prevent default of any link with an href of # |
| 398 | e.preventDefault(); |
| 399 | }, |
| 400 | onChange: function () { |
| 401 | // preview hack allows post to save with no title or content |
| 402 | $( '#_acf_changed' ).val( 1 ); |
| 403 | |
| 404 | if ( acf.isGutenbergPostEditor() ) { |
| 405 | try { |
| 406 | wp.data.dispatch('core/editor').editPost({ meta: { _acf_changed: 1 } }); |
| 407 | } catch ( error ) { |
| 408 | console.log( 'ACF: Failed to update _acf_changed meta', error ); |
| 409 | } |
| 410 | |
| 411 | } |
| 412 | }, |
| 413 | } ); |
| 414 | |
| 415 | var duplicateFieldsManager = new acf.Model( { |
| 416 | id: 'duplicateFieldsManager', |
| 417 | actions: { |
| 418 | duplicate: 'onDuplicate', |
| 419 | duplicate_fields: 'onDuplicateFields', |
| 420 | }, |
| 421 | onDuplicate: function ( $el, $el2 ) { |
| 422 | var fields = acf.getFields( { parent: $el } ); |
| 423 | if ( fields.length ) { |
| 424 | var $fields = acf.findFields( { parent: $el2 } ); |
| 425 | acf.doAction( 'duplicate_fields', fields, $fields ); |
| 426 | } |
| 427 | }, |
| 428 | onDuplicateFields: function ( fields, duplicates ) { |
| 429 | fields.map( function ( field, i ) { |
| 430 | acf.doAction( 'duplicate_field', field, $( duplicates[ i ] ) ); |
| 431 | } ); |
| 432 | }, |
| 433 | } ); |
| 434 | } )( jQuery ); |
| 435 |