How to use csf method in storybook-root

Best JavaScript code snippet using storybook-root

csf.js

Source:csf.js Github

copy

Full Screen

1/**2 *3 * -----------------------------------------------------------4 *5 * Codestar Framework6 * A Simple and Lightweight WordPress Option Framework7 *8 * -----------------------------------------------------------9 *10 */11;(function( $, window, document, undefined ) {12 'use strict';13 //14 // Constants15 //16 var CSF = CSF || {};17 CSF.funcs = {};18 CSF.vars = {19 onloaded: false,20 $body: $('body'),21 $window: $(window),22 $document: $(document),23 is_rtl: $('body').hasClass('rtl'),24 code_themes: [],25 };26 //27 // Helper Functions28 //29 CSF.helper = {30 //31 // Generate UID32 //33 uid: function( prefix ) {34 return ( prefix || '' ) + Math.random().toString(36).substr(2, 9);35 },36 // Quote regular expression characters37 //38 preg_quote: function( str ) {39 return (str+'').replace(/(\[|\-|\])/g, "\\$1");40 },41 //42 // Reneme input names43 //44 name_nested_replace: function( $selector, field_id ) {45 var checks = [];46 var regex = new RegExp('('+ CSF.helper.preg_quote(field_id) +')\\[(\\d+)\\]', 'g');47 $selector.find(':radio').each(function() {48 if( this.checked || this.orginal_checked ) {49 this.orginal_checked = true;50 }51 });52 $selector.each( function( index ) {53 $(this).find(':input').each(function() {54 this.name = this.name.replace(regex, field_id +'['+ index +']');55 if( this.orginal_checked ) {56 this.checked = true;57 }58 });59 });60 },61 //62 // Debounce63 //64 debounce: function( callback, threshold, immediate ) {65 var timeout;66 return function() {67 var context = this, args = arguments;68 var later = function() {69 timeout = null;70 if( !immediate ) {71 callback.apply(context, args);72 }73 };74 var callNow = ( immediate && !timeout );75 clearTimeout( timeout );76 timeout = setTimeout( later, threshold );77 if( callNow ) {78 callback.apply(context, args);79 }80 };81 },82 //83 // Get a cookie84 //85 get_cookie: function( name ) {86 var e, b, cookie = document.cookie, p = name + '=';87 if( ! cookie ) {88 return;89 }90 b = cookie.indexOf( '; ' + p );91 if( b === -1 ) {92 b = cookie.indexOf(p);93 if( b !== 0 ) {94 return null;95 }96 } else {97 b += 2;98 }99 e = cookie.indexOf( ';', b );100 if( e === -1 ) {101 e = cookie.length;102 }103 return decodeURIComponent( cookie.substring( b + p.length, e ) );104 },105 //106 // Set a cookie107 //108 set_cookie: function( name, value, expires, path, domain, secure ) {109 var d = new Date();110 if( typeof( expires ) === 'object' && expires.toGMTString ) {111 expires = expires.toGMTString();112 } else if( parseInt( expires, 10 ) ) {113 d.setTime( d.getTime() + ( parseInt( expires, 10 ) * 1000 ) );114 expires = d.toGMTString();115 } else {116 expires = '';117 }118 document.cookie = name + '=' + encodeURIComponent( value ) +119 ( expires ? '; expires=' + expires : '' ) +120 ( path ? '; path=' + path : '' ) +121 ( domain ? '; domain=' + domain : '' ) +122 ( secure ? '; secure' : '' );123 },124 //125 // Remove a cookie126 //127 remove_cookie: function( name, path, domain, secure ) {128 CSF.helper.set_cookie( name, '', -1000, path, domain, secure );129 },130 };131 //132 // Custom clone for textarea and select clone() bug133 //134 $.fn.csf_clone = function() {135 var base = $.fn.clone.apply(this, arguments),136 clone = this.find('select').add(this.filter('select')),137 cloned = base.find('select').add(base.filter('select'));138 for( var i = 0; i < clone.length; ++i ) {139 for( var j = 0; j < clone[i].options.length; ++j ) {140 if( clone[i].options[j].selected === true ) {141 cloned[i].options[j].selected = true;142 }143 }144 }145 this.find(':radio').each( function() {146 this.orginal_checked = this.checked;147 });148 return base;149 };150 //151 // Expand All Options152 //153 $.fn.csf_expand_all = function() {154 return this.each( function() {155 $(this).on('click', function( e ) {156 e.preventDefault();157 $('.csf-wrapper').toggleClass('csf-show-all');158 $('.csf-section').csf_reload_script();159 $(this).find('.fa').toggleClass('fa-indent').toggleClass('fa-outdent');160 });161 });162 };163 //164 // Options Navigation165 //166 $.fn.csf_nav_options = function() {167 return this.each( function() {168 var $nav = $(this),169 $links = $nav.find('a'),170 $hidden = $nav.closest('.csf').find('.csf-section-id'),171 $last_section;172 $(window).on('hashchange', function() {173 var hash = window.location.hash.match(new RegExp('tab=([^&]*)'));174 var slug = hash ? hash[1] : $links.first().attr('href').replace('#tab=', '');175 var $link = $('#csf-tab-link-'+ slug);176 if( $link.length > 0 ) {177 $link.closest('.csf-tab-depth-0').addClass('csf-tab-active').siblings().removeClass('csf-tab-active');178 $links.removeClass('csf-section-active');179 $link.addClass('csf-section-active');180 if( $last_section !== undefined ) {181 $last_section.hide();182 }183 var $section = $('#csf-section-'+slug);184 $section.css({display: 'block'});185 $section.csf_reload_script();186 $hidden.val(slug);187 $last_section = $section;188 }189 }).trigger('hashchange');190 });191 };192 //193 // Metabox Tabs194 //195 $.fn.csf_nav_metabox = function() {196 return this.each( function() {197 var $nav = $(this),198 $links = $nav.find('a'),199 unique_id = $nav.data('unique'),200 post_id = $('#post_ID').val() || 'global',201 $last_section,202 $last_link;203 $links.on('click', function( e ) {204 e.preventDefault();205 var $link = $(this),206 section_id = $link.data('section');207 if( $last_link !== undefined ) {208 $last_link.removeClass('csf-section-active');209 }210 if( $last_section !== undefined ) {211 $last_section.hide();212 }213 $link.addClass('csf-section-active');214 var $section = $('#csf-section-'+section_id);215 $section.css({display: 'block'});216 $section.csf_reload_script();217 CSF.helper.set_cookie('csf-last-metabox-tab-'+ post_id +'-'+ unique_id, section_id);218 $last_section = $section;219 $last_link = $link;220 });221 var get_cookie = CSF.helper.get_cookie('csf-last-metabox-tab-'+ post_id +'-'+ unique_id);222 if( get_cookie ) {223 $nav.find('a[data-section="'+ get_cookie +'"]').trigger('click');224 } else {225 $links.first('a').trigger('click');226 }227 });228 };229 //230 // Metabox Page Templates Listener231 //232 $.fn.csf_page_templates = function() {233 if( this.length ) {234 $(document).on('change', '.editor-page-attributes__template select, #page_template', function() {235 var maybe_value = $(this).val() || 'default';236 $('.csf-page-templates').removeClass('csf-show').addClass('csf-hide');237 $('.csf-page-'+maybe_value.toLowerCase().replace(/[^a-zA-Z0-9]+/g,'-')).removeClass('csf-hide').addClass('csf-show');238 });239 }240 };241 //242 // Metabox Post Formats Listener243 //244 $.fn.csf_post_formats = function() {245 if( this.length ) {246 $(document).on('change', '.editor-post-format select, #formatdiv input[name="post_format"]', function() {247 var maybe_value = $(this).val() || 'default';248 // Fallback for classic editor version249 maybe_value = ( maybe_value === '0' ) ? 'default' : maybe_value;250 $('.csf-post-formats').removeClass('csf-show').addClass('csf-hide');251 $('.csf-post-format-'+maybe_value).removeClass('csf-hide').addClass('csf-show');252 });253 }254 };255 //256 // Search257 //258 $.fn.csf_search = function() {259 return this.each( function() {260 var $this = $(this),261 $input = $this.find('input');262 $input.on('change keyup', function() {263 var value = $(this).val(),264 $wrapper = $('.csf-wrapper'),265 $section = $wrapper.find('.csf-section'),266 $fields = $section.find('> .csf-field:not(.hidden)'),267 $titles = $fields.find('> .csf-title, .csf-search-tags');268 if( value.length > 3 ) {269 $fields.addClass('csf-hidden');270 $wrapper.addClass('csf-search-all');271 $titles.each( function() {272 var $title = $(this);273 if( $title.text().match( new RegExp('.*?' + value + '.*?', 'i') ) ) {274 var $field = $title.closest('.csf-field');275 $field.removeClass('csf-hidden');276 $field.parent().csf_reload_script();277 }278 });279 } else {280 $fields.removeClass('csf-hidden');281 $wrapper.removeClass('csf-search-all');282 }283 });284 });285 };286 //287 // Sticky Header288 //289 $.fn.csf_sticky = function() {290 return this.each( function() {291 var $this = $(this),292 $window = $(window),293 $inner = $this.find('.csf-header-inner'),294 padding = parseInt( $inner.css('padding-left') ) + parseInt( $inner.css('padding-right') ),295 offset = 32,296 scrollTop = 0,297 lastTop = 0,298 ticking = false,299 stickyUpdate = function() {300 var offsetTop = $this.offset().top,301 stickyTop = Math.max(offset, offsetTop - scrollTop ),302 winWidth = Math.max(document.documentElement.clientWidth, window.innerWidth || 0);303 if( stickyTop <= offset && winWidth > 782 ) {304 $inner.css({width: $this.outerWidth()-padding});305 $this.css({height: $this.outerHeight()}).addClass( 'csf-sticky' );306 } else {307 $inner.removeAttr('style');308 $this.removeAttr('style').removeClass( 'csf-sticky' );309 }310 },311 requestTick = function() {312 if( !ticking ) {313 requestAnimationFrame( function() {314 stickyUpdate();315 ticking = false;316 });317 }318 ticking = true;319 },320 onSticky = function() {321 scrollTop = $window.scrollTop();322 requestTick();323 };324 $window.on( 'scroll resize', onSticky);325 onSticky();326 });327 };328 //329 // Dependency System330 //331 $.fn.csf_dependency = function() {332 return this.each( function() {333 var $this = $(this),334 ruleset = $.csf_deps.createRuleset(),335 depends = [],336 is_global = false;337 $this.children('[data-controller]').each( function() {338 var $field = $(this),339 controllers = $field.data('controller').split('|'),340 conditions = $field.data('condition').split('|'),341 values = $field.data('value').toString().split('|'),342 rules = ruleset;343 if( $field.data('depend-global') ) {344 is_global = true;345 }346 $.each(controllers, function( index, depend_id ) {347 var value = values[index] || '',348 condition = conditions[index] || conditions[0];349 rules = rules.createRule('[data-depend-id="'+ depend_id +'"]', condition, value);350 rules.include($field);351 depends.push(depend_id);352 });353 });354 if( depends.length ) {355 if( is_global ) {356 $.csf_deps.enable(CSF.vars.$body, ruleset, depends);357 } else {358 $.csf_deps.enable($this, ruleset, depends);359 }360 }361 });362 };363 //364 // Field: accordion365 //366 $.fn.csf_field_accordion = function() {367 return this.each( function() {368 var $titles = $(this).find('.csf-accordion-title');369 $titles.on('click', function() {370 var $title = $(this),371 $icon = $title.find('.csf-accordion-icon'),372 $content = $title.next();373 if( $icon.hasClass('fa-angle-right') ) {374 $icon.removeClass('fa-angle-right').addClass('fa-angle-down');375 } else {376 $icon.removeClass('fa-angle-down').addClass('fa-angle-right');377 }378 if( !$content.data( 'opened' ) ) {379 $content.csf_reload_script();380 $content.data( 'opened', true );381 }382 $content.toggleClass('csf-accordion-open');383 });384 });385 };386 //387 // Field: backup388 //389 $.fn.csf_field_backup = function() {390 return this.each( function() {391 if( window.wp.customize === undefined ) { return; }392 var base = this,393 $this = $(this),394 $body = $('body'),395 $import = $this.find('.csf-import'),396 $reset = $this.find('.csf-reset');397 base.notification = function( message_text ) {398 if( wp.customize.notifications && wp.customize.OverlayNotification ) {399 // clear if there is any saved data.400 if( !wp.customize.state('saved').get() ) {401 wp.customize.state('changesetStatus').set('trash');402 wp.customize.each( function( setting ) { setting._dirty = false; });403 wp.customize.state('saved').set(true);404 }405 // then show a notification overlay406 wp.customize.notifications.add( new wp.customize.OverlayNotification('csf_field_backup_notification', {407 type: 'info',408 message: message_text,409 loading: true410 }));411 }412 };413 $reset.on('click', function( e ) {414 e.preventDefault();415 if( CSF.vars.is_confirm ) {416 base.notification( window.csf_vars.i18n.reset_notification );417 window.wp.ajax.post('csf-reset', {418 unique: $reset.data('unique'),419 nonce: $reset.data('nonce')420 })421 .done( function( response ) {422 window.location.reload(true);423 })424 .fail( function( response ) {425 alert( response.error );426 wp.customize.notifications.remove('csf_field_backup_notification');427 });428 }429 });430 $import.on('click', function( e ) {431 e.preventDefault();432 if( CSF.vars.is_confirm ) {433 base.notification( window.csf_vars.i18n.import_notification );434 window.wp.ajax.post( 'csf-import', {435 unique: $import.data('unique'),436 nonce: $import.data('nonce'),437 import_data: $this.find('.csf-import-data').val()438 }).done( function( response ) {439 window.location.reload(true);440 }).fail( function( response ) {441 alert( response.error );442 wp.customize.notifications.remove('csf_field_backup_notification');443 });444 }445 });446 });447 };448 //449 // Field: background450 //451 $.fn.csf_field_background = function() {452 return this.each( function() {453 $(this).find('.csf--media').csf_reload_script();454 });455 };456 //457 // Field: code_editor458 //459 $.fn.csf_field_code_editor = function() {460 return this.each( function() {461 if( typeof CodeMirror !== 'function' ) { return; }462 var $this = $(this),463 $textarea = $this.find('textarea'),464 $inited = $this.find('.CodeMirror'),465 data_editor = $textarea.data('editor');466 if( $inited.length ) {467 $inited.remove();468 }469 var interval = setInterval(function () {470 if( $this.is(':visible') ) {471 var code_editor = CodeMirror.fromTextArea( $textarea[0], data_editor );472 // load code-mirror theme css.473 if( data_editor.theme !== 'default' && CSF.vars.code_themes.indexOf(data_editor.theme) === -1 ) {474 var $cssLink = $('<link>');475 $('#csf-codemirror-css').after( $cssLink );476 $cssLink.attr({477 rel: 'stylesheet',478 id: 'csf-codemirror-'+ data_editor.theme +'-css',479 href: data_editor.cdnURL +'/theme/'+ data_editor.theme +'.min.css',480 type: 'text/css',481 media: 'all'482 });483 CSF.vars.code_themes.push(data_editor.theme);484 }485 CodeMirror.modeURL = data_editor.cdnURL +'/mode/%N/%N.min.js';486 CodeMirror.autoLoadMode(code_editor, data_editor.mode);487 code_editor.on( 'change', function( editor, event ) {488 $textarea.val( code_editor.getValue() ).trigger('change');489 });490 clearInterval(interval);491 }492 });493 });494 };495 //496 // Field: date497 //498 $.fn.csf_field_date = function() {499 return this.each( function() {500 var $this = $(this),501 $inputs = $this.find('input'),502 settings = $this.find('.csf-date-settings').data('settings'),503 wrapper = '<div class="csf-datepicker-wrapper"></div>',504 $datepicker;505 var defaults = {506 showAnim: '',507 beforeShow: function(input, inst) {508 $(inst.dpDiv).addClass('csf-datepicker-wrapper');509 },510 onClose: function( input, inst ) {511 $(inst.dpDiv).removeClass('csf-datepicker-wrapper');512 },513 };514 settings = $.extend({}, settings, defaults);515 if( $inputs.length === 2 ) {516 settings = $.extend({}, settings, {517 onSelect: function( selectedDate ) {518 var $this = $(this),519 $from = $inputs.first(),520 option = ( $inputs.first().attr('id') === $(this).attr('id') ) ? 'minDate' : 'maxDate',521 date = $.datepicker.parseDate( settings.dateFormat, selectedDate );522 $inputs.not(this).datepicker('option', option, date );523 }524 });525 }526 $inputs.each( function(){527 var $input = $(this);528 if( $input.hasClass('hasDatepicker') ) {529 $input.removeAttr('id').removeClass('hasDatepicker');530 }531 $input.datepicker(settings);532 });533 });534 };535 //536 // Field: fieldset537 //538 $.fn.csf_field_fieldset = function() {539 return this.each( function() {540 $(this).find('.csf-fieldset-content').csf_reload_script();541 });542 };543 //544 // Field: gallery545 //546 $.fn.csf_field_gallery = function() {547 return this.each( function() {548 var $this = $(this),549 $edit = $this.find('.csf-edit-gallery'),550 $clear = $this.find('.csf-clear-gallery'),551 $list = $this.find('ul'),552 $input = $this.find('input'),553 $img = $this.find('img'),554 wp_media_frame;555 $this.on('click', '.csf-button, .csf-edit-gallery', function( e ) {556 var $el = $(this),557 ids = $input.val(),558 what = ( $el.hasClass('csf-edit-gallery') ) ? 'edit' : 'add',559 state = ( what === 'add' && !ids.length ) ? 'gallery' : 'gallery-edit';560 e.preventDefault();561 if( typeof window.wp === 'undefined' || ! window.wp.media || ! window.wp.media.gallery ) { return; }562 // Open media with state563 if( state === 'gallery' ) {564 wp_media_frame = window.wp.media({565 library: {566 type: 'image'567 },568 frame: 'post',569 state: 'gallery',570 multiple: true571 });572 wp_media_frame.open();573 } else {574 wp_media_frame = window.wp.media.gallery.edit( '[gallery ids="'+ ids +'"]' );575 if( what === 'add' ) {576 wp_media_frame.setState('gallery-library');577 }578 }579 // Media Update580 wp_media_frame.on( 'update', function( selection ) {581 $list.empty();582 var selectedIds = selection.models.map( function( attachment ) {583 var item = attachment.toJSON();584 var thumb = ( typeof item.sizes.thumbnail !== 'undefined' ) ? item.sizes.thumbnail.url : item.url;585 $list.append('<li><img src="'+ thumb +'"></li>');586 return item.id;587 });588 $input.val( selectedIds.join( ',' ) ).trigger('change');589 $clear.removeClass('hidden');590 $edit.removeClass('hidden');591 });592 });593 $clear.on('click', function( e ) {594 e.preventDefault();595 $list.empty();596 $input.val('').trigger('change');597 $clear.addClass('hidden');598 $edit.addClass('hidden');599 });600 });601 };602 //603 // Field: group604 //605 $.fn.csf_field_group = function() {606 return this.each( function() {607 var $this = $(this),608 $fieldset = $this.children('.csf-fieldset'),609 $group = $fieldset.length ? $fieldset : $this,610 $wrapper = $group.children('.csf-cloneable-wrapper'),611 $hidden = $group.children('.csf-cloneable-hidden'),612 $max = $group.children('.csf-cloneable-max'),613 $min = $group.children('.csf-cloneable-min'),614 field_id = $wrapper.data('field-id'),615 unique_id = $wrapper.data('unique-id'),616 is_number = Boolean( Number( $wrapper.data('title-number') ) ),617 max = parseInt( $wrapper.data('max') ),618 min = parseInt( $wrapper.data('min') );619 // clear accordion arrows if multi-instance620 if( $wrapper.hasClass('ui-accordion') ) {621 $wrapper.find('.ui-accordion-header-icon').remove();622 }623 var update_title_numbers = function( $selector ) {624 $selector.find('.csf-cloneable-title-number').each( function( index ) {625 $(this).html( ( $(this).closest('.csf-cloneable-item').index()+1 ) + '.' );626 });627 };628 $wrapper.accordion({629 header: '> .csf-cloneable-item > .csf-cloneable-title',630 collapsible : true,631 active: false,632 animate: false,633 heightStyle: 'content',634 icons: {635 'header': 'csf-cloneable-header-icon fa fa-angle-right',636 'activeHeader': 'csf-cloneable-header-icon fa fa-angle-down'637 },638 activate: function( event, ui ) {639 var $panel = ui.newPanel;640 var $header = ui.newHeader;641 if( $panel.length && !$panel.data( 'opened' ) ) {642 var $fields = $panel.children();643 var $first = $fields.first().find(':input').first();644 var $title = $header.find('.csf-cloneable-value');645 $first.on('keyup', function( event ) {646 $title.text($first.val());647 });648 $panel.csf_reload_script();649 $panel.data( 'opened', true );650 $panel.data( 'retry', false );651 } else if( $panel.data( 'retry' ) ) {652 $panel.csf_reload_script_retry();653 $panel.data( 'retry', false );654 }655 }656 });657 $wrapper.sortable({658 axis: 'y',659 handle: '.csf-cloneable-title,.csf-cloneable-sort',660 helper: 'original',661 cursor: 'move',662 placeholder: 'widget-placeholder',663 start: function( event, ui ) {664 $wrapper.accordion({ active:false });665 $wrapper.sortable('refreshPositions');666 ui.item.children('.csf-cloneable-content').data('retry', true);667 },668 update: function( event, ui ) {669 CSF.helper.name_nested_replace( $wrapper.children('.csf-cloneable-item'), field_id );670 $wrapper.csf_customizer_refresh();671 if( is_number ) {672 update_title_numbers($wrapper);673 }674 },675 });676 $group.children('.csf-cloneable-add').on('click', function( e ) {677 e.preventDefault();678 var count = $wrapper.children('.csf-cloneable-item').length;679 $min.hide();680 if( max && (count+1) > max ) {681 $max.show();682 return;683 }684 var new_field_id = unique_id + field_id + '['+ count +']';685 var $cloned_item = $hidden.csf_clone(true);686 $cloned_item.removeClass('csf-cloneable-hidden');687 $cloned_item.find(':input').each( function() {688 this.name = new_field_id + this.name.replace( ( this.name.startsWith('_nonce') ? '_nonce' : unique_id ), '');689 });690 $cloned_item.find('.csf-data-wrapper').each( function(){691 $(this).attr('data-unique-id', new_field_id );692 });693 $wrapper.append($cloned_item);694 $wrapper.accordion('refresh');695 $wrapper.accordion({active: count});696 $wrapper.csf_customizer_refresh();697 $wrapper.csf_customizer_listen({closest: true});698 if( is_number ) {699 update_title_numbers($wrapper);700 }701 });702 var event_clone = function( e ) {703 e.preventDefault();704 var count = $wrapper.children('.csf-cloneable-item').length;705 $min.hide();706 if( max && (count+1) > max ) {707 $max.show();708 return;709 }710 var $this = $(this),711 $parent = $this.parent().parent(),712 $cloned_helper = $parent.children('.csf-cloneable-helper').csf_clone(true),713 $cloned_title = $parent.children('.csf-cloneable-title').csf_clone(),714 $cloned_content = $parent.children('.csf-cloneable-content').csf_clone(),715 cloned_regex = new RegExp('('+ CSF.helper.preg_quote(field_id) +')\\[(\\d+)\\]', 'g');716 $cloned_content.find('.csf-data-wrapper').each( function(){717 var $this = $(this);718 $this.attr('data-unique-id', $this.attr('data-unique-id').replace(cloned_regex, field_id +'['+ ($parent.index()+1) +']') );719 });720 var $cloned = $('<div class="csf-cloneable-item" />');721 $cloned.append($cloned_helper);722 $cloned.append($cloned_title);723 $cloned.append($cloned_content);724 $wrapper.children().eq($parent.index()).after($cloned);725 CSF.helper.name_nested_replace( $wrapper.children('.csf-cloneable-item'), field_id );726 $wrapper.accordion('refresh');727 $wrapper.csf_customizer_refresh();728 $wrapper.csf_customizer_listen({closest: true});729 if( is_number ) {730 update_title_numbers($wrapper);731 }732 };733 $wrapper.children('.csf-cloneable-item').children('.csf-cloneable-helper').on('click', '.csf-cloneable-clone', event_clone);734 $group.children('.csf-cloneable-hidden').children('.csf-cloneable-helper').on('click', '.csf-cloneable-clone', event_clone);735 var event_remove = function( e ) {736 e.preventDefault();737 var count = $wrapper.children('.csf-cloneable-item').length;738 $max.hide();739 $min.hide();740 if( min && (count-1) < min ) {741 $min.show();742 return;743 }744 $(this).closest('.csf-cloneable-item').remove();745 CSF.helper.name_nested_replace( $wrapper.children('.csf-cloneable-item'), field_id );746 $wrapper.csf_customizer_refresh();747 if( is_number ) {748 update_title_numbers($wrapper);749 }750 };751 $wrapper.children('.csf-cloneable-item').children('.csf-cloneable-helper').on('click', '.csf-cloneable-remove', event_remove);752 $group.children('.csf-cloneable-hidden').children('.csf-cloneable-helper').on('click', '.csf-cloneable-remove', event_remove);753 });754 };755 //756 // Field: icon757 //758 $.fn.csf_field_icon = function() {759 return this.each( function() {760 var $this = $(this);761 $this.on('click', '.csf-icon-add', function( e ) {762 e.preventDefault();763 var $button = $(this);764 var $modal = $('#csf-modal-icon');765 $modal.show();766 CSF.vars.$icon_target = $this;767 if( !CSF.vars.icon_modal_loaded ) {768 $modal.find('.csf-modal-loading').show();769 window.wp.ajax.post( 'csf-get-icons', { nonce: $button.data('nonce') } ).done( function( response ) {770 $modal.find('.csf-modal-loading').hide();771 CSF.vars.icon_modal_loaded = true;772 var $load = $modal.find('.csf-modal-load').html( response.content );773 $load.on('click', 'a', function( e ) {774 e.preventDefault();775 var icon = $(this).data('csf-icon');776 CSF.vars.$icon_target.find('i').removeAttr('class').addClass(icon);777 CSF.vars.$icon_target.find('input').val(icon).trigger('change');778 CSF.vars.$icon_target.find('.csf-icon-preview').removeClass('hidden');779 CSF.vars.$icon_target.find('.csf-icon-remove').removeClass('hidden');780 $modal.hide();781 });782 $modal.on('change keyup', '.csf-icon-search', function() {783 var value = $(this).val(),784 $icons = $load.find('a');785 $icons.each( function() {786 var $elem = $(this);787 if( $elem.data('csf-icon').search( new RegExp( value, 'i' ) ) < 0 ) {788 $elem.hide();789 } else {790 $elem.show();791 }792 });793 });794 $modal.on('click', '.csf-modal-close, .csf-modal-overlay', function() {795 $modal.hide();796 });797 });798 }799 });800 $this.on('click', '.csf-icon-remove', function( e ) {801 e.preventDefault();802 $this.find('.csf-icon-preview').addClass('hidden');803 $this.find('input').val('').trigger('change');804 $(this).addClass('hidden');805 });806 });807 };808 //809 // Field: media810 //811 $.fn.csf_field_media = function() {812 return this.each( function() {813 var $this = $(this),814 $upload_button = $this.find('.csf--button'),815 $remove_button = $this.find('.csf--remove'),816 $library = $upload_button.data('library') && $upload_button.data('library').split(',') || '',817 wp_media_frame;818 $upload_button.on('click', function( e ) {819 e.preventDefault();820 if( typeof window.wp === 'undefined' || ! window.wp.media || ! window.wp.media.gallery ) {821 return;822 }823 if( wp_media_frame ) {824 wp_media_frame.open();825 return;826 }827 wp_media_frame = window.wp.media({828 library: {829 type: $library830 }831 });832 wp_media_frame.on( 'select', function() {833 var thumbnail;834 var attributes = wp_media_frame.state().get('selection').first().attributes;835 var preview_size = $upload_button.data('preview-size') || 'thumbnail';836 $this.find('.csf--url').val( attributes.url );837 $this.find('.csf--id').val( attributes.id );838 $this.find('.csf--width').val( attributes.width );839 $this.find('.csf--height').val( attributes.height );840 $this.find('.csf--alt').val( attributes.alt );841 $this.find('.csf--title').val( attributes.title );842 $this.find('.csf--description').val( attributes.description );843 if( typeof attributes.sizes !== 'undefined' && typeof attributes.sizes.thumbnail !== 'undefined' && preview_size === 'thumbnail' ) {844 thumbnail = attributes.sizes.thumbnail.url;845 } else if( typeof attributes.sizes !== 'undefined' && typeof attributes.sizes.full !== 'undefined' ) {846 thumbnail = attributes.sizes.full.url;847 } else {848 thumbnail = attributes.icon;849 }850 $remove_button.removeClass('hidden');851 $this.find('.csf--preview').removeClass('hidden');852 $this.find('.csf--src').attr('src', thumbnail);853 $this.find('.csf--thumbnail').val( thumbnail ).trigger('change');854 });855 wp_media_frame.open();856 });857 $remove_button.on('click', function( e ) {858 e.preventDefault();859 $remove_button.addClass('hidden');860 $this.find('.csf--preview').addClass('hidden');861 $this.find('input').val('');862 $this.find('.csf--thumbnail').trigger('change');863 });864 });865 };866 //867 // Field: repeater868 //869 $.fn.csf_field_repeater = function() {870 return this.each( function() {871 var $this = $(this),872 $fieldset = $this.children('.csf-fieldset'),873 $repeater = $fieldset.length ? $fieldset : $this,874 $wrapper = $repeater.children('.csf-repeater-wrapper'),875 $hidden = $repeater.children('.csf-repeater-hidden'),876 $max = $repeater.children('.csf-repeater-max'),877 $min = $repeater.children('.csf-repeater-min'),878 field_id = $wrapper.data('field-id'),879 unique_id = $wrapper.data('unique-id'),880 max = parseInt( $wrapper.data('max') ),881 min = parseInt( $wrapper.data('min') );882 $wrapper.children('.csf-repeater-item').children('.csf-repeater-content').csf_reload_script();883 $wrapper.sortable({884 axis: 'y',885 handle: '.csf-repeater-sort',886 helper: 'original',887 cursor: 'move',888 placeholder: 'widget-placeholder',889 update: function( event, ui ) {890 CSF.helper.name_nested_replace( $wrapper.children('.csf-repeater-item'), field_id );891 $wrapper.csf_customizer_refresh();892 ui.item.csf_reload_script_retry();893 }894 });895 $repeater.children('.csf-repeater-add').on('click', function( e ) {896 e.preventDefault();897 var count = $wrapper.children('.csf-repeater-item').length;898 $min.hide();899 if( max && (count+1) > max ) {900 $max.show();901 return;902 }903 var new_field_id = unique_id + field_id + '['+ count +']';904 var $cloned_item = $hidden.csf_clone(true);905 $cloned_item.removeClass('csf-repeater-hidden');906 $cloned_item.find(':input').each( function() {907 this.name = new_field_id + this.name.replace( ( this.name.startsWith('_nonce') ? '_nonce' : unique_id ), '');908 });909 $cloned_item.find('.csf-data-wrapper').each( function(){910 $(this).attr('data-unique-id', new_field_id );911 });912 $wrapper.append($cloned_item);913 $cloned_item.children('.csf-repeater-content').csf_reload_script();914 $wrapper.csf_customizer_refresh();915 $wrapper.csf_customizer_listen({closest: true});916 });917 var event_clone = function( e ) {918 e.preventDefault();919 var count = $wrapper.children('.csf-repeater-item').length;920 $min.hide();921 if( max && (count+1) > max ) {922 $max.show();923 return;924 }925 var $this = $(this),926 $parent = $this.parent().parent().parent(),927 $cloned_content = $parent.children('.csf-repeater-content').csf_clone(),928 $cloned_helper = $parent.children('.csf-repeater-helper').csf_clone(true),929 cloned_regex = new RegExp('('+ CSF.helper.preg_quote(field_id) +')\\[(\\d+)\\]', 'g');930 $cloned_content.find('.csf-data-wrapper').each( function(){931 var $this = $(this);932 $this.attr('data-unique-id', $this.attr('data-unique-id').replace(cloned_regex, field_id +'['+ ($parent.index()+1) +']') );933 });934 var $cloned = $('<div class="csf-repeater-item" />');935 $cloned.append($cloned_content);936 $cloned.append($cloned_helper);937 $wrapper.children().eq($parent.index()).after($cloned);938 $cloned.children('.csf-repeater-content').csf_reload_script();939 CSF.helper.name_nested_replace( $wrapper.children('.csf-repeater-item'), field_id );940 $wrapper.csf_customizer_refresh();941 $wrapper.csf_customizer_listen({closest: true});942 };943 $wrapper.children('.csf-repeater-item').children('.csf-repeater-helper').on('click', '.csf-repeater-clone', event_clone);944 $repeater.children('.csf-repeater-hidden').children('.csf-repeater-helper').on('click', '.csf-repeater-clone', event_clone);945 var event_remove = function( e ) {946 e.preventDefault();947 var count = $wrapper.children('.csf-repeater-item').length;948 $max.hide();949 $min.hide();950 if( min && (count-1) < min ) {951 $min.show();952 return;953 }954 $(this).closest('.csf-repeater-item').remove();955 CSF.helper.name_nested_replace( $wrapper.children('.csf-repeater-item'), field_id );956 $wrapper.csf_customizer_refresh();957 };958 $wrapper.children('.csf-repeater-item').children('.csf-repeater-helper').on('click', '.csf-repeater-remove', event_remove);959 $repeater.children('.csf-repeater-hidden').children('.csf-repeater-helper').on('click', '.csf-repeater-remove', event_remove);960 });961 };962 //963 // Field: slider964 //965 $.fn.csf_field_slider = function() {966 return this.each( function() {967 var $this = $(this),968 $input = $this.find('input'),969 $slider = $this.find('.csf-slider-ui'),970 data = $input.data(),971 value = $input.val() || 0;972 if( $slider.hasClass('ui-slider') ) {973 $slider.empty();974 }975 $slider.slider({976 range: 'min',977 value: value,978 min: data.min,979 max: data.max,980 step: data.step,981 slide: function( e, o ) {982 $input.val( o.value ).trigger('change');983 }984 });985 $input.keyup( function() {986 $slider.slider('value', $input.val());987 });988 });989 };990 //991 // Field: sortable992 //993 $.fn.csf_field_sortable = function() {994 return this.each( function() {995 var $sortable = $(this).find('.csf--sortable');996 $sortable.sortable({997 axis: 'y',998 helper: 'original',999 cursor: 'move',1000 placeholder: 'widget-placeholder',1001 update: function( event, ui ) {1002 $sortable.csf_customizer_refresh();1003 }1004 });1005 $sortable.find('.csf--sortable-content').csf_reload_script();1006 });1007 };1008 //1009 // Field: sorter1010 //1011 $.fn.csf_field_sorter = function() {1012 return this.each( function() {1013 var $this = $(this),1014 $enabled = $this.find('.csf-enabled'),1015 $has_disabled = $this.find('.csf-disabled'),1016 $disabled = ( $has_disabled.length ) ? $has_disabled : false;1017 $enabled.sortable({1018 connectWith: $disabled,1019 placeholder: 'ui-sortable-placeholder',1020 update: function( event, ui ) {1021 var $el = ui.item.find('input');1022 if( ui.item.parent().hasClass('csf-enabled') ) {1023 $el.attr('name', $el.attr('name').replace('disabled', 'enabled'));1024 } else {1025 $el.attr('name', $el.attr('name').replace('enabled', 'disabled'));1026 }1027 $this.csf_customizer_refresh();1028 }1029 });1030 if( $disabled ) {1031 $disabled.sortable({1032 connectWith: $enabled,1033 placeholder: 'ui-sortable-placeholder',1034 update: function( event, ui ) {1035 $this.csf_customizer_refresh();1036 }1037 });1038 }1039 });1040 };1041 //1042 // Field: spinner1043 //1044 $.fn.csf_field_spinner = function() {1045 return this.each( function() {1046 var $this = $(this),1047 $input = $this.find('input'),1048 $inited = $this.find('.ui-spinner-button');1049 if( $inited.length ) {1050 $inited.remove();1051 }1052 $input.spinner({1053 max: $input.data('max') || 100,1054 min: $input.data('min') || 0,1055 step: $input.data('step') || 1,1056 spin: function (event, ui ) {1057 $input.val(ui.value).trigger('change');1058 }1059 });1060 });1061 };1062 //1063 // Field: switcher1064 //1065 $.fn.csf_field_switcher = function() {1066 return this.each( function() {1067 var $switcher = $(this).find('.csf--switcher');1068 $switcher.on('click', function() {1069 var value = 0;1070 var $input = $switcher.find('input');1071 if( $switcher.hasClass('csf--active') ) {1072 $switcher.removeClass('csf--active');1073 } else {1074 value = 1;1075 $switcher.addClass('csf--active');1076 }1077 $input.val(value).trigger('change');1078 });1079 });1080 };1081 //1082 // Field: tabbed1083 //1084 $.fn.csf_field_tabbed = function() {1085 return this.each( function() {1086 var $this = $(this),1087 $links = $this.find('.csf-tabbed-nav a'),1088 $sections = $this.find('.csf-tabbed-section');1089 $sections.eq(0).csf_reload_script();1090 $links.on( 'click', function( e ) {1091 e.preventDefault();1092 var $link = $(this),1093 index = $link.index(),1094 $section = $sections.eq(index);1095 $link.addClass('csf-tabbed-active').siblings().removeClass('csf-tabbed-active');1096 $section.csf_reload_script();1097 $section.removeClass('hidden').siblings().addClass('hidden');1098 });1099 });1100 };1101 //1102 // Field: typography1103 //1104 $.fn.csf_field_typography = function() {1105 return this.each(function () {1106 var base = this;1107 var $this = $(this);1108 var loaded_fonts = [];1109 var webfonts = csf_typography_json.webfonts;1110 var googlestyles = csf_typography_json.googlestyles;1111 var defaultstyles = csf_typography_json.defaultstyles;1112 //1113 //1114 // Sanitize google font subset1115 base.sanitize_subset = function( subset ) {1116 subset = subset.replace('-ext', ' Extended');1117 subset = subset.charAt(0).toUpperCase() + subset.slice(1);1118 return subset;1119 };1120 //1121 //1122 // Sanitize google font styles (weight and style)1123 base.sanitize_style = function( style ) {1124 return googlestyles[style] ? googlestyles[style] : style;1125 };1126 //1127 //1128 // Load google font1129 base.load_google_font = function( font_family, weight, style ) {1130 if( font_family && typeof WebFont === 'object' ) {1131 weight = weight ? weight.replace('normal', '') : '';1132 style = style ? style.replace('normal', '') : '';1133 if( weight || style ) {1134 font_family = font_family +':'+ weight + style;1135 }1136 if( loaded_fonts.indexOf( font_family ) === -1 ) {1137 WebFont.load({ google: { families: [font_family] } });1138 }1139 loaded_fonts.push( font_family );1140 }1141 };1142 //1143 //1144 // Append select options1145 base.append_select_options = function( $select, options, condition, type, is_multi ) {1146 $select.find('option').not(':first').remove();1147 var opts = '';1148 $.each( options, function( key, value ) {1149 var selected;1150 var name = value;1151 // is_multi1152 if( is_multi ) {1153 selected = ( condition && condition.indexOf(value) !== -1 ) ? ' selected' : '';1154 } else {1155 selected = ( condition && condition === value ) ? ' selected' : '';1156 }1157 if( type === 'subset' ) {1158 name = base.sanitize_subset( value );1159 } else if( type === 'style' ){1160 name = base.sanitize_style( value );1161 }1162 opts += '<option value="'+ value +'"'+ selected +'>'+ name +'</option>';1163 });1164 $select.append(opts).trigger('csf.change').trigger('chosen:updated');1165 };1166 base.init = function () {1167 //1168 //1169 // Constants1170 var selected_styles = [];1171 var $typography = $this.find('.csf--typography');1172 var $type = $this.find('.csf--type');1173 var unit = $typography.data('unit');1174 var exclude_fonts = $typography.data('exclude') ? $typography.data('exclude').split(',') : [];1175 //1176 //1177 // Chosen init1178 if( $this.find('.csf--chosen').length ) {1179 var $chosen_selects = $this.find('select');1180 $chosen_selects.each( function(){1181 var $chosen_select = $(this),1182 $chosen_inited = $chosen_select.parent().find('.chosen-container');1183 if( $chosen_inited.length ) {1184 $chosen_inited.remove();1185 }1186 $chosen_select.chosen({1187 allow_single_deselect: true,1188 disable_search_threshold: 15,1189 width: '100%'1190 });1191 });1192 }1193 //1194 //1195 // Font family select1196 var $font_family_select = $this.find('.csf--font-family');1197 var first_font_family = $font_family_select.val();1198 // Clear default font family select options1199 $font_family_select.find('option').not(':first-child').remove();1200 var opts = '';1201 $.each(webfonts, function( type, group ) {1202 // Check for exclude fonts1203 if( exclude_fonts && exclude_fonts.indexOf(type) !== -1 ) { return; }1204 opts += '<optgroup label="' + group.label + '">';1205 $.each(group.fonts, function( key, value ) {1206 // use key if value is object1207 value = ( typeof value === 'object' ) ? key : value;1208 var selected = ( value === first_font_family ) ? ' selected' : '';1209 opts += '<option value="'+ value +'" data-type="'+ type +'"'+ selected +'>'+ value +'</option>';1210 });1211 opts += '</optgroup>';1212 });1213 // Append google font select options1214 $font_family_select.append(opts).trigger('chosen:updated');1215 //1216 //1217 // Font style select1218 var $font_style_block = $this.find('.csf--block-font-style');1219 if( $font_style_block.length ) {1220 var $font_style_select = $this.find('.csf--font-style-select');1221 var first_style_value = $font_style_select.val() ? $font_style_select.val().replace(/normal/g, '' ) : '';1222 //1223 // Font Style on on change listener1224 $font_style_select.on('change csf.change', function( event ) {1225 var style_value = $font_style_select.val();1226 // set a default value1227 if( !style_value && selected_styles && selected_styles.indexOf('normal') === -1 ) {1228 style_value = selected_styles[0];1229 }1230 // set font weight, for eg. replacing 800italic to 8001231 var font_normal = ( style_value && style_value !== 'italic' && style_value === 'normal' ) ? 'normal' : '';1232 var font_weight = ( style_value && style_value !== 'italic' && style_value !== 'normal' ) ? style_value.replace('italic', '') : font_normal;1233 var font_style = ( style_value && style_value.substr(-6) === 'italic' ) ? 'italic' : '';1234 $this.find('.csf--font-weight').val( font_weight );1235 $this.find('.csf--font-style').val( font_style );1236 });1237 //1238 //1239 // Extra font style select1240 var $extra_font_style_block = $this.find('.csf--block-extra-styles');1241 if( $extra_font_style_block.length ) {1242 var $extra_font_style_select = $this.find('.csf--extra-styles');1243 var first_extra_style_value = $extra_font_style_select.val();1244 }1245 }1246 //1247 //1248 // Subsets select1249 var $subset_block = $this.find('.csf--block-subset');1250 if( $subset_block.length ) {1251 var $subset_select = $this.find('.csf--subset');1252 var first_subset_select_value = $subset_select.val();1253 var subset_multi_select = $subset_select.data('multiple') || false;1254 }1255 //1256 //1257 // Backup font family1258 var $backup_font_family_block = $this.find('.csf--block-backup-font-family');1259 //1260 //1261 // Font Family on Change Listener1262 $font_family_select.on('change csf.change', function( event ) {1263 // Hide subsets on change1264 if( $subset_block.length ) {1265 $subset_block.addClass('hidden');1266 }1267 // Hide extra font style on change1268 if( $extra_font_style_block.length ) {1269 $extra_font_style_block.addClass('hidden');1270 }1271 // Hide backup font family on change1272 if( $backup_font_family_block.length ) {1273 $backup_font_family_block.addClass('hidden');1274 }1275 var $selected = $font_family_select.find(':selected');1276 var value = $selected.val();1277 var type = $selected.data('type');1278 if( type && value ) {1279 // Show backup fonts if font type google or custom1280 if( ( type === 'google' || type === 'custom' ) && $backup_font_family_block.length ) {1281 $backup_font_family_block.removeClass('hidden');1282 }1283 // Appending font style select options1284 if( $font_style_block.length ) {1285 // set styles for multi and normal style selectors1286 var styles = defaultstyles;1287 // Custom or gogle font styles1288 if( type === 'google' && webfonts[type].fonts[value][0] ) {1289 styles = webfonts[type].fonts[value][0];1290 } else if( type === 'custom' && webfonts[type].fonts[value] ) {1291 styles = webfonts[type].fonts[value];1292 }1293 selected_styles = styles;1294 // Set selected style value for avoid load errors1295 var set_auto_style = ( styles.indexOf('normal') !== -1 ) ? 'normal' : styles[0];1296 var set_style_value = ( first_style_value && styles.indexOf(first_style_value) !== -1 ) ? first_style_value : set_auto_style;1297 // Append style select options1298 base.append_select_options( $font_style_select, styles, set_style_value, 'style' );1299 // Clear first value1300 first_style_value = false;1301 // Show style select after appended1302 $font_style_block.removeClass('hidden');1303 // Appending extra font style select options1304 if( type === 'google' && $extra_font_style_block.length && styles.length > 1 ) {1305 // Append extra-style select options1306 base.append_select_options( $extra_font_style_select, styles, first_extra_style_value, 'style', true );1307 // Clear first value1308 first_extra_style_value = false;1309 // Show style select after appended1310 $extra_font_style_block.removeClass('hidden');1311 }1312 }1313 // Appending google fonts subsets select options1314 if( type === 'google' && $subset_block.length && webfonts[type].fonts[value][1] ) {1315 var subsets = webfonts[type].fonts[value][1];1316 var set_auto_subset = ( subsets.length < 2 && subsets[0] !== 'latin' ) ? subsets[0] : '';1317 var set_subset_value = ( first_subset_select_value && subsets.indexOf(first_subset_select_value) !== -1 ) ? first_subset_select_value : set_auto_subset;1318 // check for multiple subset select1319 set_subset_value = ( subset_multi_select && first_subset_select_value ) ? first_subset_select_value : set_subset_value;1320 base.append_select_options( $subset_select, subsets, set_subset_value, 'subset', subset_multi_select );1321 first_subset_select_value = false;1322 $subset_block.removeClass('hidden');1323 }1324 } else {1325 // Clear subsets options if type and value empty1326 if( $subset_block.length ) {1327 $subset_select.find('option').not(':first-child').remove();1328 $subset_select.trigger('chosen:updated');1329 }1330 // Clear font styles options if type and value empty1331 if( $font_style_block.length ) {1332 $font_style_select.find('option').not(':first-child').remove();1333 $font_style_select.trigger('chosen:updated');1334 }1335 }1336 // Update font type input value1337 $type.val(type);1338 }).trigger('csf.change');1339 //1340 //1341 // Preview1342 var $preview_block = $this.find('.csf--block-preview');1343 if( $preview_block.length ) {1344 var $preview = $this.find('.csf--preview');1345 // Set preview styles on change1346 $this.on('change', CSF.helper.debounce( function( event ) {1347 $preview_block.removeClass('hidden');1348 var font_family = $font_family_select.val(),1349 font_weight = $this.find('.csf--font-weight').val(),1350 font_style = $this.find('.csf--font-style').val(),1351 font_size = $this.find('.csf--font-size').val(),1352 font_variant = $this.find('.csf--font-variant').val(),1353 line_height = $this.find('.csf--line-height').val(),1354 text_align = $this.find('.csf--text-align').val(),1355 text_transform = $this.find('.csf--text-transform').val(),1356 text_decoration = $this.find('.csf--text-decoration').val(),1357 text_color = $this.find('.csf--color').val(),1358 word_spacing = $this.find('.csf--word-spacing').val(),1359 letter_spacing = $this.find('.csf--letter-spacing').val(),1360 custom_style = $this.find('.csf--custom-style').val(),1361 type = $this.find('.csf--type').val();1362 if( type === 'google' ) {1363 base.load_google_font(font_family, font_weight, font_style);1364 }1365 var properties = {};1366 if( font_family ) { properties.fontFamily = font_family; }1367 if( font_weight ) { properties.fontWeight = font_weight; }1368 if( font_style ) { properties.fontStyle = font_style; }1369 if( font_variant ) { properties.fontVariant = font_variant; }1370 if( font_size ) { properties.fontSize = font_size + unit; }1371 if( line_height ) { properties.lineHeight = line_height + unit; }1372 if( letter_spacing ) { properties.letterSpacing = letter_spacing + unit; }1373 if( word_spacing ) { properties.wordSpacing = word_spacing + unit; }1374 if( text_align ) { properties.textAlign = text_align; }1375 if( text_transform ) { properties.textTransform = text_transform; }1376 if( text_decoration ) { properties.textDecoration = text_decoration; }1377 if( text_color ) { properties.color = text_color; }1378 $preview.removeAttr('style');1379 // Customs style attribute1380 if( custom_style ) { $preview.attr('style', custom_style); }1381 $preview.css(properties);1382 }, 100 ) );1383 // Preview black and white backgrounds trigger1384 $preview_block.on('click', function() {1385 $preview.toggleClass('csf--black-background');1386 var $toggle = $preview_block.find('.csf--toggle');1387 if( $toggle.hasClass('fa-toggle-off') ) {1388 $toggle.removeClass('fa-toggle-off').addClass('fa-toggle-on');1389 } else {1390 $toggle.removeClass('fa-toggle-on').addClass('fa-toggle-off');1391 }1392 });1393 if( !$preview_block.hasClass('hidden') ) {1394 $this.trigger('change');1395 }1396 }1397 };1398 base.init();1399 });1400 };1401 //1402 // Field: upload1403 //1404 $.fn.csf_field_upload = function() {1405 return this.each( function() {1406 var $this = $(this),1407 $input = $this.find('input'),1408 $upload_button = $this.find('.csf--button'),1409 $remove_button = $this.find('.csf--remove'),1410 $library = $upload_button.data('library') && $upload_button.data('library').split(',') || '',1411 wp_media_frame;1412 $input.on('change', function( e ) {1413 if( $input.val() ) {1414 $remove_button.removeClass('hidden');1415 } else {1416 $remove_button.addClass('hidden');1417 }1418 });1419 $upload_button.on('click', function( e ) {1420 e.preventDefault();1421 if( typeof window.wp === 'undefined' || ! window.wp.media || ! window.wp.media.gallery ) {1422 return;1423 }1424 if( wp_media_frame ) {1425 wp_media_frame.open();1426 return;1427 }1428 wp_media_frame = window.wp.media({1429 library: {1430 type: $library1431 },1432 });1433 wp_media_frame.on( 'select', function() {1434 $input.val( wp_media_frame.state().get('selection').first().attributes.url ).trigger('change');1435 });1436 wp_media_frame.open();1437 });1438 $remove_button.on('click', function( e ) {1439 e.preventDefault();1440 $input.val('').trigger('change');1441 });1442 });1443 };1444 //1445 // Confirm1446 //1447 $.fn.csf_confirm = function() {1448 return this.each( function() {1449 $(this).on('click', function( e ) {1450 var confirm_text = $(this).data('confirm') || window.csf_vars.i18n.confirm;1451 var confirm_answer = confirm( confirm_text );1452 CSF.vars.is_confirm = true;1453 if( !confirm_answer ) {1454 e.preventDefault();1455 CSF.vars.is_confirm = false;1456 return false;1457 }1458 });1459 });1460 };1461 $.fn.serializeObject = function(){1462 var obj = {};1463 $.each( this.serializeArray(), function(i,o){1464 var n = o.name,1465 v = o.value;1466 obj[n] = obj[n] === undefined ? v1467 : $.isArray( obj[n] ) ? obj[n].concat( v )1468 : [ obj[n], v ];1469 });1470 return obj;1471 };1472 //1473 // Options Save1474 //1475 $.fn.csf_save = function() {1476 return this.each( function() {1477 var $this = $(this),1478 $buttons = $('.csf-save'),1479 $panel = $('.csf-options'),1480 flooding = false,1481 timeout;1482 $this.on('click', function( e ) {1483 if( !flooding ) {1484 var $text = $this.data('save'),1485 $value = $this.val();1486 $buttons.attr('value', $text);1487 if( $this.hasClass('csf-save-ajax') ) {1488 e.preventDefault();1489 $panel.addClass('csf-saving');1490 $buttons.prop('disabled', true);1491 window.wp.ajax.post( 'csf_'+ $panel.data('unique') +'_ajax_save', {1492 data: $('#csf-form').serializeJSONCSF()1493 })1494 .done( function( response ) {1495 clearTimeout(timeout);1496 var $result_success = $('.csf-form-success');1497 $result_success.empty().append(response.notice).slideDown('fast', function() {1498 timeout = setTimeout( function() {1499 $result_success.slideUp('fast');1500 }, 2000);1501 });1502 // clear errors1503 $('.csf-error').remove();1504 var $append_errors = $('.csf-form-error');1505 $append_errors.empty().hide();1506 if( Object.keys( response.errors ).length ) {1507 var error_icon = '<i class="csf-label-error csf-error">!</i>';1508 $.each(response.errors, function( key, error_message ) {1509 var $field = $('[data-depend-id="'+ key +'"]'),1510 $link = $('#csf-tab-link-'+ ($field.closest('.csf-section').index()+1)),1511 $tab = $link.closest('.csf-tab-depth-0');1512 $field.closest('.csf-fieldset').append( '<p class="csf-text-error csf-error">'+ error_message +'</p>' );1513 if( !$link.find('.csf-error').length ) {1514 $link.append( error_icon );1515 }1516 if( !$tab.find('.csf-arrow .csf-error').length ) {1517 $tab.find('.csf-arrow').append( error_icon );1518 }1519 console.log(error_message);1520 $append_errors.append( '<div>'+ error_icon +' '+ error_message + '</div>' );1521 });1522 $append_errors.show();1523 }1524 $panel.removeClass('csf-saving');1525 $buttons.prop('disabled', false).attr('value', $value);1526 flooding = false;1527 })1528 .fail( function( response ) {1529 alert( response.error );1530 });1531 }1532 }1533 flooding = true;1534 });1535 });1536 };1537 //1538 // Taxonomy Framework1539 //1540 $.fn.csf_taxonomy = function() {1541 return this.each( function() {1542 var $this = $(this),1543 $form = $this.parents('form');1544 if( $form.attr('id') === 'addtag' ) {1545 var $submit = $form.find('#submit'),1546 $cloned = $this.find('.csf-field').csf_clone();1547 $submit.on( 'click', function() {1548 if( !$form.find('.form-required').hasClass('form-invalid') ) {1549 $this.data('inited', false);1550 $this.empty();1551 $this.html( $cloned );1552 $cloned = $cloned.csf_clone();1553 $this.csf_reload_script();1554 }1555 });1556 }1557 });1558 };1559 //1560 // Shortcode Framework1561 //1562 $.fn.csf_shortcode = function() {1563 var base = this;1564 base.shortcode_parse = function( serialize, key ) {1565 var shortcode = '';1566 $.each(serialize, function( shortcode_key, shortcode_values ) {1567 key = ( key ) ? key : shortcode_key;1568 shortcode += '[' + key;1569 $.each(shortcode_values, function( shortcode_tag, shortcode_value ) {1570 if( shortcode_tag === 'content' ) {1571 shortcode += ']';1572 shortcode += shortcode_value;1573 shortcode += '[/'+ key +'';1574 } else {1575 shortcode += base.shortcode_tags( shortcode_tag, shortcode_value );1576 }1577 });1578 shortcode += ']';1579 });1580 return shortcode;1581 };1582 base.shortcode_tags = function( shortcode_tag, shortcode_value ) {1583 var shortcode = '';1584 if( shortcode_value !== '' ) {1585 if( typeof shortcode_value === 'object' && !$.isArray( shortcode_value ) ) {1586 $.each(shortcode_value, function( sub_shortcode_tag, sub_shortcode_value ) {1587 // sanitize spesific key/value1588 switch( sub_shortcode_tag ) {1589 case 'background-image':1590 sub_shortcode_value = ( sub_shortcode_value.url ) ? sub_shortcode_value.url : '';1591 break;1592 }1593 if( sub_shortcode_value !== '' ) {1594 shortcode += ' ' + sub_shortcode_tag.replace('-', '_') + '="' + sub_shortcode_value.toString() + '"';1595 }1596 });1597 } else {1598 shortcode += ' ' + shortcode_tag.replace('-', '_') + '="' + shortcode_value.toString() + '"';1599 }1600 }1601 return shortcode;1602 };1603 base.insertAtChars = function( _this, currentValue ) {1604 var obj = ( typeof _this[0].name !== 'undefined' ) ? _this[0] : _this;1605 if( obj.value.length && typeof obj.selectionStart !== 'undefined' ) {1606 obj.focus();1607 return obj.value.substring( 0, obj.selectionStart ) + currentValue + obj.value.substring( obj.selectionEnd, obj.value.length );1608 } else {1609 obj.focus();1610 return currentValue;1611 }1612 };1613 base.send_to_editor = function( html, editor_id ) {1614 var tinymce_editor;1615 if( typeof tinymce !== 'undefined' ) {1616 tinymce_editor = tinymce.get( editor_id );1617 }1618 if( tinymce_editor && !tinymce_editor.isHidden() ) {1619 tinymce_editor.execCommand( 'mceInsertContent', false, html );1620 } else {1621 var $editor = $('#'+editor_id);1622 $editor.val( base.insertAtChars( $editor, html ) ).trigger('change');1623 }1624 };1625 return this.each( function() {1626 var $modal = $(this),1627 $load = $modal.find('.csf-modal-load'),1628 $content = $modal.find('.csf-modal-content'),1629 $insert = $modal.find('.csf-modal-insert'),1630 $loading = $modal.find('.csf-modal-loading'),1631 $select = $modal.find('select'),1632 modal_id = $modal.data('modal-id'),1633 nonce = $modal.data('nonce'),1634 editor_id,1635 target_id,1636 gutenberg_id,1637 sc_key,1638 sc_name,1639 sc_view,1640 sc_group,1641 $cloned,1642 $button;1643 $(document).on('click', '.csf-shortcode-button[data-modal-id="'+ modal_id +'"]', function( e ) {1644 e.preventDefault();1645 $button = $(this);1646 editor_id = $button.data('editor-id') || false;1647 target_id = $button.data('target-id') || false;1648 gutenberg_id = $button.data('gutenberg-id') || false;1649 $modal.show();1650 // single usage trigger first shortcode1651 if( $modal.hasClass('csf-shortcode-single') && sc_name === undefined ) {1652 $select.trigger('change');1653 }1654 });1655 $select.on( 'change', function() {1656 var $option = $(this);1657 var $selected = $option.find(':selected');1658 sc_key = $option.val();1659 sc_name = $selected.data('shortcode');1660 sc_view = $selected.data('view') || 'normal';1661 sc_group = $selected.data('group') || sc_name;1662 $load.empty();1663 if( sc_key ) {1664 $loading.show();1665 window.wp.ajax.post( 'csf-get-shortcode-'+ modal_id, {1666 shortcode_key: sc_key,1667 nonce: nonce1668 })1669 .done( function( response ) {1670 $loading.hide();1671 var $appended = $(response.content).appendTo($load);1672 $insert.parent().removeClass('hidden');1673 $cloned = $appended.find('.csf--repeat-shortcode').csf_clone();1674 $appended.csf_reload_script();1675 $appended.find('.csf-fields').csf_reload_script();1676 });1677 } else {1678 $insert.parent().addClass('hidden');1679 }1680 });1681 $insert.on('click', function( e ) {1682 e.preventDefault();1683 var shortcode = '';1684 var serialize = $modal.find('.csf-field:not(.hidden)').find(':input').serializeObjectCSF();1685 switch ( sc_view ) {1686 case 'contents':1687 var contentsObj = ( sc_name ) ? serialize[sc_name] : serialize;1688 $.each(contentsObj, function( sc_key, sc_value ) {1689 var sc_tag = ( sc_name ) ? sc_name : sc_key;1690 shortcode += '['+ sc_tag +']'+ sc_value +'[/'+ sc_tag +']';1691 });1692 break;1693 case 'group':1694 shortcode += '[' + sc_name;1695 $.each(serialize[sc_name], function( sc_key, sc_value ) {1696 shortcode += base.shortcode_tags( sc_key, sc_value );1697 });1698 shortcode += ']';1699 shortcode += base.shortcode_parse( serialize[sc_group], sc_group );1700 shortcode += '[/' + sc_name + ']';1701 break;1702 case 'repeater':1703 shortcode += base.shortcode_parse( serialize[sc_group], sc_group );1704 break;1705 default:1706 shortcode += base.shortcode_parse( serialize );1707 break;1708 }1709 if( gutenberg_id ) {1710 var content = window.csf_gutenberg_props.attributes.hasOwnProperty('shortcode') ? window.csf_gutenberg_props.attributes.shortcode : '';1711 window.csf_gutenberg_props.setAttributes({shortcode: content + shortcode});1712 } else if( editor_id ) {1713 base.send_to_editor( shortcode, editor_id );1714 } else {1715 var $textarea = (target_id) ? $(target_id) : $button.parent().find('textarea');1716 $textarea.val( base.insertAtChars( $textarea, shortcode ) ).trigger('change');1717 }1718 $modal.hide();1719 });1720 $modal.on('click', '.csf--repeat-button', function( e ) {1721 e.preventDefault();1722 var $repeatable = $modal.find('.csf--repeatable');1723 var $new_clone = $cloned.csf_clone();1724 var $remove_btn = $new_clone.find('.csf-repeat-remove');1725 var $appended = $new_clone.appendTo( $repeatable );1726 $new_clone.find('.csf-fields').csf_reload_script();1727 CSF.helper.name_nested_replace( $modal.find('.csf--repeat-shortcode'), sc_group );1728 $remove_btn.on('click', function() {1729 $new_clone.remove();1730 CSF.helper.name_nested_replace( $modal.find('.csf--repeat-shortcode'), sc_group );1731 });1732 });1733 $modal.on('click', '.csf-modal-close, .csf-modal-overlay', function() {1734 $modal.hide();1735 });1736 });1737 };1738 //1739 // Helper Checkbox Checker1740 //1741 $.fn.csf_checkbox = function() {1742 return this.each( function() {1743 var $this = $(this),1744 $input = $this.find('.csf--input'),1745 $checkbox = $this.find('.csf--checkbox');1746 $checkbox.on('click', function() {1747 $input.val( Number( $checkbox.prop('checked') ) ).trigger('change');1748 });1749 });1750 };1751 //1752 // Field: wp_editor1753 //1754 $.fn.csf_field_wp_editor = function() {1755 return this.each( function() {1756 if( typeof window.wp.editor === 'undefined' || typeof window.tinyMCEPreInit === 'undefined' || typeof window.tinyMCEPreInit.mceInit.csf_wp_editor === 'undefined' ) {1757 return;1758 }1759 var $this = $(this),1760 $editor = $this.find('.csf-wp-editor'),1761 $textarea = $this.find('textarea');1762 // If there is wp-editor remove it for avoid dupliated wp-editor conflicts.1763 var $has_wp_editor = $this.find('.wp-editor-wrap').length || $this.find('.mce-container').length;1764 if( $has_wp_editor ) {1765 $editor.empty();1766 $editor.append($textarea);1767 $textarea.css('display', '');1768 }1769 // Generate a unique id1770 var uid = CSF.helper.uid('csf-editor-');1771 $textarea.attr('id', uid);1772 // Get default editor settings1773 var default_editor_settings = {1774 tinymce: window.tinyMCEPreInit.mceInit.csf_wp_editor,1775 quicktags: window.tinyMCEPreInit.qtInit.csf_wp_editor1776 };1777 // Get default editor settings1778 var field_editor_settings = $editor.data('editor-settings');1779 // Add on change event handle1780 var editor_on_change = function( editor ) {1781 editor.on('change', CSF.helper.debounce( function() {1782 editor.save();1783 $textarea.trigger('change');1784 }, 250 ) );1785 };1786 // Extend editor selector and on change event handler1787 default_editor_settings.tinymce = $.extend( {}, default_editor_settings.tinymce, { selector: '#'+ uid, setup: editor_on_change } );1788 // Override editor tinymce settings1789 if( field_editor_settings.tinymce === false ) {1790 default_editor_settings.tinymce = false;1791 $editor.addClass('csf-no-tinymce');1792 }1793 // Override editor quicktags settings1794 if( field_editor_settings.quicktags === false ) {1795 default_editor_settings.quicktags = false;1796 $editor.addClass('csf-no-quicktags');1797 }1798 // Wait until :visible1799 var interval = setInterval(function () {1800 if( $this.is(':visible') ) {1801 window.wp.editor.initialize(uid, default_editor_settings);1802 clearInterval(interval);1803 }1804 });1805 // Add Media buttons1806 if( field_editor_settings.media_buttons && window.csf_media_buttons ) {1807 var $editor_buttons = $editor.find('.wp-media-buttons');1808 if( $editor_buttons.length ) {1809 $editor_buttons.find('.csf-shortcode-button').data('editor-id', uid);1810 } else {1811 var $media_buttons = $(window.csf_media_buttons);1812 $media_buttons.find('.csf-shortcode-button').data('editor-id', uid);1813 $editor.prepend( $media_buttons );1814 }1815 }1816 });1817 };1818 //1819 // Siblings1820 //1821 $.fn.csf_siblings = function() {1822 return this.each( function() {1823 var $this = $(this),1824 $siblings = $this.find('.csf--sibling'),1825 multiple = $this.data('multiple') || false;1826 $siblings.on('click', function() {1827 var $sibling = $(this);1828 if( multiple ) {1829 if( $sibling.hasClass('csf--active') ) {1830 $sibling.removeClass('csf--active');1831 $sibling.find('input').prop('checked', false).trigger('change');1832 } else {1833 $sibling.addClass('csf--active');1834 $sibling.find('input').prop('checked', true).trigger('change');1835 }1836 } else {1837 $this.find('input').prop('checked', false);1838 $sibling.find('input').prop('checked', true).trigger('change');1839 $sibling.addClass('csf--active').siblings().removeClass('csf--active');1840 }1841 });1842 });1843 };1844 //1845 // WP Color Picker1846 //1847 if( typeof Color === 'function' ) {1848 Color.fn.toString = function() {1849 if( this._alpha < 1 ) {1850 return this.toCSS('rgba', this._alpha).replace(/\s+/g, '');1851 }1852 var hex = parseInt( this._color, 10 ).toString( 16 );1853 if( this.error ) { return ''; }1854 if( hex.length < 6 ) {1855 for (var i = 6 - hex.length - 1; i >= 0; i--) {1856 hex = '0' + hex;1857 }1858 }1859 return '#' + hex;1860 };1861 }1862 CSF.funcs.parse_color = function( color ) {1863 var value = color.replace(/\s+/g, ''),1864 trans = ( value.indexOf('rgba') !== -1 ) ? parseFloat( value.replace(/^.*,(.+)\)/, '$1') * 100 ) : 100,1865 rgba = ( trans < 100 ) ? true : false;1866 return { value: value, transparent: trans, rgba: rgba };1867 };1868 $.fn.csf_color = function() {1869 return this.each( function() {1870 var $input = $(this),1871 picker_color = CSF.funcs.parse_color( $input.val() ),1872 palette_color = window.csf_vars.color_palette.length ? window.csf_vars.color_palette : true,1873 $container;1874 // Destroy and Reinit1875 if( $input.hasClass('wp-color-picker') ) {1876 $input.closest('.wp-picker-container').after($input).remove();1877 }1878 $input.wpColorPicker({1879 palettes: palette_color,1880 change: function( event, ui ) {1881 var ui_color_value = ui.color.toString();1882 $container.removeClass('csf--transparent-active');1883 $container.find('.csf--transparent-offset').css('background-color', ui_color_value);1884 $input.val(ui_color_value).trigger('change');1885 },1886 create: function() {1887 $container = $input.closest('.wp-picker-container');1888 var a8cIris = $input.data('a8cIris'),1889 $transparent_wrap = $('<div class="csf--transparent-wrap">' +1890 '<div class="csf--transparent-slider"></div>' +1891 '<div class="csf--transparent-offset"></div>' +1892 '<div class="csf--transparent-text"></div>' +1893 '<div class="csf--transparent-button button button-small">transparent</div>' +1894 '</div>').appendTo( $container.find('.wp-picker-holder') ),1895 $transparent_slider = $transparent_wrap.find('.csf--transparent-slider'),1896 $transparent_text = $transparent_wrap.find('.csf--transparent-text'),1897 $transparent_offset = $transparent_wrap.find('.csf--transparent-offset'),1898 $transparent_button = $transparent_wrap.find('.csf--transparent-button');1899 if( $input.val() === 'transparent' ) {1900 $container.addClass('csf--transparent-active');1901 }1902 $transparent_button.on('click', function() {1903 if( $input.val() !== 'transparent' ) {1904 $input.val('transparent').trigger('change').removeClass('iris-error');1905 $container.addClass('csf--transparent-active');1906 } else {1907 $input.val( a8cIris._color.toString() ).trigger('change');1908 $container.removeClass('csf--transparent-active');1909 }1910 });1911 $transparent_slider.slider({1912 value: picker_color.transparent,1913 step: 1,1914 min: 0,1915 max: 100,1916 slide: function( event, ui ) {1917 var slide_value = parseFloat( ui.value / 100 );1918 a8cIris._color._alpha = slide_value;1919 $input.wpColorPicker( 'color', a8cIris._color.toString() );1920 $transparent_text.text( ( slide_value === 1 || slide_value === 0 ? '' : slide_value ) );1921 },1922 create: function() {1923 var slide_value = parseFloat( picker_color.transparent / 100 ),1924 text_value = slide_value < 1 ? slide_value : '';1925 $transparent_text.text(text_value);1926 $transparent_offset.css('background-color', picker_color.value);1927 $container.on('click', '.wp-picker-clear', function() {1928 a8cIris._color._alpha = 1;1929 $transparent_text.text('');1930 $transparent_slider.slider('option', 'value', 100);1931 $container.removeClass('csf--transparent-active');1932 $input.trigger('change');1933 });1934 $container.on('click', '.wp-picker-default', function() {1935 var default_color = CSF.funcs.parse_color( $input.data('default-color') ),1936 default_value = parseFloat( default_color.transparent / 100 ),1937 default_text = default_value < 1 ? default_value : '';1938 a8cIris._color._alpha = default_value;1939 $transparent_text.text(default_text);1940 $transparent_slider.slider('option', 'value', default_color.transparent);1941 });1942 $container.on('click', '.wp-color-result', function() {1943 $transparent_wrap.toggle();1944 });1945 $('body').on( 'click.wpcolorpicker', function() {1946 $transparent_wrap.hide();1947 });1948 }1949 });1950 }1951 });1952 });1953 };1954 //1955 // ChosenJS1956 //1957 $.fn.csf_chosen = function() {1958 return this.each( function() {1959 var $this = $(this),1960 $inited = $this.parent().find('.chosen-container'),1961 is_multi = $this.attr('multiple') || false,1962 set_width = is_multi ? '100%' : 'auto',1963 set_options = $.extend({1964 allow_single_deselect: true,1965 disable_search_threshold: 15,1966 width: set_width1967 }, $this.data());1968 if( $inited.length ) {1969 $inited.remove();1970 }1971 $this.chosen(set_options);1972 });1973 };1974 //1975 // Number (only allow numeric inputs)1976 //1977 $.fn.csf_number = function() {1978 return this.each( function() {1979 $(this).on('keypress', function( e ) {1980 if( e.keyCode !== 0 && e.keyCode !== 8 && e.keyCode !== 45 && e.keyCode !== 46 && ( e.keyCode < 48 || e.keyCode > 57 ) ) {1981 return false;1982 }1983 });1984 });1985 };1986 //1987 // Help Tooltip1988 //1989 $.fn.csf_help = function() {1990 return this.each( function() {1991 var $this = $(this),1992 $tooltip,1993 offset_left;1994 $this.on({1995 mouseenter: function() {1996 $tooltip = $( '<div class="csf-tooltip"></div>' ).html( $this.find('.csf-help-text').html() ).appendTo('body');1997 offset_left = ( CSF.vars.is_rtl ) ? ( $this.offset().left + 24 ) : ( $this.offset().left - $tooltip.outerWidth() );1998 $tooltip.css({1999 top: $this.offset().top - ( ( $tooltip.outerHeight() / 2 ) - 14 ),2000 left: offset_left,2001 });2002 },2003 mouseleave: function() {2004 if( $tooltip !== undefined ) {2005 $tooltip.remove();2006 }2007 }2008 });2009 });2010 };2011 //2012 // Customize Refresh2013 //2014 $.fn.csf_customizer_refresh = function() {2015 return this.each( function() {2016 var $this = $(this),2017 $complex = $this.closest('.csf-customize-complex');2018 if( $complex.length ) {2019 var $input = $complex.find(':input'),2020 $unique = $complex.data('unique-id'),2021 $option = $complex.data('option-id'),2022 obj = $input.serializeObjectCSF(),2023 data = ( !$.isEmptyObject(obj) ) ? obj[$unique][$option] : '',2024 control = wp.customize.control($unique +'['+ $option +']');2025 // clear the value to force refresh.2026 control.setting._value = null;2027 control.setting.set( data );2028 } else {2029 $this.find(':input').first().trigger('change');2030 }2031 $(document).trigger('csf-customizer-refresh', $this);2032 });2033 };2034 //2035 // Customize Listen Form Elements2036 //2037 $.fn.csf_customizer_listen = function( options ) {2038 var settings = $.extend({2039 closest: false,2040 }, options );2041 return this.each( function() {2042 if( window.wp.customize === undefined ) { return; }2043 var $this = ( settings.closest ) ? $(this).closest('.csf-customize-complex') : $(this),2044 $input = $this.find(':input'),2045 unique_id = $this.data('unique-id'),2046 option_id = $this.data('option-id');2047 if( unique_id === undefined ) { return; }2048 $input.on('change keyup', CSF.helper.debounce( function() {2049 var obj = $this.find(':input').serializeObjectCSF();2050 if( !$.isEmptyObject(obj) && obj[unique_id] ) {2051 window.wp.customize.control( unique_id +'['+ option_id +']' ).setting.set( obj[unique_id][option_id] );2052 }2053 }, 250 ) );2054 });2055 };2056 //2057 // Customizer Listener for Reload JS2058 //2059 $(document).on('expanded', '.control-section-csf', function() {2060 var $this = $(this);2061 if( $this.hasClass('open') && !$this.data('inited') ) {2062 $this.csf_dependency();2063 $this.find('.csf-customize-field').csf_reload_script({dependency: false});2064 $this.find('.csf-customize-complex').csf_customizer_listen();2065 $this.data('inited', true);2066 }2067 });2068 //2069 // Window on resize2070 //2071 CSF.vars.$window.on('resize csf.resize', CSF.helper.debounce( function( event ) {2072 var window_width = navigator.userAgent.indexOf('AppleWebKit/') > -1 ? CSF.vars.$window.width() : window.innerWidth;2073 if( window_width <= 782 && !CSF.vars.onloaded ) {2074 $('.csf-section').csf_reload_script();2075 CSF.vars.onloaded = true;2076 }2077 }, 200)).trigger('csf.resize');2078 //2079 // Widgets Framework2080 //2081 $.fn.csf_widgets = function() {2082 if( this.length ) {2083 $(document).on('widget-added widget-updated', function( event, $widget ) {2084 $widget.find('.csf-fields').csf_reload_script();2085 });2086 $('.widgets-sortables, .control-section-sidebar').on('sortstop', function( event, ui ) {2087 ui.item.find('.csf-fields').csf_reload_script_retry();2088 });2089 $(document).on('click', '.widget-top', function( event ) {2090 $(this).parent().find('.csf-fields').csf_reload_script();2091 });2092 }2093 };2094 //2095 // Retry Plugins2096 //2097 $.fn.csf_reload_script_retry = function() {2098 return this.each( function() {2099 var $this = $(this);2100 if( $this.data('inited') ) {2101 $this.children('.csf-field-wp_editor').csf_field_wp_editor();2102 }2103 });2104 };2105 //2106 // Reload Plugins2107 //2108 $.fn.csf_reload_script = function( options ) {2109 var settings = $.extend({2110 dependency: true,2111 }, options );2112 return this.each( function() {2113 var $this = $(this);2114 // Avoid for conflicts2115 if( !$this.data('inited') ) {2116 // Field plugins2117 $this.children('.csf-field-accordion').csf_field_accordion();2118 $this.children('.csf-field-backup').csf_field_backup();2119 $this.children('.csf-field-background').csf_field_background();2120 $this.children('.csf-field-code_editor').csf_field_code_editor();2121 $this.children('.csf-field-date').csf_field_date();2122 $this.children('.csf-field-fieldset').csf_field_fieldset();2123 $this.children('.csf-field-gallery').csf_field_gallery();2124 $this.children('.csf-field-group').csf_field_group();2125 $this.children('.csf-field-icon').csf_field_icon();2126 $this.children('.csf-field-media').csf_field_media();2127 $this.children('.csf-field-repeater').csf_field_repeater();2128 $this.children('.csf-field-slider').csf_field_slider();2129 $this.children('.csf-field-sortable').csf_field_sortable();2130 $this.children('.csf-field-sorter').csf_field_sorter();2131 $this.children('.csf-field-spinner').csf_field_spinner();2132 $this.children('.csf-field-switcher').csf_field_switcher();2133 $this.children('.csf-field-tabbed').csf_field_tabbed();2134 $this.children('.csf-field-typography').csf_field_typography();2135 $this.children('.csf-field-upload').csf_field_upload();2136 $this.children('.csf-field-wp_editor').csf_field_wp_editor();2137 // Field colors2138 $this.children('.csf-field-border').find('.csf-color').csf_color();2139 $this.children('.csf-field-background').find('.csf-color').csf_color();2140 $this.children('.csf-field-color').find('.csf-color').csf_color();2141 $this.children('.csf-field-color_group').find('.csf-color').csf_color();2142 $this.children('.csf-field-link_color').find('.csf-color').csf_color();2143 $this.children('.csf-field-typography').find('.csf-color').csf_color();2144 // Field allows only number2145 $this.children('.csf-field-dimensions').find('.csf-number').csf_number();2146 $this.children('.csf-field-slider').find('.csf-number').csf_number();2147 $this.children('.csf-field-spacing').find('.csf-number').csf_number();2148 $this.children('.csf-field-spinner').find('.csf-number').csf_number();2149 $this.children('.csf-field-typography').find('.csf-number').csf_number();2150 // Field chosenjs2151 $this.children('.csf-field-select').find('.csf-chosen').csf_chosen();2152 // Field Checkbox2153 $this.children('.csf-field-checkbox').find('.csf-checkbox').csf_checkbox();2154 // Field Siblings2155 $this.children('.csf-field-button_set').find('.csf-siblings').csf_siblings();2156 $this.children('.csf-field-image_select').find('.csf-siblings').csf_siblings();2157 $this.children('.csf-field-palette').find('.csf-siblings').csf_siblings();2158 // Help Tooptip2159 $this.children('.csf-field').find('.csf-help').csf_help();2160 if( settings.dependency ) {2161 $this.csf_dependency();2162 }2163 $this.data('inited', true);2164 $(document).trigger('csf-reload-script', $this);2165 }2166 });2167 };2168 //2169 // Document ready and run scripts2170 //2171 $(document).ready( function() {2172 $('.csf-save').csf_save();2173 $('.csf-confirm').csf_confirm();2174 $('.csf-nav-options').csf_nav_options();2175 $('.csf-nav-metabox').csf_nav_metabox();2176 $('.csf-expand-all').csf_expand_all();2177 $('.csf-search').csf_search();2178 $('.csf-sticky-header').csf_sticky();2179 $('.csf-taxonomy').csf_taxonomy();2180 $('.csf-shortcode').csf_shortcode();2181 $('.csf-page-templates').csf_page_templates();2182 $('.csf-post-formats').csf_post_formats();2183 $('.csf-onload').csf_reload_script();2184 $('.widget').csf_widgets();2185 });...

Full Screen

Full Screen

csf.min.js

Source:csf.min.js Github

copy

Full Screen

1/**2 *3 * -----------------------------------------------------------4 *5 * Codestar Framework6 * A Lightweight and easy-to-use WordPress Options Framework7 *8 * Copyright 2015 Codestar <info@codestarlive.com>9 *10 * -----------------------------------------------------------11 *12 */13;(function ( $, window, document, undefined ) {14 'use strict';15 // caching16 var CSF = {};17 var $body = $('body');18 var has_rtl = $body.hasClass('rtl');19 CSF.funcs = {};20 CSF.vars = {};21 //22 // Helper Functions - CODEVZ, Regex changed and trigger change added23 //24 CSF.helper = {25 name_replace: function( $selector ) {26 if ( $selector.closest( '.widget-content' ).length ) {27 $selector.find('.csf-cloneable-item').each( function( index ) {28 $(this).find('input').each( function(){29 this.name = this.name.replace(/\]\[\d+\]/g, ']['+ index +']');30 $( this ).trigger( 'change' );31 });32 });33 } else {34 $selector.find('.csf-cloneable-item').each( function( index ) {35 $(this).find(':input').each( function(){36 this.name = this.name.replace(/\[(\d+)\]/, '['+ index +']');37 });38 });39 }40 },41 debounce: function( callback, threshold, immediate ) {42 var timeout;43 return function() {44 var context = this, args = arguments;45 var later = function() {46 timeout = null;47 if ( !immediate ) {48 callback.apply(context, args);49 }50 };51 var callNow = ( immediate && !timeout );52 clearTimeout( timeout );53 timeout = setTimeout( later, threshold );54 if ( callNow ){55 callback.apply(context, args);56 }57 };58 }59 };60 //61 // Custom clone for textarea and select clone() bug62 //63 $.fn.csf_clone = function () {64 var base = $.fn.clone.apply(this, arguments),65 clone = this.find('select').add(this.filter('select')),66 cloned = base.find('select').add(base.filter('select'));67 for( var i = 0; i < clone.length; ++i ) {68 for( var j = 0; j < clone[i].options.length; ++j ) {69 if( clone[i].options[j].selected === true ) {70 cloned[i].options[j].selected = true;71 }72 }73 }74 return base;75 };76 //77 // Navigation78 //79 $.fn.csf_navigation = function() {80 return this.each(function() {81 var $nav = $(this),82 $parent = $nav.closest('.csf'),83 $section = $parent.find('.csf-section-id'),84 $expand = $parent.find('.csf-expand-all'),85 $tabbed;86 $nav.find('ul:first a').on('click', function (e) {87 e.preventDefault();88 var $el = $(this),89 $next = $el.next(),90 $target = $el.data('section');91 if( $next.is('ul') ) {92 $el.closest('li').toggleClass('csf-tab-active');93 } else {94 $tabbed = $('#csf-tab-'+$target);95 $tabbed.removeClass('hidden').siblings().addClass('hidden');96 $nav.find('a').removeClass('csf-section-active');97 $el.addClass('csf-section-active');98 $section.val($target);99 $tabbed.csf_reload_script();100 }101 });102 $expand.on('click', function (e) {103 e.preventDefault();104 $parent.find('.csf-wrapper').toggleClass('csf-show-all');105 $parent.find('.csf-section').not('.csf-onload').csf_reload_script();106 $(this).find('.fa').toggleClass('fa-eye-slash' ).toggleClass('fa-eye');107 });108 });109 };110 //111 // Search112 //113 $.fn.csf_search = function() {114 return this.each(function() {115 var $this = $(this),116 $input = $this.find('input');117 $input.on('change keyup', function() {118 var value = $(this).val(),119 $wrapper = $('.csf-wrapper'),120 $section = $wrapper.find('.csf-section'),121 $fields = $section.find('> .csf-field:not(.hidden)'),122 $titles = $fields.find('> .csf-title, .csf-search-tags');123 if( value.length > 3 ) {124 $fields.addClass('csf-hidden');125 $wrapper.addClass('csf-search-all');126 $titles.each( function() {127 var $title = $(this);128 if( $title.text().match( new RegExp('.*?' + value + '.*?', 'i') ) ) {129 var $field = $title.closest('.csf-field');130 $field.removeClass('csf-hidden');131 $field.parent().csf_reload_script();132 }133 });134 } else {135 $fields.removeClass('csf-hidden');136 $wrapper.removeClass('csf-search-all');137 }138 });139 });140 };141 //142 // Sticky Header143 //144 $.fn.csf_sticky = function() {145 return this.each(function() {146 var $this = $(this),147 $window = $(window),148 $inner = $this.find('.csf-header-inner'),149 padding = parseInt( $inner.css('padding-left') ) + parseInt( $inner.css('padding-right') ),150 offset = 32,151 scrollTop = 0,152 lastTop = 0,153 ticking = false,154 onSticky = function() {155 scrollTop = $window.scrollTop();156 requestTick();157 },158 requestTick = function () {159 if( !ticking ) {160 requestAnimationFrame( function() {161 stickyUpdate();162 ticking = false;163 });164 }165 ticking = true;166 },167 stickyUpdate = function() {168 var offsetTop = $this.offset().top,169 stickyTop = Math.max(offset, offsetTop - scrollTop ),170 winWidth = Math.max(document.documentElement.clientWidth, window.innerWidth || 0);171 if ( stickyTop <= offset && winWidth > 782 ) {172 $inner.css({width: $this.outerWidth()-padding});173 $this.css({height: $this.outerHeight()}).addClass( 'csf-sticky' );174 } else {175 $inner.removeAttr('style');176 $this.removeAttr('style').removeClass( 'csf-sticky' );177 }178 };179 $window.on( 'scroll resize', onSticky);180 onSticky();181 });182 };183 //184 // Dependency System185 //186 $.fn.csf_dependency = function ( param ) {187 return this.each(function () {188 var base = this,189 $this = $(this);190 base.init = function () {191 base.ruleset = $.deps.createRuleset();192 var cfg = {193 show: function( el ) {194 el.removeClass('hidden');195 },196 hide: function( el ) {197 el.addClass('hidden');198 },199 log: false,200 checkTargets: false201 };202 if( param !== undefined ) {203 base.depSub();204 } else {205 base.depRoot();206 }207 $.deps.enable( $this, base.ruleset, cfg );208 };209 base.depRoot = function() {210 $this.each( function() {211 $(this).find('[data-controller]').each( function() {212 var $this = $(this),213 _controller = $this.data('controller').split('|'),214 _condition = $this.data('condition').split('|'),215 _value = $this.data('value').toString().split('|'),216 _rules = base.ruleset;217 $.each(_controller, function(index, element) {218 var value = _value[index] || '',219 condition = _condition[index] || _condition[0];220 _rules = _rules.createRule('[data-depend-id="'+ element +'"]', condition, value);221 _rules.include($this);222 });223 });224 });225 };226 base.depSub = function() {227 $this.each( function() {228 $(this).find('[data-sub-controller]').each( function() {229 var $this = $(this),230 _controller = $this.data('sub-controller').split('|'),231 _condition = $this.data('sub-condition').split('|'),232 _value = $this.data('sub-value').toString().split('|'),233 _rules = base.ruleset;234 $.each(_controller, function(index, element) {235 var value = _value[index] || '',236 condition = _condition[index] || _condition[0];237 _rules = _rules.createRule('[data-sub-depend-id="'+ element +'"]', condition, value);238 _rules.include($this);239 });240 });241 });242 };243 base.init();244 });245 };246 //247 // Chosen Script248 //249 $.fn.csf_chosen = function() {250 return this.each(function() {251 $(this).chosen({allow_single_deselect: true, disable_search_threshold: 15, width: parseFloat( $(this).actual('width') + 25 ) +'px'});252 });253 };254 //255 // Field Image Selector256 //257 $.fn.csf_field_image_selector = function() {258 return this.each(function() {259 $(this).find('label').on('click', function () {260 $(this).siblings().find('input').prop('checked', false);261 });262 });263 };264 //265 // Field Sorter266 //267 $.fn.csf_field_sorter = function() {268 return this.each(function() {269 var $this = $(this),270 $enabled = $this.find('.csf-enabled'),271 $has_disabled = $this.find('.csf-disabled'),272 $disabled = ( $has_disabled.length ) ? $has_disabled : false;273 $enabled.sortable({274 connectWith: $disabled,275 placeholder: 'ui-sortable-placeholder',276 update: function( event, ui ) {277 var $el = ui.item.find('input');278 if( ui.item.parent().hasClass('csf-enabled') ) {279 $el.attr('name', $el.attr('name').replace('disabled', 'enabled'));280 } else {281 $el.attr('name', $el.attr('name').replace('enabled', 'disabled'));282 }283 $this.csf_customizer_refresh();284 }285 });286 if( $disabled ) {287 $disabled.sortable({288 connectWith: $enabled,289 placeholder: 'ui-sortable-placeholder'290 });291 }292 });293 };294 //295 // Field Upload296 //297 $.fn.csf_field_upload = function() {298 return this.each(function() {299 var $this = $(this),300 $button = $this.find('.csf-button'),301 $preview = $this.find('.csf-image-preview'),302 $remove = $this.find('.csf-image-remove'),303 $img = $this.find('img'),304 $input = $this.find('input'),305 extensions = ['jpg', 'gif', 'png', 'svg', 'jpeg'],306 wp_media_frame;307 $button.on('click', function( e ) {308 e.preventDefault();309 if ( typeof wp === 'undefined' || ! wp.media || ! wp.media.gallery ) {310 return;311 }312 if ( wp_media_frame ) {313 wp_media_frame.open();314 return;315 }316 wp_media_frame = wp.media({317 title: $button.data('frame-title'),318 library: {319 type: $button.data('upload-type')320 },321 button: {322 text: $button.data('insert-title'),323 }324 });325 wp_media_frame.on( 'select', function() {326 var attachment = wp_media_frame.state().get( 'selection' ).first();327 $input.val( attachment.attributes.url ).trigger('change');328 });329 wp_media_frame.open();330 });331 if( $preview.length ) {332 $input.on('change keyup', function() {333 var $this = $(this),334 value = $this.val(),335 ext = value.toLowerCase().slice((value.toLowerCase().lastIndexOf('.') - 1) + 2);336 // CODEVZ337 $preview.removeClass('hidden');338 $img.attr('src', value);339 });340 $remove.on('click', function( e ) {341 e.preventDefault();342 $input.val('').trigger('change');343 $preview.addClass('hidden');344 });345 }346 });347 };348 //349 // Field Image350 //351 $.fn.csf_field_image = function() {352 return this.each(function() {353 var $this = $(this),354 $button = $this.find('.csf-button'),355 $preview = $this.find('.csf-image-preview'),356 $remove = $this.find('.csf-image-remove'),357 $input = $this.find('input'),358 $img = $this.find('img'),359 wp_media_frame;360 $button.on('click', function( e ) {361 e.preventDefault();362 if ( typeof wp === 'undefined' || ! wp.media || ! wp.media.gallery ) {363 return;364 }365 if ( wp_media_frame ) {366 wp_media_frame.open();367 return;368 }369 wp_media_frame = wp.media({370 library: {371 type: 'image'372 }373 });374 wp_media_frame.on( 'select', function() {375 var attachment = wp_media_frame.state().get('selection').first().attributes;376 var thumbnail = ( typeof attachment.sizes !== 'undefined' && typeof attachment.sizes.thumbnail !== 'undefined' ) ? attachment.sizes.thumbnail.url : attachment.url;377 $preview.removeClass('hidden');378 $img.attr('src', thumbnail);379 $input.val( attachment.id ).trigger('change');380 });381 wp_media_frame.open();382 });383 $remove.on('click', function( e ) {384 e.preventDefault();385 $input.val('').trigger('change');386 $preview.addClass('hidden');387 });388 });389 };390 //391 // Field Gallery392 //393 $.fn.csf_field_gallery = function() {394 return this.each(function() {395 var $this = $(this),396 $edit = $this.find('.csf-edit-gallery'),397 $clear = $this.find('.csf-clear-gallery'),398 $list = $this.find('ul'),399 $input = $this.find('input'),400 $img = $this.find('img'),401 wp_media_frame,402 wp_media_click;403 $this.on('click', '.csf-button, .csf-edit-gallery', function( e ) {404 var $el = $(this),405 what = ( $el.hasClass('csf-edit-gallery') ) ? 'edit' : 'add',406 state = ( what === 'edit' ) ? 'gallery-edit' : 'gallery-library';407 e.preventDefault();408 if ( typeof wp === 'undefined' || ! wp.media || ! wp.media.gallery ) {409 return;410 }411 if ( wp_media_frame ) {412 wp_media_frame.open();413 wp_media_frame.setState(state);414 return;415 }416 wp_media_frame = wp.media({417 library: {418 type: 'image'419 },420 frame: 'post',421 state: 'gallery',422 multiple: true423 });424 wp_media_frame.on('open', function() {425 var ids = $input.val();426 if ( ids ) {427 var get_array = ids.split(',');428 var library = wp_media_frame.state('gallery-edit').get('library');429 wp_media_frame.setState(state);430 get_array.forEach(function(id) {431 var attachment = wp.media.attachment(id);432 library.add( attachment ? [ attachment ] : [] );433 });434 }435 });436 wp_media_frame.on( 'update', function() {437 var inner = '';438 var ids = [];439 var images = wp_media_frame.state().get('library');440 images.each(function(attachment) {441 var attributes = attachment.attributes;442 var thumbnail = ( typeof attributes.sizes.thumbnail !== 'undefined' ) ? attributes.sizes.thumbnail.url : attributes.url;443 inner += '<li><img src="'+ thumbnail +'"></li>';444 ids.push(attributes.id);445 });446 $input.val(ids).trigger('change');447 $list.html('').append(inner);448 $clear.removeClass('hidden');449 $edit.removeClass('hidden');450 });451 wp_media_frame.open();452 wp_media_click = what;453 });454 $clear.on('click', function( e ) {455 e.preventDefault();456 $list.html('');457 $input.val('').trigger('change');458 $clear.addClass('hidden');459 $edit.addClass('hidden');460 });461 });462 };463 // CODEVZ, Live group title464 function codevz_live_title( $this ) {465 setTimeout(function() {466 $this.find( '.csf-cloneable-content' ).each(function() {467 var en = $( this ),468 tt = en.parent().find( '> .csf-cloneable-title .csf-cloneable-text' );469 en.find( '> .csf-field:first-child [name]' ).off( 'change.cc keyup.cc' ).on( 'change.cc keyup.cc', function() {470 var een = $( this );471 if ( een.is( "select" ) ) {472 tt.html( een.find( 'option:selected' ).text() );473 } else {474 tt.html( een.val() ? een.val() : '...' );475 }476 }).trigger( 'change' );477 });478 }, 250 );479 }480 //481 // Field Group482 //483 $.fn.csf_field_group = function() {484 return this.each(function() {485 var $this = $(this),486 $wrapper = $this.find('.csf-cloneable-wrapper'),487 $data = $this.find('.csf-cloneable-data'),488 $hidden = $this.find('.csf-cloneable-hidden'),489 unique = $data.data('unique-id'),490 limit = parseInt( $data.data('limit') );491 $wrapper.accordion({492 header: '.csf-cloneable-title',493 collapsible : true,494 active: false,495 animate: false,496 heightStyle: 'content',497 icons: {498 'header': 'csf-cloneable-header-icon fa fa-angle-right',499 'activeHeader': 'csf-cloneable-header-icon fa fa-angle-down'500 },501 beforeActivate: function( event, ui ) {502 var $panel = ui.newPanel;503 if( $panel.length && !$panel.data( 'opened' ) ) {504 $panel.find('.csf-field').removeClass('csf-no-script');505 $panel.csf_reload_script('sub');506 $panel.data( 'opened', true );507 }508 }509 });510 $wrapper.sortable({511 //connectWith: ".csf-cloneable-wrapper",512 axis: 'y',513 handle: '.csf-cloneable-title',514 helper: 'original',515 cursor: 'move',516 placeholder: 'widget-placeholder',517 start: function( event, ui ) {518 $wrapper.accordion({ active:false });519 $wrapper.sortable('refreshPositions');520 },521 stop: function( event, ui ) {522 CSF.helper.name_replace( $wrapper );523 $wrapper.csf_customizer_refresh();524 }525 });526 codevz_live_title( $this );527 // CODEVZ, Add icon to group add528 $this.find( '.csf-cloneable-add' ).prepend( '<i class="fa fa-plus"></i>' );529 $this.on('click', '.csf-cloneable-add', function( e ) {530 e.preventDefault();531 var count = $wrapper.find('.csf-cloneable-item').length;532 if( limit && (count+1) > limit ) {533 $data.show();534 return;535 }536 var $cloned_item = $hidden.csf_clone().removeClass('csf-cloneable-hidden');537 $cloned_item.find(':input').each( function() {538 this.name = this.name.replace('_nonce', unique).replace('num', count);539 });540 $wrapper.append($cloned_item);541 $wrapper.accordion('refresh');542 $wrapper.accordion({active: count});543 $wrapper.csf_customizer_refresh();544 $wrapper.csf_customizer_listen(true);545 codevz_live_title( $this );546 });547 $wrapper.on('click', '.csf-cloneable-clone', function( e ) {548 e.preventDefault();549 if( limit && parseInt($wrapper.find('.csf-cloneable-item').length+1) > limit ) {550 $data.show();551 return;552 }553 var $this = $(this),554 $parent = $this.closest('.csf-cloneable-item'),555 $cloned = $parent.csf_clone().addClass('csf-cloned'),556 $childs = $wrapper.children();557 $childs.eq($parent.index()).after($cloned);558 CSF.helper.name_replace( $wrapper );559 $wrapper.accordion('refresh');560 $wrapper.csf_customizer_refresh();561 $wrapper.csf_customizer_listen(true);562 codevz_live_title( $this );563 });564 $wrapper.on('click', '.csf-cloneable-remove', function(e) {565 e.preventDefault();566 $(this).closest('.csf-cloneable-item').remove();567 CSF.helper.name_replace( $wrapper );568 $wrapper.csf_customizer_refresh();569 $data.hide();570 });571 });572 };573 //574 // Field Repeater575 //576 $.fn.csf_field_repeater = function() {577 return this.each(function() {578 var $this = $(this),579 $wrapper = $this.find('.csf-cloneable-wrapper'),580 $hidden = $this.find('.csf-cloneable-hidden'),581 $data = $this.find('.csf-cloneable-data'),582 unique = $data.data('unique-id'),583 limit = parseInt( $data.data('limit') );584 $wrapper.sortable({585 axis: 'y',586 handle: '.csf-cloneable-sort',587 helper: 'original',588 cursor: 'move',589 placeholder: 'widget-placeholder',590 stop: function( event, ui ) {591 CSF.helper.name_replace( $wrapper );592 $wrapper.csf_customizer_refresh();593 }594 });595 $this.on('click', '.csf-cloneable-add', function( e ) {596 e.preventDefault();597 var count = $wrapper.find('.csf-cloneable-item').length;598 if( limit && (count+1) > limit ) {599 $data.show();600 return;601 }602 var $cloned = $hidden.csf_clone().removeClass('csf-cloneable-hidden');603 $wrapper.append($cloned);604 $cloned.find(':input').each( function() {605 this.name = this.name.replace('_nonce', unique).replace('num', count);606 });607 $cloned.find('.csf-field').removeClass('csf-no-script');608 $cloned.csf_reload_script('sub');609 $wrapper.csf_customizer_refresh();610 $wrapper.csf_customizer_listen(true);611 });612 $wrapper.on('click', '.csf-cloneable-clone', function( e ) {613 e.preventDefault();614 if( limit && parseInt($wrapper.find('.csf-cloneable-item').length+1) > limit ) {615 $data.show();616 return;617 }618 var $this = $(this),619 $parent = $this.closest('.csf-cloneable-item'),620 $index = $parent.index(),621 $cloned = $parent.csf_clone(),622 $childs = $wrapper.children();623 $childs.eq($index).after($cloned);624 $cloned.addClass('csf-cloned').csf_reload_script('sub');625 CSF.helper.name_replace( $wrapper );626 $wrapper.csf_customizer_refresh();627 $wrapper.csf_customizer_listen(true);628 });629 $wrapper.on('click', '.csf-cloneable-remove', function(e) {630 e.preventDefault();631 $(this).closest('.csf-cloneable-item').remove();632 $data.hide();633 CSF.helper.name_replace( $wrapper );634 $wrapper.csf_customizer_refresh();635 });636 });637 };638 //639 // Field Icon640 //641 $.fn.csf_field_icon = function() {642 return this.each( function() {643 var $this = $(this);644 $this.on('click', '.csf-icon-add', function ( e ) {645 var $modal = $('#csf-modal-icon'),646 // CODEVZ647 old_item = $( this ).parent().find( 'input' ).val(),648 active_icon = function() {649 if ( old_item ) {650 $modal.find( 'a' ).each(function() {651 if ( old_item === $( this ).data( 'csf-icon' ) ) {652 $( this ).addClass( 'cz_active_icon' ).siblings().removeClass( 'cz_active_icon' );653 }654 });655 }656 };// CODEVZ657 e.preventDefault();658 $modal.show();659 $body.addClass('csf-icon-scrolling');660 CSF.vars.$icon_target = $this;661 if( !CSF.vars.icon_modal_loaded ) {662 $.ajax({663 type: 'POST',664 url: ajaxurl,665 data: {666 action: 'csf-get-icons'667 },668 success: function( content ) {669 CSF.vars.icon_modal_loaded = true;670 var $load = $modal.find('.csf-modal-content').html( content );671 $load.on('click', 'a', function ( e ) {672 e.preventDefault();673 var icon = $(this).data('csf-icon');674 CSF.vars.$icon_target.find('i').removeAttr('class').addClass(icon);675 CSF.vars.$icon_target.find('input').val(icon).trigger('change');676 CSF.vars.$icon_target.find('.csf-icon-preview').removeClass('hidden');677 CSF.vars.$icon_target.find('.csf-icon-remove').removeClass('hidden');678 $modal.hide();679 $body.removeClass('csf-icon-scrolling');680 });681 active_icon(); // CODEVZ682 $modal.on('change keyup', '.csf-icon-search', function(){683 var value = $(this).val(),684 $icons = $load.find('a');685 $icons.each(function() {686 var $elem = $(this);687 if ( $elem.data('csf-icon').search( new RegExp( value, 'i' ) ) < 0 ) {688 $elem.hide();689 } else {690 $elem.show();691 }692 });693 });694 $modal.on('click', '.csf-modal-close, .csf-modal-overlay', function() {695 $modal.hide();696 $body.removeClass('csf-icon-scrolling');697 });698 }699 });700 } else {701 active_icon(); // CODEVZ702 }703 });704 $this.on('click', '.csf-icon-remove', function ( e ) {705 e.preventDefault();706 $this.find('.csf-icon-preview').addClass('hidden');707 $this.find('input').val('').trigger('change');708 $(this).addClass('hidden');709 });710 });711 };712 //713 // CODEVZ: Field Font select714 //715 $.fn.csf_field_select_font = function() {716 return this.each( function() {717 var $this = $(this);718 $this.on('click', '.csf-font-add', function ( e ) {719 var $modal = $('#csf-modal-font'),720 old_font = $( this ).prev( 'input' ).val(),721 active_font = function() {722 if ( old_font ) {723 old_font = old_font.indexOf( '=' ) > 0 ? old_font.substring( 0, old_font.indexOf( "=" ) ) : old_font;724 $modal.find( 'span' ).each(function() {725 if ( old_font === $( this ).text() ) {726 $( this ).parent().addClass( 'cz_active_font' ).removeClass( 'cz_font' ).siblings().removeClass( 'cz_active_font' );727 }728 });729 }730 };731 e.preventDefault();732 $modal.show();733 $body.addClass('csf-font-scrolling');734 CSF.vars.$font_target = $this;735 var cz_add_google_font_link = function( fonts_elements ) {736 fonts_elements.each(function() {737 var dis = $( this ),738 font = $( 'span', dis ).html();739 if ( ! dis.hasClass( 'websafe_font' ) && ! $( 'link', dis ).length ) {740 dis.css( 'font-family', font ).append('<link rel="stylesheet" href="//fonts.googleapis.com/css?family=' + font.replace( / /g, '+' ) + ':100,200,300,400,500,600,700,800,900" type="text/css" media="all" />');741 setTimeout(function() {742 dis.show().removeClass( 'cz_font' );743 $( '#loadMore_font' ).removeClass( 'cz_loader' );744 }, 1000 );745 }746 });747 };748 if( !CSF.vars.font_modal_loaded ) {749 $.ajax({750 type: 'POST',751 url: ajaxurl,752 data: {753 action: 'csf-get-fonts'754 },755 success: function( content ) {756 CSF.vars.font_modal_loaded = true;757 var $load = $modal.find('.csf-modal-content').html( content );758 $( '.csf-modal-content', $modal ).append('<div class="clr"></div><a href="#" id="loadMore_font" class="cz_loader">Load More</a>');759 var size_li = $( '.csf-modal-content a', $modal ).size();760 var x = 20;761 cz_add_google_font_link( $( '.cz_font:lt('+x+')', $modal ) );762 $( '#loadMore_font' ).on('click', function (e) {763 $( this ).addClass( 'cz_loader' );764 cz_add_google_font_link( $( '.cz_font:lt('+x+')', $modal ) );765 e.preventDefault();766 return false;767 });768 // Font params769 $load.on('click', 'a i', function ( e ) {770 var diz = $( this ).parent().next( '.cz_font_params' );771 $( '.cz_font_params' ).not( diz ).fadeOut();772 diz.fadeToggle();773 });774 $( 'body' ).on( 'click', function( e ) {775 if ( ! $( e.target ).closest( '.csf-modal-content' ).length ) {776 $( '.cz_font_params' ).fadeOut();777 }778 });779 active_font();780 // Select font781 $load.on('click', 'a', function ( e ) {782 e.preventDefault();783 if ( $( e.target ).prop( 'tagName' ) == 'A' || $( e.target ).prop( 'tagName' ) == 'SPAN' || $( e.target ).prop( 'tagName' ) == 'DIV' ) {784 var font = $( 'span', this).html(), 785 font_css = font;786 // Font params787 if ( $( this ).next( 'div' ).find( '.cz_font_variants :checkbox:checked' ).val() ) {788 var vala = [];789 $( this ).next( 'div' ).find( '.cz_font_variants :checkbox:checked' ).each(function(i){790 vala[i] = $( this ).val();791 });792 font = font + '=' + vala.join(",");793 }794 if ( $( this ).next( 'div' ).find( '.cz_font_subsets :checkbox:checked' ).val() ) {795 var valb = [];796 $( this ).next( 'div' ).find( '.cz_font_subsets :checkbox:checked' ).each(function(i){797 valb[i] = $( this ).val();798 });799 font = font + '&' + valb.join(",");800 }801 $( '.cz_font_params' ).fadeOut();802 CSF.vars.$font_target.find('input').val( font ).trigger('change');803 CSF.vars.$font_target.find('.csf-font-preview, .csf-font-remove').removeClass('hidden');804 $modal.hide();805 $body.removeClass('csf-font-scrolling');806 }807 });808 $modal.on('change keyup', '.csf-font-search', function(){809 var value = $(this).val(),810 $fonts = $load.find('a');811 if ( ! value ) {812 var x = 20;813 $( '.csf-modal-content a', $modal ).hide().not( '.cz_font' ).show();814 $( '#loadMore_font, .websafe_font', $modal ).show();815 return false;816 }817 if ( value.length < 3 ) {818 return false;819 }820 $( '#loadMore_font' ).hide();821 $fonts.each(function() {822 var $elem = $(this),823 search = new RegExp( value, 'i' );824 if ( search && $elem.find('span').html().search( search ) < 0 ) {825 $elem.hide();826 } else {827 $elem.show();828 cz_add_google_font_link( $elem );829 }830 });831 });832 833 // Change font preview text834 $modal.on('change keyup', '.csf-font-placeholder', function(){835 var value = $( this ).val();836 if ( value ) {837 $modal.find('.csf-modal-content span').hide();838 $modal.find('.csf-modal-content .cz_preview').show().html( value );839 } else {840 $modal.find('.csf-modal-content .cz_preview').hide();841 $modal.find('.csf-modal-content span').show();842 }843 });844 $modal.on('click', '.csf-modal-close, .csf-modal-overlay', function() {845 $modal.hide();846 $body.removeClass('csf-font-scrolling');847 });848 }849 });850 } else {851 active_font();852 }853 });854 $this.on('click', '.csf-font-remove', function ( e ) {855 e.preventDefault();856 $this.find('input').val('').trigger('change');857 $( this ).addClass('hidden');858 });859 });860 };861 //862 // ADDED BY CODEVZ863 //864 // Field Demo Importer865 //866 $.fn.csf_field_demo_importer = function() {867 return this.each( function() {868 var $this = $(this);869 $this.on('click', '.csf-importer-add', function ( e ) {870 var $modal = $('#csf-modal-importer');871 e.preventDefault();872 $modal.show();873 $body.addClass('csf-importer-scrolling');874 CSF.vars.$importer_target = $this;875 if( !CSF.vars.importer_modal_loaded ) {876 $.ajax({877 type: 'POST',878 url: ajaxurl,879 data: {880 action: 'importer_modal_content'881 },882 success: function( content ) {883 CSF.vars.importer_modal_loaded = true;884 var $load = $modal.find('.csf-modal-content').html( content );885 886 $load.on('click', '.importer_settings', function ( e ) {887 e.stopPropagation();888 });889 $modal.on('change keyup', '.csf-importer-search', function(){890 var value = $(this).val(),891 $demos = $load.find('.cz_demo');892 $demos.each(function() {893 var $elem = $(this);894 if ( $elem.find( '[name="demo"]' ).val().search( new RegExp( value, 'i' ) ) < 0 ) {895 $elem.hide();896 } else {897 $elem.show();898 }899 });900 });901 $modal.on('click', '.csf-modal-close, .csf-modal-overlay', function() {902 $modal.hide();903 $body.removeClass('csf-importer-scrolling');904 });905 }906 });907 }908 });909 });910 };911 //912 // ADDED BY CODEVZ913 //914 // Field Header Preset915 //916 $.fn.csf_field_header_preset = function() {917 return this.each( function() {918 var $this = $(this);919 $this.on('click', '.csf-header-preset-add', function ( e ) {920 e.preventDefault();921 var $modal = $('#csf-modal-header-preset');922 $modal.show();923 CSF.vars.$importer_target = $this;924 if( !CSF.vars.importer_modal_loaded ) {925 $.ajax({926 type: 'POST',927 url: ajaxurl,928 data: {929 action: 'codevz_header_preset'930 },931 success: function( content ) {932 console.log( content );933 CSF.vars.importer_modal_loaded = true;934 var $load = $modal.find('.csf-modal-content').html( content );935 $load.on('click', '.cz_header_preset', function() {936 var val = $( this ).attr( 'data-header' );937 $( '[name="codevz_theme_options[header_preset]"]' ).val( val ).trigger( 'change' );938 $modal.hide();939 });940 $modal.on('click', '.csf-modal-close, .csf-modal-overlay', function() {941 $modal.hide();942 });943 }944 });945 }946 });947 });948 };949 //950 // Color Picker Helper951 //952 if( typeof Color === 'function' ) {953 Color.fn.toString = function () {954 if ( this._alpha < 1 ) {955 return this.toCSS('rgba', this._alpha).replace(/\s+/g, '');956 }957 var hex = parseInt( this._color, 10 ).toString( 16 );958 if ( this.error ) { return ''; }959 if ( hex.length < 6 ) {960 for (var i = 6 - hex.length - 1; i >= 0; i--) {961 hex = '0' + hex;962 }963 }964 return '#' + hex;965 };966 }967 CSF.funcs.PARSE_COLOR_VALUE = function( val ) {968 var value = val.replace(/\s+/g, ''),969 alpha = ( value.indexOf('rgba') !== -1 ) ? parseFloat( value.replace(/^.*,(.+)\)/, '$1') * 100 ) : 100,970 rgba = ( alpha < 100 ) ? true : false;971 return { value: value, alpha: alpha, rgba: rgba };972 };973 //974 // Field Color Picker975 //976 $.fn.csf_field_colorpicker = function() {977 return this.each(function() {978 var $this = $(this),979 $input = $this.find('.csf-wp-color-picker'),980 $wppicker = $this.find('.wp-picker-container');981 // BY CODEVZ: setTimeout for better customizer performance982 setTimeout(function() {983 // Destroy and Reinit984 // EDITED BY CODEVZ985 if( $wppicker.length && ! $this.closest( '.csf-field-codevz_box_shadow' ).length ) {986 $wppicker.after($input).remove();987 }988 if( $input.data('rgba') !== false ) {989 // CODEVZ990 var picker = CSF.funcs.PARSE_COLOR_VALUE( $input.val() ),991 primary_color = typeof codevz_primary_color == 'string' ? codevz_primary_color : '',992 secondary_color = typeof codevz_secondary_color == 'string' ? codevz_secondary_color : '',993 palettes = ['#000000','#FFFFFF','transparent','#E53935','#FF5722','#FFEB3B','#8BC34A','#3F51B5',primary_color];994 if ( secondary_color ) {995 palettes.push( secondary_color );996 }997 $input.wpColorPicker({998 palettes: palettes, // CODEVZ999 clear: function() {1000 $input.trigger('keyup');1001 },1002 change: function( event, ui ) {1003 var ui_color_value = ui.color.toString();1004 if ( ui.color.error ) {1005 ui_color_value = 'transparent';1006 }1007 $input.closest('.wp-picker-container').find('.csf-alpha-slider-offset').css('background-color', ui_color_value);1008 $input.val(ui_color_value).trigger('change');1009 },1010 create: function() {1011 var a8cIris = $input.data('a8cIris'),1012 $container = $input.closest('.wp-picker-container'),1013 $alpha_wrap = $('<div class="csf-alpha-wrap">' +1014 '<div class="csf-alpha-slider"></div>' +1015 '<div class="csf-alpha-slider-offset"></div>' +1016 '<div class="csf-alpha-text"></div>' +1017 '</div>').appendTo( $container.find('.wp-picker-holder') ),1018 $alpha_slider = $alpha_wrap.find('.csf-alpha-slider'),1019 $alpha_text = $alpha_wrap.find('.csf-alpha-text'),1020 $alpha_offset = $alpha_wrap.find('.csf-alpha-slider-offset');1021 $alpha_slider.slider({1022 slide: function( event, ui ) {1023 var slide_value = parseFloat( ui.value / 100 );1024 a8cIris._color._alpha = slide_value;1025 $input.wpColorPicker( 'color', a8cIris._color.toString() );1026 $alpha_text.text( ( slide_value < 1 ? slide_value : '' ) );1027 },1028 create: function() {1029 var slide_value = parseFloat( picker.alpha / 100 ),1030 alpha_text_value = slide_value < 1 ? slide_value : '';1031 $alpha_text.text(alpha_text_value);1032 $alpha_offset.css('background-color', picker.value);1033 $container.on('click', '.wp-picker-clear', function() {1034 a8cIris._color._alpha = 1;1035 $alpha_text.text('').trigger('change');1036 $alpha_slider.slider('option', 'value', 100).trigger('slide');1037 });1038 $container.on('click', '.wp-picker-default', function() {1039 var default_picker = CSF.funcs.PARSE_COLOR_VALUE( $input.data('default-color') ),1040 default_value = parseFloat( default_picker.alpha / 100 ),1041 default_text = default_value < 1 ? default_value : '';1042 a8cIris._color._alpha = default_value;1043 $alpha_text.text(default_text);1044 $alpha_slider.slider('option', 'value', default_picker.alpha).trigger('slide');1045 });1046 $container.on('click', '.wp-color-result', function() {1047 $alpha_wrap.toggle();1048 });1049 $body.on( 'click.wpcolorpicker', function() {1050 $alpha_wrap.hide();1051 });1052 },1053 value: picker.alpha,1054 step: 1,1055 min: 1,1056 max: 1001057 });1058 }1059 });1060 } else {1061 $input.wpColorPicker({1062 clear: function() {1063 $input.trigger('keyup');1064 },1065 change: function( event, ui ) {1066 $input.val(ui.color.toString()).trigger('change');1067 }1068 });1069 }1070 }, 500 );1071 });1072 };1073 //1074 // Field Ace Editor1075 //1076 $.fn.csf_field_ace_editor = function() {1077 return this.each(function() {1078 if( typeof ace !== 'undefined' ) {1079 var $this = $(this),1080 $textarea = $this.find('.csf-ace-editor-textarea'),1081 options = JSON.parse( $this.find( '.csf-ace-editor-options' ).val() ),1082 editor = ace.edit($this.find('.csf-ace-editor').attr('id'));1083 // global settings of ace editor1084 editor.getSession().setValue($textarea.val());1085 editor.setOptions( options );1086 editor.on( 'change', function( e ) {1087 $textarea.val( editor.getSession().getValue() ).trigger('change');1088 });1089 }1090 });1091 };1092 //1093 // Field Datepicker1094 //1095 $.fn.csf_field_datepicker = function() {1096 return this.each(function() {1097 var $this = $(this),1098 $input = $this.find('input'),1099 options = JSON.parse( $this.find('.csf-datepicker-options').val() ),1100 wrapper = '<div class="csf-datepicker-wrapper"></div>',1101 $datepicker;1102 var defaults = {1103 beforeShow: function(input, inst) {1104 $datepicker = $('#ui-datepicker-div');1105 $datepicker.wrap(wrapper);1106 },1107 onClose: function(){1108 var cancelInterval = setInterval( function() {1109 if( $datepicker.is( ':hidden' ) ) {1110 $datepicker.unwrap(wrapper);1111 clearInterval(cancelInterval);1112 }1113 }, 100 );1114 }1115 };1116 options = $.extend({}, options, defaults);1117 $input.datepicker(options);1118 });1119 };1120 //1121 // Field Tabbed1122 //1123 $.fn.csf_field_tabbed = function() {1124 return this.each( function() {1125 var $this = $(this),1126 $links = $this.find('.csf-tabbed-nav a'),1127 $section = $this.find('.csf-tabbed-section');1128 $links.on( 'click', function(e) {1129 e.preventDefault();1130 var $link = $(this),1131 index = $link.index();1132 $link.addClass('csf-tabbed-active').siblings().removeClass('csf-tabbed-active');1133 $section.eq(index).removeClass('hidden').siblings().addClass('hidden');1134 });1135 });1136 };1137 //1138 // Field Backup1139 //1140 $.fn.csf_field_backup = function() {1141 return this.each( function() {1142 var $this = $(this),1143 $reset = $this.find('.csf-reset-js'),1144 $import = $this.find('.csf-import-js'),1145 data = $this.find('.csf-data').data();1146 $reset.on( 'click', function( e ) {1147 $('.csf-options').addClass('csf-saving');1148 e.preventDefault();1149 $.ajax({1150 type: 'POST',1151 url: ajaxurl,1152 data: {1153 action: 'csf-reset-options',1154 unique: data.unique,1155 wpnonce: data.wpnonce1156 },1157 success: function() {1158 location.reload();1159 }1160 });1161 });1162 $import.on( 'click', function( e ) {1163 $('.csf-options').addClass('csf-saving');1164 e.preventDefault();1165 $.ajax({1166 type: 'POST',1167 url: ajaxurl,1168 data: {1169 action: 'csf-import-options',1170 unique: data.unique,1171 wpnonce: data.wpnonce,1172 value: $this.find('.csf-import-data').val()1173 },1174 success: function( content ) {1175 location.reload();1176 }1177 });1178 });1179 });1180 };1181 //1182 // Confirm1183 //1184 $.fn.csf_confirm = function() {1185 return this.each( function() {1186 $(this).on('click', function( e ) {1187 if ( !confirm('Are you sure?') ) {1188 e.preventDefault();1189 }1190 });1191 });1192 };1193 //1194 // Options Save1195 //1196 $.fn.csf_save = function() {1197 return this.each( function() {1198 var $this = $(this),1199 $text = $this.data('save'),1200 $value = $this.val(),1201 $ajax = $('.csf-save-ajax'),1202 $panel = $('.csf-options');1203 $(document).on('keydown', function(event) {1204 if (event.ctrlKey || event.metaKey) {1205 if( String.fromCharCode(event.which).toLowerCase() === 's' ) {1206 event.preventDefault();1207 $this.trigger('click');1208 }1209 }1210 });1211 $this.on('click', function ( e ) {1212 if( $ajax.length ) {1213 if( typeof tinyMCE === 'object' ) {1214 tinyMCE.triggerSave();1215 }1216 $panel.addClass('csf-saving');1217 $this.prop('disabled', true).attr('value', $text);1218 var serializedOptions = $('#CSF_form').serialize();1219 $.post( 'options.php', serializedOptions ).error( function() {1220 alert('Error, Please try again.');1221 }).success( function() {1222 $panel.removeClass('csf-saving');1223 $this.prop('disabled', false).attr('value', $value);1224 });1225 e.preventDefault();1226 } else {1227 $this.addClass('disabled').attr('value', $text);1228 }1229 });1230 });1231 };1232 //1233 // Taxonomy Framework1234 //1235 $.fn.csf_taxonomy = function() {1236 return this.each( function() {1237 var $this = $(this),1238 $parent = $this.parent();1239 if( $parent.attr('id') === 'addtag' ) {1240 var $submit = $parent.find('#submit'),1241 $clone = $this.find('.csf-field').csf_clone(),1242 $list = $('#the-list'),1243 flooding = false;1244 $submit.on( 'click', function() {1245 if( !flooding ) {1246 $list.on( 'DOMNodeInserted', function() {1247 if( flooding ) {1248 $this.empty();1249 $this.html( $clone );1250 $clone = $clone.csf_clone();1251 $this.csf_reload_script();1252 flooding = false;1253 }1254 });1255 }1256 flooding = true;1257 });1258 }1259 });1260 };1261 //1262 // Shortcode Framework1263 //1264 $.fn.csf_shortcode = function() {1265 var instance = this, deploy_atts;1266 instance.validate_atts = function( _atts, _this ) {1267 var el_value;1268 if( _this.data('check') !== undefined && deploy_atts === _atts ) { return ''; }1269 deploy_atts = _atts;1270 if ( _this.closest('.pseudo-field').hasClass('hidden') === true ) { return ''; }1271 if ( _this.hasClass('pseudo') === true ) { return ''; }1272 if( _this.is(':checkbox') || _this.is(':radio') ) {1273 el_value = _this.is(':checked') ? _this.val() : '';1274 } else {1275 el_value = _this.val();1276 }1277 if( _this.data('check') !== undefined ) {1278 el_value = _this.closest('.csf-field').find('input:checked').map( function() {1279 return $(this).val();1280 }).get();1281 }1282 if( el_value !== null && el_value !== undefined && el_value !== '' && el_value.length !== 0 ) {1283 return ' ' + _atts + '="' + el_value + '"';1284 }1285 return '';1286 };1287 instance.insertAtChars = function ( _this, currentValue ) {1288 var obj = ( typeof _this[0].name !== 'undefined' ) ? _this[0] : _this;1289 if ( obj.value.length && typeof obj.selectionStart !== 'undefined' ) {1290 obj.focus();1291 return obj.value.substring( 0, obj.selectionStart ) + currentValue + obj.value.substring( obj.selectionEnd, obj.value.length );1292 } else {1293 obj.focus();1294 return currentValue;1295 }1296 };1297 instance.send_to_editor = function ( html, editor_id ) {1298 var tinymce_editor;1299 if ( typeof tinymce !== 'undefined' ) {1300 tinymce_editor = tinymce.get( editor_id );1301 }1302 if ( tinymce_editor && !tinymce_editor.isHidden() ) {1303 tinymce_editor.execCommand( 'mceInsertContent', false, html );1304 } else {1305 var $editor = $('#'+editor_id);1306 $editor.val( instance.insertAtChars( $editor, html ) ).trigger('change');1307 }1308 };1309 return this.each( function() {1310 var $this = $(this),1311 $content = $this.find('.csf-modal-content'),1312 $insert = $this.find('.csf-modal-insert'),1313 $select = $this.find('select'),1314 modal_id = $this.data('modal-id'),1315 editor_id,1316 sc_name,1317 sc_view,1318 sc_clone,1319 $sc_elem;1320 $(document).on('click', '.csf-shortcode-button[data-modal-button-id="'+ modal_id +'"]', function ( e ) {1321 var $button = $(this);1322 e.preventDefault();1323 $sc_elem = $button;1324 editor_id = $button.data('editor-id') || false;1325 $this.show();1326 $body.addClass('csf-shortcode-scrolling');1327 });1328 $select.on( 'change', function() {1329 var $elem = $(this);1330 sc_name = $elem.val();1331 sc_view = $elem.find(':selected').data('view');1332 if( sc_name.length ){1333 $.ajax({1334 type: 'POST',1335 url: ajaxurl,1336 data: {1337 action: 'csf-get-shortcode-'+modal_id,1338 shortcode: sc_name1339 },1340 success: function( content ) {1341 $content.html( content );1342 $insert.parent().removeClass('hidden');1343 sc_clone = $('.csf-shortcode-clone', $this).csf_clone();1344 $content.csf_reload_script('sub');1345 }1346 });1347 } else {1348 $insert.parent().addClass('hidden');1349 $content.html('');1350 }1351 });1352 $insert.on('click', function ( e ) {1353 e.preventDefault();1354 var shortcode = '',1355 ruleAttr = 'data-atts',1356 cloneAttr = 'data-clone-atts',1357 cloneID = 'data-clone-id';1358 switch ( sc_view ) {1359 case 'contents':1360 $this.find('[' + ruleAttr + ']').each( function() {1361 var _this = $(this), _atts = _this.data('atts');1362 shortcode += '['+_atts+']';1363 shortcode += _this.val();1364 shortcode += '[/'+_atts+']';1365 });1366 break;1367 case 'clone':1368 shortcode += '[' + sc_name;1369 $('[' + ruleAttr + ']', $this.find('.csf-field:not(.hidden)') ).each( function() {1370 var _this_main = $(this), _this_main_atts = _this_main.data('atts');1371 shortcode += instance.validate_atts( _this_main_atts, _this_main );1372 });1373 shortcode += ']';1374 $this.find('[' + cloneID + ']').each( function() {1375 var _this_clone = $(this),1376 _clone_id = _this_clone.data('clone-id');1377 shortcode += '[' + _clone_id;1378 $('[' + cloneAttr + ']', _this_clone.find('.csf-field:not(.hidden)') ).each( function() {1379 var _this_multiple = $(this), _atts_multiple = _this_multiple.data('clone-atts');1380 if( _atts_multiple !== 'content' ) {1381 shortcode += instance.validate_atts( _atts_multiple, _this_multiple );1382 } else if ( _atts_multiple === 'content' ) {1383 shortcode += ']';1384 shortcode += _this_multiple.val();1385 shortcode += '[/'+_clone_id+'';1386 }1387 });1388 shortcode += ']';1389 });1390 shortcode += '[/' + sc_name + ']';1391 break;1392 case 'clone_duplicate':1393 $this.find('[' + cloneID + ']').each( function() {1394 var _this_clone = $(this),1395 _clone_id = _this_clone.data('clone-id');1396 shortcode += '[' + _clone_id;1397 $('[' + cloneAttr + ']', _this_clone.find('.csf-field:not(.hidden)') ).each( function() {1398 var _this_multiple = $(this),1399 _atts_multiple = _this_multiple.data('clone-atts');1400 if( _atts_multiple !== 'content' ) {1401 shortcode += instance.validate_atts( _atts_multiple, _this_multiple );1402 } else if ( _atts_multiple === 'content' ) {1403 shortcode += ']';1404 shortcode += _this_multiple.val();1405 shortcode += '[/'+_clone_id+'';1406 }1407 });1408 shortcode += ']';1409 });1410 break;1411 default:1412 shortcode += '[' + sc_name;1413 $('[' + ruleAttr + ']', $this.find('.csf-field:not(.hidden)') ).each( function() {1414 var _this = $(this), _atts = _this.data('atts');1415 if( _atts !== 'content' ) {1416 shortcode += instance.validate_atts( _atts, _this );1417 } else if ( _atts === 'content' ) {1418 shortcode += ']';1419 shortcode += _this.val();1420 shortcode += '[/'+sc_name+'';1421 }1422 });1423 shortcode += ']';1424 break;1425 }1426 if( !editor_id ) {1427 var $textarea = $sc_elem.next();1428 $textarea.val( instance.insertAtChars( $textarea, shortcode ) ).trigger('change');1429 } else {1430 instance.send_to_editor( shortcode, editor_id );1431 }1432 deploy_atts = null;1433 $this.hide();1434 $body.removeClass('csf-shortcode-scrolling');1435 });1436 $content.on('click', '.csf-clone-button', function( e ) {1437 e.preventDefault();1438 var $cloned = sc_clone.csf_clone().addClass('csf-shortcode-cloned');1439 $content.find('.csf-clone-button-wrapper').before( $cloned );1440 $cloned.find(':input').attr('name', '_nonce_' + $cloned.index());1441 $cloned.find('.csf-remove-clone').show().on('click', function( e ) {1442 $cloned.remove();1443 e.preventDefault();1444 });1445 // reloadPlugins1446 $cloned.csf_reload_script('sub');1447 });1448 $this.on('click', '.csf-modal-close, .csf-modal-overlay', function() {1449 $this.hide();1450 $body.removeClass('csf-shortcode-scrolling');1451 });1452 });1453 };1454 //1455 // Helper Tooltip1456 //1457 $.fn.csf_tooltip = function() {1458 return this.each(function() {1459 var $this = $(this),1460 $tooltip,1461 tooltip_left;1462 $this.on({1463 mouseenter: function () {1464 $tooltip = $( '<div class="csf-tooltip"></div>' ).html( $this.attr( 'data-title' ) ).appendTo('body');1465 tooltip_left = ( has_rtl ) ? ( $this.offset().left + 24 ) : ( $this.offset().left - $tooltip.outerWidth() );1466 $tooltip.css({1467 top: $this.offset().top - ( ( $tooltip.outerHeight() / 2 ) - 12 ),1468 left: tooltip_left,1469 });1470 },1471 mouseleave: function () {1472 if( $tooltip !== undefined ) {1473 $tooltip.remove();1474 }1475 }1476 });1477 });1478 };1479 //1480 // Customize Refresh1481 //1482 $.fn.csf_customizer_refresh = function() {1483 return this.each(function() {1484 var $this = $(this),1485 $complex = $this.closest('.csf-customize-complex');1486 $(document).trigger('csf-customizer-refresh', $this);1487 if( wp.customize === undefined || $complex.length === 0 ) { return; }1488 var $input = $complex.find(':input'),1489 $unique = $complex.data('unique-id'),1490 $option = $complex.data('option-id'),1491 obj = $input.serializeObjectCSF(),1492 data = ( !$.isEmptyObject(obj) ) ? obj[$unique][$option] : '';1493 wp.customize.control( $unique +'['+ $option +']' ).setting.set( data );1494 });1495 };1496 //1497 // Customize Listen Form Elements - CODEVZ EDITED DELAY1498 //1499 $.fn.csf_customizer_listen = function( has_closest ) {1500 return this.each(function() {1501 if( wp.customize === undefined ) { return; }1502 var $this = ( has_closest ) ? $(this).closest('.csf-customize-complex') : $(this),1503 $input = $this.find(':input'),1504 $unique = $this.data('unique-id'),1505 $option = $this.data('option-id'),1506 delay = ( $option.indexOf( '_css_' ) >= 0 ) ? 0 : 250;1507 if( $unique === undefined ) { return; }1508 $input.on('change keyup', CSF.helper.debounce( function() {1509 var obj = $this.find(':input').serializeObjectCSF();1510 var data = ( !$.isEmptyObject(obj) ) ? obj[$unique][$option] : '';1511 wp.customize.control( $unique +'['+ $option +']' ).setting.set( data );1512 }, 250 ) );1513 });1514 };1515 //1516 // Customizer Listener for Reload JS1517 //1518 $(document).on( 'expanded', '.control-section-csf', function() {1519 var $this = $(this);1520 if( !$this.data('inited') ) {1521 $this.csf_reload_script();1522 $this.find('.csf-customize-complex').csf_customizer_listen();1523 }1524 });1525 //1526 // Widgets Framework1527 //1528 $.fn.csf_widgets = function() {1529 return this.each(function() {1530 var $this = $(this),1531 $widgets = $this.find('.widget-liquid-right .widget');1532 $widgets.each( function() {1533 var $widget = $(this),1534 $title = $widget.find('.widget-top');1535 $title.on('click', function() {1536 $widget.csf_reload_script();1537 });1538 });1539 });1540 };1541 //1542 // Widget Listener for Reload JS1543 //1544 $(document).on( 'widget-added widget-updated', function( event, $widget ) {1545 $widget.data( 'inited', false ); // CODEVZ1546 $widget.csf_reload_script();1547 });1548 //1549 // Reload Widget Plugins1550 //1551 $.fn.csf_reload_script = function( has_sub ) {1552 return this.each(function() {1553 var $this = $(this);1554 // Avoid for conflicts1555 if( !$this.data('inited') ) {1556 $this.find('.csf-field-image-selector').not('.csf-no-script').csf_field_image_selector();1557 $this.find('.csf-field-image').not('.csf-no-script').csf_field_image();1558 $this.find('.csf-field-gallery').not('.csf-no-script').csf_field_gallery();1559 //$this.find('.csf-field-sorter').not('.csf-no-script').csf_field_sorter();1560 $this.find('.csf-field-upload').not('.csf-no-script').csf_field_upload();1561 $this.find('.csf-field-color_picker').not('.csf-no-script').csf_field_colorpicker();1562 $this.find('.csf-field-icon').not('.csf-no-script').csf_field_icon();1563 $this.find('.csf-field-select_font').not('.csf-no-script').csf_field_select_font();1564 $this.find('.csf-field-demo_importer').not('.csf-no-script').csf_field_demo_importer();1565 $this.find('.csf-field-header-preset').not('.csf-no-script').csf_field_header_preset();1566 $this.find('.csf-field-group').not('.csf-no-script').csf_field_group();1567 //$this.find('.csf-field-repeater').not('.csf-no-script').csf_field_repeater();1568 //$this.find('.csf-field-ace_editor').not('.csf-no-script').csf_field_ace_editor();1569 //$this.find('.csf-field-date').not('.csf-no-script').csf_field_datepicker();1570 //$this.find('.csf-field-tabbed').not('.csf-no-script').csf_field_tabbed();1571 $this.find('.csf-field-backup').not('.csf-no-script').csf_field_backup();1572 $this.find('.csf-help').not('.csf-no-script').csf_tooltip();1573 //$this.find('.chosen').not('.csf-no-script').csf_chosen();1574 $this.csf_dependency();1575 if( has_sub === 'sub' ) {1576 $this.csf_dependency('sub');1577 }1578 $this.data('inited', true);1579 $(document).trigger('csf-reload-script', [ $this ]);1580 }1581 });1582 };1583 $(document).ready( function() {1584 $('.csf-save').csf_save();1585 $('.csf-confirm').csf_confirm();1586 $('.csf-nav').csf_navigation();1587 $('.csf-search').csf_search();1588 $('.csf-sticky-header').csf_sticky();1589 $('.csf-taxonomy').csf_taxonomy();1590 //$('.csf-shortcode').csf_shortcode();1591 $('.widgets-php').csf_widgets();1592 $('.csf-onload').csf_reload_script();1593 });...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1const { configure, addDecorator } = require('@storybook/react');2const { withKnobs } = require('@storybook/addon-knobs');3const { withA11y } = require('@storybook/addon-a11y');4const { withInfo } = require('@storybook/addon-info');5addDecorator(withKnobs);6addDecorator(withA11y);7addDecorator(withInfo);8configure(require.context('../stories', true, /\.stories\.js$/), module);9const path = require('path');10const webpackConfig = require('../webpack.config.js');11module.exports = (storybookBaseConfig, configType) => {12 storybookBaseConfig.resolve = webpackConfig.resolve;13 storybookBaseConfig.module.rules = webpackConfig.module.rules;14 return storybookBaseConfig;15};16const webpack = require('webpack');17const path = require('path');18const HtmlWebpackPlugin = require('html-webpack-plugin');19const CleanWebpackPlugin = require('clean-webpack-plugin');20const CopyWebpackPlugin = require('copy-webpack-plugin');21const ExtractTextPlugin = require('extract-text-webpack-plugin');22const autoprefixer = require('autoprefixer');23const NODE_ENV = process.env.NODE_ENV || 'development';24const PATHS = {25 source: path.join(__dirname, 'src'),26 build: path.join(__dirname, 'build'),27};28 new HtmlWebpackPlugin({29 template: path.join(PATHS.source, 'index.html'),30 }),31 new CleanWebpackPlugin([PATHS.build]),32 new CopyWebpackPlugin([33 {34 from: path.join(PATHS.source, 'assets'),35 to: path.join(PATHS.build, 'assets'),36 },37 new webpack.DefinePlugin({38 NODE_ENV: JSON.stringify(NODE_ENV),39 }),40 new webpack.NoEmitOnErrorsPlugin(),41 new ExtractTextPlugin('styles.css'),42];43const config = {44 entry: {45 app: path.join(PATHS.source, 'index.js'),46 },47 output: {48 },49 resolve: {50 modules: ['node_modules', path.resolve(__dirname, 'src')],51 },

Full Screen

Using AI Code Generation

copy

Full Screen

1import { csf } from 'storybook-root'2import { csf } from 'storybook-root'3import { csf } from 'storybook-root'4import { csf } from 'storybook-root'5import { csf } from 'storybook-root'6import { csf } from 'storybook-root'7import { csf } from 'storybook-root'8import { csf } from 'storybook-root'9import { csf } from 'storybook-root'10import { csf } from 'storybook-root'11import { csf } from 'storybook-root'12import { csf } from 'storybook-root'13import { csf } from 'storybook-root'14import { csf } from 'storybook-root'15import { csf } from 'storybook-root'16import { csf } from 'storybook-root'17import { csf } from 'storybook-root'18import { csf } from 'storybook-root'19import { csf } from 'storybook-root'20import { csf } from 'storybook-root'

Full Screen

Using AI Code Generation

copy

Full Screen

1import { csf } from 'storybook-root';2export default csf('Hello World', module)3 .add('Hello', () => <div>Hello</div>)4 .add('World', () => <div>World</div>);5csf('Hello World', module)6 .add('Hello', () => <div>Hello</div>)7 .add('World', () => <div>World</div>);8csf('Hello World', module)9 .add('Hello', () => <div>Hello</div>)10 .add('World', () => <div>World</div>);11csf('Hello World', module)12 .add('Hello', () => <div>Hello</div>)13 .add('World', () => <div>World</div>);14csf('Hello World', module)15 .add('Hello', () => <div>Hello</div>)16 .add('World', () => <div>World</div>);17csf('Hello World', module)18 .add('Hello', () => <div>Hello</div>)19 .add('World', () => <div>World</div>);20csf('Hello World', module)21 .add('Hello', () => <div>Hello</div>)22 .add('World', () => <div>World</div>);23csf('Hello World', module)24 .add('Hello', () => <div>Hello</div>)25 .add('World', () => <div>World</div>);26csf('Hello World', module)27 .add('Hello', () => <div>Hello</div>)28 .add('World', () => <div>World</div>);29csf('Hello World', module)30 .add('Hello', () => <div>Hello</div>)31 .add('World', () => <div>World</div>);32csf('Hello World', module)33 .add('Hello', () => <div>Hello</div>)34 .add('World', () => <div>World</div>);35csf('Hello World', module)36 .add('Hello', () => <div>Hello</div>)37 .add('World', () => <div>World</div>);38csf('Hello World', module)

Full Screen

Using AI Code Generation

copy

Full Screen

1import { storiesOf } from 'storybook-root';2storiesOf('test', module)3 .add('test1', () => (4 .add('test2', () => (5 ));6storiesOf('test', module)7 .add('test3', () => (8 .add('test4', () => (9 ));10import { storiesOf } from 'storybook-root';11storiesOf('test', module)12 .add('test5', () => (13 .add('test6', () => (14 ));15storiesOf('test', module)16 .add('test7', () => (17 .add('test8', () => (18 ));19import { storiesOf } from 'storybook-root';20storiesOf('test', module)21 .add('test9', () => (22 .add('test10', () => (23 ));24storiesOf('test', module)25 .add('test11', () => (26 .add('test12', () => (27 ));28import { storiesOf } from 'storybook-root';

Full Screen

Using AI Code Generation

copy

Full Screen

1import { csf } from 'storybook-addon-csf';2export default csf('MyComponent', {3 subcomponents: {4 }5});6import { csf } from '@storybook/react';7export default csf('MyComponent', {8 subcomponents: {9 }10});11import { csf } from '@storybook/vue';12export default csf('MyComponent', {13 subcomponents: {14 }15});16import { csf } from '@storybook/angular';17export default csf('MyComponent', {18 subcomponents: {19 }20});21import { csf } from '@storybook/web-components';22export default csf('MyComponent', {23 subcomponents: {24 }25});26import { csf } from '@storybook/html';27export default csf('MyComponent', {28 subcomponents: {29 }30});31import { csf } from '@storybook/svelte';32export default csf('MyComponent', {33 subcomponents: {34 }35});36import { csf } from '@storybook/ember';37export default csf('MyComponent', {38 subcomponents: {39 }40});41import { csf } from '@storybook/preact';42export default csf('MyComponent', {

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run storybook-root automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful