/** * * ----------------------------------------------------------- * * ShapedPlugin Framework * A Lightweight and easy-to-use WordPress Options Framework * * Copyright 2017 www.shapedplugin.com * * ----------------------------------------------------------- * */ ;(function ( $, window, document, undefined ) { 'use strict'; $.SPFRAMEWORK = $.SPFRAMEWORK || {}; // caching selector var $sp_body = $('body'); // caching variables var sp_is_rtl = $sp_body.hasClass('rtl'); // ====================================================== // SPFRAMEWORK TAB NAVIGATION // ------------------------------------------------------ $.fn.SPFRAMEWORK_TAB_NAVIGATION = function() { return this.each(function() { var $this = $(this), $nav = $this.find('.sp-nav'), $reset = $this.find('.sp-reset'), $expand = $this.find('.sp-expand-all'); $nav.find('ul:first a').on('click', function (e) { e.preventDefault(); var $el = $(this), $next = $el.next(), $target = $el.data('section'); if( $next.is('ul') ) { $next.slideToggle( 'fast' ); $el.closest('li').toggleClass('sp-tab-active'); } else { $('#sp-tab-'+$target).fadeIn('fast').siblings().hide(); $nav.find('a').removeClass('sp-section-active'); $el.addClass('sp-section-active'); $reset.val($target); } }); $expand.on('click', function (e) { e.preventDefault(); $this.find('.sp-body').toggleClass('sp-show-all'); $(this).find('.fa').toggleClass('fa-eye-slash' ).toggleClass('fa-eye'); }); }); }; // ====================================================== // ====================================================== // SPFRAMEWORK CHOSEN // ------------------------------------------------------ $.fn.SPFRAMEWORK_CHOSEN = function() { return this.each(function() { $(this).chosen({allow_single_deselect: true, disable_search_threshold: 15, width: 150 + 25 +'px'}); }); }; // ====================================================== // ====================================================== // SPFRAMEWORK RESET CONFIRM // ------------------------------------------------------ $.fn.SPFRAMEWORK_CONFIRM = function() { return this.each( function() { $(this).on('click', function( e ) { if ( !confirm('Are you sure?') ) { e.preventDefault(); } }); }); }; // ====================================================== // ====================================================== // SPFRAMEWORK SAVE OPTIONS // ------------------------------------------------------ $.fn.SPFRAMEWORK_SAVE = function() { return this.each( function() { var $this = $(this), $text = $this.data('save'), $value = $this.val(), $ajax = $('#sp-save-ajax'); $(document).on('keydown', function(event) { if (event.ctrlKey || event.metaKey) { if( String.fromCharCode(event.which).toLowerCase() === 's' ) { event.preventDefault(); $this.trigger('click'); } } }); $this.on('click', function ( e ) { if( $ajax.length ) { if( typeof tinyMCE === 'object' ) { tinyMCE.triggerSave(); } $this.prop('disabled', true).attr('value', $text); var serializedOptions = $('#sp_framework_form').serialize(); $.post( 'options.php', serializedOptions ).error( function() { alert('Error, Please try again.'); }).success( function() { $this.prop('disabled', false).attr('value', $value); $ajax.hide().fadeIn().delay(250).fadeOut(); }); e.preventDefault(); } else { $this.addClass('disabled').attr('value', $text); } }); }); }; // ====================================================== // ====================================================== // SPFRAMEWORK UI DIALOG OVERLAY HELPER // ------------------------------------------------------ if( typeof $.widget !== 'undefined' && typeof $.ui !== 'undefined' && typeof $.ui.dialog !== 'undefined' ) { $.widget( 'ui.dialog', $.ui.dialog, { _createOverlay: function() { this._super(); if ( !this.options.modal ) { return; } this._on(this.overlay, {click: 'close'}); } } ); } // ====================================================== // SPFRAMEWORK COLORPICKER // ------------------------------------------------------ if( typeof Color === 'function' ) { // adding alpha support for Automattic Color.js toString function. Color.fn.toString = function () { // check for alpha if ( this._alpha < 1 ) { return this.toCSS('rgba', this._alpha).replace(/\s+/g, ''); } var hex = parseInt( this._color, 10 ).toString( 16 ); if ( this.error ) { return ''; } // maybe left pad it if ( hex.length < 6 ) { for (var i = 6 - hex.length - 1; i >= 0; i--) { hex = '0' + hex; } } return '#' + hex; }; } $.SPFRAMEWORK.PARSE_COLOR_VALUE = function( val ) { var value = val.replace(/\s+/g, ''), alpha = ( value.indexOf('rgba') !== -1 ) ? parseFloat( value.replace(/^.*,(.+)\)/, '$1') * 100 ) : 100, rgba = ( alpha < 100 ) ? true : false; return { value: value, alpha: alpha, rgba: rgba }; }; $.fn.SPFRAMEWORK_COLORPICKER = function() { return this.each(function() { var $this = $(this); // check for rgba enabled/disable if( $this.data('rgba') !== false ) { // parse value var picker = $.SPFRAMEWORK.PARSE_COLOR_VALUE( $this.val() ); // wpColorPicker core $this.wpColorPicker({ // wpColorPicker: clear clear: function() { $this.trigger('keyup'); }, // wpColorPicker: change change: function( event, ui ) { var ui_color_value = ui.color.toString(); // update checkerboard background color $this.closest('.wp-picker-container').find('.sp-alpha-slider-offset').css('background-color', ui_color_value); $this.val(ui_color_value).trigger('change'); }, // wpColorPicker: create create: function() { // set variables for alpha slider var a8cIris = $this.data('a8cIris'), $container = $this.closest('.wp-picker-container'), // appending alpha wrapper $alpha_wrap = $('
' + '
' + '
' + '
' + '
').appendTo( $container.find('.wp-picker-holder') ), $alpha_slider = $alpha_wrap.find('.sp-alpha-slider'), $alpha_text = $alpha_wrap.find('.sp-alpha-text'), $alpha_offset = $alpha_wrap.find('.sp-alpha-slider-offset'); // alpha slider $alpha_slider.slider({ // slider: slide slide: function( event, ui ) { var slide_value = parseFloat( ui.value / 100 ); // update iris data alpha && wpColorPicker color option && alpha text a8cIris._color._alpha = slide_value; $this.wpColorPicker( 'color', a8cIris._color.toString() ); $alpha_text.text( ( slide_value < 1 ? slide_value : '' ) ); }, // slider: create create: function() { var slide_value = parseFloat( picker.alpha / 100 ), alpha_text_value = slide_value < 1 ? slide_value : ''; // update alpha text && checkerboard background color $alpha_text.text(alpha_text_value); $alpha_offset.css('background-color', picker.value); // wpColorPicker clear for update iris data alpha && alpha text && slider color option $container.on('click', '.wp-picker-clear', function() { a8cIris._color._alpha = 1; $alpha_text.text(''); $alpha_slider.slider('option', 'value', 100).trigger('slide'); }); // wpColorPicker default button for update iris data alpha && alpha text && slider color option $container.on('click', '.wp-picker-default', function() { var default_picker = $.SPFRAMEWORK.PARSE_COLOR_VALUE( $this.data('default-color') ), default_value = parseFloat( default_picker.alpha / 100 ), default_text = default_value < 1 ? default_value : ''; a8cIris._color._alpha = default_value; $alpha_text.text(default_text); $alpha_slider.slider('option', 'value', default_picker.alpha).trigger('slide'); }); // show alpha wrapper on click color picker button $container.on('click', '.wp-color-result', function() { $alpha_wrap.toggle(); }); // hide alpha wrapper on click body $sp_body.on( 'click.wpcolorpicker', function() { $alpha_wrap.hide(); }); }, // slider: options value: picker.alpha, step: 1, min: 1, max: 100 }); } }); } else { // wpColorPicker default picker $this.wpColorPicker({ clear: function() { $this.trigger('keyup'); }, change: function( event, ui ) { $this.val(ui.color.toString()).trigger('change'); } }); } }); }; // ====================================================== // ====================================================== // ON WIDGET-ADDED RELOAD FRAMEWORK PLUGINS // ------------------------------------------------------ $.SPFRAMEWORK.WIDGET_RELOAD_PLUGINS = function() { $(document).on('widget-added widget-updated', function( event, $widget ) { $widget.SPFRAMEWORK_RELOAD_PLUGINS(); }); }; // ====================================================== // TOOLTIP HELPER // ------------------------------------------------------ $.fn.SPFRAMEWORK_TOOLTIP = function() { return this.each(function() { var placement = ( sp_is_rtl ) ? 'right' : 'left'; $(this).sptooltip({html:true, placement:placement, container:'body'}); }); }; // ====================================================== // RELOAD FRAMEWORK PLUGINS // ------------------------------------------------------ $.fn.SPFRAMEWORK_RELOAD_PLUGINS = function() { return this.each(function() { $('.chosen', this).SPFRAMEWORK_CHOSEN(); $('.sp-field-color-picker', this).SPFRAMEWORK_COLORPICKER(); $('.sp-help', this).SPFRAMEWORK_TOOLTIP(); }); }; // ====================================================== // JQUERY DOCUMENT READY // ------------------------------------------------------ $(document).ready( function() { $('.sp-advp-framework').SPFRAMEWORK_TAB_NAVIGATION(); $('.sp-reset-confirm, .sp-import-backup').SPFRAMEWORK_CONFIRM(); $('.sp-save').SPFRAMEWORK_SAVE(); $sp_body.SPFRAMEWORK_RELOAD_PLUGINS(); $.SPFRAMEWORK.WIDGET_RELOAD_PLUGINS(); }); })( jQuery, window, document );