/** * aspb Fields JS * * JS functionalities for some of the default fields */ jQuery.noConflict(); /** Fire up jQuery - let's dance! */ jQuery(document).ready(function($){ /** Colorpicker Field ----------------------------------------------- */ function aspb_colorpicker() { $('#page-builder .input-color-picker').each(function(){ var $this = $(this), parent = $this.parent(); $this.wpColorPicker(); }); } aspb_colorpicker(); $('ul.blocks').bind('sortstop', function() { aspb_colorpicker(); }); /** Media Uploader ----------------------------------------------- */ $(document).on('click', '.as_upload_button', function(event) { var $clicked = $(this), frame, input_id = $clicked.prev().attr('id'), media_type = $clicked.attr('rel'); event.preventDefault(); // If the media frame already exists, reopen it. if ( frame ) { frame.open(); return; } // Create the media frame. frame = wp.media.frames.as_media_uploader = wp.media({ // Set the media type library: { type: media_type }, view: { } }); // When an image is selected, run a callback. frame.on( 'select', function() { // Grab the selected attachment. var attachment = frame.state().get('selection').first(); $('#' + input_id).val(attachment.attributes.url); if(media_type == 'image') $('#' + input_id).parent().parent().parent().find('.screenshot img').attr('src', attachment.attributes.url); }); frame.open(); }); /** Sortable Lists ----------------------------------------------- */ // AJAX Add New function as_sortable_list_add_item(action_id, items) { var blockID = items.attr('rel'), numArr = items.find('li').map(function(i, e){ return $(e).attr("rel"); }); var maxNum = Math.max.apply(Math, numArr); if (maxNum < 1 ) { maxNum = 0}; var newNum = maxNum + 1; var data = { action: 'as_block_'+action_id+'_add_new', security: $('#aspb-nonce').val(), count: newNum, block_id: blockID }; $.post(ajaxurl, data, function(response) { var check = response.charAt(response.length - 1); //check nonce if(check == '-1') { alert('An unknown error has occurred'); } else { items.append(response); } }); }; // Initialise sortable list fields function as_sortable_list_init() { $('.as-sortable-list').sortable({ containment: "parent", placeholder: "ui-state-highlight" }); } as_sortable_list_init(); $('ul.blocks').bind('sortstop', function() { as_sortable_list_init(); }); $(document).on('click', 'a.as-sortable-add-new', function() { var action_id = $(this).attr('rel'), items = $(this).parent().children('ul.as-sortable-list'); as_sortable_list_add_item(action_id, items); as_sortable_list_init return false; }); // Delete Sortable Item $(document).on('click', '.as-sortable-list a.sortable-delete', function() { var $parent = $(this.parentNode.parentNode.parentNode); $parent.children('.block-tabs-tab-head').css('background', 'red'); $parent.slideUp(function() { $(this).remove(); }).fadeOut('fast'); return false; }); // Open/Close Sortable Item $(document).on('click', '.as-sortable-list .sortable-handle a', function() { var $clicked = $(this); $clicked.addClass('sortable-clicked'); $clicked.parents('.as-sortable-list').find('.sortable-body').each(function(i, el) { if($(el).is(':visible') && $(el).prev().find('a').hasClass('sortable-clicked') == false) { $(el).slideUp(); } }); $(this.parentNode.parentNode.parentNode).children('.sortable-body').slideToggle(); $clicked.removeClass('sortable-clicked'); return false; }); // Dialog Modal var current_input = ""; $(document).on('click', '.show-icon', function(e) { current_input = $(this); ShowDialog(false); e.preventDefault(); }); $("#btnShowModal").click(function (e) { ShowDialog(true); e.preventDefault(); }); $("#btnClose").click(function (e) { HideDialog(); e.preventDefault(); }); $("#btnSubmit").click(function (e) { var brand = $("#brands input:radio:checked").val(); $(current_input).val(brand); HideDialog(); e.preventDefault(); }); function ShowDialog(modal) { $("#overlay").show(); $("#dialog").fadeIn(300); if (modal) { $("#overlay").unbind("click"); } else { $("#overlay").click(function (e) { HideDialog(); }); } } function HideDialog() { $("#overlay").hide(); $("#dialog").fadeOut(300); } }); /******************************************** *** Alpha Opacity *********************************************/ jQuery(document).ready(function($) { Color.prototype.toString = function(remove_alpha) { if (remove_alpha == 'no-alpha') { return this.toCSS('rgba', '1').replace(/\s+/g, ''); } 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 ''; if (hex.length < 6) { for (var i = 6 - hex.length - 1; i >= 0; i--) { hex = '0' + hex; } } return '#' + hex; }; $('.aakanksha-color-control').each(function() { var $control = $(this), value = $control.val().replace(/\s+/g, ''); // Manage Palettes /*var palette_input = $control.attr('data-palette'); if (palette_input == 'false' || palette_input == false) { var palette = false; } else if (palette_input == 'true' || palette_input == true) { var palette = true; } else { var palette = $control.attr('data-palette').split(","); }*/ $control.wpColorPicker({ // change some things with the color picker clear: function(event, ui) { // TODO reset Alpha Slider to 100 }, change: function(event, ui) { // send ajax request to wp.customizer to enable Save & Publish button var _new_value = $control.val(); var key = $control.attr('name'); //$control.val() = _new_value; /*wp.customize(key, function(obj) { obj.set(_new_value); });*/ // change the background color of our transparency container whenever a color is updated var $transparency = $control.parents('.wp-picker-container:first').find('.transparency'); // we only want to show the color at 100% alpha $transparency.css('backgroundColor', ui.color.toString('no-alpha')); }, //palettes: palette // remove the color palettes }); $('
').appendTo($control.parents('.wp-picker-container')); var $alpha_slider = $control.parents('.wp-picker-container:first').find('.slider-alpha'); // if in format RGBA - grab A channel value if (value.match(/rgba\(\d+\,\d+\,\d+\,([^\)]+)\)/)) { var alpha_val = parseFloat(value.match(/rgba\(\d+\,\d+\,\d+\,([^\)]+)\)/)[1]) * 100; var alpha_val = parseInt(alpha_val); } else { var alpha_val = 100; } $alpha_slider.slider({ slide: function(event, ui) { $(this).find('.ui-slider-handle').text(ui.value); // show value on slider handle // send ajax request to wp.customizer to enable Save & Publish button var _new_value = $control.val(); var key = $control.attr('name'); /*wp.customize(key, function(obj) { obj.set(_new_value); });*/ }, create: function(event, ui) { var v = $(this).slider('value'); $(this).find('.ui-slider-handle').text(v); }, value: alpha_val, range: "max", step: 1, min: 1, max: 100 }); // slider $alpha_slider.slider().on('slidechange', function(event, ui) { var new_alpha_val = parseFloat(ui.value), iris = $control.data('a8cIris'), color_picker = $control.data('wpWpColorPicker'); iris._color._alpha = new_alpha_val / 100.0; $control.val(iris._color.toString()); color_picker.toggler.css({ backgroundColor: $control.val() }); // fix relationship between alpha slider and the 'side slider not updating. var get_val = $control.val(); $($control).wpColorPicker('color', get_val); }); }); });