PluginProbe ʕ •ᴥ•ʔ
Secure Custom Fields / 6.5.4
Secure Custom Fields v6.5.4
6.9.2 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-field-taxonomy.js
secure-custom-fields / assets / src / js Last commit date
bindings 1 year ago commands 1 year ago pro 1 year ago _acf-compatibility.js 1 year ago _acf-condition-types.js 1 year ago _acf-condition.js 1 year ago _acf-conditions.js 1 year ago _acf-field-accordion.js 1 year ago _acf-field-button-group.js 1 year ago _acf-field-checkbox.js 1 year ago _acf-field-color-picker.js 1 year ago _acf-field-date-picker.js 1 year ago _acf-field-date-time-picker.js 1 year ago _acf-field-file.js 1 year ago _acf-field-google-map.js 1 year ago _acf-field-icon-picker.js 1 year ago _acf-field-image.js 1 year ago _acf-field-link.js 1 year ago _acf-field-oembed.js 1 year ago _acf-field-page-link.js 1 year ago _acf-field-post-object.js 1 year ago _acf-field-radio.js 1 year ago _acf-field-range.js 1 year ago _acf-field-relationship.js 1 year ago _acf-field-select.js 1 year ago _acf-field-tab.js 1 year ago _acf-field-taxonomy.js 1 year ago _acf-field-time-picker.js 1 year ago _acf-field-true-false.js 1 year ago _acf-field-url.js 1 year ago _acf-field-user.js 1 year ago _acf-field-wysiwyg.js 1 year 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 1 year ago _acf-media.js 1 year ago _acf-modal.js 1 year ago _acf-model.js 1 year ago _acf-notice.js 1 year ago _acf-panel.js 1 year ago _acf-popup.js 1 year ago _acf-postbox.js 1 year ago _acf-screen.js 1 year ago _acf-select2.js 1 year ago _acf-tinymce.js 1 year ago _acf-tooltip.js 1 year ago _acf-unload.js 1 year ago _acf-validation.js 1 year ago _acf.js 1 year ago _browse-fields-modal.js 1 year ago _field-group-compatibility.js 1 year ago _field-group-conditions.js 1 year ago _field-group-field.js 1 year ago _field-group-fields.js 1 year ago _field-group-locations.js 1 year ago _field-group-settings.js 1 year ago _field-group.js 1 year 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-field-taxonomy.js
326 lines
1 ( function ( $, undefined ) {
2 var Field = acf.Field.extend( {
3 type: 'taxonomy',
4
5 data: {
6 ftype: 'select',
7 },
8
9 select2: false,
10
11 wait: 'load',
12
13 events: {
14 'click a[data-name="add"]': 'onClickAdd',
15 'click input[type="radio"]': 'onClickRadio',
16 removeField: 'onRemove',
17 },
18
19 $control: function () {
20 return this.$( '.acf-taxonomy-field' );
21 },
22
23 $input: function () {
24 return this.getRelatedPrototype().$input.apply( this, arguments );
25 },
26
27 getRelatedType: function () {
28 // vars
29 var fieldType = this.get( 'ftype' );
30
31 // normalize
32 if ( fieldType == 'multi_select' ) {
33 fieldType = 'select';
34 }
35
36 // return
37 return fieldType;
38 },
39
40 getRelatedPrototype: function () {
41 return acf.getFieldType( this.getRelatedType() ).prototype;
42 },
43
44 getValue: function () {
45 return this.getRelatedPrototype().getValue.apply( this, arguments );
46 },
47
48 setValue: function () {
49 return this.getRelatedPrototype().setValue.apply( this, arguments );
50 },
51
52 initialize: function () {
53 this.getRelatedPrototype().initialize.apply( this, arguments );
54 },
55
56 onRemove: function () {
57 var proto = this.getRelatedPrototype();
58 if ( proto.onRemove ) {
59 proto.onRemove.apply( this, arguments );
60 }
61 },
62
63 onClickAdd: function ( e, $el ) {
64 // vars
65 var field = this;
66 var popup = false;
67 var $form = false;
68 var $name = false;
69 var $parent = false;
70 var $button = false;
71 var $message = false;
72 var notice = false;
73
74 // step 1.
75 var step1 = function () {
76 // popup
77 popup = acf.newPopup( {
78 title: $el.attr( 'title' ),
79 loading: true,
80 width: '300px',
81 } );
82
83 // ajax
84 var ajaxData = {
85 action: 'acf/fields/taxonomy/add_term',
86 field_key: field.get( 'key' ),
87 nonce: field.get( 'nonce' ),
88 };
89
90 // get HTML
91 $.ajax( {
92 url: acf.get( 'ajaxurl' ),
93 data: acf.prepareForAjax( ajaxData ),
94 type: 'post',
95 dataType: 'html',
96 success: step2,
97 } );
98 };
99
100 // step 2.
101 var step2 = function ( html ) {
102 // update popup
103 popup.loading( false );
104 popup.content( html );
105
106 // vars
107 $form = popup.$( 'form' );
108 $name = popup.$( 'input[name="term_name"]' );
109 $parent = popup.$( 'select[name="term_parent"]' );
110 $button = popup.$( '.acf-submit-button' );
111
112 // focus
113 $name.trigger( 'focus' );
114
115 // submit form
116 popup.on( 'submit', 'form', step3 );
117 };
118
119 // step 3.
120 var step3 = function ( e, $el ) {
121 // prevent
122 e.preventDefault();
123 e.stopImmediatePropagation();
124
125 // basic validation
126 if ( $name.val() === '' ) {
127 $name.trigger( 'focus' );
128 return false;
129 }
130
131 // disable
132 acf.startButtonLoading( $button );
133
134 // ajax
135 var ajaxData = {
136 action: 'acf/fields/taxonomy/add_term',
137 field_key: field.get( 'key' ),
138 nonce: field.get( 'nonce' ),
139 term_name: $name.val(),
140 term_parent: $parent.length ? $parent.val() : 0,
141 };
142
143 $.ajax( {
144 url: acf.get( 'ajaxurl' ),
145 data: acf.prepareForAjax( ajaxData ),
146 type: 'post',
147 dataType: 'json',
148 success: step4,
149 } );
150 };
151
152 // step 4.
153 var step4 = function ( json ) {
154 // enable
155 acf.stopButtonLoading( $button );
156
157 // remove prev notice
158 if ( notice ) {
159 notice.remove();
160 }
161
162 // success
163 if ( acf.isAjaxSuccess( json ) ) {
164 // clear name
165 $name.val( '' );
166
167 // update term lists
168 step5( json.data );
169
170 // notice
171 notice = acf.newNotice( {
172 type: 'success',
173 text: acf.getAjaxMessage( json ),
174 target: $form,
175 timeout: 2000,
176 dismiss: false,
177 } );
178 } else {
179 // notice
180 notice = acf.newNotice( {
181 type: 'error',
182 text: acf.getAjaxError( json ),
183 target: $form,
184 timeout: 2000,
185 dismiss: false,
186 } );
187 }
188
189 // focus
190 $name.trigger( 'focus' );
191 };
192
193 // step 5.
194 var step5 = function ( term ) {
195 // update parent dropdown
196 var $option = $(
197 '<option value="' +
198 term.term_id +
199 '">' +
200 term.term_label +
201 '</option>'
202 );
203 if ( term.term_parent ) {
204 $parent
205 .children( 'option[value="' + term.term_parent + '"]' )
206 .after( $option );
207 } else {
208 $parent.append( $option );
209 }
210
211 // add this new term to all taxonomy field
212 var fields = acf.getFields( {
213 type: 'taxonomy',
214 } );
215
216 fields.map( function ( otherField ) {
217 if (
218 otherField.get( 'taxonomy' ) == field.get( 'taxonomy' )
219 ) {
220 otherField.appendTerm( term );
221 }
222 } );
223
224 // select
225 field.selectTerm( term.term_id );
226 };
227
228 // run
229 step1();
230 },
231
232 appendTerm: function ( term ) {
233 if ( this.getRelatedType() == 'select' ) {
234 this.appendTermSelect( term );
235 } else {
236 this.appendTermCheckbox( term );
237 }
238 },
239
240 appendTermSelect: function ( term ) {
241 this.select2.addOption( {
242 id: term.term_id,
243 text: term.term_label,
244 } );
245 },
246
247 appendTermCheckbox: function ( term ) {
248 // vars
249 var name = this.$( '[name]:first' ).attr( 'name' );
250 var $ul = this.$( 'ul:first' );
251
252 // allow multiple selection
253 if ( this.getRelatedType() == 'checkbox' ) {
254 name += '[]';
255 }
256
257 // create new li
258 var $li = $(
259 [
260 '<li data-id="' + term.term_id + '">',
261 '<label>',
262 '<input type="' +
263 this.get( 'ftype' ) +
264 '" value="' +
265 term.term_id +
266 '" name="' +
267 name +
268 '" /> ',
269 '<span>' + term.term_name + '</span>',
270 '</label>',
271 '</li>',
272 ].join( '' )
273 );
274
275 // find parent
276 if ( term.term_parent ) {
277 // vars
278 var $parent = $ul.find(
279 'li[data-id="' + term.term_parent + '"]'
280 );
281
282 // update vars
283 $ul = $parent.children( 'ul' );
284
285 // create ul
286 if ( ! $ul.exists() ) {
287 $ul = $( '<ul class="children acf-bl"></ul>' );
288 $parent.append( $ul );
289 }
290 }
291
292 // append
293 $ul.append( $li );
294 },
295
296 selectTerm: function ( id ) {
297 if ( this.getRelatedType() == 'select' ) {
298 this.select2.selectOption( id );
299 } else {
300 var $input = this.$( 'input[value="' + id + '"]' );
301 $input.prop( 'checked', true ).trigger( 'change' );
302 }
303 },
304
305 onClickRadio: function ( e, $el ) {
306 // vars
307 var $label = $el.parent( 'label' );
308 var selected = $label.hasClass( 'selected' );
309
310 // remove previous selected
311 this.$( '.selected' ).removeClass( 'selected' );
312
313 // add active class
314 $label.addClass( 'selected' );
315
316 // allow null
317 if ( this.get( 'allow_null' ) && selected ) {
318 $label.removeClass( 'selected' );
319 $el.prop( 'checked', false ).trigger( 'change' );
320 }
321 },
322 } );
323
324 acf.registerFieldType( Field );
325 } )( jQuery );
326