PluginProbe
Easy Hotel – Powerful Hotel Booking / trunk
Easy Hotel – Powerful Hotel Booking vtrunk
2.0.8 2.0.7 2.0.6 2.0.5 2.0.4 2.0.3 2.0.2 2.0.1 2.0.0 1.9.9 1.9.8 1.9.7 1.9.6 1.9.5 1.9.4 1.9.3 1.9.2 1.8.1 1.8.2 1.8.3 1.8.4 1.8.5 1.8.6 1.8.7 1.8.8 All 110 releases
easy-hotel / admin / includes / framework / assets / js / main.js

main.js in Easy Hotel – Powerful Hotel Booking trunk, at admin/includes/framework/assets/js/main.js

3,533 lines 102.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /**
2 *
3 * -----------------------------------------------------------
4 *
5 * Codestar Framework
6 * A Simple and Lightweight WordPress Option Framework
7 *
8 * -----------------------------------------------------------
9 *
10 */
11 ;(function( $, window, document, undefined ) {
12 'use strict';
13
14 //
15 // Constants
16 //
17 var CSF = CSF || {};
18
19 CSF.funcs = {};
20
21 CSF.vars = {
22 onloaded: false,
23 $body: $('body'),
24 $window: $(window),
25 $document: $(document),
26 $form_warning: null,
27 is_confirm: false,
28 form_modified: false,
29 code_themes: [],
30 is_rtl: $('body').hasClass('rtl'),
31 };
32
33 //
34 // Helper Functions
35 //
36 CSF.helper = {
37
38 //
39 // Generate UID
40 //
41 uid: function( prefix ) {
42 return ( prefix || '' ) + Math.random().toString(36).substr(2, 9);
43 },
44
45 // Quote regular expression characters
46 //
47 preg_quote: function( str ) {
48 return (str+'').replace(/(\[|\])/g, "\\$1");
49 },
50
51 //
52 // Reneme input names
53 //
54 name_nested_replace: function( $selector, field_id ) {
55
56 var checks = [];
57 var regex = new RegExp(CSF.helper.preg_quote(field_id +'[\\d+]'), 'g');
58
59 $selector.find(':radio').each(function() {
60 if ( this.checked || this.orginal_checked ) {
61 this.orginal_checked = true;
62 }
63 });
64
65 $selector.each( function( index ) {
66 $(this).find(':input').each(function() {
67 this.name = this.name.replace(regex, field_id +'['+ index +']');
68 if ( this.orginal_checked ) {
69 this.checked = true;
70 }
71 });
72 });
73
74 },
75
76 //
77 // Debounce
78 //
79 debounce: function( callback, threshold, immediate ) {
80 var timeout;
81 return function() {
82 var context = this, args = arguments;
83 var later = function() {
84 timeout = null;
85 if ( !immediate ) {
86 callback.apply(context, args);
87 }
88 };
89 var callNow = ( immediate && !timeout );
90 clearTimeout( timeout );
91 timeout = setTimeout( later, threshold );
92 if ( callNow ) {
93 callback.apply(context, args);
94 }
95 };
96 },
97
98 };
99
100 //
101 // Custom clone for textarea and select clone() bug
102 //
103 $.fn.eshb_clone = function() {
104
105 var base = $.fn.clone.apply(this, arguments),
106 clone = this.find('select').add(this.filter('select')),
107 cloned = base.find('select').add(base.filter('select'));
108
109 for( var i = 0; i < clone.length; ++i ) {
110 for( var j = 0; j < clone[i].options.length; ++j ) {
111
112 if ( clone[i].options[j].selected === true ) {
113 cloned[i].options[j].selected = true;
114 }
115
116 }
117 }
118
119 this.find(':radio').each( function() {
120 this.orginal_checked = this.checked;
121 });
122
123 return base;
124
125 };
126
127 //
128 // Expand All Options
129 //
130 $.fn.eshb_expand_all = function() {
131 return this.each( function() {
132 $(this).on('click', function( e ) {
133
134 e.preventDefault();
135 $('.csf-wrapper').toggleClass('csf-show-all');
136 $('.csf-section').eshb_reload_script();
137 $(this).find('.fa').toggleClass('fa-indent').toggleClass('fa-outdent');
138
139 });
140 });
141 };
142
143 //
144 // Options Navigation
145 //
146 $.fn.eshb_nav_options = function() {
147 return this.each( function() {
148
149 var $nav = $(this),
150 $window = $(window),
151 $wpwrap = $('#wpwrap'),
152 $links = $nav.find('a'),
153 $last;
154
155 $window.on('hashchange csf.hashchange', function() {
156
157 var hash = window.location.hash.replace('#tab=', '');
158 var slug = hash ? hash : $links.first().attr('href').replace('#tab=', '');
159 var $link = $('[data-tab-id="'+slug+'"]');
160
161 if ( $link.length ) {
162
163 $link.closest('.csf-tab-item').addClass('csf-tab-expanded').siblings().removeClass('csf-tab-expanded');
164
165 if( $link.next().is('ul') ) {
166
167 $link = $link.next().find('li').first().find('a');
168 slug = $link.data('tab-id');
169
170 }
171
172 $links.removeClass('csf-active');
173 $link.addClass('csf-active');
174
175 if ( $last ) {
176 $last.addClass('hidden');
177 }
178
179 var $section = $('[data-section-id="'+slug+'"]');
180
181 $section.removeClass('hidden');
182 $section.eshb_reload_script();
183
184 $('.csf-section-id').val( $section.index()+1 );
185
186 $last = $section;
187
188 if ( $wpwrap.hasClass('wp-responsive-open') ) {
189 $('html, body').animate({scrollTop:($section.offset().top-50)}, 200);
190 $wpwrap.removeClass('wp-responsive-open');
191 }
192
193 }
194
195 }).trigger('csf.hashchange');
196
197 });
198 };
199
200 //
201 // Metabox Tabs
202 //
203 $.fn.eshb_nav_metabox = function() {
204 return this.each( function() {
205
206 var $nav = $(this),
207 $links = $nav.find('a'),
208 $sections = $nav.parent().find('.csf-section'),
209 $last;
210
211 $links.each( function( index ) {
212
213 $(this).on('click', function( e ) {
214
215 e.preventDefault();
216
217 var $link = $(this);
218
219 $links.removeClass('csf-active');
220 $link.addClass('csf-active');
221
222 if ( $last !== undefined ) {
223 $last.addClass('hidden');
224 }
225
226 var $section = $sections.eq(index);
227
228 $section.removeClass('hidden');
229 $section.eshb_reload_script();
230
231 $last = $section;
232
233 });
234
235 });
236
237 $links.first().trigger('click');
238
239 });
240 };
241
242 //
243 // Metabox Page Templates Listener
244 //
245 $.fn.eshb_page_templates = function() {
246 if ( this.length ) {
247
248 $(document).on('change', '.editor-page-attributes__template select, #page_template, .edit-post-post-status + div select', function() {
249
250 var maybe_value = $(this).val() || 'default';
251
252 $('.csf-page-templates').removeClass('csf-metabox-show').addClass('csf-metabox-hide');
253 $('.csf-page-'+maybe_value.toLowerCase().replace(/[^a-zA-Z0-9]+/g,'-')).removeClass('csf-metabox-hide').addClass('csf-metabox-show');
254
255 });
256
257 }
258 };
259
260 //
261 // Metabox Post Formats Listener
262 //
263 $.fn.eshb_post_formats = function() {
264 if ( this.length ) {
265
266 $(document).on('change', '.editor-post-format select, #formatdiv input[name="post_format"]', function() {
267
268 var maybe_value = $(this).val() || 'default';
269
270 // Fallback for classic editor version
271 maybe_value = ( maybe_value === '0' ) ? 'default' : maybe_value;
272
273 $('.csf-post-formats').removeClass('csf-metabox-show').addClass('csf-metabox-hide');
274 $('.csf-post-format-'+maybe_value).removeClass('csf-metabox-hide').addClass('csf-metabox-show');
275
276 });
277
278 }
279 };
280
281 //
282 // Search
283 //
284 $.fn.eshb_search = function() {
285 return this.each( function() {
286
287 var $this = $(this),
288 $input = $this.find('input');
289
290 $input.on('change keyup', function() {
291
292 var value = $(this).val(),
293 $wrapper = $('.csf-wrapper'),
294 $section = $wrapper.find('.csf-section'),
295 $fields = $section.find('> .csf-field:not(.csf-depend-on)'),
296 $titles = $fields.find('> .csf-title, .csf-search-tags');
297
298 if ( value.length > 3 ) {
299
300 $fields.addClass('csf-metabox-hide');
301 $wrapper.addClass('csf-search-all');
302
303 $titles.each( function() {
304
305 var $title = $(this);
306
307 if ( $title.text().match( new RegExp('.*?' + value + '.*?', 'i') ) ) {
308
309 var $field = $title.closest('.csf-field');
310
311 $field.removeClass('csf-metabox-hide');
312 $field.parent().eshb_reload_script();
313
314 }
315
316 });
317
318 } else {
319
320 $fields.removeClass('csf-metabox-hide');
321 $wrapper.removeClass('csf-search-all');
322
323 }
324
325 });
326
327 });
328 };
329
330 //
331 // Sticky Header
332 //
333 $.fn.eshb_sticky = function() {
334 return this.each( function() {
335
336 var $this = $(this),
337 $window = $(window),
338 $inner = $this.find('.csf-header-inner'),
339 padding = parseInt( $inner.css('padding-left') ) + parseInt( $inner.css('padding-right') ),
340 offset = 32,
341 scrollTop = 0,
342 lastTop = 0,
343 ticking = false,
344 stickyUpdate = function() {
345
346 var offsetTop = $this.offset().top,
347 stickyTop = Math.max(offset, offsetTop - scrollTop ),
348 winWidth = $window.innerWidth();
349
350 if ( stickyTop <= offset && winWidth > 782 ) {
351 $inner.css({width: $this.outerWidth()-padding});
352 $this.css({height: $this.outerHeight()}).addClass( 'csf-sticky' );
353 } else {
354 $inner.removeAttr('style');
355 $this.removeAttr('style').removeClass( 'csf-sticky' );
356 }
357
358 },
359 requestTick = function() {
360
361 if ( !ticking ) {
362 requestAnimationFrame( function() {
363 stickyUpdate();
364 ticking = false;
365 });
366 }
367
368 ticking = true;
369
370 },
371 onSticky = function() {
372
373 scrollTop = $window.scrollTop();
374 requestTick();
375
376 };
377
378 $window.on( 'scroll resize', onSticky);
379
380 onSticky();
381
382 });
383 };
384
385 //
386 // Dependency System
387 //
388 $.fn.eshb_dependency = function() {
389 return this.each( function() {
390
391 var $this = $(this),
392 $fields = $this.children('[data-controller]');
393
394 if( $fields.length ) {
395
396 var normal_ruleset = $.eshb_deps.createRuleset(),
397 global_ruleset = $.eshb_deps.createRuleset(),
398 normal_depends = [],
399 global_depends = [];
400
401 $fields.each( function() {
402
403 var $field = $(this),
404 controllers = $field.data('controller').split('|'),
405 conditions = $field.data('condition').split('|'),
406 values = $field.data('value').toString().split('|'),
407 is_global = $field.data('depend-global') ? true : false,
408 ruleset = ( is_global ) ? global_ruleset : normal_ruleset;
409
410 $.each(controllers, function( index, depend_id ) {
411
412 depend_id = depend_id.replace(/^"(.*)"$/, '$1');
413
414 var value = values[index] || '',
415 condition = conditions[index] || conditions[0];
416
417 ruleset = ruleset.createRule('[data-depend-id='+ depend_id +']', condition, value);
418
419 ruleset.include($field);
420
421 if ( is_global ) {
422 global_depends.push(depend_id);
423 } else {
424 normal_depends.push(depend_id);
425 }
426
427 });
428
429 });
430
431 if ( normal_depends.length ) {
432 $.eshb_deps.enable($this, normal_ruleset, normal_depends);
433 }
434
435 if ( global_depends.length ) {
436 $.eshb_deps.enable(CSF.vars.$body, global_ruleset, global_depends);
437 }
438
439 }
440
441 });
442 };
443
444 //
445 // Field: accordion
446 //
447 $.fn.eshb_field_accordion = function() {
448 return this.each( function() {
449
450 var $titles = $(this).find('.csf-accordion-title');
451
452 $titles.on('click', function() {
453
454 var $title = $(this),
455 $icon = $title.find('.csf-accordion-icon'),
456 $content = $title.next();
457
458 if ( $icon.hasClass('fa-angle-right') ) {
459 $icon.removeClass('fa-angle-right').addClass('fa-angle-down');
460 } else {
461 $icon.removeClass('fa-angle-down').addClass('fa-angle-right');
462 }
463
464 if ( !$content.data( 'opened' ) ) {
465
466 $content.eshb_reload_script();
467 $content.data( 'opened', true );
468
469 }
470
471 $content.toggleClass('csf-accordion-open');
472
473 });
474
475 });
476 };
477
478 //
479 // Field: backup
480 //
481 $.fn.eshb_field_backup = function() {
482 return this.each( function() {
483
484 if ( window.wp.customize === undefined ) { return; }
485
486 var base = this,
487 $this = $(this),
488 $body = $('body'),
489 $import = $this.find('.eshb-import'),
490 $reset = $this.find('.eshb-reset');
491
492 base.notificationOverlay = function() {
493
494 if ( wp.customize.notifications && wp.customize.OverlayNotification ) {
495
496 // clear if there is any saved data.
497 if ( !wp.customize.state('saved').get() ) {
498 wp.customize.state('changesetStatus').set('trash');
499 wp.customize.each( function( setting ) { setting._dirty = false; });
500 wp.customize.state('saved').set(true);
501 }
502
503 // then show a notification overlay
504 wp.customize.notifications.add( new wp.customize.OverlayNotification('eshb_field_backup_notification', {
505 type: 'default',
506 message: '&nbsp;',
507 loading: true
508 }));
509
510 }
511
512 };
513
514 $reset.on('click', function( e ) {
515
516 e.preventDefault();
517
518 if ( CSF.vars.is_confirm ) {
519
520 base.notificationOverlay();
521
522 window.wp.ajax.post('eshb-reset', {
523 unique: $reset.data('unique'),
524 nonce: $reset.data('nonce')
525 })
526 .done( function( response ) {
527 window.location.reload(true);
528 })
529 .fail( function( response ) {
530 alert( response.error );
531 wp.customize.notifications.remove('eshb_field_backup_notification');
532 });
533
534 }
535
536 });
537
538 $import.on('click', function( e ) {
539
540 e.preventDefault();
541
542 if ( CSF.vars.is_confirm ) {
543
544 base.notificationOverlay();
545
546 window.wp.ajax.post( 'eshb-import', {
547 unique: $import.data('unique'),
548 nonce: $import.data('nonce'),
549 data: $this.find('.eshb-import-data').val()
550 }).done( function( response ) {
551 window.location.reload(true);
552 }).fail( function( response ) {
553 alert( response.error );
554 wp.customize.notifications.remove('eshb_field_backup_notification');
555 });
556
557 }
558
559 });
560
561 });
562 };
563
564 //
565 // Field: background
566 //
567 $.fn.eshb_field_background = function() {
568 return this.each( function() {
569 $(this).find('.csf--background-image').eshb_reload_script();
570 });
571 };
572
573 //
574 // Field: code_editor
575 //
576 $.fn.eshb_field_code_editor = function() {
577 return this.each( function() {
578
579 if ( typeof CodeMirror !== 'function' ) { return; }
580
581 var $this = $(this),
582 $textarea = $this.find('textarea'),
583 $inited = $this.find('.CodeMirror'),
584 data_editor = $textarea.data('editor');
585
586 if ( $inited.length ) {
587 $inited.remove();
588 }
589
590 var interval = setInterval(function () {
591 if ( $this.is(':visible') ) {
592
593 var code_editor = CodeMirror.fromTextArea( $textarea[0], data_editor );
594
595 // load code-mirror theme css.
596 if ( data_editor.theme !== 'default' && CSF.vars.code_themes.indexOf(data_editor.theme) === -1 ) {
597
598 var $cssLink = $('<link>');
599
600 $('#csf-codemirror-css').after( $cssLink );
601
602 $cssLink.attr({
603 rel: 'stylesheet',
604 id: 'csf-codemirror-'+ data_editor.theme +'-css',
605 href: data_editor.cdnURL +'/theme/'+ data_editor.theme +'.min.css',
606 type: 'text/css',
607 media: 'all'
608 });
609
610 CSF.vars.code_themes.push(data_editor.theme);
611
612 }
613
614 CodeMirror.modeURL = data_editor.cdnURL +'/mode/%N/%N.min.js';
615 CodeMirror.autoLoadMode(code_editor, data_editor.mode);
616
617 code_editor.on( 'change', function( editor, event ) {
618 $textarea.val( code_editor.getValue() ).trigger('change');
619 });
620
621 clearInterval(interval);
622
623 }
624 });
625
626 });
627 };
628
629 //
630 // Field: date
631 //
632 $.fn.eshb_field_date = function() {
633 return this.each( function() {
634
635 // jQuery UI datepicker is enqueued by ESHB_Field_date::enqueue(), which only runs
636 // for field types the framework has seen through ESHB::createSection(). A date
637 // field registered any other way — through the sections filter, say — arrives
638 // without it, and throwing here would take down the rest of the
639 // eshb_reload_script() call that got us here. Leave the inputs as the plain text
640 // fields they already are instead.
641 if ( ! $.fn.datepicker ) {
642 return;
643 }
644
645 var $this = $(this),
646 $inputs = $this.find('input'),
647 settings = $this.find('.csf-date-settings').data('settings'),
648 wrapper = '<div class="csf-datepicker-wrapper"></div>';
649
650 var defaults = {
651 showAnim: '',
652 beforeShow: function(input, inst) {
653 $(inst.dpDiv).addClass('csf-datepicker-wrapper');
654 },
655 onClose: function( input, inst ) {
656 $(inst.dpDiv).removeClass('csf-datepicker-wrapper');
657 },
658 };
659
660 settings = $.extend({}, settings, defaults);
661
662 if ( $inputs.length === 2 ) {
663
664 settings = $.extend({}, settings, {
665 onSelect: function( selectedDate ) {
666
667 var $this = $(this),
668 $from = $inputs.first(),
669 option = ( $inputs.first().attr('id') === $(this).attr('id') ) ? 'minDate' : 'maxDate',
670 date = $.datepicker.parseDate( settings.dateFormat, selectedDate );
671
672 $inputs.not(this).datepicker('option', option, date );
673
674 }
675 });
676
677 }
678
679 $inputs.each( function() {
680
681 var $input = $(this);
682
683 if ( $input.hasClass('hasDatepicker') ) {
684 $input.removeAttr('id').removeClass('hasDatepicker');
685 }
686
687 $input.datepicker(settings);
688
689 });
690
691 });
692 };
693
694 //
695 // Field: datetime
696 //
697 $.fn.eshb_field_datetime = function() {
698 return this.each( function() {
699
700 var $this = $(this),
701 $inputs = $this.find('input'),
702 settings = $this.find('.csf-datetime-settings').data('settings');
703
704 settings = $.extend({}, settings, {
705 onReady: function( selectedDates, dateStr, instance) {
706 $(instance.calendarContainer).addClass('csf-flatpickr');
707 },
708 });
709
710 if ( $inputs.length === 2 ) {
711 settings = $.extend({}, settings, {
712 onChange: function( selectedDates, dateStr, instance) {
713 if ( $(instance.element).data('type') === 'from' ) {
714 $inputs.last().get(0)._flatpickr.set( 'minDate', selectedDates[0] );
715 } else {
716 $inputs.first().get(0)._flatpickr.set( 'maxDate', selectedDates[0] );
717 }
718 },
719 });
720 }
721
722 $inputs.each( function() {
723 $(this).flatpickr(settings);
724 });
725
726 });
727 };
728
729 //
730 // Field: fieldset
731 //
732 $.fn.eshb_field_fieldset = function() {
733 return this.each( function() {
734 $(this).find('.csf-fieldset-content').eshb_reload_script();
735 });
736 };
737
738 //
739 // Field: gallery
740 //
741 $.fn.eshb_field_gallery = function() {
742 return this.each( function() {
743
744 var $this = $(this),
745 $edit = $this.find('.csf-edit-gallery'),
746 $clear = $this.find('.csf-clear-gallery'),
747 $list = $this.find('ul'),
748 $input = $this.find('input'),
749 $img = $this.find('img'),
750 wp_media_frame;
751
752 $this.on('click', '.csf-button, .csf-edit-gallery', function( e ) {
753
754 var $el = $(this),
755 ids = $input.val(),
756 what = ( $el.hasClass('csf-edit-gallery') ) ? 'edit' : 'add',
757 state = ( what === 'add' && !ids.length ) ? 'gallery' : 'gallery-edit';
758
759 e.preventDefault();
760
761 if ( typeof window.wp === 'undefined' || ! window.wp.media || ! window.wp.media.gallery ) { return; }
762
763 // Open media with state
764 if ( state === 'gallery' ) {
765
766 wp_media_frame = window.wp.media({
767 library: {
768 type: 'image'
769 },
770 frame: 'post',
771 state: 'gallery',
772 multiple: true
773 });
774
775 wp_media_frame.open();
776
777 } else {
778
779 wp_media_frame = window.wp.media.gallery.edit( '[gallery ids="'+ ids +'"]' );
780
781 if ( what === 'add' ) {
782 wp_media_frame.setState('gallery-library');
783 }
784
785 }
786
787 // Media Update
788 wp_media_frame.on( 'update', function( selection ) {
789
790 $list.empty();
791
792 var selectedIds = selection.models.map( function( attachment ) {
793
794 var item = attachment.toJSON();
795 var thumb = ( item.sizes && item.sizes.thumbnail && item.sizes.thumbnail.url ) ? item.sizes.thumbnail.url : item.url;
796
797 $list.append('<li><img src="'+ thumb +'"></li>');
798
799 return item.id;
800
801 });
802
803 $input.val( selectedIds.join( ',' ) ).trigger('change');
804 $clear.removeClass('hidden');
805 $edit.removeClass('hidden');
806
807 });
808
809 });
810
811 $clear.on('click', function( e ) {
812 e.preventDefault();
813 $list.empty();
814 $input.val('').trigger('change');
815 $clear.addClass('hidden');
816 $edit.addClass('hidden');
817 });
818
819 });
820
821 };
822
823 //
824 // Field: group
825 //
826 $.fn.eshb_field_group = function() {
827 return this.each( function() {
828
829 var $this = $(this),
830 $fieldset = $this.children('.csf-fieldset'),
831 $group = $fieldset.length ? $fieldset : $this,
832 $wrapper = $group.children('.csf-cloneable-wrapper'),
833 $hidden = $group.children('.csf-cloneable-hidden'),
834 $max = $group.children('.csf-cloneable-max'),
835 $min = $group.children('.csf-cloneable-min'),
836 title_by = $wrapper.data('title-by'),
837 title_prefix = $wrapper.data('title-by-prefix'),
838 field_id = $wrapper.data('field-id'),
839 is_number = Boolean( Number( $wrapper.data('title-number') ) ),
840 max = parseInt( $wrapper.data('max') ),
841 min = parseInt( $wrapper.data('min') );
842
843 // clear accordion arrows if multi-instance
844 if ( $wrapper.hasClass('ui-accordion') ) {
845 $wrapper.find('.ui-accordion-header-icon').remove();
846 }
847
848 var update_title_numbers = function( $selector ) {
849 $selector.find('.csf-cloneable-title-number').each( function( index ) {
850 $(this).html( ( $(this).closest('.csf-cloneable-item').index()+1 ) + '.' );
851 });
852 };
853
854 $wrapper.accordion({
855 header: '> .csf-cloneable-item > .csf-cloneable-title',
856 collapsible : true,
857 active: false,
858 animate: false,
859 heightStyle: 'content',
860 icons: {
861 'header': 'csf-cloneable-header-icon fas fa-angle-right',
862 'activeHeader': 'csf-cloneable-header-icon fas fa-angle-down'
863 },
864 activate: function( event, ui ) {
865
866 var $panel = ui.newPanel;
867 var $header = ui.newHeader;
868
869 if ( $panel.length && !$panel.data( 'opened' ) ) {
870
871 var $title = $header.find('.csf-cloneable-value');
872 var inputs = [];
873
874 $.each(title_by, function( key, title_key ) {
875
876 inputs.push($panel.find( '[data-depend-id="'+ title_key +'"]' ));
877 });
878
879 $.each(inputs, function( key, $input ) {
880
881 $input.on('change keyup csf.keyup', function() {
882
883 var titles = [];
884
885 $.each(inputs, function( key, $input ) {
886
887 var input_value = $input.val();
888
889 if ( input_value ) {
890 titles.push( input_value );
891 }
892
893 });
894
895 if ( titles.length ) {
896 $title.text( titles.join( title_prefix ) );
897 }
898
899 }).trigger('csf.keyup');
900
901 });
902
903 $panel.eshb_reload_script();
904 $panel.data( 'opened', true );
905 $panel.data( 'retry', false );
906
907 } else if ( $panel.data( 'retry' ) ) {
908
909 $panel.eshb_reload_script_retry();
910 $panel.data( 'retry', false );
911
912 }
913
914 }
915 });
916
917 $wrapper.sortable({
918 axis: 'y',
919 handle: '.csf-cloneable-title,.csf-cloneable-sort',
920 helper: 'original',
921 cursor: 'move',
922 placeholder: 'widget-placeholder',
923 start: function( event, ui ) {
924
925 $wrapper.accordion({ active:false });
926 $wrapper.sortable('refreshPositions');
927 ui.item.children('.csf-cloneable-content').data('retry', true);
928
929 },
930 update: function( event, ui ) {
931
932 CSF.helper.name_nested_replace( $wrapper.children('.csf-cloneable-item'), field_id );
933 $wrapper.eshb_customizer_refresh();
934
935 if ( is_number ) {
936 update_title_numbers($wrapper);
937 }
938
939 },
940 });
941
942 $group.children('.csf-cloneable-add').on('click', function( e ) {
943
944 e.preventDefault();
945
946 var count = $wrapper.children('.csf-cloneable-item').length;
947
948 $min.hide();
949
950 if ( max && (count+1) > max ) {
951 $max.show();
952 return;
953 }
954
955 var $cloned_item = $hidden.eshb_clone(true);
956
957 $cloned_item.removeClass('csf-cloneable-hidden');
958
959 $cloned_item.find(':input[name!="_pseudo"]').each( function() {
960 this.name = this.name.replace( '___', '' ).replace( field_id +'[0]', field_id +'['+ count +']' );
961 });
962
963 $wrapper.append($cloned_item);
964 $wrapper.accordion('refresh');
965 $wrapper.accordion({active: count});
966 $wrapper.eshb_customizer_refresh();
967 $wrapper.eshb_customizer_listen({closest: true});
968
969 if ( is_number ) {
970 update_title_numbers($wrapper);
971 }
972
973 });
974
975 var event_clone = function( e ) {
976
977 e.preventDefault();
978
979 var count = $wrapper.children('.csf-cloneable-item').length;
980
981 $min.hide();
982
983 if ( max && (count+1) > max ) {
984 $max.show();
985 return;
986 }
987
988 var $this = $(this),
989 $parent = $this.parent().parent(),
990 $cloned_helper = $parent.children('.csf-cloneable-helper').eshb_clone(true),
991 $cloned_title = $parent.children('.csf-cloneable-title').eshb_clone(),
992 $cloned_content = $parent.children('.csf-cloneable-content').eshb_clone(),
993 $cloned_item = $('<div class="csf-cloneable-item" />');
994
995 $cloned_item.append($cloned_helper);
996 $cloned_item.append($cloned_title);
997 $cloned_item.append($cloned_content);
998
999 $wrapper.children().eq($parent.index()).after($cloned_item);
1000
1001 CSF.helper.name_nested_replace( $wrapper.children('.csf-cloneable-item'), field_id );
1002
1003 $wrapper.accordion('refresh');
1004 $wrapper.eshb_customizer_refresh();
1005 $wrapper.eshb_customizer_listen({closest: true});
1006
1007 if ( is_number ) {
1008 update_title_numbers($wrapper);
1009 }
1010
1011 };
1012
1013 $wrapper.children('.csf-cloneable-item').children('.csf-cloneable-helper').on('click', '.csf-cloneable-clone', event_clone);
1014 $group.children('.csf-cloneable-hidden').children('.csf-cloneable-helper').on('click', '.csf-cloneable-clone', event_clone);
1015
1016 var event_remove = function( e ) {
1017
1018 e.preventDefault();
1019
1020 var count = $wrapper.children('.csf-cloneable-item').length;
1021
1022 $max.hide();
1023 $min.hide();
1024
1025 if ( min && (count-1) < min ) {
1026 $min.show();
1027 return;
1028 }
1029
1030 $(this).closest('.csf-cloneable-item').remove();
1031
1032 CSF.helper.name_nested_replace( $wrapper.children('.csf-cloneable-item'), field_id );
1033
1034 $wrapper.eshb_customizer_refresh();
1035
1036 if ( is_number ) {
1037 update_title_numbers($wrapper);
1038 }
1039
1040 };
1041
1042 $wrapper.children('.csf-cloneable-item').children('.csf-cloneable-helper').on('click', '.csf-cloneable-remove', event_remove);
1043 $group.children('.csf-cloneable-hidden').children('.csf-cloneable-helper').on('click', '.csf-cloneable-remove', event_remove);
1044
1045 });
1046 };
1047
1048 //
1049 // Field: icon
1050 //
1051 $.fn.eshb_field_icon = function() {
1052 return this.each( function() {
1053
1054 var $this = $(this);
1055
1056 $this.on('click', '.csf-icon-add', function( e ) {
1057
1058 e.preventDefault();
1059
1060 var $button = $(this);
1061 var $modal = $('#csf-modal-icon');
1062
1063 $modal.removeClass('hidden');
1064
1065 CSF.vars.$icon_target = $this;
1066
1067 if ( !CSF.vars.icon_modal_loaded ) {
1068
1069 $modal.find('.csf-modal-loading').show();
1070
1071 window.wp.ajax.post( 'eshb-get-icons', {
1072 nonce: $button.data('nonce')
1073 }).done( function( response ) {
1074
1075 $modal.find('.csf-modal-loading').hide();
1076
1077 CSF.vars.icon_modal_loaded = true;
1078
1079 var $load = $modal.find('.csf-modal-load').html( response.content );
1080
1081 $load.on('click', 'i', function( e ) {
1082
1083 e.preventDefault();
1084
1085 var icon = $(this).attr('title');
1086
1087 CSF.vars.$icon_target.find('.csf-icon-preview i').removeAttr('class').addClass(icon);
1088 CSF.vars.$icon_target.find('.csf-icon-preview').removeClass('hidden');
1089 CSF.vars.$icon_target.find('.csf-icon-remove').removeClass('hidden');
1090 CSF.vars.$icon_target.find('input').val(icon).trigger('change');
1091
1092 $modal.addClass('hidden');
1093
1094 });
1095
1096 $modal.on('change keyup', '.csf-icon-search', function() {
1097
1098 var value = $(this).val(),
1099 $icons = $load.find('i');
1100
1101 $icons.each( function() {
1102
1103 var $elem = $(this);
1104
1105 if ( $elem.attr('title').search( new RegExp( value, 'i' ) ) < 0 ) {
1106 $elem.hide();
1107 } else {
1108 $elem.show();
1109 }
1110
1111 });
1112
1113 });
1114
1115 $modal.on('click', '.csf-modal-close, .csf-modal-overlay', function() {
1116 $modal.addClass('hidden');
1117 });
1118
1119 }).fail( function( response ) {
1120 $modal.find('.csf-modal-loading').hide();
1121 $modal.find('.csf-modal-load').html( response.error );
1122 $modal.on('click', function() {
1123 $modal.addClass('hidden');
1124 });
1125 });
1126 }
1127
1128 });
1129
1130 $this.on('click', '.csf-icon-remove', function( e ) {
1131 e.preventDefault();
1132 $this.find('.csf-icon-preview').addClass('hidden');
1133 $this.find('input').val('').trigger('change');
1134 $(this).addClass('hidden');
1135 });
1136
1137 });
1138 };
1139
1140 //
1141 // Field: map
1142 //
1143 $.fn.eshb_field_map = function() {
1144 return this.each( function() {
1145
1146 if ( typeof L === 'undefined' ) { return; }
1147
1148 var $this = $(this),
1149 $map = $this.find('.csf--map-osm'),
1150 $search_input = $this.find('.csf--map-search input'),
1151 $latitude = $this.find('.csf--latitude'),
1152 $longitude = $this.find('.csf--longitude'),
1153 $zoom = $this.find('.csf--zoom'),
1154 map_data = $map.data( 'map' );
1155
1156 var mapInit = L.map( $map.get(0), map_data);
1157
1158 L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
1159 attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
1160 }).addTo(mapInit);
1161
1162 var mapMarker = L.marker(map_data.center,{draggable: true}).addTo(mapInit);
1163
1164 var update_latlng = function( data ) {
1165 $latitude.val( data.lat );
1166 $longitude.val( data.lng );
1167 $zoom.val( mapInit.getZoom() );
1168 };
1169
1170 mapInit.on( 'click', function ( data ) {
1171 mapMarker.setLatLng( data.latlng );
1172 update_latlng( data.latlng );
1173 });
1174
1175 mapInit.on( 'zoom', function () {
1176 update_latlng( mapMarker.getLatLng() );
1177 });
1178
1179 mapMarker.on( 'drag', function () {
1180 update_latlng( mapMarker.getLatLng() );
1181 });
1182
1183 if ( ! $search_input.length ) {
1184 $search_input = $( '[data-depend-id="'+ $this.find('.csf--address-field').data( 'address-field' ) +'"]' );
1185 }
1186
1187 var cache = {};
1188
1189 $search_input.autocomplete({
1190 source: function ( request, response ) {
1191
1192 var term = request.term;
1193
1194 if ( term in cache ) {
1195 response( cache[term] );
1196 return;
1197 }
1198
1199 $.get( 'https://nominatim.openstreetmap.org/search', {
1200 format: 'json',
1201 q: term,
1202 }, function( results ) {
1203
1204 var data;
1205
1206 if ( results.length ) {
1207 data = results.map( function( item ) {
1208 return {
1209 value: item.display_name,
1210 label: item.display_name,
1211 lat: item.lat,
1212 lon: item.lon
1213 };
1214 }, 'json');
1215 } else {
1216 data = [{
1217 value: 'no-data',
1218 label: 'No Results.'
1219 }];
1220 }
1221
1222 cache[term] = data;
1223 response(data);
1224
1225 });
1226
1227 },
1228 select: function ( event, ui ) {
1229
1230 if ( ui.item.value === 'no-data' ) { return false; }
1231
1232 var latLng = L.latLng( ui.item.lat, ui.item.lon );
1233
1234 mapInit.panTo( latLng );
1235 mapMarker.setLatLng( latLng );
1236 update_latlng( latLng );
1237
1238 },
1239 create: function (event, ui) {
1240 $(this).autocomplete('widget').addClass('csf-map-ui-autocomplate');
1241 }
1242 });
1243
1244 var input_update_latlng = function() {
1245
1246 var latLng = L.latLng( $latitude.val(), $longitude.val() );
1247
1248 mapInit.panTo( latLng );
1249 mapMarker.setLatLng( latLng );
1250
1251 };
1252
1253 $latitude.on('change', input_update_latlng );
1254 $longitude.on('change', input_update_latlng );
1255
1256 });
1257 };
1258
1259 //
1260 // Field: link
1261 //
1262 $.fn.eshb_field_link = function() {
1263 return this.each( function() {
1264
1265 var $this = $(this),
1266 $link = $this.find('.csf--link'),
1267 $add = $this.find('.csf--add'),
1268 $edit = $this.find('.csf--edit'),
1269 $remove = $this.find('.csf--remove'),
1270 $result = $this.find('.csf--result'),
1271 uniqid = CSF.helper.uid('csf-wplink-textarea-');
1272
1273 $add.on('click', function( e ) {
1274
1275 e.preventDefault();
1276
1277 window.wpLink.open(uniqid);
1278
1279 });
1280
1281 $edit.on('click', function( e ) {
1282
1283 e.preventDefault();
1284
1285 $add.trigger('click');
1286
1287 $('#wp-link-url').val($this.find('.csf--url').val());
1288 $('#wp-link-text').val($this.find('.csf--text').val());
1289 $('#wp-link-target').prop('checked', ($this.find('.csf--target').val() === '_blank'));
1290
1291 });
1292
1293 $remove.on('click', function( e ) {
1294
1295 e.preventDefault();
1296
1297 $this.find('.csf--url').val('').trigger('change');
1298 $this.find('.csf--text').val('');
1299 $this.find('.csf--target').val('');
1300
1301 $add.removeClass('hidden');
1302 $edit.addClass('hidden');
1303 $remove.addClass('hidden');
1304 $result.parent().addClass('hidden');
1305
1306 });
1307
1308 $link.attr('id', uniqid).on('change', function() {
1309
1310 var atts = window.wpLink.getAttrs(),
1311 href = atts.href,
1312 text = $('#wp-link-text').val(),
1313 target = ( atts.target ) ? atts.target : '';
1314
1315 $this.find('.csf--url').val(href).trigger('change');
1316 $this.find('.csf--text').val(text);
1317 $this.find('.csf--target').val(target);
1318
1319 $result.html('{url:"'+href+'", text:"'+text+'", target:"'+target+'"}');
1320
1321 $add.addClass('hidden');
1322 $edit.removeClass('hidden');
1323 $remove.removeClass('hidden');
1324 $result.parent().removeClass('hidden');
1325
1326 });
1327
1328 });
1329
1330 };
1331
1332 //
1333 // Field: media
1334 //
1335 $.fn.eshb_field_media = function() {
1336 return this.each( function() {
1337
1338 var $this = $(this),
1339 $upload_button = $this.find('.csf--button'),
1340 $remove_button = $this.find('.csf--remove'),
1341 $library = $upload_button.data('library') && $upload_button.data('library').split(',') || '',
1342 $auto_attributes = ( $this.hasClass('csf-assign-field-background') ) ? $this.closest('.csf-field-background').find('.csf--auto-attributes') : false,
1343 wp_media_frame;
1344
1345 $upload_button.on('click', function( e ) {
1346
1347 e.preventDefault();
1348
1349 if ( typeof window.wp === 'undefined' || ! window.wp.media || ! window.wp.media.gallery ) {
1350 return;
1351 }
1352
1353 if ( wp_media_frame ) {
1354 wp_media_frame.open();
1355 return;
1356 }
1357
1358 wp_media_frame = window.wp.media({
1359 library: {
1360 type: $library
1361 }
1362 });
1363
1364 wp_media_frame.on( 'select', function() {
1365
1366 var thumbnail;
1367 var attributes = wp_media_frame.state().get('selection').first().attributes;
1368 var preview_size = $upload_button.data('preview-size') || 'thumbnail';
1369
1370 if ( $library.length && $library.indexOf(attributes.subtype) === -1 && $library.indexOf(attributes.type) === -1 ) {
1371 return;
1372 }
1373
1374 $this.find('.csf--id').val( attributes.id );
1375 $this.find('.csf--width').val( attributes.width );
1376 $this.find('.csf--height').val( attributes.height );
1377 $this.find('.csf--alt').val( attributes.alt );
1378 $this.find('.csf--title').val( attributes.title );
1379 $this.find('.csf--description').val( attributes.description );
1380
1381 if ( typeof attributes.sizes !== 'undefined' && typeof attributes.sizes.thumbnail !== 'undefined' && preview_size === 'thumbnail' ) {
1382 thumbnail = attributes.sizes.thumbnail.url;
1383 } else if ( typeof attributes.sizes !== 'undefined' && typeof attributes.sizes.full !== 'undefined' ) {
1384 thumbnail = attributes.sizes.full.url;
1385 } else if ( attributes.type === 'image' ) {
1386 thumbnail = attributes.url;
1387 } else {
1388 thumbnail = attributes.icon;
1389 }
1390
1391 if ( $auto_attributes ) {
1392 $auto_attributes.removeClass('csf--attributes-hidden');
1393 }
1394
1395 $remove_button.removeClass('hidden');
1396
1397 $this.find('.csf--preview').removeClass('hidden');
1398 $this.find('.csf--src').attr('src', thumbnail);
1399 $this.find('.csf--thumbnail').val( thumbnail );
1400 $this.find('.csf--url').val( attributes.url ).trigger('change');
1401
1402 });
1403
1404 wp_media_frame.open();
1405
1406 });
1407
1408 $remove_button.on('click', function( e ) {
1409
1410 e.preventDefault();
1411
1412 if ( $auto_attributes ) {
1413 $auto_attributes.addClass('csf--attributes-hidden');
1414 }
1415
1416 $remove_button.addClass('hidden');
1417 $this.find('input').val('');
1418 $this.find('.csf--preview').addClass('hidden');
1419 $this.find('.csf--url').trigger('change');
1420
1421 });
1422
1423 });
1424
1425 };
1426
1427 //
1428 // Field: repeater
1429 //
1430 $.fn.eshb_field_repeater = function() {
1431 return this.each( function() {
1432
1433 var $this = $(this),
1434 $fieldset = $this.children('.csf-fieldset'),
1435 $repeater = $fieldset.length ? $fieldset : $this,
1436 $wrapper = $repeater.children('.csf-repeater-wrapper'),
1437 $hidden = $repeater.children('.csf-repeater-hidden'),
1438 $max = $repeater.children('.csf-repeater-max'),
1439 $min = $repeater.children('.csf-repeater-min'),
1440 field_id = $wrapper.data('field-id'),
1441 max = parseInt( $wrapper.data('max') ),
1442 min = parseInt( $wrapper.data('min') );
1443
1444 $wrapper.sortable({
1445 axis: 'y',
1446 handle: '.csf-repeater-sort',
1447 helper: 'original',
1448 cursor: 'move',
1449 placeholder: 'widget-placeholder',
1450 update: function( event, ui ) {
1451
1452 CSF.helper.name_nested_replace( $wrapper.children('.csf-repeater-item'), field_id );
1453 $wrapper.eshb_customizer_refresh();
1454 ui.item.eshb_reload_script_retry();
1455
1456 }
1457 });
1458
1459 $repeater.children('.csf-repeater-add').on('click', function( e ) {
1460
1461 e.preventDefault();
1462
1463 var count = $wrapper.children('.csf-repeater-item').length;
1464
1465 $min.hide();
1466
1467 if ( max && (count+1) > max ) {
1468 $max.show();
1469 return;
1470 }
1471
1472 var $cloned_item = $hidden.eshb_clone(true);
1473
1474 $cloned_item.removeClass('csf-repeater-hidden');
1475
1476 $cloned_item.find(':input[name!="_pseudo"]').each( function() {
1477 this.name = this.name.replace( '___', '' ).replace( field_id +'[0]', field_id +'['+ count +']' );
1478 });
1479
1480 $wrapper.append($cloned_item);
1481 $cloned_item.children('.csf-repeater-content').eshb_reload_script();
1482 $wrapper.eshb_customizer_refresh();
1483 $wrapper.eshb_customizer_listen({closest: true});
1484
1485 });
1486
1487 var event_clone = function( e ) {
1488
1489 e.preventDefault();
1490
1491 var count = $wrapper.children('.csf-repeater-item').length;
1492
1493 $min.hide();
1494
1495 if ( max && (count+1) > max ) {
1496 $max.show();
1497 return;
1498 }
1499
1500 var $this = $(this),
1501 $parent = $this.parent().parent().parent(),
1502 $cloned_content = $parent.children('.csf-repeater-content').eshb_clone(),
1503 $cloned_helper = $parent.children('.csf-repeater-helper').eshb_clone(true),
1504 $cloned_item = $('<div class="csf-repeater-item" />');
1505
1506 $cloned_item.append($cloned_content);
1507 $cloned_item.append($cloned_helper);
1508
1509 $wrapper.children().eq($parent.index()).after($cloned_item);
1510
1511 $cloned_item.children('.csf-repeater-content').eshb_reload_script();
1512
1513 CSF.helper.name_nested_replace( $wrapper.children('.csf-repeater-item'), field_id );
1514
1515 $wrapper.eshb_customizer_refresh();
1516 $wrapper.eshb_customizer_listen({closest: true});
1517
1518 };
1519
1520 $wrapper.children('.csf-repeater-item').children('.csf-repeater-helper').on('click', '.csf-repeater-clone', event_clone);
1521 $repeater.children('.csf-repeater-hidden').children('.csf-repeater-helper').on('click', '.csf-repeater-clone', event_clone);
1522
1523 var event_remove = function( e ) {
1524
1525 e.preventDefault();
1526
1527 var count = $wrapper.children('.csf-repeater-item').length;
1528
1529 $max.hide();
1530 $min.hide();
1531
1532 if ( min && (count-1) < min ) {
1533 $min.show();
1534 return;
1535 }
1536
1537 $(this).closest('.csf-repeater-item').remove();
1538
1539 CSF.helper.name_nested_replace( $wrapper.children('.csf-repeater-item'), field_id );
1540
1541 $wrapper.eshb_customizer_refresh();
1542
1543 };
1544
1545 $wrapper.children('.csf-repeater-item').children('.csf-repeater-helper').on('click', '.csf-repeater-remove', event_remove);
1546 $repeater.children('.csf-repeater-hidden').children('.csf-repeater-helper').on('click', '.csf-repeater-remove', event_remove);
1547
1548 // Last, not first. This walks into every saved row and starts up whatever plugins
1549 // its fields need, which is the one step here that can throw — a field whose
1550 // script never loaded takes the rest of the function with it. Running it after the
1551 // handlers above are bound means such a row can no longer leave the repeater
1552 // without its Add, Duplicate and Delete buttons.
1553 $wrapper.children('.csf-repeater-item').children('.csf-repeater-content').eshb_reload_script();
1554
1555 });
1556 };
1557
1558 //
1559 // Field: slider
1560 //
1561 $.fn.eshb_field_slider = function() {
1562 return this.each( function() {
1563
1564 var $this = $(this),
1565 $input = $this.find('input'),
1566 $slider = $this.find('.csf-slider-ui'),
1567 data = $input.data(),
1568 value = $input.val() || 0;
1569
1570 if ( $slider.hasClass('ui-slider') ) {
1571 $slider.empty();
1572 }
1573
1574 $slider.slider({
1575 range: 'min',
1576 value: value,
1577 min: data.min || 0,
1578 max: data.max || 100,
1579 step: data.step || 1,
1580 slide: function( e, o ) {
1581 $input.val( o.value ).trigger('change');
1582 }
1583 });
1584
1585 $input.on('keyup', function() {
1586 $slider.slider('value', $input.val());
1587 });
1588
1589 });
1590 };
1591
1592 //
1593 // Field: sortable
1594 //
1595 $.fn.eshb_field_sortable = function() {
1596 return this.each( function() {
1597
1598 var $sortable = $(this).find('.csf-sortable');
1599
1600 $sortable.sortable({
1601 axis: 'y',
1602 helper: 'original',
1603 cursor: 'move',
1604 placeholder: 'widget-placeholder',
1605 update: function( event, ui ) {
1606 $sortable.eshb_customizer_refresh();
1607 }
1608 });
1609
1610 $sortable.find('.csf-sortable-content').eshb_reload_script();
1611
1612 });
1613 };
1614
1615 //
1616 // Field: sorter
1617 //
1618 $.fn.eshb_field_sorter = function() {
1619 return this.each( function() {
1620
1621 var $this = $(this),
1622 $enabled = $this.find('.csf-enabled'),
1623 $has_disabled = $this.find('.csf-disabled'),
1624 $disabled = ( $has_disabled.length ) ? $has_disabled : false;
1625
1626 $enabled.sortable({
1627 connectWith: $disabled,
1628 placeholder: 'ui-sortable-placeholder',
1629 update: function( event, ui ) {
1630
1631 var $el = ui.item.find('input');
1632
1633 if ( ui.item.parent().hasClass('csf-enabled') ) {
1634 $el.attr('name', $el.attr('name').replace('disabled', 'enabled'));
1635 } else {
1636 $el.attr('name', $el.attr('name').replace('enabled', 'disabled'));
1637 }
1638
1639 $this.eshb_customizer_refresh();
1640
1641 }
1642 });
1643
1644 if ( $disabled ) {
1645
1646 $disabled.sortable({
1647 connectWith: $enabled,
1648 placeholder: 'ui-sortable-placeholder',
1649 update: function( event, ui ) {
1650 $this.eshb_customizer_refresh();
1651 }
1652 });
1653
1654 }
1655
1656 });
1657 };
1658
1659 //
1660 // Field: spinner
1661 //
1662 $.fn.eshb_field_spinner = function() {
1663 return this.each( function() {
1664
1665 var $this = $(this),
1666 $input = $this.find('input'),
1667 $inited = $this.find('.ui-button'),
1668 data = $input.data();
1669
1670 if ( $inited.length ) {
1671 $inited.remove();
1672 }
1673
1674 $input.spinner({
1675 min: data.min || 0,
1676 max: data.max || 100,
1677 step: data.step || 1,
1678 create: function( event, ui ) {
1679 if ( data.unit ) {
1680 $input.after('<span class="ui-button csf--unit">'+ data.unit +'</span>');
1681 }
1682 },
1683 spin: function (event, ui ) {
1684 $input.val(ui.value).trigger('change');
1685 }
1686 });
1687
1688 });
1689 };
1690
1691 //
1692 // Field: switcher
1693 //
1694 $.fn.eshb_field_switcher = function() {
1695 return this.each( function() {
1696
1697 var $switcher = $(this).find('.csf--switcher');
1698
1699 $switcher.on('click', function() {
1700
1701 var value = 0;
1702 var $input = $switcher.find('input');
1703
1704 if ( $switcher.hasClass('csf--active') ) {
1705 $switcher.removeClass('csf--active');
1706 } else {
1707 value = 1;
1708 $switcher.addClass('csf--active');
1709 }
1710
1711 $input.val(value).trigger('change');
1712
1713 });
1714
1715 });
1716 };
1717
1718 //
1719 // Field: tabbed
1720 //
1721 $.fn.eshb_field_tabbed = function() {
1722 return this.each( function() {
1723
1724 var $this = $(this),
1725 $links = $this.find('.csf-tabbed-nav a'),
1726 $contents = $this.find('.csf-tabbed-content');
1727
1728 $contents.eq(0).eshb_reload_script();
1729
1730 $links.on( 'click', function( e ) {
1731
1732 e.preventDefault();
1733
1734 var $link = $(this),
1735 index = $link.index(),
1736 $content = $contents.eq(index);
1737
1738 $link.addClass('csf-tabbed-active').siblings().removeClass('csf-tabbed-active');
1739 $content.eshb_reload_script();
1740 $content.removeClass('hidden').siblings().addClass('hidden');
1741
1742 });
1743
1744 });
1745 };
1746
1747 //
1748 // Field: typography
1749 //
1750 $.fn.eshb_field_typography = function() {
1751 return this.each(function () {
1752
1753 var base = this;
1754 var $this = $(this);
1755 var loaded_fonts = [];
1756 var webfonts = eshb_typography_json.webfonts;
1757 var googlestyles = eshb_typography_json.googlestyles;
1758 var defaultstyles = eshb_typography_json.defaultstyles;
1759
1760 //
1761 //
1762 // Sanitize google font subset
1763 base.sanitize_subset = function( subset ) {
1764 subset = subset.replace('-ext', ' Extended');
1765 subset = subset.charAt(0).toUpperCase() + subset.slice(1);
1766 return subset;
1767 };
1768
1769 //
1770 //
1771 // Sanitize google font styles (weight and style)
1772 base.sanitize_style = function( style ) {
1773 return googlestyles[style] ? googlestyles[style] : style;
1774 };
1775
1776 //
1777 //
1778 // Load google font
1779 base.load_google_font = function( font_family, weight, style ) {
1780
1781 if ( font_family && typeof WebFont === 'object' ) {
1782
1783 weight = weight ? weight.replace('normal', '') : '';
1784 style = style ? style.replace('normal', '') : '';
1785
1786 if ( weight || style ) {
1787 font_family = font_family +':'+ weight + style;
1788 }
1789
1790 if ( loaded_fonts.indexOf( font_family ) === -1 ) {
1791 WebFont.load({ google: { families: [font_family] } });
1792 }
1793
1794 loaded_fonts.push( font_family );
1795
1796 }
1797
1798 };
1799
1800 //
1801 //
1802 // Append select options
1803 base.append_select_options = function( $select, options, condition, type, is_multi ) {
1804
1805 $select.find('option').not(':first').remove();
1806
1807 var opts = '';
1808
1809 $.each( options, function( key, value ) {
1810
1811 var selected;
1812 var name = value;
1813
1814 // is_multi
1815 if ( is_multi ) {
1816 selected = ( condition && condition.indexOf(value) !== -1 ) ? ' selected' : '';
1817 } else {
1818 selected = ( condition && condition === value ) ? ' selected' : '';
1819 }
1820
1821 if ( type === 'subset' ) {
1822 name = base.sanitize_subset( value );
1823 } else if ( type === 'style' ){
1824 name = base.sanitize_style( value );
1825 }
1826
1827 opts += '<option value="'+ value +'"'+ selected +'>'+ name +'</option>';
1828
1829 });
1830
1831 $select.append(opts).trigger('csf.change').trigger('chosen:updated');
1832
1833 };
1834
1835 base.init = function () {
1836
1837 //
1838 //
1839 // Constants
1840 var selected_styles = [];
1841 var $typography = $this.find('.csf--typography');
1842 var $type = $this.find('.csf--type');
1843 var $styles = $this.find('.csf--block-font-style');
1844 var unit = $typography.data('unit');
1845 var line_height_unit = $typography.data('line-height-unit');
1846 var exclude_fonts = $typography.data('exclude') ? $typography.data('exclude').split(',') : [];
1847
1848 //
1849 //
1850 // Chosen init
1851 if ( $this.find('.csf--chosen').length ) {
1852
1853 var $chosen_selects = $this.find('select');
1854
1855 $chosen_selects.each( function() {
1856
1857 var $chosen_select = $(this),
1858 $chosen_inited = $chosen_select.parent().find('.chosen-container');
1859
1860 if ( $chosen_inited.length ) {
1861 $chosen_inited.remove();
1862 }
1863
1864 $chosen_select.chosen({
1865 allow_single_deselect: true,
1866 disable_search_threshold: 15,
1867 width: '100%'
1868 });
1869
1870 });
1871
1872 }
1873
1874 //
1875 //
1876 // Font family select
1877 var $font_family_select = $this.find('.csf--font-family');
1878 var first_font_family = $font_family_select.val();
1879
1880 // Clear default font family select options
1881 $font_family_select.find('option').not(':first-child').remove();
1882
1883 var opts = '';
1884
1885 $.each(webfonts, function( type, group ) {
1886
1887 // Check for exclude fonts
1888 if ( exclude_fonts && exclude_fonts.indexOf(type) !== -1 ) { return; }
1889
1890 opts += '<optgroup label="' + group.label + '">';
1891
1892 $.each(group.fonts, function( key, value ) {
1893
1894 // use key if value is object
1895 value = ( typeof value === 'object' ) ? key : value;
1896 var selected = ( value === first_font_family ) ? ' selected' : '';
1897 opts += '<option value="'+ value +'" data-type="'+ type +'"'+ selected +'>'+ value +'</option>';
1898
1899 });
1900
1901 opts += '</optgroup>';
1902
1903 });
1904
1905 // Append google font select options
1906 $font_family_select.append(opts).trigger('chosen:updated');
1907
1908 //
1909 //
1910 // Font style select
1911 var $font_style_block = $this.find('.csf--block-font-style');
1912
1913 if ( $font_style_block.length ) {
1914
1915 var $font_style_select = $this.find('.csf--font-style-select');
1916 var first_style_value = $font_style_select.val() ? $font_style_select.val().replace(/normal/g, '' ) : '';
1917
1918 //
1919 // Font Style on on change listener
1920 $font_style_select.on('change csf.change', function( event ) {
1921
1922 var style_value = $font_style_select.val();
1923
1924 // set a default value
1925 if ( !style_value && selected_styles && selected_styles.indexOf('normal') === -1 ) {
1926 style_value = selected_styles[0];
1927 }
1928
1929 // set font weight, for eg. replacing 800italic to 800
1930 var font_normal = ( style_value && style_value !== 'italic' && style_value === 'normal' ) ? 'normal' : '';
1931 var font_weight = ( style_value && style_value !== 'italic' && style_value !== 'normal' ) ? style_value.replace('italic', '') : font_normal;
1932 var font_style = ( style_value && style_value.substr(-6) === 'italic' ) ? 'italic' : '';
1933
1934 $this.find('.csf--font-weight').val( font_weight );
1935 $this.find('.csf--font-style').val( font_style );
1936
1937 });
1938
1939 //
1940 //
1941 // Extra font style select
1942 var $extra_font_style_block = $this.find('.csf--block-extra-styles');
1943
1944 if ( $extra_font_style_block.length ) {
1945 var $extra_font_style_select = $this.find('.csf--extra-styles');
1946 var first_extra_style_value = $extra_font_style_select.val();
1947 }
1948
1949 }
1950
1951 //
1952 //
1953 // Subsets select
1954 var $subset_block = $this.find('.csf--block-subset');
1955 if ( $subset_block.length ) {
1956 var $subset_select = $this.find('.csf--subset');
1957 var first_subset_select_value = $subset_select.val();
1958 var subset_multi_select = $subset_select.data('multiple') || false;
1959 }
1960
1961 //
1962 //
1963 // Backup font family
1964 var $backup_font_family_block = $this.find('.csf--block-backup-font-family');
1965
1966 //
1967 //
1968 // Font Family on Change Listener
1969 $font_family_select.on('change csf.change', function( event ) {
1970
1971 // Hide subsets on change
1972 if ( $subset_block.length ) {
1973 $subset_block.addClass('hidden');
1974 }
1975
1976 // Hide extra font style on change
1977 if ( $extra_font_style_block.length ) {
1978 $extra_font_style_block.addClass('hidden');
1979 }
1980
1981 // Hide backup font family on change
1982 if ( $backup_font_family_block.length ) {
1983 $backup_font_family_block.addClass('hidden');
1984 }
1985
1986 var $selected = $font_family_select.find(':selected');
1987 var value = $selected.val();
1988 var type = $selected.data('type');
1989
1990 if ( type && value ) {
1991
1992 // Show backup fonts if font type google or custom
1993 if ( ( type === 'google' || type === 'custom' ) && $backup_font_family_block.length ) {
1994 $backup_font_family_block.removeClass('hidden');
1995 }
1996
1997 // Appending font style select options
1998 if ( $font_style_block.length ) {
1999
2000 // set styles for multi and normal style selectors
2001 var styles = defaultstyles;
2002
2003 // Custom or gogle font styles
2004 if ( type === 'google' && webfonts[type].fonts[value][0] ) {
2005 styles = webfonts[type].fonts[value][0];
2006 } else if ( type === 'custom' && webfonts[type].fonts[value] ) {
2007 styles = webfonts[type].fonts[value];
2008 }
2009
2010 selected_styles = styles;
2011
2012 // Set selected style value for avoid load errors
2013 var set_auto_style = ( styles.indexOf('normal') !== -1 ) ? 'normal' : styles[0];
2014 var set_style_value = ( first_style_value && styles.indexOf(first_style_value) !== -1 ) ? first_style_value : set_auto_style;
2015
2016 // Append style select options
2017 base.append_select_options( $font_style_select, styles, set_style_value, 'style' );
2018
2019 // Clear first value
2020 first_style_value = false;
2021
2022 // Show style select after appended
2023 $font_style_block.removeClass('hidden');
2024
2025 // Appending extra font style select options
2026 if ( type === 'google' && $extra_font_style_block.length && styles.length > 1 ) {
2027
2028 // Append extra-style select options
2029 base.append_select_options( $extra_font_style_select, styles, first_extra_style_value, 'style', true );
2030
2031 // Clear first value
2032 first_extra_style_value = false;
2033
2034 // Show style select after appended
2035 $extra_font_style_block.removeClass('hidden');
2036
2037 }
2038
2039 }
2040
2041 // Appending google fonts subsets select options
2042 if ( type === 'google' && $subset_block.length && webfonts[type].fonts[value][1] ) {
2043
2044 var subsets = webfonts[type].fonts[value][1];
2045 var set_auto_subset = ( subsets.length < 2 && subsets[0] !== 'latin' ) ? subsets[0] : '';
2046 var set_subset_value = ( first_subset_select_value && subsets.indexOf(first_subset_select_value) !== -1 ) ? first_subset_select_value : set_auto_subset;
2047
2048 // check for multiple subset select
2049 set_subset_value = ( subset_multi_select && first_subset_select_value ) ? first_subset_select_value : set_subset_value;
2050
2051 base.append_select_options( $subset_select, subsets, set_subset_value, 'subset', subset_multi_select );
2052
2053 first_subset_select_value = false;
2054
2055 $subset_block.removeClass('hidden');
2056
2057 }
2058
2059 } else {
2060
2061 // Clear Styles
2062 $styles.find(':input').val('');
2063
2064 // Clear subsets options if type and value empty
2065 if ( $subset_block.length ) {
2066 $subset_select.find('option').not(':first-child').remove();
2067 $subset_select.trigger('chosen:updated');
2068 }
2069
2070 // Clear font styles options if type and value empty
2071 if ( $font_style_block.length ) {
2072 $font_style_select.find('option').not(':first-child').remove();
2073 $font_style_select.trigger('chosen:updated');
2074 }
2075
2076 }
2077
2078 // Update font type input value
2079 $type.val(type);
2080
2081 }).trigger('csf.change');
2082
2083 //
2084 //
2085 // Preview
2086 var $preview_block = $this.find('.csf--block-preview');
2087
2088 if ( $preview_block.length ) {
2089
2090 var $preview = $this.find('.csf--preview');
2091
2092 // Set preview styles on change
2093 $this.on('change', CSF.helper.debounce( function( event ) {
2094
2095 $preview_block.removeClass('hidden');
2096
2097 var font_family = $font_family_select.val(),
2098 font_weight = $this.find('.csf--font-weight').val(),
2099 font_style = $this.find('.csf--font-style').val(),
2100 font_size = $this.find('.csf--font-size').val(),
2101 font_variant = $this.find('.csf--font-variant').val(),
2102 line_height = $this.find('.csf--line-height').val(),
2103 text_align = $this.find('.csf--text-align').val(),
2104 text_transform = $this.find('.csf--text-transform').val(),
2105 text_decoration = $this.find('.csf--text-decoration').val(),
2106 text_color = $this.find('.csf--color').val(),
2107 word_spacing = $this.find('.csf--word-spacing').val(),
2108 letter_spacing = $this.find('.csf--letter-spacing').val(),
2109 custom_style = $this.find('.csf--custom-style').val(),
2110 type = $this.find('.csf--type').val();
2111
2112 if ( type === 'google' ) {
2113 base.load_google_font(font_family, font_weight, font_style);
2114 }
2115
2116 var properties = {};
2117
2118 if ( font_family ) { properties.fontFamily = font_family; }
2119 if ( font_weight ) { properties.fontWeight = font_weight; }
2120 if ( font_style ) { properties.fontStyle = font_style; }
2121 if ( font_variant ) { properties.fontVariant = font_variant; }
2122 if ( font_size ) { properties.fontSize = font_size + unit; }
2123 if ( line_height ) { properties.lineHeight = line_height + line_height_unit; }
2124 if ( letter_spacing ) { properties.letterSpacing = letter_spacing + unit; }
2125 if ( word_spacing ) { properties.wordSpacing = word_spacing + unit; }
2126 if ( text_align ) { properties.textAlign = text_align; }
2127 if ( text_transform ) { properties.textTransform = text_transform; }
2128 if ( text_decoration ) { properties.textDecoration = text_decoration; }
2129 if ( text_color ) { properties.color = text_color; }
2130
2131 $preview.removeAttr('style');
2132
2133 // Customs style attribute
2134 if ( custom_style ) { $preview.attr('style', custom_style); }
2135
2136 $preview.css(properties);
2137
2138 }, 100 ) );
2139
2140 // Preview black and white backgrounds trigger
2141 $preview_block.on('click', function() {
2142
2143 $preview.toggleClass('csf--black-background');
2144
2145 var $toggle = $preview_block.find('.csf--toggle');
2146
2147 if ( $toggle.hasClass('fa-toggle-off') ) {
2148 $toggle.removeClass('fa-toggle-off').addClass('fa-toggle-on');
2149 } else {
2150 $toggle.removeClass('fa-toggle-on').addClass('fa-toggle-off');
2151 }
2152
2153 });
2154
2155 if ( !$preview_block.hasClass('hidden') ) {
2156 $this.trigger('change');
2157 }
2158
2159 }
2160
2161 };
2162
2163 base.init();
2164
2165 });
2166 };
2167
2168 //
2169 // Field: upload
2170 //
2171 $.fn.eshb_field_upload = function() {
2172 return this.each( function() {
2173
2174 var $this = $(this),
2175 $input = $this.find('input'),
2176 $upload_button = $this.find('.csf--button'),
2177 $remove_button = $this.find('.csf--remove'),
2178 $preview_wrap = $this.find('.csf--preview'),
2179 $preview_src = $this.find('.csf--src'),
2180 $library = $upload_button.data('library') && $upload_button.data('library').split(',') || '',
2181 wp_media_frame;
2182
2183 $upload_button.on('click', function( e ) {
2184
2185 e.preventDefault();
2186
2187 if ( typeof window.wp === 'undefined' || ! window.wp.media || ! window.wp.media.gallery ) {
2188 return;
2189 }
2190
2191 if ( wp_media_frame ) {
2192 wp_media_frame.open();
2193 return;
2194 }
2195
2196 wp_media_frame = window.wp.media({
2197 library: {
2198 type: $library
2199 },
2200 });
2201
2202 wp_media_frame.on( 'select', function() {
2203
2204 var src;
2205 var attributes = wp_media_frame.state().get('selection').first().attributes;
2206
2207 if ( $library.length && $library.indexOf(attributes.subtype) === -1 && $library.indexOf(attributes.type) === -1 ) {
2208 return;
2209 }
2210
2211 $input.val(attributes.url).trigger('change');
2212
2213 });
2214
2215 wp_media_frame.open();
2216
2217 });
2218
2219 $remove_button.on('click', function( e ) {
2220 e.preventDefault();
2221 $input.val('').trigger('change');
2222 });
2223
2224 $input.on('change', function( e ) {
2225
2226 var $value = $input.val();
2227
2228 if ( $value ) {
2229 $remove_button.removeClass('hidden');
2230 } else {
2231 $remove_button.addClass('hidden');
2232 }
2233
2234 if ( $preview_wrap.length ) {
2235
2236 if ( $.inArray( $value.split('.').pop().toLowerCase(), ['jpg', 'jpeg', 'gif', 'png', 'svg', 'webp'] ) !== -1 ) {
2237 $preview_wrap.removeClass('hidden');
2238 $preview_src.attr('src', $value);
2239 } else {
2240 $preview_wrap.addClass('hidden');
2241 }
2242
2243 }
2244
2245 });
2246
2247 });
2248
2249 };
2250
2251 //
2252 // Field: wp_editor
2253 //
2254 $.fn.eshb_field_wp_editor = function() {
2255 return this.each( function() {
2256
2257 if ( typeof window.wp.editor === 'undefined' || typeof window.tinyMCEPreInit === 'undefined' || typeof window.tinyMCEPreInit.mceInit.eshb_wp_editor === 'undefined' ) {
2258 return;
2259 }
2260
2261 var $this = $(this),
2262 $editor = $this.find('.csf-wp-editor'),
2263 $textarea = $this.find('textarea');
2264
2265 // If there is wp-editor remove it for avoid dupliated wp-editor conflicts.
2266 var $has_wp_editor = $this.find('.wp-editor-wrap').length || $this.find('.mce-container').length;
2267
2268 if ( $has_wp_editor ) {
2269 $editor.empty();
2270 $editor.append($textarea);
2271 $textarea.css('display', '');
2272 }
2273
2274 // Generate a unique id
2275 var uid = CSF.helper.uid('csf-editor-');
2276
2277 $textarea.attr('id', uid);
2278
2279 // Get default editor settings
2280 var default_editor_settings = {
2281 tinymce: window.tinyMCEPreInit.mceInit.eshb_wp_editor,
2282 quicktags: window.tinyMCEPreInit.qtInit.eshb_wp_editor
2283 };
2284
2285 // Get default editor settings
2286 var field_editor_settings = $editor.data('editor-settings');
2287
2288 // Callback for old wp editor
2289 var wpEditor = wp.oldEditor ? wp.oldEditor : wp.editor;
2290
2291 if ( wpEditor && wpEditor.hasOwnProperty('autop') ) {
2292 wp.editor.autop = wpEditor.autop;
2293 wp.editor.removep = wpEditor.removep;
2294 wp.editor.initialize = wpEditor.initialize;
2295 }
2296
2297 // Add on change event handle
2298 var editor_on_change = function( editor ) {
2299 editor.on('change keyup', function() {
2300 var value = ( field_editor_settings.wpautop ) ? editor.getContent() : wp.editor.removep(editor.getContent());
2301 $textarea.val(value).trigger('change');
2302 });
2303 };
2304
2305 // Extend editor selector and on change event handler
2306 default_editor_settings.tinymce = $.extend( {}, default_editor_settings.tinymce, { selector: '#'+ uid, setup: editor_on_change } );
2307
2308 // Override editor tinymce settings
2309 if ( field_editor_settings.tinymce === false ) {
2310 default_editor_settings.tinymce = false;
2311 $editor.addClass('csf-no-tinymce');
2312 }
2313
2314 // Override editor quicktags settings
2315 if ( field_editor_settings.quicktags === false ) {
2316 default_editor_settings.quicktags = false;
2317 $editor.addClass('csf-no-quicktags');
2318 }
2319
2320 // Wait until :visible
2321 var interval = setInterval(function () {
2322 if ( $this.is(':visible') ) {
2323 window.wp.editor.initialize(uid, default_editor_settings);
2324 clearInterval(interval);
2325 }
2326 });
2327
2328 // Add Media buttons
2329 if ( field_editor_settings.media_buttons && window.eshb_media_buttons ) {
2330
2331 var $editor_buttons = $editor.find('.wp-media-buttons');
2332
2333 if ( $editor_buttons.length ) {
2334
2335 $editor_buttons.find('.csf-shortcode-button').data('editor-id', uid);
2336
2337 } else {
2338
2339 var $media_buttons = $(window.eshb_media_buttons);
2340
2341 $media_buttons.find('.csf-shortcode-button').data('editor-id', uid);
2342
2343 $editor.prepend( $media_buttons );
2344
2345 }
2346
2347 }
2348
2349 });
2350
2351 };
2352
2353 //
2354 // Confirm
2355 //
2356 $.fn.eshb_confirm = function() {
2357 return this.each( function() {
2358 $(this).on('click', function( e ) {
2359
2360 var confirm_text = $(this).data('confirm') || window.eshb_vars.i18n.confirm;
2361 var confirm_answer = confirm( confirm_text );
2362
2363 if ( confirm_answer ) {
2364 CSF.vars.is_confirm = true;
2365 CSF.vars.form_modified = false;
2366 } else {
2367 e.preventDefault();
2368 return false;
2369 }
2370
2371 });
2372 });
2373 };
2374
2375 $.fn.serializeObject = function() {
2376
2377 var obj = {};
2378
2379 $.each( this.serializeArray(), function(i,o){
2380 var n = o.name,
2381 v = o.value;
2382
2383 obj[n] = obj[n] === undefined ? v
2384 : $.isArray( obj[n] ) ? obj[n].concat( v )
2385 : [ obj[n], v ];
2386 });
2387
2388 return obj;
2389
2390 };
2391
2392 //
2393 // Options Save
2394 //
2395 $.fn.eshb_save = function() {
2396 return this.each( function() {
2397
2398 var $this = $(this),
2399 $buttons = $('.csf-save'),
2400 $panel = $('.csf-options'),
2401 flooding = false,
2402 timeout;
2403
2404 $this.on('click', function( e ) {
2405
2406 if ( !flooding ) {
2407
2408 var $text = $this.data('save'),
2409 $value = $this.val();
2410
2411 $buttons.attr('value', $text);
2412
2413 if ( $this.hasClass('csf-save-ajax') ) {
2414
2415 e.preventDefault();
2416
2417 $panel.addClass('csf-saving');
2418 $buttons.prop('disabled', true);
2419
2420 window.wp.ajax.post( 'eshb_'+ $panel.data('unique') +'_ajax_save', {
2421 data: $('#csf-form').serializeJSONCSF()
2422 })
2423 .done( function( response ) {
2424
2425
2426 // clear errors
2427 $('.csf-error').remove();
2428
2429 if ( Object.keys( response.errors ).length ) {
2430
2431 var error_icon = '<i class="csf-label-error csf-error">!</i>';
2432
2433 $.each(response.errors, function( key, error_message ) {
2434
2435 var $field = $('[data-depend-id="'+ key +'"]'),
2436 $link = $('a[href="#tab='+ $field.closest('.csf-section').data('section-id') +'"]' ),
2437 $tab = $link.closest('.csf-tab-item');
2438
2439 $field.closest('.csf-fieldset').append( '<p class="csf-error csf-error-text">'+ error_message +'</p>' );
2440
2441 if ( !$link.find('.csf-error').length ) {
2442 $link.append( error_icon );
2443 }
2444
2445 if ( !$tab.find('.csf-arrow .csf-error').length ) {
2446 $tab.find('.csf-arrow').append( error_icon );
2447 }
2448
2449 });
2450
2451 }
2452
2453 $panel.removeClass('csf-saving');
2454 $buttons.prop('disabled', false).attr('value', $value);
2455 flooding = false;
2456
2457 CSF.vars.form_modified = false;
2458 CSF.vars.$form_warning.hide();
2459
2460 clearTimeout(timeout);
2461
2462 var $result_success = $('.csf-form-success');
2463 $result_success.empty().append(response.notice).fadeIn('fast', function() {
2464 timeout = setTimeout( function() {
2465 $result_success.fadeOut('fast');
2466 }, 1000);
2467 });
2468
2469 })
2470 .fail( function( response ) {
2471 alert( response.error );
2472 });
2473
2474 } else {
2475
2476 CSF.vars.form_modified = false;
2477
2478 }
2479
2480 }
2481
2482 flooding = true;
2483
2484 });
2485
2486 });
2487 };
2488
2489 //
2490 // Option Framework
2491 //
2492 $.fn.eshb_options = function() {
2493 return this.each( function() {
2494
2495 var $this = $(this),
2496 $content = $this.find('.csf-content'),
2497 $form_success = $this.find('.csf-form-success'),
2498 $form_warning = $this.find('.csf-form-warning'),
2499 $save_button = $this.find('.csf-header .csf-save');
2500
2501 CSF.vars.$form_warning = $form_warning;
2502
2503 // Shows a message white leaving theme options without saving
2504 if ( $form_warning.length ) {
2505
2506 window.onbeforeunload = function() {
2507 return ( CSF.vars.form_modified ) ? true : undefined;
2508 };
2509
2510 $content.on('change keypress', ':input', function() {
2511 if ( !CSF.vars.form_modified ) {
2512 $form_success.hide();
2513 $form_warning.fadeIn('fast');
2514 CSF.vars.form_modified = true;
2515 }
2516 });
2517
2518 }
2519
2520 if ( $form_success.hasClass('csf-form-show') ) {
2521 setTimeout( function() {
2522 $form_success.fadeOut('fast');
2523 }, 1000);
2524 }
2525
2526 $(document).keydown(function (event) {
2527 if ( ( event.ctrlKey || event.metaKey ) && event.which === 83 ) {
2528 $save_button.trigger('click');
2529 event.preventDefault();
2530 return false;
2531 }
2532 });
2533
2534 });
2535 };
2536
2537 //
2538 // Taxonomy Framework
2539 //
2540 $.fn.eshb_taxonomy = function() {
2541 return this.each( function() {
2542
2543 var $this = $(this),
2544 $form = $this.parents('form');
2545
2546 if ( $form.attr('id') === 'addtag' ) {
2547
2548 var $submit = $form.find('#submit'),
2549 $cloned = $this.children('.csf-field').eshb_clone();
2550
2551 $submit.on( 'click', function() {
2552
2553 if ( !$form.find('.form-required').hasClass('form-invalid') ) {
2554
2555 $this.data('inited', false);
2556
2557 $this.empty();
2558
2559 $this.html( $cloned );
2560
2561 $cloned = $cloned.eshb_clone();
2562
2563 $this.eshb_reload_script();
2564
2565 }
2566
2567 });
2568
2569 }
2570
2571 });
2572 };
2573
2574 //
2575 // Shortcode Framework
2576 //
2577 $.fn.eshb_shortcode = function() {
2578
2579 var base = this;
2580
2581 base.shortcode_parse = function( serialize, key ) {
2582
2583 var shortcode = '';
2584
2585 $.each(serialize, function( shortcode_key, shortcode_values ) {
2586
2587 key = ( key ) ? key : shortcode_key;
2588
2589 shortcode += '[' + key;
2590
2591 $.each(shortcode_values, function( shortcode_tag, shortcode_value ) {
2592
2593 if ( shortcode_tag === 'content' ) {
2594
2595 shortcode += ']';
2596 shortcode += shortcode_value;
2597 shortcode += '[/'+ key +'';
2598
2599 } else {
2600
2601 shortcode += base.shortcode_tags( shortcode_tag, shortcode_value );
2602
2603 }
2604
2605 });
2606
2607 shortcode += ']';
2608
2609 });
2610
2611 return shortcode;
2612
2613 };
2614
2615 base.shortcode_tags = function( shortcode_tag, shortcode_value ) {
2616
2617 var shortcode = '';
2618
2619 if ( shortcode_value !== '' ) {
2620
2621 if ( typeof shortcode_value === 'object' && !$.isArray( shortcode_value ) ) {
2622
2623 $.each(shortcode_value, function( sub_shortcode_tag, sub_shortcode_value ) {
2624
2625 // sanitize spesific key/value
2626 switch( sub_shortcode_tag ) {
2627
2628 case 'background-image':
2629 sub_shortcode_value = ( sub_shortcode_value.url ) ? sub_shortcode_value.url : '';
2630 break;
2631
2632 }
2633
2634 if ( sub_shortcode_value !== '' ) {
2635 shortcode += ' ' + sub_shortcode_tag + '="' + sub_shortcode_value.toString() + '"';
2636 }
2637
2638 });
2639
2640 } else {
2641
2642 shortcode += ' ' + shortcode_tag + '="' + shortcode_value.toString() + '"';
2643
2644 }
2645
2646 }
2647
2648 return shortcode;
2649
2650 };
2651
2652 base.insertAtChars = function( _this, currentValue ) {
2653
2654 var obj = ( typeof _this[0].name !== 'undefined' ) ? _this[0] : _this;
2655
2656 if ( obj.value.length && typeof obj.selectionStart !== 'undefined' ) {
2657 obj.focus();
2658 return obj.value.substring( 0, obj.selectionStart ) + currentValue + obj.value.substring( obj.selectionEnd, obj.value.length );
2659 } else {
2660 obj.focus();
2661 return currentValue;
2662 }
2663
2664 };
2665
2666 base.send_to_editor = function( html, editor_id ) {
2667
2668 var tinymce_editor;
2669
2670 if ( typeof tinymce !== 'undefined' ) {
2671 tinymce_editor = tinymce.get( editor_id );
2672 }
2673
2674 if ( tinymce_editor && !tinymce_editor.isHidden() ) {
2675 tinymce_editor.execCommand( 'mceInsertContent', false, html );
2676 } else {
2677 var $editor = $('#'+editor_id);
2678 $editor.val( base.insertAtChars( $editor, html ) ).trigger('change');
2679 }
2680
2681 };
2682
2683 return this.each( function() {
2684
2685 var $modal = $(this),
2686 $load = $modal.find('.csf-modal-load'),
2687 $content = $modal.find('.csf-modal-content'),
2688 $insert = $modal.find('.csf-modal-insert'),
2689 $loading = $modal.find('.csf-modal-loading'),
2690 $select = $modal.find('select'),
2691 modal_id = $modal.data('modal-id'),
2692 nonce = $modal.data('nonce'),
2693 editor_id,
2694 target_id,
2695 gutenberg_id,
2696 sc_key,
2697 sc_name,
2698 sc_view,
2699 sc_group,
2700 $cloned,
2701 $button;
2702
2703 $(document).on('click', '.csf-shortcode-button[data-modal-id="'+ modal_id +'"]', function( e ) {
2704
2705 e.preventDefault();
2706
2707 $button = $(this);
2708 editor_id = $button.data('editor-id') || false;
2709 target_id = $button.data('target-id') || false;
2710 gutenberg_id = $button.data('gutenberg-id') || false;
2711
2712 $modal.removeClass('hidden');
2713
2714 // single usage trigger first shortcode
2715 if ( $modal.hasClass('csf-shortcode-single') && sc_name === undefined ) {
2716 $select.trigger('change');
2717 }
2718
2719 });
2720
2721 $select.on( 'change', function() {
2722
2723 var $option = $(this);
2724 var $selected = $option.find(':selected');
2725
2726 sc_key = $option.val();
2727 sc_name = $selected.data('shortcode');
2728 sc_view = $selected.data('view') || 'normal';
2729 sc_group = $selected.data('group') || sc_name;
2730
2731 $load.empty();
2732
2733 if ( sc_key ) {
2734
2735 $loading.show();
2736
2737 window.wp.ajax.post( 'csf-get-shortcode-'+ modal_id, {
2738 shortcode_key: sc_key,
2739 nonce: nonce
2740 })
2741 .done( function( response ) {
2742
2743 $loading.hide();
2744
2745 var $appended = $(response.content).appendTo($load);
2746
2747 $insert.parent().removeClass('hidden');
2748
2749 $cloned = $appended.find('.csf--repeat-shortcode').eshb_clone();
2750
2751 $appended.eshb_reload_script();
2752 $appended.find('.csf-fields').eshb_reload_script();
2753
2754 });
2755
2756 } else {
2757
2758 $insert.parent().addClass('hidden');
2759
2760 }
2761
2762 });
2763
2764 $insert.on('click', function( e ) {
2765
2766 e.preventDefault();
2767
2768 if ( $insert.prop('disabled') || $insert.attr('disabled') ) { return; }
2769
2770 var shortcode = '';
2771 var serialize = $modal.find('.csf-field:not(.csf-depend-on)').find(':input:not(.ignore)').serializeObjectCSF();
2772
2773 switch ( sc_view ) {
2774
2775 case 'contents':
2776 var contentsObj = ( sc_name ) ? serialize[sc_name] : serialize;
2777 $.each(contentsObj, function( sc_key, sc_value ) {
2778 var sc_tag = ( sc_name ) ? sc_name : sc_key;
2779 shortcode += '['+ sc_tag +']'+ sc_value +'[/'+ sc_tag +']';
2780 });
2781 break;
2782
2783 case 'group':
2784
2785 shortcode += '[' + sc_name;
2786 $.each(serialize[sc_name], function( sc_key, sc_value ) {
2787 shortcode += base.shortcode_tags( sc_key, sc_value );
2788 });
2789 shortcode += ']';
2790 shortcode += base.shortcode_parse( serialize[sc_group], sc_group );
2791 shortcode += '[/' + sc_name + ']';
2792
2793 break;
2794
2795 case 'repeater':
2796 shortcode += base.shortcode_parse( serialize[sc_group], sc_group );
2797 break;
2798
2799 default:
2800 shortcode += base.shortcode_parse( serialize );
2801 break;
2802
2803 }
2804
2805 shortcode = ( shortcode === '' ) ? '['+ sc_name +']' : shortcode;
2806
2807 if ( gutenberg_id ) {
2808
2809 var content = window.eshb_gutenberg_props.attributes.hasOwnProperty('shortcode') ? window.eshb_gutenberg_props.attributes.shortcode : '';
2810 window.eshb_gutenberg_props.setAttributes({shortcode: content + shortcode});
2811
2812 } else if ( editor_id ) {
2813
2814 base.send_to_editor( shortcode, editor_id );
2815
2816 } else {
2817
2818 var $textarea = (target_id) ? $(target_id) : $button.parent().find('textarea');
2819 $textarea.val( base.insertAtChars( $textarea, shortcode ) ).trigger('change');
2820
2821 }
2822
2823 $modal.addClass('hidden');
2824
2825 });
2826
2827 $modal.on('click', '.csf--repeat-button', function( e ) {
2828
2829 e.preventDefault();
2830
2831 var $repeatable = $modal.find('.csf--repeatable');
2832 var $new_clone = $cloned.eshb_clone();
2833 var $remove_btn = $new_clone.find('.csf-repeat-remove');
2834
2835 var $appended = $new_clone.appendTo( $repeatable );
2836
2837 $new_clone.find('.csf-fields').eshb_reload_script();
2838
2839 CSF.helper.name_nested_replace( $modal.find('.csf--repeat-shortcode'), sc_group );
2840
2841 $remove_btn.on('click', function() {
2842
2843 $new_clone.remove();
2844
2845 CSF.helper.name_nested_replace( $modal.find('.csf--repeat-shortcode'), sc_group );
2846
2847 });
2848
2849 });
2850
2851 $modal.on('click', '.csf-modal-close, .csf-modal-overlay', function() {
2852 $modal.addClass('hidden');
2853 });
2854
2855 });
2856 };
2857
2858 //
2859 // WP Color Picker
2860 //
2861 if ( typeof Color === 'function' ) {
2862
2863 Color.prototype.toString = function() {
2864
2865 if ( this._alpha < 1 ) {
2866 return this.toCSS('rgba', this._alpha).replace(/\s+/g, '');
2867 }
2868
2869 var hex = parseInt( this._color, 10 ).toString( 16 );
2870
2871 if ( this.error ) { return ''; }
2872
2873 if ( hex.length < 6 ) {
2874 for (var i = 6 - hex.length - 1; i >= 0; i--) {
2875 hex = '0' + hex;
2876 }
2877 }
2878
2879 return '#' + hex;
2880
2881 };
2882
2883 }
2884
2885 CSF.funcs.parse_color = function( color ) {
2886
2887 var value = color.replace(/\s+/g, ''),
2888 trans = ( value.indexOf('rgba') !== -1 ) ? parseFloat( value.replace(/^.*,(.+)\)/, '$1') * 100 ) : 100,
2889 rgba = ( trans < 100 ) ? true : false;
2890
2891 return { value: value, transparent: trans, rgba: rgba };
2892
2893 };
2894
2895 $.fn.eshb_color = function() {
2896 return this.each( function() {
2897
2898 var $input = $(this),
2899 picker_color = CSF.funcs.parse_color( $input.val() ),
2900 palette_color = window.eshb_vars.color_palette.length ? window.eshb_vars.color_palette : true,
2901 $container;
2902
2903 // Destroy and Reinit
2904 if ( $input.hasClass('wp-color-picker') ) {
2905 $input.closest('.wp-picker-container').after($input).remove();
2906 }
2907
2908 $input.wpColorPicker({
2909 palettes: palette_color,
2910 change: function( event, ui ) {
2911
2912 var ui_color_value = ui.color.toString();
2913
2914 $container.removeClass('csf--transparent-active');
2915 $container.find('.csf--transparent-offset').css('background-color', ui_color_value);
2916 $input.val(ui_color_value).trigger('change');
2917
2918 },
2919 create: function() {
2920
2921 $container = $input.closest('.wp-picker-container');
2922
2923 var a8cIris = $input.data('a8cIris'),
2924 $transparent_wrap = $('<div class="csf--transparent-wrap">' +
2925 '<div class="csf--transparent-slider"></div>' +
2926 '<div class="csf--transparent-offset"></div>' +
2927 '<div class="csf--transparent-text"></div>' +
2928 '<div class="csf--transparent-button">transparent <i class="fas fa-toggle-off"></i></div>' +
2929 '</div>').appendTo( $container.find('.wp-picker-holder') ),
2930 $transparent_slider = $transparent_wrap.find('.csf--transparent-slider'),
2931 $transparent_text = $transparent_wrap.find('.csf--transparent-text'),
2932 $transparent_offset = $transparent_wrap.find('.csf--transparent-offset'),
2933 $transparent_button = $transparent_wrap.find('.csf--transparent-button');
2934
2935 if ( $input.val() === 'transparent' ) {
2936 $container.addClass('csf--transparent-active');
2937 }
2938
2939 $transparent_button.on('click', function() {
2940 if ( $input.val() !== 'transparent' ) {
2941 $input.val('transparent').trigger('change').removeClass('iris-error');
2942 $container.addClass('csf--transparent-active');
2943 } else {
2944 $input.val( a8cIris._color.toString() ).trigger('change');
2945 $container.removeClass('csf--transparent-active');
2946 }
2947 });
2948
2949 $transparent_slider.slider({
2950 value: picker_color.transparent,
2951 step: 1,
2952 min: 0,
2953 max: 100,
2954 slide: function( event, ui ) {
2955
2956 var slide_value = parseFloat( ui.value / 100 );
2957 a8cIris._color._alpha = slide_value;
2958 $input.wpColorPicker( 'color', a8cIris._color.toString() );
2959 $transparent_text.text( ( slide_value === 1 || slide_value === 0 ? '' : slide_value ) );
2960
2961 },
2962 create: function() {
2963
2964 var slide_value = parseFloat( picker_color.transparent / 100 ),
2965 text_value = slide_value < 1 ? slide_value : '';
2966
2967 $transparent_text.text(text_value);
2968 $transparent_offset.css('background-color', picker_color.value);
2969
2970 $container.on('click', '.wp-picker-clear', function() {
2971
2972 a8cIris._color._alpha = 1;
2973 $transparent_text.text('');
2974 $transparent_slider.slider('option', 'value', 100);
2975 $container.removeClass('csf--transparent-active');
2976 $input.trigger('change');
2977
2978 });
2979
2980 $container.on('click', '.wp-picker-default', function() {
2981
2982 var default_color = CSF.funcs.parse_color( $input.data('default-color') ),
2983 default_value = parseFloat( default_color.transparent / 100 ),
2984 default_text = default_value < 1 ? default_value : '';
2985
2986 a8cIris._color._alpha = default_value;
2987 $transparent_text.text(default_text);
2988 $transparent_slider.slider('option', 'value', default_color.transparent);
2989
2990 if ( default_color.value === 'transparent' ) {
2991 $input.removeClass('iris-error');
2992 $container.addClass('csf--transparent-active');
2993 }
2994
2995 });
2996
2997 }
2998 });
2999 }
3000 });
3001
3002 });
3003 };
3004
3005 //
3006 // ChosenJS
3007 //
3008 $.fn.eshb_chosen = function() {
3009 return this.each( function() {
3010
3011 var $this = $(this),
3012 $inited = $this.parent().find('.chosen-container'),
3013 is_sortable = $this.hasClass('eshb-chosen-sortable') || false,
3014 is_ajax = $this.hasClass('eshb-chosen-ajax') || false,
3015 is_multiple = $this.attr('multiple') || false,
3016 set_width = is_multiple ? '100%' : 'auto',
3017 set_options = $.extend({
3018 allow_single_deselect: true,
3019 disable_search_threshold: 10,
3020 width: set_width,
3021 no_results_text: window.eshb_vars.i18n.no_results_text,
3022 }, $this.data('chosen-settings'));
3023
3024 if ( $inited.length ) {
3025 $inited.remove();
3026 }
3027
3028 // Chosen ajax
3029 if ( is_ajax ) {
3030
3031 var set_ajax_options = $.extend({
3032 data: {
3033 type: 'post',
3034 nonce: '',
3035 },
3036 allow_single_deselect: true,
3037 disable_search_threshold: -1,
3038 width: '100%',
3039 min_length: 3,
3040 type_delay: 500,
3041 typing_text: window.eshb_vars.i18n.typing_text,
3042 searching_text: window.eshb_vars.i18n.searching_text,
3043 no_results_text: window.eshb_vars.i18n.no_results_text,
3044 }, $this.data('chosen-settings'));
3045
3046 $this.CSFAjaxChosen(set_ajax_options);
3047
3048 } else {
3049
3050 $this.chosen(set_options);
3051
3052 }
3053
3054 // Chosen keep options order
3055 if ( is_multiple ) {
3056
3057 var $hidden_select = $this.parent().find('.csf-hide-select');
3058 var $hidden_value = $hidden_select.val() || [];
3059
3060 $this.on('change', function(obj, result) {
3061
3062 if ( result && result.selected ) {
3063 $hidden_select.append( '<option value="'+ result.selected +'" selected="selected">'+ result.selected +'</option>' );
3064 } else if ( result && result.deselected ) {
3065 $hidden_select.find('option[value="'+ result.deselected +'"]').remove();
3066 }
3067
3068 // Force customize refresh
3069 if ( window.wp.customize !== undefined && $hidden_select.children().length === 0 && $hidden_select.data('customize-setting-link') ) {
3070 window.wp.customize.control( $hidden_select.data('customize-setting-link') ).setting.set('');
3071 }
3072
3073 $hidden_select.trigger('change');
3074
3075 });
3076
3077 // Chosen order abstract
3078 $this.CSFChosenOrder($hidden_value, true);
3079
3080 }
3081
3082 // Chosen sortable
3083 if ( is_sortable ) {
3084
3085 var $chosen_container = $this.parent().find('.chosen-container');
3086 var $chosen_choices = $chosen_container.find('.chosen-choices');
3087
3088 $chosen_choices.bind('mousedown', function( event ) {
3089 if ( $(event.target).is('span') ) {
3090 event.stopPropagation();
3091 }
3092 });
3093
3094 $chosen_choices.sortable({
3095 items: 'li:not(.search-field)',
3096 helper: 'orginal',
3097 cursor: 'move',
3098 placeholder: 'search-choice-placeholder',
3099 start: function(e,ui) {
3100 ui.placeholder.width( ui.item.innerWidth() );
3101 ui.placeholder.height( ui.item.innerHeight() );
3102 },
3103 update: function( e, ui ) {
3104
3105 var select_options = '';
3106 var chosen_object = $this.data('chosen');
3107 var $prev_select = $this.parent().find('.csf-hide-select');
3108
3109 $chosen_choices.find('.search-choice-close').each( function() {
3110 var option_array_index = $(this).data('option-array-index');
3111 $.each(chosen_object.results_data, function(index, data) {
3112 if ( data.array_index === option_array_index ){
3113 select_options += '<option value="'+ data.value +'" selected>'+ data.value +'</option>';
3114 }
3115 });
3116 });
3117
3118 $prev_select.children().remove();
3119 $prev_select.append(select_options);
3120 $prev_select.trigger('change');
3121
3122 }
3123 });
3124
3125 }
3126
3127 });
3128 };
3129
3130 //
3131 // Helper Checkbox Checker
3132 //
3133 $.fn.eshb_checkbox = function() {
3134 return this.each( function() {
3135
3136 var $this = $(this),
3137 $input = $this.find('.csf--input'),
3138 $checkbox = $this.find('.csf--checkbox');
3139
3140 $checkbox.on('click', function() {
3141 $input.val( Number( $checkbox.prop('checked') ) ).trigger('change');
3142 });
3143
3144 });
3145 };
3146
3147 //
3148 // Helper Check/Uncheck All
3149 //
3150 $.fn.eshb_checkbox_all = function() {
3151 return this.each( function() {
3152
3153 var $this = $(this);
3154
3155 $this.on('click', function() {
3156
3157 var $inputs = $this.closest('.csf-field-checkbox').find(':input'),
3158 uncheck = false;
3159
3160 $inputs.each(function() {
3161 if ( ! $(this).prop('checked') ) {
3162 uncheck = true;
3163 }
3164 });
3165
3166 if ( uncheck ) {
3167 $inputs.prop('checked', 'checked');
3168 $inputs.attr('checked', 'checked');
3169 } else {
3170 $inputs.prop('checked', '');
3171 $inputs.removeAttr('checked');
3172 }
3173
3174 $inputs.first().trigger('change');
3175
3176 });
3177
3178 });
3179 };
3180
3181 //
3182 // Siblings
3183 //
3184 $.fn.eshb_siblings = function() {
3185 return this.each( function() {
3186
3187 var $this = $(this),
3188 $siblings = $this.find('.csf--sibling'),
3189 multiple = $this.data('multiple') || false;
3190
3191 $siblings.on('click', function() {
3192
3193 var $sibling = $(this);
3194
3195 if ( multiple ) {
3196
3197 if ( $sibling.hasClass('csf--active') ) {
3198 $sibling.removeClass('csf--active');
3199 $sibling.find('input').prop('checked', false).trigger('change');
3200 } else {
3201 $sibling.addClass('csf--active');
3202 $sibling.find('input').prop('checked', true).trigger('change');
3203 }
3204
3205 } else {
3206
3207 $this.find('input').prop('checked', false);
3208 $sibling.find('input').prop('checked', true).trigger('change');
3209 $sibling.addClass('csf--active').siblings().removeClass('csf--active');
3210
3211 }
3212
3213 });
3214
3215 });
3216 };
3217
3218 //
3219 // Help Tooltip
3220 //
3221 $.fn.eshb_help = function() {
3222 return this.each( function() {
3223
3224 var $this = $(this),
3225 $tooltip,
3226 offset_left;
3227
3228 $this.on({
3229 mouseenter: function() {
3230
3231 $tooltip = $( '<div class="csf-tooltip"></div>' ).html( $this.find('.csf-help-text').html() ).appendTo('body');
3232 offset_left = ( CSF.vars.is_rtl ) ? ( $this.offset().left + 24 ) : ( $this.offset().left - $tooltip.outerWidth() );
3233
3234 $tooltip.css({
3235 top: $this.offset().top - ( ( $tooltip.outerHeight() / 2 ) - 14 ),
3236 left: offset_left,
3237 });
3238
3239 },
3240 mouseleave: function() {
3241
3242 if ( $tooltip !== undefined ) {
3243 $tooltip.remove();
3244 }
3245
3246 }
3247
3248 });
3249
3250 });
3251 };
3252
3253 //
3254 // Customize Refresh
3255 //
3256 $.fn.eshb_customizer_refresh = function() {
3257 return this.each( function() {
3258
3259 var $this = $(this),
3260 $complex = $this.closest('.csf-customize-complex');
3261
3262 if ( $complex.length ) {
3263
3264 var unique_id = $complex.data('unique-id');
3265
3266 if ( unique_id === undefined ) {
3267 return;
3268 }
3269
3270 var $input = $complex.find(':input'),
3271 option_id = $complex.data('option-id'),
3272 obj = $input.serializeObjectCSF(),
3273 data = ( ! $.isEmptyObject(obj) && obj[unique_id] && obj[unique_id][option_id] ) ? obj[unique_id][option_id] : '',
3274 control = window.wp.customize.control(unique_id +'['+ option_id +']');
3275
3276 // clear the value to force refresh.
3277 control.setting._value = null;
3278
3279 control.setting.set( data );
3280
3281 } else {
3282
3283 $this.find(':input').first().trigger('change');
3284
3285 }
3286
3287 $(document).trigger('csf-customizer-refresh', $this);
3288
3289 });
3290 };
3291
3292 //
3293 // Customize Listen Form Elements
3294 //
3295 $.fn.eshb_customizer_listen = function( options ) {
3296
3297 var settings = $.extend({
3298 closest: false,
3299 }, options );
3300
3301 return this.each( function() {
3302
3303 if ( window.wp.customize === undefined ) { return; }
3304
3305 var $this = ( settings.closest ) ? $(this).closest('.csf-customize-complex') : $(this),
3306 $input = $this.find(':input'),
3307 unique_id = $this.data('unique-id'),
3308 option_id = $this.data('option-id');
3309
3310 if ( unique_id === undefined ) {
3311 return;
3312 }
3313
3314 $input.on('change keyup csf.change', function() {
3315
3316 var obj = $this.find(':input').serializeObjectCSF();
3317 var val = ( !$.isEmptyObject(obj) && obj[unique_id] && obj[unique_id][option_id] ) ? obj[unique_id][option_id] : '';
3318
3319 window.wp.customize.control( unique_id +'['+ option_id +']' ).setting.set( val );
3320
3321 });
3322
3323 });
3324 };
3325
3326 //
3327 // Customizer Listener for Reload JS
3328 //
3329 $(document).on('expanded', '.control-section', function() {
3330
3331 var $this = $(this);
3332
3333 if ( $this.hasClass('open') && !$this.data('inited') ) {
3334
3335 var $fields = $this.find('.csf-customize-field');
3336 var $complex = $this.find('.csf-customize-complex');
3337
3338 if ( $fields.length ) {
3339 $this.eshb_dependency();
3340 $fields.eshb_reload_script({dependency: false});
3341 $complex.eshb_customizer_listen();
3342 }
3343
3344 $this.data('inited', true);
3345
3346 }
3347
3348 });
3349
3350 //
3351 // Window on resize
3352 //
3353 CSF.vars.$window.on('resize csf.resize', CSF.helper.debounce( function( event ) {
3354
3355 var window_width = navigator.userAgent.indexOf('AppleWebKit/') > -1 ? CSF.vars.$window.width() : window.innerWidth;
3356
3357 if ( window_width <= 782 && !CSF.vars.onloaded ) {
3358 $('.csf-section').eshb_reload_script();
3359 CSF.vars.onloaded = true;
3360 }
3361
3362 }, 200)).trigger('csf.resize');
3363
3364 //
3365 // Widgets Framework
3366 //
3367 $.fn.eshb_widgets = function() {
3368 return this.each( function() {
3369
3370 $(document).on('widget-added widget-updated', function( event, $widget ) {
3371
3372 var $fields = $widget.find('.csf-fields');
3373
3374 if ( $fields.length ) {
3375 $fields.eshb_reload_script();
3376 }
3377
3378 });
3379
3380 $(document).on('click', '.widget-top', function( event ) {
3381
3382 var $fields = $(this).parent().find('.csf-fields');
3383
3384 if ( $fields.length ) {
3385 $fields.eshb_reload_script();
3386 }
3387
3388 });
3389
3390 $('.widgets-sortables, .control-section-sidebar').on('sortstop', function( event, ui ) {
3391 ui.item.find('.csf-fields').eshb_reload_script_retry();
3392 });
3393
3394 });
3395 };
3396
3397 //
3398 // Nav Menu Options Framework
3399 //
3400 $.fn.eshb_nav_menu = function() {
3401 return this.each( function() {
3402
3403 var $navmenu = $(this);
3404
3405 $navmenu.on('click', 'a.item-edit', function() {
3406 $(this).closest('li.menu-item').find('.csf-fields').eshb_reload_script();
3407 });
3408
3409 $navmenu.on('sortstop', function( event, ui ) {
3410 ui.item.find('.csf-fields').eshb_reload_script_retry();
3411 });
3412
3413 });
3414 };
3415
3416 //
3417 // Retry Plugins
3418 //
3419 $.fn.eshb_reload_script_retry = function() {
3420 return this.each( function() {
3421
3422 var $this = $(this);
3423
3424 if ( $this.data('inited') ) {
3425 $this.children('.csf-field-wp_editor').eshb_field_wp_editor();
3426 }
3427
3428 });
3429 };
3430
3431 //
3432 // Reload Plugins
3433 //
3434 $.fn.eshb_reload_script = function( options ) {
3435
3436 var settings = $.extend({
3437 dependency: true,
3438 }, options );
3439
3440 return this.each( function() {
3441
3442 var $this = $(this);
3443
3444 // Avoid for conflicts
3445 if ( !$this.data('inited') ) {
3446
3447 // Field plugins
3448 $this.children('.csf-field-accordion').eshb_field_accordion();
3449 $this.children('.csf-field-backup').eshb_field_backup();
3450 $this.children('.csf-field-background').eshb_field_background();
3451 $this.children('.csf-field-code_editor').eshb_field_code_editor();
3452 $this.children('.csf-field-date').eshb_field_date();
3453 $this.children('.csf-field-datetime').eshb_field_datetime();
3454 $this.children('.csf-field-fieldset').eshb_field_fieldset();
3455 $this.children('.csf-field-gallery').eshb_field_gallery();
3456 $this.children('.csf-field-group').eshb_field_group();
3457 $this.children('.csf-field-icon').eshb_field_icon();
3458 $this.children('.csf-field-link').eshb_field_link();
3459 $this.children('.csf-field-media').eshb_field_media();
3460 $this.children('.csf-field-map').eshb_field_map();
3461 $this.children('.csf-field-repeater').eshb_field_repeater();
3462 $this.children('.csf-field-slider').eshb_field_slider();
3463 $this.children('.csf-field-sortable').eshb_field_sortable();
3464 $this.children('.csf-field-sorter').eshb_field_sorter();
3465 $this.children('.csf-field-spinner').eshb_field_spinner();
3466 $this.children('.csf-field-switcher').eshb_field_switcher();
3467 $this.children('.csf-field-tabbed').eshb_field_tabbed();
3468 $this.children('.csf-field-typography').eshb_field_typography();
3469 $this.children('.csf-field-upload').eshb_field_upload();
3470 $this.children('.csf-field-wp_editor').eshb_field_wp_editor();
3471
3472 // Field colors
3473 $this.children('.csf-field-border').find('.csf-color').eshb_color();
3474 $this.children('.csf-field-background').find('.csf-color').eshb_color();
3475 $this.children('.csf-field-color').find('.csf-color').eshb_color();
3476 $this.children('.csf-field-color_group').find('.csf-color').eshb_color();
3477 $this.children('.csf-field-link_color').find('.csf-color').eshb_color();
3478 $this.children('.csf-field-typography').find('.csf-color').eshb_color();
3479
3480 // Field chosenjs
3481 $this.children('.csf-field-select').find('.eshb-chosen').eshb_chosen();
3482
3483 // Field Checkbox
3484 $this.children('.csf-field-checkbox').find('.csf-checkbox').eshb_checkbox();
3485 $this.children('.csf-field-checkbox').find('.csf-checkbox-all').eshb_checkbox_all();
3486
3487 // Field Siblings
3488 $this.children('.csf-field-button_set').find('.csf-siblings').eshb_siblings();
3489 $this.children('.csf-field-image_select').find('.csf-siblings').eshb_siblings();
3490 $this.children('.csf-field-palette').find('.csf-siblings').eshb_siblings();
3491
3492 // Help Tooptip
3493 $this.children('.csf-field').find('.csf-help').eshb_help();
3494
3495 if ( settings.dependency ) {
3496 $this.eshb_dependency();
3497 }
3498
3499 $this.data('inited', true);
3500
3501 $(document).trigger('csf-reload-script', $this);
3502
3503 }
3504
3505 });
3506 };
3507
3508 //
3509 // Document ready and run scripts
3510 //
3511 $(document).ready( function() {
3512
3513 $('.csf-save').eshb_save();
3514 $('.csf-options').eshb_options();
3515 $('.csf-sticky-header').eshb_sticky();
3516 $('.csf-nav-options').eshb_nav_options();
3517 $('.csf-nav-metabox').eshb_nav_metabox();
3518 $('.csf-taxonomy').eshb_taxonomy();
3519 $('.csf-page-templates').eshb_page_templates();
3520 $('.csf-post-formats').eshb_post_formats();
3521 $('.csf-shortcode').eshb_shortcode();
3522 $('.csf-search').eshb_search();
3523 $('.csf-confirm').eshb_confirm();
3524 $('.csf-expand-all').eshb_expand_all();
3525 $('.csf-onload').eshb_reload_script();
3526 $('#widgets-editor').eshb_widgets();
3527 $('#widgets-right').eshb_widgets();
3528 $('#menu-to-edit').eshb_nav_menu();
3529
3530 });
3531
3532 })( jQuery, window, document );
3533