PluginProbe
Advanced Custom Fields (ACF®) / 3.1.1
Advanced Custom Fields (ACF®) v3.1.1
6.8.9 6.8.8 6.8.7 6.8.6 6.8.5 6.8.4 6.8.3 6.8.2 6.8.1 5.8.5 5.8.6 5.8.7 5.8.8 5.8.9 5.9.0 5.9.1 5.9.2 5.9.3 5.9.4 5.9.5 5.9.6 5.9.7 5.9.8 5.9.9 6.0.0 All 230 releases
advanced-custom-fields / js / fields.js

fields.js in Advanced Custom Fields (ACF®) 3.1.1, at js/fields.js

418 lines 9.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 (function($){
2
3 /*----------------------------------------------------------------------
4 *
5 * Exists
6 *
7 *---------------------------------------------------------------------*/
8
9 $.fn.exists = function()
10 {
11 return $(this).length>0;
12 };
13
14
15
16 /*----------------------------------------------------------------------
17 *
18 * Update Names
19 *
20 *---------------------------------------------------------------------*/
21
22 $.fn.update_names = function(new_no, new_sub_no)
23 {
24
25 //alert('passed through '+total_fields);
26 $(this).find('[name]').each(function()
27 {
28
29 var name = $(this).attr('name');
30 var id = $(this).attr('id');
31
32 if(name && name.indexOf("fields[999]") != -1)
33 {
34 name = name.replace('fields[999]','fields['+new_no+']');
35 }
36 if(id && id.indexOf("fields[999]") != -1)
37 {
38 id = id.replace('fields[999]','fields['+new_no+']');
39 }
40
41 if($(this).closest('.sub_field').hasClass('field_clone'))
42 {
43 // dont change this input (its the clone sub field!)
44 }
45 else
46 {
47 if(name && name.indexOf("[sub_fields][999]") != -1)
48 {
49 name = name.replace('[sub_fields][999]','[sub_fields]['+new_sub_no+']');
50 }
51 if(id && id.indexOf("[sub_fields][999]") != -1)
52 {
53 id = id.replace('[sub_fields][999]','[sub_fields]['+new_sub_no+']');
54 }
55 }
56
57 $(this).attr('name', name);
58 $(this).attr('id', id);
59 });
60 }
61
62
63 /*----------------------------------------------------------------------
64 *
65 * Update Order Numbers
66 *
67 *---------------------------------------------------------------------*/
68
69 function update_order_numbers(){
70
71 $('#acf_fields .fields').each(function(){
72 $(this).children('.field').each(function(i){
73 $(this).find('td.field_order .circle').first().html(i+1);
74 });
75 });
76
77 }
78
79
80 /*----------------------------------------------------------------------
81 *
82 * setup_fields
83 *
84 *---------------------------------------------------------------------*/
85
86 function setup_fields()
87 {
88
89 function uniqid()
90 {
91 var newDate = new Date;
92 return newDate.getTime();
93 }
94
95
96 // add edit button functionality
97 $('#acf_fields a.acf_edit_field').live('click', function(){
98
99 var field = $(this).closest('.field');
100
101 if(field.hasClass('form_open'))
102 {
103 field.removeClass('form_open');
104 }
105 else
106 {
107 field.addClass('form_open');
108 }
109
110 field.children('.field_form_mask').animate({'height':'toggle'}, 500);
111
112 });
113
114
115 // add delete button functionality
116 $('#acf_fields a.acf_delete_field').live('click', function(){
117
118 var field = $(this).closest('.field');
119 var fields = field.closest('.fields');
120 var temp = $('<div style="height:' + field.height() + 'px"></div>');
121 //field.css({'-moz-transform' : 'translate(50px, 0)', 'opacity' : 0, '-moz-transition' : 'all 250ms ease-out'});
122 field.animate({'left' : '50px', 'opacity' : 0}, 250, function(){
123 field.before(temp);
124 field.remove();
125
126 temp.animate({'height' : 0 }, 250, function(){
127 temp.remove();
128 })
129
130 update_order_numbers();
131
132 if(!fields.children('.field').exists())
133 {
134 // no more fields, show the message
135 fields.children('.no_fields_message').show();
136 }
137
138 });
139
140
141
142 });
143
144
145 // show field type options
146 $('#acf_fields tr.field_type select').live('change', function(){
147
148 var tbody = $(this).closest('tbody');
149 var type = $(this).val();
150
151 tbody.children('tr.field_option').hide();
152 tbody.children('tr.field_option').find('[name]').attr('disabled', 'true');
153
154 var tr = tbody.children('tr.field_option_'+type);
155 tr.find('[name]').removeAttr('disabled');
156
157 var tr_top = tbody.children('tr.field_type');
158
159 tr.insertAfter(tr_top);
160 tr.show();
161
162 // firefox radio button fix
163 tr.find('input[type="radio"][data-checked="checked"]').each(function(){
164
165 $(this).removeAttr('checked').attr('checked', 'checked');
166
167 });
168
169
170 }).trigger('change');
171
172
173 // Add Field Button
174 $('#acf_fields #add_field').live('click',function(){
175
176 var table_footer = $(this).closest('.table_footer');
177 var fields = table_footer.siblings('.fields');
178
179
180 // clone last tr
181 var new_field = fields.children('.field_clone').clone();
182 new_field.removeClass('field_clone').addClass('field');
183
184
185 // update input names
186 if(new_field.hasClass('sub_field'))
187 {
188
189 // it is a sub field
190 //console.log(fields.parents('.fields').last());
191 var field_length = fields.parents('.fields').last().children('.field').length;
192 var sub_field_length = fields.children('.field').length;
193 //alert(sub_field_length);
194 //alert('update numbers for sub field! field:'+field_length+', sub:'+sub_field_length);
195
196 new_field.update_names(uniqid(), uniqid());
197 }
198 else
199 {
200 var field_length = fields.children('.field').length;
201 new_field.update_names(uniqid(), 0);
202
203 //alert('update numbers for field! field:'+field_length);
204 }
205
206
207 // append to table
208 fields.children('.field_clone').before(new_field);
209 //fields.append(new_field);
210
211
212 // remove no fields message
213 if(fields.children('.no_fields_message').exists())
214 {
215 fields.children('.no_fields_message').hide();
216 }
217
218 // clear name
219 new_field.find('.field_form input[type="text"]').val('');
220 new_field.find('.field_form input[type="text"]').first().focus();
221 new_field.find('tr.field_type select').trigger('change');
222
223 // open up form
224 new_field.find('a.acf_edit_field').first().trigger('click');
225
226
227 // update order numbers
228 update_order_numbers();
229
230 return false;
231
232
233 });
234
235
236 // Auto complete field name
237 $('#acf_fields tr.field_label input.label').live('blur', function()
238 {
239 var label = $(this);
240 var name = $(this).closest('tr').siblings('tr.field_name').find('input.name');
241
242 if(name.val() == '')
243 {
244 var val = label.val().toLowerCase().split(' ').join('_').split('\'').join('');
245 name.val(val);
246 name.trigger('keyup');
247 }
248 });
249
250
251 // update field meta
252 $('#acf_fields .field_form tr.field_label input.label').live('keyup', function()
253 {
254 var val = $(this).val();
255 var name = $(this).closest('.field').find('td.field_label strong a').first().html(val);
256 });
257 $('.field_form tr.field_name input.name').live('keyup', function()
258 {
259 var val = $(this).val();
260 var name = $(this).closest('.field').find('td.field_name').first().html(val);
261 });
262 $('.field_form tr.field_type select').live('change', function()
263 {
264 var val = $(this).val();
265 var label = $(this).find('option[value="' + val + '"]').html();
266
267 // update field type (if not a clone field)
268 if($(this).closest('.field_clone').length == 0)
269 {
270 $(this).closest('.field').find('td.field_type').html(label);
271 }
272
273 });
274
275
276 // sortable
277 $('#acf_fields td.field_order').live('mouseover', function(){
278
279 var fields = $(this).closest('.fields');
280
281 if(fields.hasClass('sortable')) return false;
282
283 fields.addClass('sortable').sortable({
284 update: function(event, ui){
285 update_order_numbers();
286 },
287 handle: 'td.field_order'
288 });
289 });
290
291 }
292
293
294 /*----------------------------------------------------------------------
295 *
296 * setup_rules
297 *
298 *---------------------------------------------------------------------*/
299
300 function setup_rules()
301 {
302 var tbody = $('table#location_rules tbody');
303
304
305 // show field type options
306 tbody.find('td.param select').live('change', function(){
307
308 var tr = $(this).closest('tr');
309 var val = $(this).val();
310
311
312 // does it have options?
313 if(!$(this).find('option[value="options_page"]').exists())
314 {
315 //console.log('select: '+type+'. parent length: '+$(this).closest('.repeater').length);
316 $(this).append('<option value="options_page" disabled="true">Options Page (Unlock field with activation code)</option>');
317
318 }
319
320
321 tr.find('td.value div').hide();
322 tr.find('td.value div [name]').attr('disabled', 'true');
323
324 tr.find('td.value div[rel="'+val+'"]').show();
325 tr.find('td.value div[rel="'+val+'"] [name]').removeAttr('disabled');
326
327 }).trigger('change');
328
329
330 // Add Button
331 tbody.find('td.buttons a.add').live('click',function(){
332
333 var tr_count = $(this).closest('tbody').children('tr').length;
334 var tr = $(this).closest('tr').clone();
335
336 tr.insertAfter($(this).closest('tr'));
337
338 update_names();
339
340 can_remove_more();
341
342 return false;
343
344 });
345
346
347 // Remove Button
348 tbody.find('td.buttons a.remove').live('click',function(){
349
350 var tr = $(this).closest('tr').remove();
351
352 can_remove_more();
353
354 return false;
355
356 });
357
358 function can_remove_more()
359 {
360 if(tbody.children('tr').length == 1)
361 {
362 tbody.children('tr').each(function(){
363 $(this).find('td.buttons a.remove').addClass('disabled');
364 });
365 }
366 else
367 {
368 tbody.children('tr').each(function(){
369 $(this).find('td.buttons a.remove').removeClass('disabled');
370 });
371 }
372
373 }
374
375 can_remove_more();
376
377 function update_names()
378 {
379 tbody.children('tr').each(function(i){
380
381 $(this).find('[name]').each(function(){
382
383 var name = $(this).attr('name').split("][");
384
385 var new_name = name[0] + "][" + i + "][" + name[2];
386
387 $(this).attr('name', new_name).attr('id', new_name);
388 });
389
390 })
391 }
392
393 }
394
395 /*----------------------------------------------------------------------
396 *
397 * Document Ready
398 *
399 *---------------------------------------------------------------------*/
400
401 $(document).ready(function(){
402
403 // firefox radio button bug (fixed on line 152)
404 //if($.browser.mozilla) $("form").attr("autocomplete", "off");
405
406
407 // add active to Settings Menu
408 //$('#adminmenu #menu-settings').removeClass('wp-not-current-submenu').addClass('current wp-menu-open wp-has-current-submenu');
409
410 // setup fields
411 setup_fields();
412 setup_rules();
413
414 });
415
416
417 })(jQuery);
418