PluginProbe ʕ •ᴥ•ʔ
Secure Custom Fields / 6.9.1
Secure Custom Fields v6.9.1
6.9.1 6.9.0 6.8.9 6.8.7 6.8.8 6.8.6 6.8.4 6.8.5 trunk 6.4.0-beta1 6.4.0-beta2 6.4.1 6.4.1-beta3 6.4.1-beta4 6.4.1-beta5 6.4.1-beta6 6.4.1-beta7 6.4.2 6.5.0 6.5.1 6.5.2 6.5.3 6.5.4 6.5.5 6.5.6 6.5.7 6.6.0 6.7.0 6.7.1 6.8.0 6.8.1 6.8.2 6.8.3
secure-custom-fields / assets / src / js / _acf-model.js
secure-custom-fields / assets / src / js Last commit date
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-model.js
901 lines
1 ( function ( $, undefined ) {
2 // Cached regex to split keys for `addEvent`.
3 var delegateEventSplitter = /^(\S+)\s*(.*)$/;
4
5 /**
6 * extend
7 *
8 * Helper function to correctly set up the prototype chain for subclasses
9 * Heavily inspired by backbone.js
10 *
11 * @date 14/12/17
12 * @since ACF 5.6.5
13 *
14 * @param object protoProps New properties for this object.
15 * @return function.
16 */
17
18 var extend = function ( protoProps ) {
19 // vars
20 var Parent = this;
21 var Child;
22
23 // The constructor function for the new subclass is either defined by you
24 // (the "constructor" property in your `extend` definition), or defaulted
25 // by us to simply call the parent constructor.
26 if ( protoProps && protoProps.hasOwnProperty( 'constructor' ) ) {
27 Child = protoProps.constructor;
28 } else {
29 Child = function () {
30 return Parent.apply( this, arguments );
31 };
32 }
33
34 // Add static properties to the constructor function, if supplied.
35 $.extend( Child, Parent );
36
37 // Set the prototype chain to inherit from `parent`, without calling
38 // `parent`'s constructor function and add the prototype properties.
39 Child.prototype = Object.create( Parent.prototype );
40 $.extend( Child.prototype, protoProps );
41 Child.prototype.constructor = Child;
42
43 // Set a convenience property in case the parent's prototype is needed later.
44 //Child.prototype.__parent__ = Parent.prototype;
45
46 // return
47 return Child;
48 };
49
50 /**
51 * Model
52 *
53 * Base class for all inheritance
54 *
55 * @date 14/12/17
56 * @since ACF 5.6.5
57 *
58 * @param object props
59 * @return function.
60 */
61
62 var Model = ( acf.Model = function () {
63 // generate unique client id
64 this.cid = acf.uniqueId( 'acf' );
65
66 // set vars to avoid modifying prototype
67 this.data = $.extend( true, {}, this.data );
68
69 // pass props to setup function
70 this.setup.apply( this, arguments );
71
72 // store on element (allow this.setup to create this.$el)
73 if ( this.$el && ! this.$el.data( 'acf' ) ) {
74 this.$el.data( 'acf', this );
75 }
76
77 // initialize
78 var initialize = function () {
79 this.initialize();
80 this.addEvents();
81 this.addActions();
82 this.addFilters();
83 };
84
85 // initialize on action
86 if ( this.wait && ! acf.didAction( this.wait ) ) {
87 this.addAction( this.wait, initialize );
88
89 // initialize now
90 } else {
91 initialize.apply( this );
92 }
93 } );
94
95 // Attach all inheritable methods to the Model prototype.
96 $.extend( Model.prototype, {
97 // Unique model id
98 id: '',
99
100 // Unique client id
101 cid: '',
102
103 // jQuery element
104 $el: null,
105
106 // Data specific to this instance
107 data: {},
108
109 // toggle used when changing data
110 busy: false,
111 changed: false,
112
113 // Setup events hooks
114 events: {},
115 actions: {},
116 filters: {},
117
118 // class used to avoid nested event triggers
119 eventScope: '',
120
121 // action to wait until initialize
122 wait: false,
123
124 // action priority default
125 priority: 10,
126
127 /**
128 * get
129 *
130 * Gets a specific data value
131 *
132 * @date 14/12/17
133 * @since ACF 5.6.5
134 *
135 * @param string name
136 * @return mixed
137 */
138
139 get: function ( name ) {
140 return this.data[ name ];
141 },
142
143 /**
144 * has
145 *
146 * Returns `true` if the data exists and is not null
147 *
148 * @date 14/12/17
149 * @since ACF 5.6.5
150 *
151 * @param string name
152 * @return boolean
153 */
154
155 has: function ( name ) {
156 return this.get( name ) != null;
157 },
158
159 /**
160 * set
161 *
162 * Sets a specific data value
163 *
164 * @date 14/12/17
165 * @since ACF 5.6.5
166 *
167 * @param string name
168 * @param mixed value
169 * @return this
170 */
171
172 set: function ( name, value, silent ) {
173 // bail if unchanged
174 var prevValue = this.get( name );
175 if ( prevValue == value ) {
176 return this;
177 }
178
179 // set data
180 this.data[ name ] = value;
181
182 // trigger events
183 if ( ! silent ) {
184 this.changed = true;
185 this.trigger( 'changed:' + name, [ value, prevValue ] );
186 this.trigger( 'changed', [ name, value, prevValue ] );
187 }
188
189 // return
190 return this;
191 },
192
193 /**
194 * inherit
195 *
196 * Inherits the data from a jQuery element
197 *
198 * @date 14/12/17
199 * @since ACF 5.6.5
200 *
201 * @param jQuery $el
202 * @return this
203 */
204
205 inherit: function ( data ) {
206 // allow jQuery
207 if ( data instanceof jQuery ) {
208 data = data.data();
209 }
210
211 // extend
212 $.extend( this.data, data );
213
214 // return
215 return this;
216 },
217
218 /**
219 * prop
220 *
221 * mimics the jQuery prop function
222 *
223 * @date 4/6/18
224 * @since ACF 5.6.9
225 *
226 * @param type $var Description. Default.
227 * @return type Description.
228 */
229
230 prop: function () {
231 return this.$el.prop.apply( this.$el, arguments );
232 },
233
234 /**
235 * setup
236 *
237 * Run during constructor function
238 *
239 * @date 14/12/17
240 * @since ACF 5.6.5
241 *
242 * @param n/a
243 * @return n/a
244 */
245
246 setup: function ( props ) {
247 $.extend( this, props );
248 },
249
250 /**
251 * initialize
252 *
253 * Also run during constructor function
254 *
255 * @date 14/12/17
256 * @since ACF 5.6.5
257 *
258 * @param n/a
259 * @return n/a
260 */
261
262 initialize: function () {},
263
264 /**
265 * addElements
266 *
267 * Adds multiple jQuery elements to this object
268 *
269 * @date 9/5/18
270 * @since ACF 5.6.9
271 *
272 * @param type $var Description. Default.
273 * @return type Description.
274 */
275
276 addElements: function ( elements ) {
277 elements = elements || this.elements || null;
278 if ( ! elements || ! Object.keys( elements ).length ) return false;
279 for ( var i in elements ) {
280 this.addElement( i, elements[ i ] );
281 }
282 },
283
284 /**
285 * addElement
286 *
287 * description
288 *
289 * @date 9/5/18
290 * @since ACF 5.6.9
291 *
292 * @param type $var Description. Default.
293 * @return type Description.
294 */
295
296 addElement: function ( name, selector ) {
297 this[ '$' + name ] = this.$( selector );
298 },
299
300 /**
301 * addEvents
302 *
303 * Adds multiple event handlers
304 *
305 * @date 14/12/17
306 * @since ACF 5.6.5
307 *
308 * @param object events {event1 : callback, event2 : callback, etc }
309 * @return n/a
310 */
311
312 addEvents: function ( events ) {
313 events = events || this.events || null;
314 if ( ! events ) return false;
315 for ( var key in events ) {
316 var match = key.match( delegateEventSplitter );
317 this.on( match[ 1 ], match[ 2 ], events[ key ] );
318 }
319 },
320
321 /**
322 * removeEvents
323 *
324 * Removes multiple event handlers
325 *
326 * @date 14/12/17
327 * @since ACF 5.6.5
328 *
329 * @param object events {event1 : callback, event2 : callback, etc }
330 * @return n/a
331 */
332
333 removeEvents: function ( events ) {
334 events = events || this.events || null;
335 if ( ! events ) return false;
336 for ( var key in events ) {
337 var match = key.match( delegateEventSplitter );
338 this.off( match[ 1 ], match[ 2 ], events[ key ] );
339 }
340 },
341
342 /**
343 * getEventTarget
344 *
345 * Returns a jQuery element to trigger an event on.
346 *
347 * @date 5/6/18
348 * @since ACF 5.6.9
349 *
350 * @param jQuery $el The default jQuery element. Optional.
351 * @param string event The event name. Optional.
352 * @return jQuery
353 */
354
355 getEventTarget: function ( $el, event ) {
356 return $el || this.$el || $( document );
357 },
358
359 /**
360 * validateEvent
361 *
362 * Returns true if the event target's closest $el is the same as this.$el
363 * Requires both this.el and this.$el to be defined
364 *
365 * @date 5/6/18
366 * @since ACF 5.6.9
367 *
368 * @param type $var Description. Default.
369 * @return type Description.
370 */
371
372 validateEvent: function ( e ) {
373 if ( this.eventScope ) {
374 return $( e.target ).closest( this.eventScope ).is( this.$el );
375 } else {
376 return true;
377 }
378 },
379
380 /**
381 * proxyEvent
382 *
383 * Returns a new event callback function scoped to this model
384 *
385 * @date 29/3/18
386 * @since ACF 5.6.9
387 *
388 * @param function callback
389 * @return function
390 */
391
392 proxyEvent: function ( callback ) {
393 return this.proxy( function ( e ) {
394 // validate
395 if ( ! this.validateEvent( e ) ) {
396 return;
397 }
398
399 // construct args
400 var args = acf.arrayArgs( arguments );
401 var extraArgs = args.slice( 1 );
402 var eventArgs = [ e, $( e.currentTarget ) ].concat( extraArgs );
403
404 // callback
405 callback.apply( this, eventArgs );
406 } );
407 },
408
409 /**
410 * on
411 *
412 * Adds an event handler similar to jQuery
413 * Uses the instance 'cid' to namespace event
414 *
415 * @date 14/12/17
416 * @since ACF 5.6.5
417 *
418 * @param string name
419 * @param string callback
420 * @return n/a
421 */
422
423 on: function ( a1, a2, a3, a4 ) {
424 // vars
425 var $el, event, selector, callback, args;
426
427 // find args
428 if ( a1 instanceof jQuery ) {
429 // 1. args( $el, event, selector, callback )
430 if ( a4 ) {
431 $el = a1;
432 event = a2;
433 selector = a3;
434 callback = a4;
435
436 // 2. args( $el, event, callback )
437 } else {
438 $el = a1;
439 event = a2;
440 callback = a3;
441 }
442 } else {
443 // 3. args( event, selector, callback )
444 if ( a3 ) {
445 event = a1;
446 selector = a2;
447 callback = a3;
448
449 // 4. args( event, callback )
450 } else {
451 event = a1;
452 callback = a2;
453 }
454 }
455
456 // element
457 $el = this.getEventTarget( $el );
458
459 // modify callback
460 if ( typeof callback === 'string' ) {
461 callback = this.proxyEvent( this[ callback ] );
462 }
463
464 // modify event
465 event = event + '.' + this.cid;
466
467 // args
468 if ( selector ) {
469 args = [ event, selector, callback ];
470 } else {
471 args = [ event, callback ];
472 }
473
474 // on()
475 $el.on.apply( $el, args );
476 },
477
478 /**
479 * off
480 *
481 * Removes an event handler similar to jQuery
482 *
483 * @date 14/12/17
484 * @since ACF 5.6.5
485 *
486 * @param string name
487 * @param string callback
488 * @return n/a
489 */
490
491 off: function ( a1, a2, a3 ) {
492 // vars
493 var $el, event, selector, args;
494
495 // find args
496 if ( a1 instanceof jQuery ) {
497 // 1. args( $el, event, selector )
498 if ( a3 ) {
499 $el = a1;
500 event = a2;
501 selector = a3;
502
503 // 2. args( $el, event )
504 } else {
505 $el = a1;
506 event = a2;
507 }
508 } else {
509 // 3. args( event, selector )
510 if ( a2 ) {
511 event = a1;
512 selector = a2;
513
514 // 4. args( event )
515 } else {
516 event = a1;
517 }
518 }
519
520 // element
521 $el = this.getEventTarget( $el );
522
523 // modify event
524 event = event + '.' + this.cid;
525
526 // args
527 if ( selector ) {
528 args = [ event, selector ];
529 } else {
530 args = [ event ];
531 }
532
533 // off()
534 $el.off.apply( $el, args );
535 },
536
537 /**
538 * trigger
539 *
540 * Triggers an event similar to jQuery
541 *
542 * @date 14/12/17
543 * @since ACF 5.6.5
544 *
545 * @param string name
546 * @param string callback
547 * @return n/a
548 */
549
550 trigger: function ( name, args, bubbles ) {
551 var $el = this.getEventTarget();
552 if ( bubbles ) {
553 $el.trigger.apply( $el, arguments );
554 } else {
555 $el.triggerHandler.apply( $el, arguments );
556 }
557 return this;
558 },
559
560 /**
561 * addActions
562 *
563 * Adds multiple action handlers
564 *
565 * @date 14/12/17
566 * @since ACF 5.6.5
567 *
568 * @param object actions {action1 : callback, action2 : callback, etc }
569 * @return n/a
570 */
571
572 addActions: function ( actions ) {
573 actions = actions || this.actions || null;
574 if ( ! actions ) return false;
575 for ( var i in actions ) {
576 this.addAction( i, actions[ i ] );
577 }
578 },
579
580 /**
581 * removeActions
582 *
583 * Removes multiple action handlers
584 *
585 * @date 14/12/17
586 * @since ACF 5.6.5
587 *
588 * @param object actions {action1 : callback, action2 : callback, etc }
589 * @return n/a
590 */
591
592 removeActions: function ( actions ) {
593 actions = actions || this.actions || null;
594 if ( ! actions ) return false;
595 for ( var i in actions ) {
596 this.removeAction( i, actions[ i ] );
597 }
598 },
599
600 /**
601 * addAction
602 *
603 * Adds an action using the wp.hooks library
604 *
605 * @date 14/12/17
606 * @since ACF 5.6.5
607 *
608 * @param string name
609 * @param string callback
610 * @return n/a
611 */
612
613 addAction: function ( name, callback, priority ) {
614 //console.log('addAction', name, priority);
615 // defaults
616 priority = priority || this.priority;
617
618 // modify callback
619 if ( typeof callback === 'string' ) {
620 callback = this[ callback ];
621 }
622
623 // add
624 acf.addAction( name, callback, priority, this );
625 },
626
627 /**
628 * removeAction
629 *
630 * Remove an action using the wp.hooks library
631 *
632 * @date 14/12/17
633 * @since ACF 5.6.5
634 *
635 * @param string name
636 * @param string callback
637 * @return n/a
638 */
639
640 removeAction: function ( name, callback ) {
641 acf.removeAction( name, this[ callback ] );
642 },
643
644 /**
645 * addFilters
646 *
647 * Adds multiple filter handlers
648 *
649 * @date 14/12/17
650 * @since ACF 5.6.5
651 *
652 * @param object filters {filter1 : callback, filter2 : callback, etc }
653 * @return n/a
654 */
655
656 addFilters: function ( filters ) {
657 filters = filters || this.filters || null;
658 if ( ! filters ) return false;
659 for ( var i in filters ) {
660 this.addFilter( i, filters[ i ] );
661 }
662 },
663
664 /**
665 * addFilter
666 *
667 * Adds a filter using the wp.hooks library
668 *
669 * @date 14/12/17
670 * @since ACF 5.6.5
671 *
672 * @param string name
673 * @param string callback
674 * @return n/a
675 */
676
677 addFilter: function ( name, callback, priority ) {
678 // defaults
679 priority = priority || this.priority;
680
681 // modify callback
682 if ( typeof callback === 'string' ) {
683 callback = this[ callback ];
684 }
685
686 // add
687 acf.addFilter( name, callback, priority, this );
688 },
689
690 /**
691 * removeFilters
692 *
693 * Removes multiple filter handlers
694 *
695 * @date 14/12/17
696 * @since ACF 5.6.5
697 *
698 * @param object filters {filter1 : callback, filter2 : callback, etc }
699 * @return n/a
700 */
701
702 removeFilters: function ( filters ) {
703 filters = filters || this.filters || null;
704 if ( ! filters ) return false;
705 for ( var i in filters ) {
706 this.removeFilter( i, filters[ i ] );
707 }
708 },
709
710 /**
711 * removeFilter
712 *
713 * Remove a filter using the wp.hooks library
714 *
715 * @date 14/12/17
716 * @since ACF 5.6.5
717 *
718 * @param string name
719 * @param string callback
720 * @return n/a
721 */
722
723 removeFilter: function ( name, callback ) {
724 acf.removeFilter( name, this[ callback ] );
725 },
726
727 /**
728 * $
729 *
730 * description
731 *
732 * @date 16/12/17
733 * @since ACF 5.6.5
734 *
735 * @param type $var Description. Default.
736 * @return type Description.
737 */
738
739 $: function ( selector ) {
740 return this.$el.find( selector );
741 },
742
743 /**
744 * remove
745 *
746 * Removes the element and listeners
747 *
748 * @date 19/12/17
749 * @since ACF 5.6.5
750 *
751 * @param type $var Description. Default.
752 * @return type Description.
753 */
754
755 remove: function () {
756 this.removeEvents();
757 this.removeActions();
758 this.removeFilters();
759 this.$el.remove();
760 },
761
762 /**
763 * setTimeout
764 *
765 * description
766 *
767 * @date 16/1/18
768 * @since ACF 5.6.5
769 *
770 * @param type $var Description. Default.
771 * @return type Description.
772 */
773
774 setTimeout: function ( callback, milliseconds ) {
775 return setTimeout( this.proxy( callback ), milliseconds );
776 },
777
778 /**
779 * time
780 *
781 * used for debugging
782 *
783 * @date 7/3/18
784 * @since ACF 5.6.9
785 *
786 * @param type $var Description. Default.
787 * @return type Description.
788 */
789
790 time: function () {
791 console.time( this.id || this.cid );
792 },
793
794 /**
795 * timeEnd
796 *
797 * used for debugging
798 *
799 * @date 7/3/18
800 * @since ACF 5.6.9
801 *
802 * @param type $var Description. Default.
803 * @return type Description.
804 */
805
806 timeEnd: function () {
807 console.timeEnd( this.id || this.cid );
808 },
809
810 /**
811 * show
812 *
813 * description
814 *
815 * @date 15/3/18
816 * @since ACF 5.6.9
817 *
818 * @param type $var Description. Default.
819 * @return type Description.
820 */
821
822 show: function () {
823 acf.show( this.$el );
824 },
825
826 /**
827 * hide
828 *
829 * description
830 *
831 * @date 15/3/18
832 * @since ACF 5.6.9
833 *
834 * @param type $var Description. Default.
835 * @return type Description.
836 */
837
838 hide: function () {
839 acf.hide( this.$el );
840 },
841
842 /**
843 * proxy
844 *
845 * Returns a new function scoped to this model
846 *
847 * @date 29/3/18
848 * @since ACF 5.6.9
849 *
850 * @param function callback
851 * @return function
852 */
853
854 proxy: function ( callback ) {
855 return $.proxy( callback, this );
856 },
857 } );
858
859 // Set up inheritance for the model
860 Model.extend = extend;
861
862 // Global model storage
863 acf.models = {};
864
865 /**
866 * acf.getInstance
867 *
868 * This function will get an instance from an element
869 *
870 * @date 5/3/18
871 * @since ACF 5.6.9
872 *
873 * @param type $var Description. Default.
874 * @return type Description.
875 */
876
877 acf.getInstance = function ( $el ) {
878 return $el.data( 'acf' );
879 };
880
881 /**
882 * acf.getInstances
883 *
884 * This function will get an array of instances from multiple elements
885 *
886 * @date 5/3/18
887 * @since ACF 5.6.9
888 *
889 * @param type $var Description. Default.
890 * @return type Description.
891 */
892
893 acf.getInstances = function ( $el ) {
894 var instances = [];
895 $el.each( function () {
896 instances.push( acf.getInstance( $( this ) ) );
897 } );
898 return instances;
899 };
900 } )( jQuery );
901