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