PluginProbe
StreamCast – bring live radio to your site with a sleek player / 2.2.3
StreamCast – bring live radio to your site with a sleek player v2.2.3
2.4.5 2.4.4 trunk 1.0 1.1 2.0.0 2.1.10 2.1.2 2.1.4 2.1.5 2.1.8 2.2.1 2.2.3 2.2.4 2.2.5 2.3.0 2.3.1 2.3.2 2.3.3 2.3.4 2.3.5 2.3.6 2.3.7 2.3.8 2.3.9 All 29 releases
streamcast / admin / codestar-framework / assets / js / main.js

main.js in StreamCast – bring live radio to your site with a sleek player 2.2.3, at admin/codestar-framework/assets/js/main.js

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