PluginProbe
Easy Hotel – Powerful Hotel Booking / 2.0.2
Easy Hotel – Powerful Hotel Booking v2.0.2
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 2.0.2, at admin/includes/framework/assets/js/main.js

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