PluginProbe
MaxButtons – Create buttons / 7.11
MaxButtons – Create buttons v7.11
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.11, at js/maxbuttons-admin.js

998 lines 25.1 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(); });
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 var nonce = $('input[name="' + action + '_nonce"]').val();
238 var paged = $('input[name="paged"]').val();
239
240
241 var url = maxajax.ajax_url;
242 var data =
243 {
244 action: 'mb_button_action',
245 button_action: action,
246 button_id: button_id,
247 nonce: nonce,
248
249 };
250
251 if (typeof paged !== 'undefined')
252 data['paged'] = paged;
253
254 $.post({
255 url: url,
256 data: data,
257 success: function (data) {
258 response = JSON.parse(data);
259
260 if (typeof response.redirection != 'undefined')
261 {
262 window.location = response.redirection;
263 }
264 },
265 error: function () {
266 console.error('error in button action' + action);
267 },
268 });
269 }
270
271 /* Check the copy modal and display a warning if the button has been changes */
272 maxAdmin.prototype.checkCopyModal = function(modal)
273 {
274 if (this.form_updated)
275 {
276 modal.currentModal.find('.mb-message').show();
277
278 }
279 else
280 $(modal.currentModal).find('.mb-message').hide();
281 }
282
283 maxAdmin.prototype.toggle_preview = function (e)
284 {
285 if ( $('.output .inner').is(':hidden') )
286 {
287 $('.output .inner').show();
288 $('.output').css('height', 'auto');
289 $('.preview .preview-toggle').removeClass('dashicons-arrow-down').addClass('dashicons-arrow-up');
290 }
291 else
292 {
293 $('.output .inner').hide();
294 $('.output').css('height', 'auto');
295 $('.preview .preview-toggle').removeClass('dashicons-arrow-up').addClass('dashicons-arrow-down');
296 }
297 };
298
299
300 maxAdmin.prototype.putCSS = function(data,value,state)
301 {
302 state = state || 'both';
303
304 var element = '.maxbutton';
305 if (state == 'hover')
306 element = 'a.hover ';
307 else if(state == 'normal')
308 element = 'a.normal ';
309
310 if (typeof data.csspart != 'undefined')
311 {
312 var parts = data.csspart.split(",");
313 for(i=0; i < parts.length; i++)
314 {
315 var cpart = parts[i];
316 var fullpart = element + " ." + cpart;
317 $('.output .result').find(fullpart).css(data.css, value);
318 }
319 }
320 else
321 $('.output .result').find(element).css(data.css, value);
322
323 }
324
325 maxAdmin.prototype.update_color = function(event, ui, color)
326 {
327 event.preventDefault();
328 this.saveIndicator(true);
329
330 var target = $(event.target);
331 console.log('update_color :: ' + color);
332 if (color.indexOf('#') === -1 && color.indexOf('rgba') < 0)
333 {
334 color = '#' + color;
335 $('#' + id).val(color); // otherwise field value is running 1 click behind.
336 }
337 var id = target.attr('id');
338
339 // toggle transparency when needed.
340 if ( $(target).val() == '')
341 {
342 $(target).parents('.mbcolor').find('.wp-color-result').children('.the_color').css('background-image', 'url(' + maxadmin_settings.icon_transparent + ')');
343 if (typeof event.type !== 'undefined' && event.type == 'change')
344 this.update_color(e, null, 'rgba(0,0,0,0)');
345 }
346 else {
347 $(target).parents('.mbcolor').find('.wp-color-result').children('.the_color').css('background-image', 'none');
348 }
349
350
351 if(id.indexOf('box_shadow') !== -1)
352 {
353 this.updateBoxShadow(target);
354 }
355 else if(id.indexOf('text_shadow') !== -1)
356 {
357 this.updateTextShadow(target);
358 }
359 else if (id.indexOf('gradient') !== -1)
360 {
361 if (id.indexOf('hover') == -1)
362 this.updateGradient();
363 else
364 this.updateGradient(true);
365 }
366 else if (id == 'button_preview')
367 {
368 if (color.indexOf('rgba') >= 0)
369 {
370 // color = '#ffffff';
371 // $('#' + id).val(color);
372 // this.checkTransparent(event);
373 }
374 $(".output .result").css('backgroundColor', color);
375 }
376 else // simple update
377 {
378
379 /* if (id.indexOf('hover') == -1)
380 {
381 state = 'normal';
382 }
383 else
384 {
385 state = 'hover';
386 } */
387
388 var data = this.fields[id];
389 var state = 'normal';
390 if (typeof data.pseudo !== 'undefined')
391 {
392 state = data.pseudo;
393 }
394
395 this.putCSS(data, color, state);
396 return;
397 }
398
399
400 };
401
402 maxAdmin.prototype.copyColor = function (e)
403 {
404 e.preventDefault();
405 e.stopPropagation(); // stop the color picker from closing itself.
406
407 var target = $(e.target);
408 var bindto = $(e.target).parents('[data-bind]');
409 var fieldId = '#' + bindto.data('id'); // Field which is used
410 var bindId = '#' + bindto.data('bind'); // Field is bound to.
411
412 // check which arrow was pressed
413 if (target.hasClass('arrow-right'))
414 var arrow_click = 'right';
415 else
416 var arrow_click = 'left';
417
418 // check on which side the interface is. If arrows are on right side, it's the left side (...)
419 if (bindto.hasClass('right') )
420 var if_side = 'left';
421 else
422 var if_side = 'right';
423
424 /* 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.
425 Left : right click - copy, left replace.
426 Right : right click - replace, left copy.
427 */
428 if (if_side == 'left')
429 {
430 if (arrow_click == 'right')
431 copy = true;
432 else
433 copy = false;
434 }
435 else if (if_side == 'right')
436 {
437 if (arrow_click == 'right')
438 copy = false;
439 else
440 copy = true;
441 }
442
443 if ( copy )
444 {
445 $(bindId).val( $(fieldId).val() );
446 $(bindId).trigger('change');
447 $(bindId).wpColorPicker('color', $(fieldId).val());
448 }
449 else
450 {
451 $(fieldId).val( $(bindId).val() );
452 $(fieldId).trigger('change');
453 $(fieldId).wpColorPicker('color', $(bindId).val());
454 }
455
456 }
457
458 maxAdmin.prototype.updateGradient = function(hover)
459 {
460 hover = hover || false;
461
462 var hovtarget = '';
463 if (hover)
464 hovtarget = "_hover";
465
466 var stop = parseInt($('#gradient_stop').val());
467
468 if (isNaN(stop) )
469 stop = 45;
470
471 var gradients_on = $('#use_gradient').prop('checked');
472
473 var color = $('#gradient_start_color' + hovtarget).val();
474 var endcolor = $('#gradient_end_color' + hovtarget).val();
475
476 if (color == '') color = 'rgba(0,0,0,0)';
477 if (endcolor == '') endcolor = 'rgba(0,0,0,0)';
478
479 var start = this.hexToRgb(color);
480 var end = this.hexToRgb(endcolor);
481 var startop = parseInt($('#gradient_start_opacity' + hovtarget).val());
482 var endop = parseInt($('#gradient_end_opacity' + hovtarget).val());
483
484 if (! gradients_on)
485 {
486 end = start;
487 endop = startop;
488 }
489
490 if(isNaN(startop)) startop = 100;
491 if(isNaN(endop)) endop = 100;
492
493 if (!hover)
494 var button = $('.output .result').find('a.normal');
495 else
496 var button = $('.output .result').find('a.hover');
497
498 if (start.indexOf('rgba') < 0)
499 var startrgba = "rgba(" + start + "," + (startop/100) + ") ";
500 else
501 var startrgba = start;
502
503 if (end.indexOf('rgba') < 0)
504 var endrgba = "rgba(" + end + "," + (endop/100) + ") ";
505 else
506 var endrgba = end;
507
508 console.log(start.indexOf('rgba') + ' ' + start + ' ' + startrgba + ' ' + endrgba);
509
510
511 button.css("background", "linear-gradient(" + startrgba + stop + "%," + endrgba + ')');
512 //button.css("background", "linear-gradient( rgba(" + start + "," + (startop/100) + ") " + stop + "%," + " rgba(" + end + "," + (endop/100) + ") )");
513 //button.css("background", "-moz-linear-gradient(" startrgba + stop + "%," + endrgba ")");
514 //button.css("background", "-o-linear-gradient( rgba(" + start + "," + (startop/100) + ") " + stop + "%," + " rgba(" + end + "," + (endop/100) + ") )");
515 //button.css("background", "-webkit-gradient(linear, left top, left bottom, color-stop(" +stop+ "%, rgba(" + start + "," + (startop/100) + ")), color-stop(1, rgba(" + end + "," + (endop/100) + ") ));");
516
517 }
518
519 maxAdmin.prototype.hexToRgb = function(hex) {
520 if (hex.indexOf('rgba') >= 0)
521 return hex;
522
523 hex = hex.replace('#','');
524 var bigint = parseInt(hex, 16);
525 var r = (bigint >> 16) & 255;
526 var g = (bigint >> 8) & 255;
527 var b = bigint & 255;
528
529 return r + "," + g + "," + b;
530 }
531
532 maxAdmin.prototype.updateBoxShadow = function (target)
533 {
534 target = target || null;
535
536 var left = $("#box_shadow_offset_left").val();
537 var top = $("#box_shadow_offset_top").val();
538 var width = $("#box_shadow_width").val();
539 var spread = $('#box_shadow_spread').val();
540
541 var color = $("#box_shadow_color").val();
542 var hovcolor = $("#box_shadow_color_hover").val();
543
544 if (color == '') color = 'rgba(0,0,0,0)';
545 if (hovcolor == '') hovcolor = 'rgba(0,0,0,0)';
546
547 $('.output .result').find('a.normal').css("boxShadow",left + 'px ' + top + 'px ' + width + 'px ' + spread + 'px ' + color);
548 $('.output .result').find('a.hover').css("boxShadow",left + 'px ' + top + 'px ' + width + 'px ' + spread + 'px ' + hovcolor);
549 }
550
551 maxAdmin.prototype.updateTextShadow = function(target,hover)
552 {
553 hover = hover || false;
554
555 var left = $("#text_shadow_offset_left").val();
556 var top = $("#text_shadow_offset_top").val();
557 var width = $("#text_shadow_width").val();
558
559 var color = $("#text_shadow_color").val();
560 var hovcolor = $("#text_shadow_color_hover").val();
561
562 var id = $(target).attr('id');
563 var data = this.fields[id];
564
565 data.css = 'textShadow';
566
567 if (color == '') color = 'rgba(0,0,0,0)';
568 if (hovcolor == '') hovcolor = 'rgba(0,0,0,0)';
569
570 var value = left + 'px ' + top + 'px ' + width + 'px ' + color;
571 this.putCSS(data, value, 'normal');
572
573 value = left + 'px ' + top + 'px ' + width + 'px ' + hovcolor;
574 this.putCSS(data, value, 'hover');
575
576 }
577
578 maxAdmin.prototype.updateAnchorText = function (target)
579 {
580 var preview_text = $('.output .result').find('a .mb-text');
581
582 // This can happen when the text is removed, button is saved, so the preview doesn't load the text element.
583 if (preview_text.length === 0)
584 {
585 $('.output .result').find('a').append('<span class="mb-text"></span>');
586 $('.output .result').find('a .mb-text').css({'display':'block','line-height':'1em','box-sizing':'border-box'});
587
588 this.repaint_preview();
589 }
590 $('.output .result').find('a .mb-text').text(target.val());
591 }
592
593 maxAdmin.prototype.updateGradientOpacity = function(target)
594 {
595 this.updateGradient(true);
596 this.updateGradient(false);
597 }
598
599 maxAdmin.prototype.updateDimension = function (target)
600 {
601 var dimension = $(target).val();
602 var id = $(target).attr('id');
603 var data = this.fields[id];
604 if (dimension > 0)
605 this.putCSS(data, dimension);
606 else
607 this.putCSS(data, 'auto');
608 }
609
610 maxAdmin.prototype.updateRadius = function(target)
611 {
612 var value = target.val();
613 var fields = ['radius_bottom_left', 'radius_bottom_right', 'radius_top_left', 'radius_top_right'];
614
615 if ( $('#radius_toggle').data('lock') == 'lock')
616 {
617 for(i=0; i < fields.length; i++)
618 {
619 var id = fields[i];
620 $('#' + id).val(value);
621 var data = this.fields[id];
622 this.putCSS(data,value + 'px');
623
624 }
625 }
626 else { // update as regular single field
627 var value = $(target).val();
628 var id = $(target).attr('id');
629 var data = this.fields[id];
630 this.putCSS(data, value);
631 }
632 }
633
634 maxAdmin.prototype.toggleRadiusLock = function (event)
635 {
636 var target = $(event.target);
637 var lock = $(target).data('lock');
638 if (lock == 'lock')
639 {
640 $(target).removeClass('dashicons-lock').addClass('dashicons-unlock');
641 $(target).data('lock', 'unlock');
642 }
643 else if (lock == 'unlock')
644 {
645 $(target).removeClass('dashicons-unlock').addClass('dashicons-lock');
646 $(target).data('lock', 'lock');
647 }
648
649 }
650
651
652 maxAdmin.prototype.initResponsive = function()
653 {
654
655 window.maxFoundry.maxadmin.responsive = new mbResponsive();
656 window.maxFoundry.maxadmin.responsive.init(this);
657
658 }
659
660
661 maxAdmin.prototype.do_paging = function(e)
662 {
663 var page = parseInt($(e.target).val());
664
665 if (page <= parseInt($(e.target).attr('max')) )
666 {
667 var url = $(e.target).data("url");
668 window.location = url + "&paged=" + page;
669
670 }
671 }
672
673
674 maxAdmin.prototype.toggleShortcode = function (e)
675 {
676 if ($('.shortcode-expand').hasClass('closed'))
677 {
678 $(' .mb-message.shortcode .expanded').css('display','inline-block');
679 $('.shortcode-expand span').removeClass('dashicons-arrow-down').addClass('dashicons-arrow-up');
680 $('.shortcode-expand').removeClass('closed').addClass('open');
681 }
682 else
683 {
684 $(' .mb-message.shortcode .expanded').css('display','none');
685 $('.shortcode-expand span').addClass('dashicons-arrow-down').removeClass('dashicons-arrow-up');
686 $('.shortcode-expand').addClass('closed').removeClass('open');
687 }
688
689 }
690
691 maxAdmin.prototype.toggleManual = function (e)
692 {
693 e.preventDefault();
694 var $target = $(e.target);
695
696 var subject = $target.data("target");
697 var $newWindow = $('.manual-entry[data-manual="' + subject + '"]');
698
699 if ($newWindow.is(':visible'))
700 {
701 $newWindow.hide();
702 return true;
703 }
704 var $destination = $target.parents('.option-container');
705
706 $newWindow.css('top', '0px');
707 $newWindow.css('right','-25%');
708 $newWindow.prependTo($destination);
709 $newWindow.show();
710 }
711
712 maxAdmin.prototype.resetConditionals = function()
713 {
714 $('[data-show], [data-has]').each(function () {
715 var condition = $(this).data('show');
716 if (typeof condition === 'undefined')
717 {
718 condition = $(this).data('has');
719 }
720 if (typeof condition === 'undefined')
721 {
722 console.error($(this) + 'has a improperly set conditional');
723 return;
724 }
725 var target = condition.target;
726 $(document).off('change','[name="' + target + '"]'); // turn off event
727 });
728
729
730 }
731
732 maxAdmin.prototype.initConditionials = function ()
733 {
734 var mAP = this;
735 this.resetConditionals();
736
737 $('[data-show]').each(function () {
738 var condition = $(this).data('show');
739 var target = condition.target;
740 var values = condition.values;
741 var self = this;
742
743
744 $(document).on('change','[name="' + target + '"]', {child: this, values: values}, $.proxy(mAP.updateConditional, mAP) );
745
746 if ( $('[name="' + target + '"]').length > 1) // trigger change to test condition
747 {
748 $('[name="' + target + '"]:checked').trigger('change', ['conditional']); // radio / checkbox button
749 }
750 else {
751 $('[name="' + target + '"]').trigger('change', ['conditional']);
752 }
753 });
754
755 // problem here is fields having same target, will add the same event over and over //
756 // the target, input array, have a lot of input fields, which all receive the events. this is the issue.
757
758 var updatelist = [];
759
760 $('[data-has]').each(function () {
761 var condition = $(this).data('has');
762 var target = condition.target;
763 var values = condition.values;
764
765
766 $('[name="' + target + '"]').on('change', {target: target, child: this, values: values}, $.proxy(mAP.updateHasConditional, mAP) );
767
768 var targetdecl = '[name="' + target + '"]';
769 if (! $.inArray(targetdecl, updatelist));
770 updatelist.push(targetdecl);
771 });
772
773 if (updatelist.length > 0)
774 {
775 // the issue will a lot of event checking still exist..
776 $(updatelist.toString()).first().trigger('change', ['conditional']);
777 }
778
779 }
780
781 maxAdmin.prototype.updateConditional = function (event)
782 {
783 var data = event.data;
784 var cond_values = data.values;
785 var cond_child = data.child;
786
787 var target = $(event.currentTarget);
788 var value = $(target).val();
789
790 // if type = checkbox: cond_value checked means it has to be 'checked' to show. Otherwise 'unchecked' go hide.
791 if (target.attr('type') === 'checkbox')
792 {
793
794 var checked = $(target).prop('checked');
795
796 if (cond_values == 'checked' && checked)
797 value = 'checked';
798 else if (cond_values == 'unchecked' && !checked)
799 value = 'unchecked';
800 else
801 value = 0;
802 }
803
804 if (cond_values.indexOf(value) >= 0)
805 {
806
807 $(cond_child).fadeIn('fast');
808 $(cond_child).find('input, select').trigger('change');
809 }
810 else
811 {
812 $(cond_child).fadeOut('fast');
813 $(cond_child).find('input, select').trigger('change');
814 }
815 }
816
817 maxAdmin.prototype.updateHasConditional = function(event)
818 {
819 var mAP = this;
820 var data = event.data;
821
822 var cond_values = data.values;
823 var cond_child = data.child;
824
825 var target = data.target;
826
827 var hascond = false;
828
829 /** 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.
830 */
831 var filter = [];
832 $(cond_values).each(function (el)
833 {
834
835 filter.push( '[value=' + this + ']');
836 } );
837
838 if ($('[name="' + target + '"]').filter( filter.toString() ).length > 0)
839 {
840 hascond = true;
841 }
842 else {
843 hascond = false
844 }
845
846 if (hascond)
847 {
848 $(cond_child).fadeIn('fast');
849 }
850 else
851 {
852 $(cond_child).fadeOut('fast');
853 }
854
855 }
856
857 maxAdmin.prototype.updateRange = function (event)
858 {
859 if (typeof event == 'undefined' || event === null )
860 {
861 var targets = $('input[type="range"]');
862 }
863 else
864 {
865 var targets = [event.target];
866 }
867
868 $(targets).each(function () {
869 var value = $(this).val();
870 $(this).parents('.input').find('.range_value output').val(value + '%');
871
872 });
873
874 }
875
876 maxAdmin.prototype.saveIndicator = function(toggle)
877 {
878
879 if (toggle)
880 this.form_updated = true;
881 else
882 this.form_updated = false;
883 }
884
885 // General AJAX form save
886 maxAdmin.prototype.formAjaxSave = function (e)
887 {
888 e.preventDefault();
889 var url = mb_ajax.ajaxurl;
890 var form = $(e.target);
891
892 var data = form.serialize();
893
894
895 $.ajax({
896 type: "POST",
897 url: url,
898 data: data,
899
900 }).done($.proxy(this.saveDone, this));
901 }
902
903 maxAdmin.prototype.saveDone = function (res)
904 {
905 $('[data-form]').prop('disabled', false);
906
907 var json = $.parseJSON(res);
908
909 var result = json.result;
910 var title = json.title;
911
912
913 var collection_id = json.data.id;
914
915 if (typeof json.data.new_nonce !== 'undefined')
916 {
917 var nonce = json.data.new_nonce;
918 $('input[name="nonce"]').val(json.data.new_nonce);
919 }
920
921 if (result)
922 {
923 // if collection is new - add collection_id to the field
924 $('input[name="collection_id"]').val(collection_id);
925
926 // replace the location to the correct collection
927 var href = window.location.href;
928 if (href.indexOf('collection_id') === -1)
929 window.history.replaceState({}, '', href + '&collection_id=' + collection_id);
930
931 // trigger other updates if needed
932 $(document).trigger('mbFormSaved');
933
934 // update previous selection to current state;
935 var order = $('input[name="sorted"]').val();
936 $('input[name="previous_selection"]').val(order);
937
938 // in case the interface needs to be reloaded.
939 if (json.data.reload)
940 {
941 document.location.reload(true);
942 }
943
944 }
945 if (! result)
946 {
947 $modal = window.maxFoundry.maxModal;
948 $modal.newModal('collection_error');
949 $modal.setTitle(title);
950 $modal.setContent(json.body);
951
952 $modal.setControls('<button class="modal_close button-primary">' + json.close_text + '</button>');
953 $modal.show();
954
955 }
956 }
957
958 maxAdmin.prototype.openURLDialog = function(e)
959 {
960 window.wpActiveEditor = 'url'; // $('input[name="url"]'); //true; //we need to override this var as the link dialogue is expecting an actual wp_editor instance
961
962 wpLink.open(); //open the link popup
963 $('#link-options').hide();
964 $('.query-results').css('top', '70px');
965 $('#wp-link-submit').off('click keyup change');
966 $('#wp-link-submit').on('click', $.proxy(this.updateLink, this) );
967 return false;
968 }
969
970 maxAdmin.prototype.updateLink = function (e)
971 {
972 e.preventDefault();
973 var url = $('#wp-link-url').val();
974 var host = maxadmin_settings.homeurl;
975
976 url = url.replace(host, '');
977
978 $('#url').val(url);
979
980 wpLink.close();
981 return false;
982 }
983
984 maxAdmin.prototype.toggleSidebar = function(e)
985 {
986 var target = e.target;
987 var $sidebar = $(target).parents('.block_sidebar');
988
989 if ($sidebar.hasClass('active'))
990 {
991 $sidebar.removeClass('active');
992 }
993 else {
994 $sidebar.addClass('active');
995 }
996
997 }
998