/** * A color picker based on the Beaver Builder Color Picker, * with support for accents */ (function( v, $, undefined ) { 'use strict'; var FLBuilderColorPresets = [], UA = navigator.userAgent.toLowerCase(); /** * @since 1.8.4 * @method flBuilderParseColorValue * @return {Array} */ flBuilderParseColorValue = 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 }; } /** * Main color picker class for Beaver Builder's custom implementation. * * @class window.VAMTAM.ColorPicker * @param {Object} settings * @since 1.6.4 */ v.ColorPicker = function( settings ) { this._html = '
'; // default settings var defaults = { elements : null, color : '', mode : 'hsl', controls : { horiz : 's', // horizontal defaults to saturation vert : 'l', // vertical defaults to lightness strip : 'h' // right strip defaults to hue }, target : false, // a DOM element / jQuery selector that the element will be appended within. Only used when called on an input. width : 200, // the width of the collection of UI elements presets: [], labels : { colorPresets: 'Color Presets', colorPicker: 'Color Picker', placeholder: 'Paste color here...', removePresetConfirm: 'Are you sure?', noneColorSelected: 'None color selected.', alreadySaved: '%s is already a saved preset.', noPresets: 'Add a color preset first.', accentsTitle: 'Accent Colors', presetAdded: '%s added to presets!', } }; // setting plugin options this.options = $.extend( {}, defaults, settings ); // initialize the color picker single instance this._init(); }; v.ColorPicker.prototype = Object.create( FLBuilderColorPicker.prototype ); /** * Prototype for new instances. * * @since 1.6.4 * @property {Object} prototype */ v.ColorPicker.prototype = Object.assign( v.ColorPicker.prototype, { /** * Initializes this instance. * * @since 1.6.4 * @method _init * * ** MODIFIED ** */ _init: function(){ var self = this, el = $( self.options.elements ); this.el = el; el.on( 'change', function( e ) { FLBuilder.preview.delayPreview( e ); var accent = self._sanitizeAccent( e.target.value ); if ( accent ) { $( e.target ).parent().find( '.vamtam-color-picker-color' ).css( 'background', accent ); } } ); this._color = new Color( '#ff0000' ).setHSpace( self.options.mode ); // Set picker color presets FLBuilderColorPresets = this.options.presets; // appends color picker markup to the body // check if there's already a color picker instance if( $('html').hasClass( 'vamtam-color-picker-init' ) ){ self.picker = $( '.vamtam-color-picker-ui' ); } else { self.picker = $( this._html ).appendTo( 'body' ); } // Browsers / Versions // Feature detection doesn't work for these, and $.browser is deprecated if ( UA.indexOf('compatible') < 0 && UA.indexOf('khtml') < 0 && UA.match( /mozilla/ ) ) { self.picker.addClass( 'iris-mozilla' ); } // prep 'em for re-use self.controls = { square : self.picker.find( '.iris-square' ), squareDrag : self.picker.find( '.iris-square-value' ), horiz : self.picker.find( '.iris-square-horiz' ), vert : self.picker.find( '.iris-square-vert' ), strip : self.picker.find( '.iris-strip' ), stripSlider : self.picker.find( '.iris-strip .iris-slider-offset' ) }; // small sanity check - if we chose hsv, change default controls away from hsl if ( self.options.mode === 'hsv' && self._has('l', self.options.controls) ) { self.options.controls = self._defaultHSVControls; } else if ( self.options.mode === 'hsl' && self._has('v', self.options.controls) ) { self.options.controls = self._defaultHSLControls; } // store it. HSL gets squirrely self.hue = self._color.h(); this._setTemplates(); // COLOR PRESETS UI -------------------------------------// // cache reference to the picker wrapper this._ui = $( '.vamtam-color-picker-ui' ); this._iris = $( '.iris-picker' ); this._wrapper = $( 'body' ); if( !$('html').hasClass( 'vamtam-color-picker-init' ) ){ this._ui .prepend( this._hexHtml ) .append( this._presetsHtml ); } self.element = this._ui.find( '.vamtam-color-picker-input' ); self._initControls(); self.active = 'external'; //self._dimensions(); self._change(); // binds listeners to all color picker instances self._addInputListeners( self.element ); // build the presets UI this._buildUI(); // adds needed markup and bind functions to all color fields this._prepareColorFields(); // bind picker control events this._pickerControls(); // bind presets control events this._presetsControls(); // adds opacity/alpha support this._buildAlphaUI(); // now we know that the picker is already added to the body $('html').addClass( 'vamtam-color-picker-init' ); }, _sanitizeAccent: function( value ) { var accent = value.match( /^accent(\d)/ ); if ( accent ) { return this.options.labels.accents[ accent[1] ]; } return false; }, /** * @since 1.6.4 * @method _prepareColorFields * * ** MODIFIED ** */ _prepareColorFields: function(){ var self = this; // append presets initial html and trigger that toggles the picker self.el.each( function(){ var $this = $( this ), $colorValue = $this.val(), $colorTrigger = $this.parent().find( '.vamtam-color-picker-color' ), $parsedValue = flBuilderParseColorValue( $colorValue ), $bgColor = '', accent = self._sanitizeAccent( $parsedValue.value ); if( $colorValue ){ // set initial color, check for alpha support if ( $colorTrigger.hasClass('vamtam-color-picker-alpha-enabled') && $parsedValue.rgba ) { $bgColor = $this.val().toString(); } else if ( ! $colorTrigger.hasClass('vamtam-color-picker-alpha-enabled') && $parsedValue.rgba ) { var $newColorValue = $colorValue.replace('rgba', 'rgb') $newColorValue = $newColorValue.substr(0, $newColorValue.lastIndexOf(",")) + ')'; self._color._alpha = 1; $bgColor = $newColorValue; $this.val($newColorValue); } else { $bgColor = accent || $this.val().toString(); } $colorTrigger.css({ backgroundColor: $bgColor }); } }); }, /** * Sets templates to build the color picker markup. * * @since 1.6.4 * @method _setTemplates * * ** MODIFIED ** */ _setTemplates: function(){ this._alphaHtml = '