PluginProbe
MaxButtons – Create buttons / 7.13.2
MaxButtons – Create buttons v7.13.2
6.23 6.24 6.25 6.26 6.26.1 6.27 6.28 6.3 6.4 6.5 6.6 6.7 6.8 6.9 7.0 7.1 7.1.1 7.1.2 7.1.3 7.10 7.11 7.13 7.13.1 7.13.2 7.13.3 All 100 releases
maxbuttons / js / maxbuttons-admin.js

maxbuttons-admin.js in MaxButtons – Create buttons 7.13.2, at js/maxbuttons-admin.js

1,000 lines 25.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 var $ = jQuery;
2
3 function maxAdmin()
4 { }
5
6 maxAdmin.prototype = {
7 //initialized: false,
8 colorUpdateTime: true,
9 colorPalettes: true,
10 fields: null,
11 button_id: null,
12 form_updated: false,
13 tabs: null,
14 }; // MaxAdmin
15
16 maxAdmin.prototype.init = function () {
17
18 this.button_id = $('input[name="button_id"]').val();
19 // Prevents the output button from being clickable (also in admin list view )
20 $(document).on('click', ".maxbutton-preview", function(e) { e.preventDefault(); e.stopPropagation(); });
21 $(document).on('click', '.output .preview-toggle', $.proxy(this.toggle_preview, this));
22
23 // overview input paging
24 $('#maxbuttons .input-paging').on('change', $.proxy(this.do_paging, this));
25
26 $('.manual-toggle').on('click', $.proxy(this.toggleManual, this));
27 $('.manual-entry').draggable({
28 cancel: 'p, li',
29 });
30
31 $(document).on('submit', 'form.mb_ajax_save', $.proxy(this.formAjaxSave, this));
32
33 // copy / delete / trash action buttons via ajax
34 $(document).on('click', '[data-buttonaction]', $.proxy(this.button_action, this ));
35
36 // conditionals
37 $(document).on('reInitConditionals', $.proxy(this.initConditionials, this));
38 this.initConditionials(); // conditional options
39
40 // range inputs
41 $(document).on('change, input', 'input[type="range"]', $.proxy(this.updateRange, this ));
42 this.updateRange(null);
43
44 /*
45 ****
46 ### After this only init for button main edit screen
47 ****
48
49 */
50 if ($('#new-button-form').length == 0)
51 return;
52
53
54 if (this.button_id > 0) {
55 $("#maxbuttons .mb-message").show();
56 }
57
58 this.initResponsive(); // responsive edit interface
59
60 $("#maxbuttons .output").draggable({
61 cancel: '.nodrag',
62 });
63
64 $('#maxbuttons .color-field').alphaColorPicker(
65 {
66 width: 300,
67 palettes: this.colorPalettes,
68 //changeFunc: $.proxy(this.update_color, this),
69 changeFunc: $.proxy( _.throttle(function(event, ui) {
70 event.preventDefault();
71 var color = ui.color.toString();
72 this.update_color(event,ui, color);
73
74 }, 200), this),
75
76 }
77 );
78
79 // clicking on color picker circle scrolls to top and loads # on the page, which is bad.
80 $('.iris-picker-inner .iris-square-value').removeAttr('href');
81 $(document).on('click', '.iris-picker-inner .iris-square-value', function (e) {
82 e.preventDefault(); e.stopPropagation(); return false; });
83
84 /* Copy Color Interface */
85 $('.input.mbcolor .arrows').on('click', $.proxy(this.copyColor, this) );
86
87 $('#radius_toggle').on('click', $.proxy(this.toggleRadiusLock,this));
88
89 if ( typeof buttonFieldMap != 'undefined')
90 this.fields = $.parseJSON(buttonFieldMap);
91
92 // bind to all inputs, except for color-field or items with different handler.
93 $('#maxbuttons input').not('.color-field').on('keyup change', $.proxy(this.update_preview,this));
94 $('#maxbuttons input.color-field').on('focus', $.proxy(this.select_field, this));
95 // $('#maxbuttons input.color-field').on('change', $.proxy(this.checkTransparent, this));
96
97 // init check on transparency
98 /*$('#maxbuttons input.color-field').each($.proxy(function(index, el) {
99 obj = {}
100 obj.target = el;
101 obj.event = false;
102 this.checkTransparent(obj); // fakers be fakers
103 },this)); */
104
105
106 $('#maxbuttons select').on('change', $.proxy(this.update_preview, this));
107
108 $(window).on('beforeunload', $.proxy(function () { if (this.form_updated) return maxajax.leave_page; }, this));
109 $(document).on('keyup', 'input', function (e) {
110
111 if (e.keyCode && e.keyCode == 13)
112 {
113 $(":input")[$(":input").index(document.activeElement) + 1].focus();
114 return false;
115 }
116 });
117
118 $(".button-save").click( $.proxy(function() {
119 this.saveIndicator(false); // prevent alert when saving.
120 $("#new-button-form").submit();
121 return false;
122 }, this) );
123
124 // Expand shortcode tabs for more examples.
125 $('.shortcode-expand').on('click', this.toggleShortcode);
126
127 // URL Linker.
128 $('#url_button').on('click', $.proxy(this.openURLDialog, this) );
129
130 // Sidebar slider
131 $('.block_sidebar .open_control').on('click', this.toggleSidebar);
132
133 }; // INIT
134
135
136 maxAdmin.prototype.repaint_preview = function ()
137 {
138 $('.mb_tab input[type="text"]').trigger('change');
139 $('.mb_tab input[type="number"]').trigger('change');
140 $('.mb_tab select').trigger('change');
141 $('.mb_tab input[type="hidden"]').trigger('change');
142 $('.mb_tab input[type="radio"]:checked').trigger('change');
143 $('.mb_tab input[type="checkbox"]:checked').trigger('change');
144 }
145
146 /** Updates the preview buttons with new CSS lines. Extracts several fields from the fieldmap.
147 * state = csspseudo */
148 maxAdmin.prototype.update_preview = function(e)
149 {
150 e.preventDefault();
151 this.saveIndicator(true);
152 var target = $(e.target);
153
154 // migration to data field
155 var field = $(target).data('field');
156 if (typeof field == 'undefined')
157 var id = $(target).attr('id'); // this should change to be ready for the option to have two the same fields on multi locations.
158 else
159 var id = field;
160
161 var data = this.fields[id];
162 var state = null;
163
164 if (typeof data == 'undefined')
165 return; // field doesn't have updates
166 // check all attributes. Fields can use any of those for different processes.
167 if (typeof data.css != 'undefined')
168 {
169 value = target.val();
170
171 if (typeof data.css_unit != 'undefined' && value.indexOf(data.css_unit) == -1)
172 value += data.css_unit;
173
174 // a target that is checkbox but not checked should unset (empty) value.
175 if (target.is(':checkbox') && ! target.is(':checked') )
176 value = '';
177
178 if (typeof data.pseudo !== 'undefined')
179 {
180 state = data.pseudo;
181 }
182 this.putCSS(data, value, state);
183 }
184 if (typeof data.attr !== 'undefined')
185 {
186 $('.output .result').find('a').attr(data.attr, target.val());
187 }
188
189 if (typeof data.func !== 'undefined')
190 {
191 var funcName = data.func;
192 var self = this;
193 if (funcName.indexOf('.') < 0)
194 {
195 funcName = 'self.' + funcName + '(target)';
196 }
197 else {
198 funcName = funcName + '(target)';
199 }
200
201 try
202 {
203 var callFunc = new Function ('target', 'self', funcName);
204 callFunc(target, this);
205 }
206 catch(err)
207 {
208 console.error(err);
209 }
210
211 }
212
213 };
214
215 maxAdmin.prototype.select_field = function(e)
216 {
217 $(e.target).select();
218 }
219
220 maxAdmin.prototype.button_action = function(e)
221 {
222 e.preventDefault();
223 var action = $(e.target).data('buttonaction');
224 var confirm = $(e.target).data('confirm');
225
226
227 this.form_updated = false;
228
229 if (typeof confirm !== 'undefined')
230 {
231 var ret = window.confirm(confirm);
232 if (! ret)
233 return;
234 }
235
236 var button_id = $(e.target).data('buttonid');
237 console.log(e.target);
238 console.log(button_id);
239 var nonce = $('input[name="' + action + '_nonce"]').val();
240 var paged = $('input[name="paged"]').val();
241
242
243 var url = maxajax.ajax_url;
244 var data =
245 {
246 action: 'mb_button_action',
247 button_action: action,
248 button_id: button_id,
249 nonce: nonce,
250
251 };
252
253 if (typeof paged !== 'undefined')
254 data['paged'] = paged;
255
256 $.post({
257 url: url,
258 data: data,
259 success: function (data) {
260 response = JSON.parse(data);
261
262 if (typeof response.redirection != 'undefined')
263 {
264 window.location = response.redirection;
265 }
266 },
267 error: function () {
268 console.error('error in button action' + action);
269 },
270 });
271 }
272
273 /* Check the copy modal and display a warning if the button has been changes */
274 maxAdmin.prototype.checkCopyModal = function(modal)
275 {
276 if (this.form_updated)
277 {
278 modal.currentModal.find('.mb-message').show();
279
280 }
281 else
282 $(modal.currentModal).find('.mb-message').hide();
283 }
284
285 maxAdmin.prototype.toggle_preview = function (e)
286 {
287 if ( $('.output .inner').is(':hidden') )
288 {
289 $('.output .inner').show();
290 $('.output').css('height', 'auto');
291 $('.preview .preview-toggle').removeClass('dashicons-arrow-down').addClass('dashicons-arrow-up');
292 }
293 else
294 {
295 $('.output .inner').hide();
296 $('.output').css('height', 'auto');
297 $('.preview .preview-toggle').removeClass('dashicons-arrow-up').addClass('dashicons-arrow-down');
298 }
299 };
300
301
302 maxAdmin.prototype.putCSS = function(data,value,state)
303 {
304 state = state || 'both';
305
306 var element = '.maxbutton';
307 if (state == 'hover')
308 element = 'a.hover ';
309 else if(state == 'normal')
310 element = 'a.normal ';
311
312 if (typeof data.csspart != 'undefined')
313 {
314 var parts = data.csspart.split(",");
315 for(i=0; i < parts.length; i++)
316 {
317 var cpart = parts[i];
318 var fullpart = element + " ." + cpart;
319 $('.output .result').find(fullpart).css(data.css, value);
320 }
321 }
322 else
323 $('.output .result').find(element).css(data.css, value);
324
325 }
326
327 maxAdmin.prototype.update_color = function(event, ui, color)
328 {
329 event.preventDefault();
330 this.saveIndicator(true);
331
332 var target = $(event.target);
333 console.log('update_color :: ' + color);
334 if (color.indexOf('#') === -1 && color.indexOf('rgba') < 0)
335 {
336 color = '#' + color;
337 $('#' + id).val(color); // otherwise field value is running 1 click behind.
338 }
339 var id = target.attr('id');
340
341 // toggle transparency when needed.
342 if ( $(target).val() == '')
343 {
344 $(target).parents('.mbcolor').find('.wp-color-result').children('.the_color').css('background-image', 'url(' + maxadmin_settings.icon_transparent + ')');
345 if (typeof event.type !== 'undefined' && event.type == 'change')
346 this.update_color(e, null, 'rgba(0,0,0,0)');
347 }
348 else {
349 $(target).parents('.mbcolor').find('.wp-color-result').children('.the_color').css('background-image', 'none');
350 }
351
352
353 if(id.indexOf('box_shadow') !== -1)
354 {
355 this.updateBoxShadow(target);
356 }
357 else if(id.indexOf('text_shadow') !== -1)
358 {
359 this.updateTextShadow(target);
360 }
361 else if (id.indexOf('gradient') !== -1)
362 {
363 if (id.indexOf('hover') == -1)
364 this.updateGradient();
365 else
366 this.updateGradient(true);
367 }
368 else if (id == 'button_preview')
369 {
370 if (color.indexOf('rgba') >= 0)
371 {
372 // color = '#ffffff';
373 // $('#' + id).val(color);
374 // this.checkTransparent(event);
375 }
376 $(".output .result").css('backgroundColor', color);
377 }
378 else // simple update
379 {
380
381 /* if (id.indexOf('hover') == -1)
382 {
383 state = 'normal';
384 }
385 else
386 {
387 state = 'hover';
388 } */
389
390 var data = this.fields[id];
391 var state = 'normal';
392 if (typeof data.pseudo !== 'undefined')
393 {
394 state = data.pseudo;
395 }
396
397 this.putCSS(data, color, state);
398 return;
399 }
400
401
402 };
403
404 maxAdmin.prototype.copyColor = function (e)
405 {
406 e.preventDefault();
407 e.stopPropagation(); // stop the color picker from closing itself.
408
409 var target = $(e.target);
410 var bindto = $(e.target).parents('[data-bind]');
411 var fieldId = '#' + bindto.data('id'); // Field which is used
412 var bindId = '#' + bindto.data('bind'); // Field is bound to.
413
414 // check which arrow was pressed
415 if (target.hasClass('arrow-right'))
416 var arrow_click = 'right';
417 else
418 var arrow_click = 'left';
419
420 // check on which side the interface is. If arrows are on right side, it's the left side (...)
421 if (bindto.hasClass('right') )
422 var if_side = 'left';
423 else
424 var if_side = 'right';
425
426 /* Decide which color to replace. If interface is left - then right click is copy to other element, but if interface is right, right is overwrite current element.
427 Left : right click - copy, left replace.
428 Right : right click - replace, left copy.
429 */
430 if (if_side == 'left')
431 {
432 if (arrow_click == 'right')
433 copy = true;
434 else
435 copy = false;
436 }
437 else if (if_side == 'right')
438 {
439 if (arrow_click == 'right')
440 copy = false;
441 else
442 copy = true;
443 }
444
445 if ( copy )
446 {
447 $(bindId).val( $(fieldId).val() );
448 $(bindId).trigger('change');
449 $(bindId).wpColorPicker('color', $(fieldId).val());
450 }
451 else
452 {
453 $(fieldId).val( $(bindId).val() );
454 $(fieldId).trigger('change');
455 $(fieldId).wpColorPicker('color', $(bindId).val());
456 }
457
458 }
459
460 maxAdmin.prototype.updateGradient = function(hover)
461 {
462 hover = hover || false;
463
464 var hovtarget = '';
465 if (hover)
466 hovtarget = "_hover";
467
468 var stop = parseInt($('#gradient_stop').val());
469
470 if (isNaN(stop) )
471 stop = 45;
472
473 var gradients_on = $('#use_gradient').prop('checked');
474
475 var color = $('#gradient_start_color' + hovtarget).val();
476 var endcolor = $('#gradient_end_color' + hovtarget).val();
477
478 if (color == '') color = 'rgba(0,0,0,0)';
479 if (endcolor == '') endcolor = 'rgba(0,0,0,0)';
480
481 var start = this.hexToRgb(color);
482 var end = this.hexToRgb(endcolor);
483 var startop = parseInt($('#gradient_start_opacity' + hovtarget).val());
484 var endop = parseInt($('#gradient_end_opacity' + hovtarget).val());
485
486 if (! gradients_on)
487 {
488 end = start;
489 endop = startop;
490 }
491
492 if(isNaN(startop)) startop = 100;
493 if(isNaN(endop)) endop = 100;
494
495 if (!hover)
496 var button = $('.output .result').find('a.normal');
497 else
498 var button = $('.output .result').find('a.hover');
499
500 if (start.indexOf('rgba') < 0)
501 var startrgba = "rgba(" + start + "," + (startop/100) + ") ";
502 else
503 var startrgba = start;
504
505 if (end.indexOf('rgba') < 0)
506 var endrgba = "rgba(" + end + "," + (endop/100) + ") ";
507 else
508 var endrgba = end;
509
510 console.log(start.indexOf('rgba') + ' ' + start + ' ' + startrgba + ' ' + endrgba);
511
512
513 button.css("background", "linear-gradient(" + startrgba + stop + "%," + endrgba + ')');
514 //button.css("background", "linear-gradient( rgba(" + start + "," + (startop/100) + ") " + stop + "%," + " rgba(" + end + "," + (endop/100) + ") )");
515 //button.css("background", "-moz-linear-gradient(" startrgba + stop + "%," + endrgba ")");
516 //button.css("background", "-o-linear-gradient( rgba(" + start + "," + (startop/100) + ") " + stop + "%," + " rgba(" + end + "," + (endop/100) + ") )");
517 //button.css("background", "-webkit-gradient(linear, left top, left bottom, color-stop(" +stop+ "%, rgba(" + start + "," + (startop/100) + ")), color-stop(1, rgba(" + end + "," + (endop/100) + ") ));");
518
519 }
520
521 maxAdmin.prototype.hexToRgb = function(hex) {
522 if (hex.indexOf('rgba') >= 0)
523 return hex;
524
525 hex = hex.replace('#','');
526 var bigint = parseInt(hex, 16);
527 var r = (bigint >> 16) & 255;
528 var g = (bigint >> 8) & 255;
529 var b = bigint & 255;
530
531 return r + "," + g + "," + b;
532 }
533
534 maxAdmin.prototype.updateBoxShadow = function (target)
535 {
536 target = target || null;
537
538 var left = $("#box_shadow_offset_left").val();
539 var top = $("#box_shadow_offset_top").val();
540 var width = $("#box_shadow_width").val();
541 var spread = $('#box_shadow_spread').val();
542
543 var color = $("#box_shadow_color").val();
544 var hovcolor = $("#box_shadow_color_hover").val();
545
546 if (color == '') color = 'rgba(0,0,0,0)';
547 if (hovcolor == '') hovcolor = 'rgba(0,0,0,0)';
548
549 $('.output .result').find('a.normal').css("boxShadow",left + 'px ' + top + 'px ' + width + 'px ' + spread + 'px ' + color);
550 $('.output .result').find('a.hover').css("boxShadow",left + 'px ' + top + 'px ' + width + 'px ' + spread + 'px ' + hovcolor);
551 }
552
553 maxAdmin.prototype.updateTextShadow = function(target,hover)
554 {
555 hover = hover || false;
556
557 var left = $("#text_shadow_offset_left").val();
558 var top = $("#text_shadow_offset_top").val();
559 var width = $("#text_shadow_width").val();
560
561 var color = $("#text_shadow_color").val();
562 var hovcolor = $("#text_shadow_color_hover").val();
563
564 var id = $(target).attr('id');
565 var data = this.fields[id];
566
567 data.css = 'textShadow';
568
569 if (color == '') color = 'rgba(0,0,0,0)';
570 if (hovcolor == '') hovcolor = 'rgba(0,0,0,0)';
571
572 var value = left + 'px ' + top + 'px ' + width + 'px ' + color;
573 this.putCSS(data, value, 'normal');
574
575 value = left + 'px ' + top + 'px ' + width + 'px ' + hovcolor;
576 this.putCSS(data, value, 'hover');
577
578 }
579
580 maxAdmin.prototype.updateAnchorText = function (target)
581 {
582 var preview_text = $('.output .result').find('a .mb-text');
583
584 // This can happen when the text is removed, button is saved, so the preview doesn't load the text element.
585 if (preview_text.length === 0)
586 {
587 $('.output .result').find('a').append('<span class="mb-text"></span>');
588 $('.output .result').find('a .mb-text').css({'display':'block','line-height':'1em','box-sizing':'border-box'});
589
590 this.repaint_preview();
591 }
592 $('.output .result').find('a .mb-text').text(target.val());
593 }
594
595 maxAdmin.prototype.updateGradientOpacity = function(target)
596 {
597 this.updateGradient(true);
598 this.updateGradient(false);
599 }
600
601 maxAdmin.prototype.updateDimension = function (target)
602 {
603 var dimension = $(target).val();
604 var id = $(target).attr('id');
605 var data = this.fields[id];
606 if (dimension > 0)
607 this.putCSS(data, dimension);
608 else
609 this.putCSS(data, 'auto');
610 }
611
612 maxAdmin.prototype.updateRadius = function(target)
613 {
614 var value = target.val();
615 var fields = ['radius_bottom_left', 'radius_bottom_right', 'radius_top_left', 'radius_top_right'];
616
617 if ( $('#radius_toggle').data('lock') == 'lock')
618 {
619 for(i=0; i < fields.length; i++)
620 {
621 var id = fields[i];
622 $('#' + id).val(value);
623 var data = this.fields[id];
624 this.putCSS(data,value + 'px');
625
626 }
627 }
628 else { // update as regular single field
629 var value = $(target).val();
630 var id = $(target).attr('id');
631 var data = this.fields[id];
632 this.putCSS(data, value);
633 }
634 }
635
636 maxAdmin.prototype.toggleRadiusLock = function (event)
637 {
638 var target = $(event.target);
639 var lock = $(target).data('lock');
640 if (lock == 'lock')
641 {
642 $(target).removeClass('dashicons-lock').addClass('dashicons-unlock');
643 $(target).data('lock', 'unlock');
644 }
645 else if (lock == 'unlock')
646 {
647 $(target).removeClass('dashicons-unlock').addClass('dashicons-lock');
648 $(target).data('lock', 'lock');
649 }
650
651 }
652
653
654 maxAdmin.prototype.initResponsive = function()
655 {
656
657 window.maxFoundry.maxadmin.responsive = new mbResponsive();
658 window.maxFoundry.maxadmin.responsive.init(this);
659
660 }
661
662
663 maxAdmin.prototype.do_paging = function(e)
664 {
665 var page = parseInt($(e.target).val());
666
667 if (page <= parseInt($(e.target).attr('max')) )
668 {
669 var url = $(e.target).data("url");
670 window.location = url + "&paged=" + page;
671
672 }
673 }
674
675
676 maxAdmin.prototype.toggleShortcode = function (e)
677 {
678 if ($('.shortcode-expand').hasClass('closed'))
679 {
680 $(' .mb-message.shortcode .expanded').css('display','inline-block');
681 $('.shortcode-expand span').removeClass('dashicons-arrow-down').addClass('dashicons-arrow-up');
682 $('.shortcode-expand').removeClass('closed').addClass('open');
683 }
684 else
685 {
686 $(' .mb-message.shortcode .expanded').css('display','none');
687 $('.shortcode-expand span').addClass('dashicons-arrow-down').removeClass('dashicons-arrow-up');
688 $('.shortcode-expand').addClass('closed').removeClass('open');
689 }
690
691 }
692
693 maxAdmin.prototype.toggleManual = function (e)
694 {
695 e.preventDefault();
696 var $target = $(e.target);
697
698 var subject = $target.data("target");
699 var $newWindow = $('.manual-entry[data-manual="' + subject + '"]');
700
701 if ($newWindow.is(':visible'))
702 {
703 $newWindow.hide();
704 return true;
705 }
706 var $destination = $target.parents('.option-container');
707
708 $newWindow.css('top', '0px');
709 $newWindow.css('right','-25%');
710 $newWindow.prependTo($destination);
711 $newWindow.show();
712 }
713
714 maxAdmin.prototype.resetConditionals = function()
715 {
716 $('[data-show], [data-has]').each(function () {
717 var condition = $(this).data('show');
718 if (typeof condition === 'undefined')
719 {
720 condition = $(this).data('has');
721 }
722 if (typeof condition === 'undefined')
723 {
724 console.error($(this) + 'has a improperly set conditional');
725 return;
726 }
727 var target = condition.target;
728 $(document).off('change','[name="' + target + '"]'); // turn off event
729 });
730
731
732 }
733
734 maxAdmin.prototype.initConditionials = function ()
735 {
736 var mAP = this;
737 this.resetConditionals();
738
739 $('[data-show]').each(function () {
740 var condition = $(this).data('show');
741 var target = condition.target;
742 var values = condition.values;
743 var self = this;
744
745
746 $(document).on('change','[name="' + target + '"]', {child: this, values: values}, $.proxy(mAP.updateConditional, mAP) );
747
748 if ( $('[name="' + target + '"]').length > 1) // trigger change to test condition
749 {
750 $('[name="' + target + '"]:checked').trigger('change', ['conditional']); // radio / checkbox button
751 }
752 else {
753 $('[name="' + target + '"]').trigger('change', ['conditional']);
754 }
755 });
756
757 // problem here is fields having same target, will add the same event over and over //
758 // the target, input array, have a lot of input fields, which all receive the events. this is the issue.
759
760 var updatelist = [];
761
762 $('[data-has]').each(function () {
763 var condition = $(this).data('has');
764 var target = condition.target;
765 var values = condition.values;
766
767
768 $('[name="' + target + '"]').on('change', {target: target, child: this, values: values}, $.proxy(mAP.updateHasConditional, mAP) );
769
770 var targetdecl = '[name="' + target + '"]';
771 if (! $.inArray(targetdecl, updatelist));
772 updatelist.push(targetdecl);
773 });
774
775 if (updatelist.length > 0)
776 {
777 // the issue will a lot of event checking still exist..
778 $(updatelist.toString()).first().trigger('change', ['conditional']);
779 }
780
781 }
782
783 maxAdmin.prototype.updateConditional = function (event)
784 {
785 var data = event.data;
786 var cond_values = data.values;
787 var cond_child = data.child;
788
789 var target = $(event.currentTarget);
790 var value = $(target).val();
791
792 // if type = checkbox: cond_value checked means it has to be 'checked' to show. Otherwise 'unchecked' go hide.
793 if (target.attr('type') === 'checkbox')
794 {
795
796 var checked = $(target).prop('checked');
797
798 if (cond_values == 'checked' && checked)
799 value = 'checked';
800 else if (cond_values == 'unchecked' && !checked)
801 value = 'unchecked';
802 else
803 value = 0;
804 }
805
806 if (cond_values.indexOf(value) >= 0)
807 {
808
809 $(cond_child).fadeIn('fast');
810 $(cond_child).find('input, select').trigger('change');
811 }
812 else
813 {
814 $(cond_child).fadeOut('fast');
815 $(cond_child).find('input, select').trigger('change');
816 }
817 }
818
819 maxAdmin.prototype.updateHasConditional = function(event)
820 {
821 var mAP = this;
822 var data = event.data;
823
824 var cond_values = data.values;
825 var cond_child = data.child;
826
827 var target = data.target;
828
829 var hascond = false;
830
831 /** The issue here is to change this calls, to searches directly for value form cond_values ( mostly 1-3 options ) and not run the entire DOM each time.
832 */
833 var filter = [];
834 $(cond_values).each(function (el)
835 {
836
837 filter.push( '[value=' + this + ']');
838 } );
839
840 if ($('[name="' + target + '"]').filter( filter.toString() ).length > 0)
841 {
842 hascond = true;
843 }
844 else {
845 hascond = false
846 }
847
848 if (hascond)
849 {
850 $(cond_child).fadeIn('fast');
851 }
852 else
853 {
854 $(cond_child).fadeOut('fast');
855 }
856
857 }
858
859 maxAdmin.prototype.updateRange = function (event)
860 {
861 if (typeof event == 'undefined' || event === null )
862 {
863 var targets = $('input[type="range"]');
864 }
865 else
866 {
867 var targets = [event.target];
868 }
869
870 $(targets).each(function () {
871 var value = $(this).val();
872 $(this).parents('.input').find('.range_value output').val(value + '%');
873
874 });
875
876 }
877
878 maxAdmin.prototype.saveIndicator = function(toggle)
879 {
880
881 if (toggle)
882 this.form_updated = true;
883 else
884 this.form_updated = false;
885 }
886
887 // General AJAX form save
888 maxAdmin.prototype.formAjaxSave = function (e)
889 {
890 e.preventDefault();
891 var url = mb_ajax.ajaxurl;
892 var form = $(e.target);
893
894 var data = form.serialize();
895
896
897 $.ajax({
898 type: "POST",
899 url: url,
900 data: data,
901
902 }).done($.proxy(this.saveDone, this));
903 }
904
905 maxAdmin.prototype.saveDone = function (res)
906 {
907 $('[data-form]').prop('disabled', false);
908
909 var json = $.parseJSON(res);
910
911 var result = json.result;
912 var title = json.title;
913
914
915 var collection_id = json.data.id;
916
917 if (typeof json.data.new_nonce !== 'undefined')
918 {
919 var nonce = json.data.new_nonce;
920 $('input[name="nonce"]').val(json.data.new_nonce);
921 }
922
923 if (result)
924 {
925 // if collection is new - add collection_id to the field
926 $('input[name="collection_id"]').val(collection_id);
927
928 // replace the location to the correct collection
929 var href = window.location.href;
930 if (href.indexOf('collection_id') === -1)
931 window.history.replaceState({}, '', href + '&collection_id=' + collection_id);
932
933 // trigger other updates if needed
934 $(document).trigger('mbFormSaved');
935
936 // update previous selection to current state;
937 var order = $('input[name="sorted"]').val();
938 $('input[name="previous_selection"]').val(order);
939
940 // in case the interface needs to be reloaded.
941 if (json.data.reload)
942 {
943 document.location.reload(true);
944 }
945
946 }
947 if (! result)
948 {
949 $modal = window.maxFoundry.maxModal;
950 $modal.newModal('collection_error');
951 $modal.setTitle(title);
952 $modal.setContent(json.body);
953
954 $modal.setControls('<button class="modal_close button-primary">' + json.close_text + '</button>');
955 $modal.show();
956
957 }
958 }
959
960 maxAdmin.prototype.openURLDialog = function(e)
961 {
962 window.wpActiveEditor = 'url'; // $('input[name="url"]'); //true; //we need to override this var as the link dialogue is expecting an actual wp_editor instance
963
964 wpLink.open(); //open the link popup
965 $('#link-options').hide();
966 $('.query-results').css('top', '70px');
967 $('#wp-link-submit').off('click keyup change');
968 $('#wp-link-submit').on('click', $.proxy(this.updateLink, this) );
969 return false;
970 }
971
972 maxAdmin.prototype.updateLink = function (e)
973 {
974 e.preventDefault();
975 var url = $('#wp-link-url').val();
976 var host = maxadmin_settings.homeurl;
977
978 url = url.replace(host, '');
979
980 $('#url').val(url);
981
982 wpLink.close();
983 return false;
984 }
985
986 maxAdmin.prototype.toggleSidebar = function(e)
987 {
988 var target = e.target;
989 var $sidebar = $(target).parents('.block_sidebar');
990
991 if ($sidebar.hasClass('active'))
992 {
993 $sidebar.removeClass('active');
994 }
995 else {
996 $sidebar.addClass('active');
997 }
998
999 }
1000