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
_field-group-fields.js
499 lines
| 1 | ( function ( $, undefined ) { |
| 2 | /** |
| 3 | * acf.findFieldObject |
| 4 | * |
| 5 | * Returns a single fieldObject $el for a given field key |
| 6 | * |
| 7 | * @date 1/2/18 |
| 8 | * @since ACF 5.7.0 |
| 9 | * |
| 10 | * @param string key The field key |
| 11 | * @return jQuery |
| 12 | */ |
| 13 | |
| 14 | acf.findFieldObject = function ( key ) { |
| 15 | return acf.findFieldObjects( { |
| 16 | key: key, |
| 17 | limit: 1, |
| 18 | } ); |
| 19 | }; |
| 20 | |
| 21 | /** |
| 22 | * acf.findFieldObjects |
| 23 | * |
| 24 | * Returns an array of fieldObject $el for the given args |
| 25 | * |
| 26 | * @date 1/2/18 |
| 27 | * @since ACF 5.7.0 |
| 28 | * |
| 29 | * @param object args |
| 30 | * @return jQuery |
| 31 | */ |
| 32 | |
| 33 | acf.findFieldObjects = function ( args ) { |
| 34 | // vars |
| 35 | args = args || {}; |
| 36 | var selector = '.acf-field-object'; |
| 37 | var $fields = false; |
| 38 | |
| 39 | // args |
| 40 | args = acf.parseArgs( args, { |
| 41 | id: '', |
| 42 | key: '', |
| 43 | type: '', |
| 44 | limit: false, |
| 45 | list: null, |
| 46 | parent: false, |
| 47 | sibling: false, |
| 48 | child: false, |
| 49 | } ); |
| 50 | |
| 51 | // id |
| 52 | if ( args.id ) { |
| 53 | selector += '[data-id="' + args.id + '"]'; |
| 54 | } |
| 55 | |
| 56 | // key |
| 57 | if ( args.key ) { |
| 58 | selector += '[data-key="' + args.key + '"]'; |
| 59 | } |
| 60 | |
| 61 | // type |
| 62 | if ( args.type ) { |
| 63 | selector += '[data-type="' + args.type + '"]'; |
| 64 | } |
| 65 | |
| 66 | // query |
| 67 | if ( args.list ) { |
| 68 | $fields = args.list.children( selector ); |
| 69 | } else if ( args.parent ) { |
| 70 | $fields = args.parent.find( selector ); |
| 71 | } else if ( args.sibling ) { |
| 72 | $fields = args.sibling.siblings( selector ); |
| 73 | } else if ( args.child ) { |
| 74 | $fields = args.child.parents( selector ); |
| 75 | } else { |
| 76 | $fields = $( selector ); |
| 77 | } |
| 78 | |
| 79 | // limit |
| 80 | if ( args.limit ) { |
| 81 | $fields = $fields.slice( 0, args.limit ); |
| 82 | } |
| 83 | |
| 84 | // return |
| 85 | return $fields; |
| 86 | }; |
| 87 | |
| 88 | /** |
| 89 | * acf.getFieldObject |
| 90 | * |
| 91 | * Returns a single fieldObject instance for a given $el|key |
| 92 | * |
| 93 | * @date 1/2/18 |
| 94 | * @since ACF 5.7.0 |
| 95 | * |
| 96 | * @param string|jQuery $field The field $el or key |
| 97 | * @return jQuery |
| 98 | */ |
| 99 | |
| 100 | acf.getFieldObject = function ( $field ) { |
| 101 | // allow key |
| 102 | if ( typeof $field === 'string' ) { |
| 103 | $field = acf.findFieldObject( $field ); |
| 104 | } |
| 105 | |
| 106 | // instantiate |
| 107 | var field = $field.data( 'acf' ); |
| 108 | if ( ! field ) { |
| 109 | field = acf.newFieldObject( $field ); |
| 110 | } |
| 111 | |
| 112 | // return |
| 113 | return field; |
| 114 | }; |
| 115 | |
| 116 | /** |
| 117 | * acf.getFieldObjects |
| 118 | * |
| 119 | * Returns an array of fieldObject instances for the given args |
| 120 | * |
| 121 | * @date 1/2/18 |
| 122 | * @since ACF 5.7.0 |
| 123 | * |
| 124 | * @param object args |
| 125 | * @return array |
| 126 | */ |
| 127 | |
| 128 | acf.getFieldObjects = function ( args ) { |
| 129 | // query |
| 130 | var $fields = acf.findFieldObjects( args ); |
| 131 | |
| 132 | // loop |
| 133 | var fields = []; |
| 134 | $fields.each( function () { |
| 135 | var field = acf.getFieldObject( $( this ) ); |
| 136 | fields.push( field ); |
| 137 | } ); |
| 138 | |
| 139 | // return |
| 140 | return fields; |
| 141 | }; |
| 142 | |
| 143 | /** |
| 144 | * acf.newFieldObject |
| 145 | * |
| 146 | * Initializes and returns a new FieldObject instance |
| 147 | * |
| 148 | * @date 1/2/18 |
| 149 | * @since ACF 5.7.0 |
| 150 | * |
| 151 | * @param jQuery $field The field $el |
| 152 | * @return object |
| 153 | */ |
| 154 | |
| 155 | acf.newFieldObject = function ( $field ) { |
| 156 | // instantiate |
| 157 | var field = new acf.FieldObject( $field ); |
| 158 | |
| 159 | // action |
| 160 | acf.doAction( 'new_field_object', field ); |
| 161 | |
| 162 | // return |
| 163 | return field; |
| 164 | }; |
| 165 | |
| 166 | /** |
| 167 | * actionManager |
| 168 | * |
| 169 | * description |
| 170 | * |
| 171 | * @date 15/12/17 |
| 172 | * @since ACF 5.6.5 |
| 173 | * |
| 174 | * @param type $var Description. Default. |
| 175 | * @return type Description. |
| 176 | */ |
| 177 | |
| 178 | var eventManager = new acf.Model( { |
| 179 | priority: 5, |
| 180 | |
| 181 | initialize: function () { |
| 182 | // actions |
| 183 | var actions = [ 'prepare', 'ready', 'append', 'remove' ]; |
| 184 | |
| 185 | // loop |
| 186 | actions.map( function ( action ) { |
| 187 | this.addFieldActions( action ); |
| 188 | }, this ); |
| 189 | }, |
| 190 | |
| 191 | addFieldActions: function ( action ) { |
| 192 | // vars |
| 193 | var pluralAction = action + '_field_objects'; // ready_field_objects |
| 194 | var singleAction = action + '_field_object'; // ready_field_object |
| 195 | var singleEvent = action + 'FieldObject'; // readyFieldObject |
| 196 | |
| 197 | // global action |
| 198 | var callback = function ( $el /*, arg1, arg2, etc*/ ) { |
| 199 | // vars |
| 200 | var fieldObjects = acf.getFieldObjects( { parent: $el } ); |
| 201 | |
| 202 | // call plural |
| 203 | if ( fieldObjects.length ) { |
| 204 | /// get args [$el, arg1] |
| 205 | var args = acf.arrayArgs( arguments ); |
| 206 | |
| 207 | // modify args [pluralAction, fields, arg1] |
| 208 | args.splice( 0, 1, pluralAction, fieldObjects ); |
| 209 | acf.doAction.apply( null, args ); |
| 210 | } |
| 211 | }; |
| 212 | |
| 213 | // plural action |
| 214 | var pluralCallback = function ( |
| 215 | fieldObjects /*, arg1, arg2, etc*/ |
| 216 | ) { |
| 217 | /// get args [fields, arg1] |
| 218 | var args = acf.arrayArgs( arguments ); |
| 219 | |
| 220 | // modify args [singleAction, fields, arg1] |
| 221 | args.unshift( singleAction ); |
| 222 | |
| 223 | // loop |
| 224 | fieldObjects.map( function ( fieldObject ) { |
| 225 | // modify args [singleAction, field, arg1] |
| 226 | args[ 1 ] = fieldObject; |
| 227 | acf.doAction.apply( null, args ); |
| 228 | } ); |
| 229 | }; |
| 230 | |
| 231 | // single action |
| 232 | var singleCallback = function ( |
| 233 | fieldObject /*, arg1, arg2, etc*/ |
| 234 | ) { |
| 235 | /// get args [$field, arg1] |
| 236 | var args = acf.arrayArgs( arguments ); |
| 237 | |
| 238 | // modify args [singleAction, $field, arg1] |
| 239 | args.unshift( singleAction ); |
| 240 | |
| 241 | // action variations (ready_field/type=image) |
| 242 | var variations = [ 'type', 'name', 'key' ]; |
| 243 | variations.map( function ( variation ) { |
| 244 | args[ 0 ] = |
| 245 | singleAction + |
| 246 | '/' + |
| 247 | variation + |
| 248 | '=' + |
| 249 | fieldObject.get( variation ); |
| 250 | acf.doAction.apply( null, args ); |
| 251 | } ); |
| 252 | |
| 253 | // modify args [arg1] |
| 254 | args.splice( 0, 2 ); |
| 255 | |
| 256 | // event |
| 257 | fieldObject.trigger( singleEvent, args ); |
| 258 | }; |
| 259 | |
| 260 | // add actions |
| 261 | acf.addAction( action, callback, 5 ); |
| 262 | acf.addAction( pluralAction, pluralCallback, 5 ); |
| 263 | acf.addAction( singleAction, singleCallback, 5 ); |
| 264 | }, |
| 265 | } ); |
| 266 | |
| 267 | /** |
| 268 | * fieldManager |
| 269 | * |
| 270 | * description |
| 271 | * |
| 272 | * @date 4/1/18 |
| 273 | * @since ACF 5.6.5 |
| 274 | * |
| 275 | * @param type $var Description. Default. |
| 276 | * @return type Description. |
| 277 | */ |
| 278 | |
| 279 | var fieldManager = new acf.Model( { |
| 280 | id: 'fieldManager', |
| 281 | |
| 282 | events: { |
| 283 | 'submit #post': 'onSubmit', |
| 284 | 'mouseenter .acf-field-list': 'onHoverSortable', |
| 285 | 'click .add-field': 'onClickAdd', |
| 286 | }, |
| 287 | |
| 288 | actions: { |
| 289 | removed_field_object: 'onRemovedField', |
| 290 | sortstop_field_object: 'onReorderField', |
| 291 | delete_field_object: 'onDeleteField', |
| 292 | change_field_object_type: 'onChangeFieldType', |
| 293 | duplicate_field_object: 'onDuplicateField', |
| 294 | }, |
| 295 | |
| 296 | onSubmit: function ( e, $el ) { |
| 297 | // vars |
| 298 | var fields = acf.getFieldObjects(); |
| 299 | |
| 300 | // loop |
| 301 | fields.map( function ( field ) { |
| 302 | field.submit(); |
| 303 | } ); |
| 304 | }, |
| 305 | |
| 306 | setFieldMenuOrder: function ( field ) { |
| 307 | this.renderFields( field.$el.parent() ); |
| 308 | }, |
| 309 | |
| 310 | onHoverSortable: function ( e, $el ) { |
| 311 | // bail early if already sortable |
| 312 | if ( $el.hasClass( 'ui-sortable' ) ) return; |
| 313 | |
| 314 | // sortable |
| 315 | $el.sortable( { |
| 316 | helper: function( event, element ) { |
| 317 | // https://core.trac.wordpress.org/ticket/16972#comment:22 |
| 318 | return element.clone() |
| 319 | .find( ':input' ) |
| 320 | .attr( 'name', function( i, currentName ) { |
| 321 | return 'sort_' + parseInt( Math.random() * 100000, 10 ).toString() + '_' + currentName; |
| 322 | } ) |
| 323 | .end(); |
| 324 | }, |
| 325 | handle: '.acf-sortable-handle', |
| 326 | zIndex: 9999, |
| 327 | connectWith: '.acf-field-list', |
| 328 | start: function ( e, ui ) { |
| 329 | var field = acf.getFieldObject( ui.item ); |
| 330 | ui.placeholder.height( ui.item.height() ); |
| 331 | acf.doAction( 'sortstart_field_object', field, $el ); |
| 332 | }, |
| 333 | update: function ( e, ui ) { |
| 334 | var field = acf.getFieldObject( ui.item ); |
| 335 | acf.doAction( 'sortstop_field_object', field, $el ); |
| 336 | }, |
| 337 | } ); |
| 338 | }, |
| 339 | |
| 340 | onRemovedField: function ( field, $list ) { |
| 341 | this.renderFields( $list ); |
| 342 | }, |
| 343 | |
| 344 | onReorderField: function ( field, $list ) { |
| 345 | field.updateParent(); |
| 346 | this.renderFields( $list ); |
| 347 | }, |
| 348 | |
| 349 | onDeleteField: function ( field ) { |
| 350 | // delete children |
| 351 | field.getFields().map( function ( child ) { |
| 352 | child.delete( { animate: false } ); |
| 353 | } ); |
| 354 | }, |
| 355 | |
| 356 | onChangeFieldType: function ( field ) { |
| 357 | // enable browse field modal button |
| 358 | field.$el.find( 'button.browse-fields' ).prop( 'disabled', false ); |
| 359 | }, |
| 360 | |
| 361 | onDuplicateField: function ( field, newField ) { |
| 362 | // check for children |
| 363 | var children = newField.getFields(); |
| 364 | if ( children.length ) { |
| 365 | // loop |
| 366 | children.map( function ( child ) { |
| 367 | // wipe field |
| 368 | child.wipe(); |
| 369 | |
| 370 | // if the child is open, re-fire the open method to ensure it's initialised correctly. |
| 371 | if ( child.isOpen() ) { |
| 372 | child.open(); |
| 373 | } |
| 374 | |
| 375 | // update parent |
| 376 | child.updateParent(); |
| 377 | } ); |
| 378 | |
| 379 | // action |
| 380 | acf.doAction( |
| 381 | 'duplicate_field_objects', |
| 382 | children, |
| 383 | newField, |
| 384 | field |
| 385 | ); |
| 386 | } |
| 387 | |
| 388 | // set menu order |
| 389 | this.setFieldMenuOrder( newField ); |
| 390 | }, |
| 391 | |
| 392 | renderFields: function ( $list ) { |
| 393 | // vars |
| 394 | var fields = acf.getFieldObjects( { |
| 395 | list: $list, |
| 396 | } ); |
| 397 | |
| 398 | // no fields |
| 399 | if ( ! fields.length ) { |
| 400 | $list.addClass( '-empty' ); |
| 401 | $list |
| 402 | .parents( '.acf-field-list-wrap' ) |
| 403 | .first() |
| 404 | .addClass( '-empty' ); |
| 405 | return; |
| 406 | } |
| 407 | |
| 408 | // has fields |
| 409 | $list.removeClass( '-empty' ); |
| 410 | $list |
| 411 | .parents( '.acf-field-list-wrap' ) |
| 412 | .first() |
| 413 | .removeClass( '-empty' ); |
| 414 | |
| 415 | // prop |
| 416 | fields.map( function ( field, i ) { |
| 417 | field.prop( 'menu_order', i ); |
| 418 | } ); |
| 419 | }, |
| 420 | |
| 421 | onClickAdd: function ( e, $el ) { |
| 422 | let $list; |
| 423 | |
| 424 | if ( $el.hasClass( 'add-first-field' ) ) { |
| 425 | $list = $el.parents( '.acf-field-list' ).eq( 0 ); |
| 426 | } else if ( |
| 427 | $el.parent().hasClass( 'acf-headerbar-actions' ) || |
| 428 | $el.parent().hasClass( 'no-fields-message-inner' ) |
| 429 | ) { |
| 430 | $list = $( '.acf-field-list:first' ); |
| 431 | } else if ( $el.parent().hasClass( 'acf-sub-field-list-header' ) ) { |
| 432 | $list = $el |
| 433 | .parents( '.acf-input:first' ) |
| 434 | .find( '.acf-field-list:first' ); |
| 435 | } else { |
| 436 | $list = $el |
| 437 | .closest( '.acf-tfoot' ) |
| 438 | .siblings( '.acf-field-list' ); |
| 439 | } |
| 440 | |
| 441 | this.addField( $list ); |
| 442 | }, |
| 443 | |
| 444 | addField: function ( $list ) { |
| 445 | // vars |
| 446 | var html = $( '#tmpl-acf-field' ).html(); |
| 447 | var $el = $( html ); |
| 448 | var prevId = $el.data( 'id' ); |
| 449 | var newKey = acf.uniqid( 'field_' ); |
| 450 | |
| 451 | // duplicate |
| 452 | var $newField = acf.duplicate( { |
| 453 | target: $el, |
| 454 | search: prevId, |
| 455 | replace: newKey, |
| 456 | append: function ( $el, $el2 ) { |
| 457 | $list.append( $el2 ); |
| 458 | }, |
| 459 | } ); |
| 460 | |
| 461 | // get instance |
| 462 | var newField = acf.getFieldObject( $newField ); |
| 463 | |
| 464 | // props |
| 465 | newField.prop( 'key', newKey ); |
| 466 | newField.prop( 'ID', 0 ); |
| 467 | newField.prop( 'label', '' ); |
| 468 | newField.prop( 'name', '' ); |
| 469 | |
| 470 | // attr |
| 471 | $newField.attr( 'data-key', newKey ); |
| 472 | $newField.attr( 'data-id', newKey ); |
| 473 | |
| 474 | // update parent prop |
| 475 | newField.updateParent(); |
| 476 | |
| 477 | // focus type |
| 478 | var $type = newField.$input( 'type' ); |
| 479 | setTimeout( function () { |
| 480 | if ( $list.hasClass( 'acf-auto-add-field' ) ) { |
| 481 | $list.removeClass( 'acf-auto-add-field' ); |
| 482 | } else { |
| 483 | $type.trigger( 'focus' ); |
| 484 | } |
| 485 | }, 251 ); |
| 486 | |
| 487 | // open |
| 488 | newField.open(); |
| 489 | |
| 490 | // set menu order |
| 491 | this.renderFields( $list ); |
| 492 | |
| 493 | // action |
| 494 | acf.doAction( 'add_field_object', newField ); |
| 495 | acf.doAction( 'append_field_object', newField ); |
| 496 | }, |
| 497 | } ); |
| 498 | } )( jQuery ); |
| 499 |