From 86452d0723a3c3d6017a8d6cb3b1a83588fcceea Mon Sep 17 00:00:00 2001 From: Adam Walter Date: Tue, 12 Feb 2019 12:19:18 -0500 Subject: [PATCH] Fixes (for good!) problems with multiple instances of color pickers --- assets/acf-restrict-color-picker.js | 84 ++++------------------------- 1 file changed, 9 insertions(+), 75 deletions(-) diff --git a/assets/acf-restrict-color-picker.js b/assets/acf-restrict-color-picker.js index 62d20af..ff5e600 100755 --- a/assets/acf-restrict-color-picker.js +++ b/assets/acf-restrict-color-picker.js @@ -1,80 +1,14 @@ (function($) { - var input, pickerWrap, valBox; + function getMasterPalette() { + var rcpOptions = JSON.parse(acfRCPOptions); + return rcpOptions['master']; + } - acf.add_action('ready append', function() { + acf.add_filter('color_picker_args', function(args) { + args.palettes = getMasterPalette(); - var acfRCPColorPalettes = JSON.parse(acfRCPOptions); - var acfRCPColorPaletteMaster = JSON.stringify(acfRCPColorPalettes['master']); - var acfRCPColorInput = ''; - var acfRCPColorInputDefaultIndex = -1; - var acfRCPColorInputDefaultColor = ''; + return args; + }); - acf.get_fields({type : 'color_picker'}).each(function() { - - var acfRCPColorPalette = ''; // Stores the result palette for this color picker - - if( !$(this).hasClass('color_restricted') ) { // Set this class to avoid modifying Color Picker more than once per load - - acfRCPColorPalette = JSON.stringify(acfRCPColorPalettes[acf.get_field_key($(this))]); - - if(acfRCPColorPalette === undefined) { // If there was no default set for this color picker field, load the master palette - acfRCPColorPalette = acfRCPColorPaletteMaster; - } - - $(this).find('input[type="text"]').each(function() { // For each color text input: find, load and sanitize the default val (if necessary) - - acfRCPColorInput = $(this).val(); - - if((!acfRCPColorInput) || acfRCPColorInput.indexOf(',') !== -1) { // If there's no input value set, or it contains a comma-delimited list of hex values - - if(!acfRCPColorInput) { // If there's no input value set - acfRCPColorInput = acfRCPColorPalette.replace(/[\[\"\]]/g, ''); // Load it with the current color palette - } - - acfRCPColorInputDefaultIndex = acfRCPColorInput.indexOf('default-'); // Find the index of the default hex - - if(acfRCPColorInputDefaultIndex !== -1) { // If there's a default hex - acfRCPColorInputDefaultColor = acfRCPColorInput.substring(acfRCPColorInputDefaultIndex + 8); - if(acfRCPColorInput.indexOf(',', acfRCPColorInputDefaultIndex) !== -1) { // Remove anything after ',', inclusive - acfRCPColorInputDefaultColor = acfRCPColorInputDefaultColor.substring(0, acfRCPColorInputDefaultColor.indexOf(',', 0)); - } - $(this).val(acfRCPColorInputDefaultColor); - $(this).parents('.wp-picker-container').find('.wp-color-result').css('background-color', acfRCPColorInputDefaultColor); - - } else { - $(this).val(''); - $(this).parents('.wp-picker-container').find('.wp-color-result').css('background-color', ''); - } - } - }); - - $(this).addClass('color_restricted'); - var thisSelector = $(this).attr('class'); // Get the current selector - thisSelector = '.' + thisSelector.replace(/\ /g, '.'); - - var waitForEl = function(selector, callback) { - if ($(selector).length) { - callback(); - } else { - setTimeout(function() { - waitForEl(selector, callback); - }, 100); - } - }; - - waitForEl(thisSelector + ' .wp-picker-holder', function() { // Once the older Iris has loaded, load our new Iris with custom palette - if(!$(thisSelector + ' .wp-picker-holder').hasClass('restrict-color-picker-loaded')) { - $(thisSelector + ' .wp-picker-holder').iris({ - palettes: JSON.parse(acfRCPColorPalette.replace(/default\-/g, '')), // Remove 'default-' from JSON so that it's a valid hex value for color picker - hide: true, - change: function(event, ui) { - $(thisSelector + ' .wp-picker-container').find('.wp-color-result').css('background-color', ui.color.toString()); - } - }).addClass('restrict-color-picker-loaded'); - } - }); - } - }); - }); -})(jQuery); \ No newline at end of file +})(jQuery);