diff --git a/dist/wp-color-picker-alpha.min.js b/dist/wp-color-picker-alpha.min.js old mode 100755 new mode 100644 index db88dc7..4f6df11 --- a/dist/wp-color-picker-alpha.min.js +++ b/dist/wp-color-picker-alpha.min.js @@ -8,4 +8,4 @@ * https://github.com/kallookoo/wp-color-picker-alpha * Licensed under the GPLv2 license or later. */ -!function(t){if(!t.wp.wpColorPicker.prototype._hasAlpha){var o="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAIAAAHnlligAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAHJJREFUeNpi+P///4EDBxiAGMgCCCAGFB5AADGCRBgYDh48CCRZIJS9vT2QBAggFBkmBiSAogxFBiCAoHogAKIKAlBUYTELAiAmEtABEECk20G6BOmuIl0CIMBQ/IEMkO0myiSSraaaBhZcbkUOs0HuBwDplz5uFJ3Z4gAAAABJRU5ErkJggg==",r='
',e='',a='',i=void 0!==wpColorPickerL10n.current;if(i)var n='';else{n='';var l="",s=''}Color.fn.toString=function(){if(this._alpha<1)return this.toCSS("rgba",this._alpha).replace(/\s+/g,"");var t=parseInt(this._color,10).toString(16);return this.error?"":(t.length<6&&(t=("00000"+t).substr(-6)),"#"+t)},t.widget("wp.wpColorPicker",t.wp.wpColorPicker,{_hasAlpha:!0,_create:function(){if(t.support.iris){var p=this,c=p.element;if(t.extend(p.options,c.data()),"hue"===p.options.type)return p._createHueOnly();p.close=t.proxy(p.close,p),p.initialValue=c.val(),c.addClass("wp-color-picker"),i?(c.hide().wrap(e),p.wrap=c.parent(),p.toggler=t(n).insertBefore(c).css({backgroundColor:p.initialValue}).attr("title",wpColorPickerL10n.pick).attr("data-current",wpColorPickerL10n.current),p.pickerContainer=t(r).insertAfter(c),p.button=t(a).addClass("hidden")):(c.parent("label").length||(c.wrap(l),p.wrappingLabelText=t(s).insertBefore(c).text(wpColorPickerL10n.defaultLabel)),p.wrappingLabel=c.parent(),p.wrappingLabel.wrap(e),p.wrap=p.wrappingLabel.parent(),p.toggler=t(n).insertBefore(p.wrappingLabel).css({backgroundColor:p.initialValue}),p.toggler.find(".wp-color-result-text").text(wpColorPickerL10n.pick),p.pickerContainer=t(r).insertAfter(p.wrappingLabel),p.button=t(a)),p.options.defaultColor?(p.button.addClass("wp-picker-default").val(wpColorPickerL10n.defaultString),i||p.button.attr("aria-label",wpColorPickerL10n.defaultAriaLabel)):(p.button.addClass("wp-picker-clear").val(wpColorPickerL10n.clear),i||p.button.attr("aria-label",wpColorPickerL10n.clearAriaLabel)),i?c.wrap('').after(p.button):(p.wrappingLabel.wrap('').after(p.button),p.inputWrapper=c.closest(".wp-picker-input-wrap")),c.iris({target:p.pickerContainer,hide:p.options.hide,width:p.options.width,mode:p.options.mode,palettes:p.options.palettes,change:function(r,e){p.options.alpha?(p.toggler.css({"background-image":"url("+o+")"}),i?p.toggler.html(''):(p.toggler.css({position:"relative"}),0==p.toggler.find("span.color-alpha").length&&p.toggler.append('')),p.toggler.find("span.color-alpha").css({width:"30px",height:"100%",position:"absolute",top:0,left:0,"border-top-left-radius":"2px","border-bottom-left-radius":"2px",background:e.color.toString()})):p.toggler.css({backgroundColor:e.color.toString()}),t.isFunction(p.options.change)&&p.options.change.call(this,r,e)}}),c.val(p.initialValue),p._addListeners(),p.options.hide||p.toggler.click()}},_addListeners:function(){var o=this;o.wrap.on("click.wpcolorpicker",function(t){t.stopPropagation()}),o.toggler.click(function(){o.toggler.hasClass("wp-picker-open")?o.close():o.open()}),o.element.on("change",function(r){(""===t(this).val()||o.element.hasClass("iris-error"))&&(o.options.alpha?(i&&o.toggler.removeAttr("style"),o.toggler.find("span.color-alpha").css("backgroundColor","")):o.toggler.css("backgroundColor",""),t.isFunction(o.options.clear)&&o.options.clear.call(this,r))}),o.button.on("click",function(r){t(this).hasClass("wp-picker-clear")?(o.element.val(""),o.options.alpha?(i&&o.toggler.removeAttr("style"),o.toggler.find("span.color-alpha").css("backgroundColor","")):o.toggler.css("backgroundColor",""),t.isFunction(o.options.clear)&&o.options.clear.call(this,r),o.element.trigger("change")):t(this).hasClass("wp-picker-default")&&o.element.val(o.options.defaultColor).change()})}}),t.widget("a8c.iris",t.a8c.iris,{_create:function(){if(this._super(),this.options.alpha=this.element.data("alpha")||!1,this.element.is(":input")||(this.options.alpha=!1),void 0!==this.options.alpha&&this.options.alpha){var o=this,r=o.element,e=t(' ').appendTo(o.picker.find(".iris-picker-inner")),a={aContainer:e,aSlider:e.find(".iris-slider-offset-alpha")};void 0!==r.data("custom-width")?o.options.customWidth=parseInt(r.data("custom-width"))||0:o.options.customWidth=100,o.options.defaultWidth=r.width(),(o._color._alpha<1||-1!=o._color.toString().indexOf("rgb"))&&r.width(parseInt(o.options.defaultWidth+o.options.customWidth)),t.each(a,function(t,r){o.controls[t]=r}),o.controls.square.css({"margin-right":"0"});var i=o.picker.width()-o.controls.square.width()-20,n=i/6,l=i/2-n;t.each(["aContainer","strip"],function(t,r){o.controls[r].width(l).css({"margin-left":n+"px"})}),o._initControls(),o._change()}},_initControls:function(){if(this._super(),this.options.alpha){var t=this;t.controls.aSlider.slider({orientation:"vertical",min:0,max:100,step:1,value:parseInt(100*t._color._alpha),slide:function(o,r){t._color._alpha=parseFloat(r.value/100),t._change.apply(t,arguments)}})}},_change:function(){this._super();var t=this,r=t.element;if(this.options.alpha){var e=t.controls,a=parseInt(100*t._color._alpha),i=t._color.toRgb(),n=["rgb("+i.r+","+i.g+","+i.b+") 0%","rgba("+i.r+","+i.g+","+i.b+", 0) 100%"],l=t.options.defaultWidth,s=t.options.customWidth,p=t.picker.closest(".wp-picker-container").find(".wp-color-result");e.aContainer.css({background:"linear-gradient(to bottom, "+n.join(", ")+"), url("+o+")"}),p.hasClass("wp-picker-open")&&(e.aSlider.slider("value",a),t._color._alpha<1?(e.strip.attr("style",e.strip.attr("style").replace(/rgba\(([0-9]+,)(\s+)?([0-9]+,)(\s+)?([0-9]+)(,(\s+)?[0-9\.]+)\)/g,"rgb($1$3$5)")),r.width(parseInt(l+s))):r.width(l))}(r.data("reset-alpha")||!1)&&t.picker.find(".iris-palette-container").on("click.palette",".iris-palette",function(){t._color._alpha=1,t.active="external",t._change()}),r.trigger("change")},_addInputListeners:function(t){var o=this,r=function(r){var e=new Color(t.val()),a=t.val();t.removeClass("iris-error"),e.error?""!==a&&t.addClass("iris-error"):e.toString()!==o._color.toString()&&("keyup"===r.type&&a.match(/^[0-9a-fA-F]{3}$/)||o._setOption("color",e.toString()))};t.on("change",r).on("keyup",o._debounce(r,100)),o.options.hide&&t.on("focus",function(){o.show()})}})}}(jQuery),jQuery(document).ready(function(t){t(".color-picker").wpColorPicker()}); +!function(t){if(!t.wp.wpColorPicker.prototype._hasAlpha){var o="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAIAAAHnlligAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAHJJREFUeNpi+P///4EDBxiAGMgCCCAGFB5AADGCRBgYDh48CCRZIJS9vT2QBAggFBkmBiSAogxFBiCAoHogAKIKAlBUYTELAiAmEtABEECk20G6BOmuIl0CIMBQ/IEMkO0myiSSraaaBhZcbkUOs0HuBwDplz5uFJ3Z4gAAAABJRU5ErkJggg==",r='',e='',a='',i='',l=!1;if("undefined"!=typeof wpColorPickerL10n){(l=void 0!==wpColorPickerL10n.current)&&(i='');var n=wpColorPickerL10n.defaultLabel,s=wpColorPickerL10n.pick,p=wpColorPickerL10n.defaultString,c=wpColorPickerL10n.defaultAriaLabel,d=wpColorPickerL10n.clear,g=wpColorPickerL10n.clearAriaLabel}else{var h=wp.i18n.__;n=h("Color value"),s=h("Select Color"),p=h("Default"),c=h("Select default color"),d=h("Clear"),g=h("Clear color")}Color.fn.toString=function(){if(this._alpha<1)return this.toCSS("rgba",this._alpha).replace(/\s+/g,"");var t=parseInt(this._color,10).toString(16);return this.error?"":(t.length<6&&(t=("00000"+t).substr(-6)),"#"+t)},t.widget("wp.wpColorPicker",t.wp.wpColorPicker,{_hasAlpha:!0,_create:function(){if(t.support.iris){var h=this,u=h.element;if(t.extend(h.options,u.data()),"hue"===h.options.type)return h._createHueOnly();h.close=t.proxy(h.close,h),h.initialValue=u.val(),u.addClass("wp-color-picker"),l?(u.hide().wrap(e),h.wrap=u.parent(),h.toggler=t(i).insertBefore(u).css({backgroundColor:h.initialValue}).attr("title",s).attr("data-current",wpColorPickerL10n.current),h.pickerContainer=t(r).insertAfter(u),h.button=t(a).addClass("hidden")):(u.parent("label").length||(u.wrap(""),h.wrappingLabelText=t('').insertBefore(u).text(n)),h.wrappingLabel=u.parent(),h.wrappingLabel.wrap(e),h.wrap=h.wrappingLabel.parent(),h.toggler=t(i).insertBefore(h.wrappingLabel).css({backgroundColor:h.initialValue}),h.toggler.find(".wp-color-result-text").text(s),h.pickerContainer=t(r).insertAfter(h.wrappingLabel),h.button=t(a)),h.options.defaultColor?(h.button.addClass("wp-picker-default").val(p),l||h.button.attr("aria-label",c)):(h.button.addClass("wp-picker-clear").val(d),l||h.button.attr("aria-label",g)),l?u.wrap('').after(h.button):(h.wrappingLabel.wrap('').after(h.button),h.inputWrapper=u.closest(".wp-picker-input-wrap")),u.iris({target:h.pickerContainer,hide:h.options.hide,width:h.options.width,mode:h.options.mode,palettes:h.options.palettes,change:function(r,e){h.options.alpha?(h.toggler.css({"background-image":"url("+o+")"}),l?h.toggler.html(''):(h.toggler.css({position:"relative"}),0==h.toggler.find("span.color-alpha").length&&h.toggler.append('')),h.toggler.find("span.color-alpha").css({width:"30px",height:"100%",position:"absolute",top:0,left:0,"border-top-left-radius":"2px","border-bottom-left-radius":"2px",background:e.color.toString()})):h.toggler.css({backgroundColor:e.color.toString()}),t.isFunction(h.options.change)&&h.options.change.call(this,r,e)}}),u.val(h.initialValue),h._addListeners(),h.options.hide||h.toggler.click()}},_addListeners:function(){var o=this;o.wrap.on("click.wpcolorpicker",function(t){t.stopPropagation()}),o.toggler.click(function(){o.toggler.hasClass("wp-picker-open")?o.close():o.open()}),o.element.on("change",function(r){(""===t(this).val()||o.element.hasClass("iris-error"))&&(o.options.alpha?(l&&o.toggler.removeAttr("style"),o.toggler.find("span.color-alpha").css("backgroundColor","")):o.toggler.css("backgroundColor",""),t.isFunction(o.options.clear)&&o.options.clear.call(this,r))}),o.button.on("click",function(r){t(this).hasClass("wp-picker-clear")?(o.element.val(""),o.options.alpha?(l&&o.toggler.removeAttr("style"),o.toggler.find("span.color-alpha").css("backgroundColor","")):o.toggler.css("backgroundColor",""),t.isFunction(o.options.clear)&&o.options.clear.call(this,r),o.element.trigger("change")):t(this).hasClass("wp-picker-default")&&o.element.val(o.options.defaultColor).change()})}}),t.widget("a8c.iris",t.a8c.iris,{_create:function(){if(this._super(),this.options.alpha=this.element.data("alpha")||!1,this.element.is(":input")||(this.options.alpha=!1),void 0!==this.options.alpha&&this.options.alpha){var o=this,r=o.element,e=t(' ').appendTo(o.picker.find(".iris-picker-inner")),a=e.find(".iris-slider-offset-alpha"),i={aContainer:e,aSlider:a};void 0!==r.data("custom-width")?o.options.customWidth=parseInt(r.data("custom-width"))||0:o.options.customWidth=100,o.options.defaultWidth=r.width(),(o._color._alpha<1||-1!=o._color.toString().indexOf("rgb"))&&r.width(parseInt(o.options.defaultWidth+o.options.customWidth)),t.each(i,function(t,r){o.controls[t]=r}),o.controls.square.css({"margin-right":"0"});var l=o.picker.width()-o.controls.square.width()-20,n=l/6,s=l/2-n;t.each(["aContainer","strip"],function(t,r){o.controls[r].width(s).css({"margin-left":n+"px"})}),o._initControls(),o._change()}},_initControls:function(){if(this._super(),this.options.alpha){var t=this;t.controls.aSlider.slider({orientation:"vertical",min:0,max:100,step:1,value:parseInt(100*t._color._alpha),slide:function(o,r){t._color._alpha=parseFloat(r.value/100),t._change.apply(t,arguments)}})}},_change:function(){this._super();var t=this,r=t.element;if(this.options.alpha){var e=t.controls,a=parseInt(100*t._color._alpha),i=t._color.toRgb(),l=["rgb("+i.r+","+i.g+","+i.b+") 0%","rgba("+i.r+","+i.g+","+i.b+", 0) 100%"],n=t.options.defaultWidth,s=t.options.customWidth,p=t.picker.closest(".wp-picker-container").find(".wp-color-result");e.aContainer.css({background:"linear-gradient(to bottom, "+l.join(", ")+"), url("+o+")"}),p.hasClass("wp-picker-open")&&(e.aSlider.slider("value",a),t._color._alpha<1?(e.strip.attr("style",e.strip.attr("style").replace(/rgba\(([0-9]+,)(\s+)?([0-9]+,)(\s+)?([0-9]+)(,(\s+)?[0-9\.]+)\)/g,"rgb($1$3$5)")),r.width(parseInt(n+s))):r.width(n))}(r.data("reset-alpha")||!1)&&t.picker.find(".iris-palette-container").on("click.palette",".iris-palette",function(){t._color._alpha=1,t.active="external",t._change()}),r.trigger("change")},_addInputListeners:function(t){var o=this,r=function(r){var e=new Color(t.val()),a=t.val();t.removeClass("iris-error"),e.error?""!==a&&t.addClass("iris-error"):e.toString()!==o._color.toString()&&("keyup"===r.type&&a.match(/^[0-9a-fA-F]{3}$/)||o._setOption("color",e.toString()))};t.on("change",r).on("keyup",o._debounce(r,100)),o.options.hide&&t.on("focus",function(){o.show()})}})}}(jQuery),jQuery(document).ready(function(t){t(".color-picker").wpColorPicker()}); \ No newline at end of file diff --git a/src/wp-color-picker-alpha.js b/src/wp-color-picker-alpha.js old mode 100755 new mode 100644 index 3a7d5bb..9ca8f5c --- a/src/wp-color-picker-alpha.js +++ b/src/wp-color-picker-alpha.js @@ -20,15 +20,35 @@ _after = '', _wrap = '', _button = '', - // Prevent CSS issues in < WordPress 4.9 - _deprecated = ( wpColorPickerL10n.current !== undefined ); - // Declare some global variables when is deprecated or not - if ( _deprecated ) { - var _before = ''; + _before = '', + _wrappingLabel = '', + _wrappingLabelText = '', + _deprecated = false; + // Check if wpColorPickerL10n is defined + if ( typeof wpColorPickerL10n !== "undefined" ) { + // wpColorPickerL10n is defined in <= WordPress 5.4 + // Prevent CSS issues in < WordPress 4.9. current can only be checked if we know wpColorPickerL10n is defined + _deprecated = ( typeof wpColorPickerL10n.current !== "undefined" ); + if ( _deprecated ) { + // Update _before if wpColorPickerL10n.current is defined (< WP 4.9) + _before = ''; + } + + var _colorValue = wpColorPickerL10n.defaultLabel, + _selectColor = wpColorPickerL10n.pick, + _defaultString = wpColorPickerL10n.defaultString, + _defaultAriaLabel = wpColorPickerL10n.defaultAriaLabel, + _clearString = wpColorPickerL10n.clear, + _clearAriaLabel = wpColorPickerL10n.clearAriaLabel; } else { - var _before = '', - _wrappingLabel = '', - _wrappingLabelText = ''; + // wpColorPickerL10n is not defined in WordPress 5.5+ + var __ = wp.i18n.__, + _colorValue = __( 'Color value' ), + _selectColor = __( 'Select Color' ), + _defaultString = __( 'Default' ), + _defaultAriaLabel = __( 'Select default color' ), + _clearString = __( 'Clear' ), + _clearAriaLabel = __( 'Clear color' ); } /** * Overwrite Color @@ -96,7 +116,7 @@ self.toggler = $( _before ) .insertBefore( el ) .css( { backgroundColor : self.initialValue } ) - .attr( 'title', wpColorPickerL10n.pick ) + .attr( 'title', _selectColor ) .attr( 'data-current', wpColorPickerL10n.current ); self.pickerContainer = $( _after ).insertAfter( el ); self.button = $( _button ).addClass('hidden'); @@ -111,7 +131,7 @@ // Insert the default label text. self.wrappingLabelText = $( _wrappingLabelText ) .insertBefore( el ) - .text( wpColorPickerL10n.defaultLabel ); + .text( _colorValue ); } /* @@ -129,7 +149,7 @@ .insertBefore( self.wrappingLabel ) .css( { backgroundColor: self.initialValue } ); // Set the toggle button span element text. - self.toggler.find( '.wp-color-result-text' ).text( wpColorPickerL10n.pick ); + self.toggler.find( '.wp-color-result-text' ).text( _selectColor ); // Set up the Iris container and insert it after the wrapping label. self.pickerContainer = $( _after ).insertAfter( self.wrappingLabel ); // Store a reference to the Clear/Default button. @@ -138,14 +158,14 @@ // Set up the Clear/Default button. if ( self.options.defaultColor ) { - self.button.addClass( 'wp-picker-default' ).val( wpColorPickerL10n.defaultString ); + self.button.addClass( 'wp-picker-default' ).val( _defaultString ); if ( ! _deprecated ) { - self.button.attr( 'aria-label', wpColorPickerL10n.defaultAriaLabel ); + self.button.attr( 'aria-label', _defaultAriaLabel ); } } else { - self.button.addClass( 'wp-picker-clear' ).val( wpColorPickerL10n.clear ); + self.button.addClass( 'wp-picker-clear' ).val( _clearString ); if ( ! _deprecated ) { - self.button.attr( 'aria-label', wpColorPickerL10n.clearAriaLabel ); + self.button.attr( 'aria-label', _clearAriaLabel ); } }