From 32d9e73ce8ce68db943dd53e47c649ec28b90fd3 Mon Sep 17 00:00:00 2001 From: mondrake Date: Mon, 6 Feb 2017 16:46:45 +0100 Subject: [PATCH] Issue #2850131 by mondrake: Add a JQuery Colorpicker color selector plugin --- README.md | 8 +- ...ects.jquery_colorpicker_color_selector.css | 7 ++ image_effects.libraries.yml | 10 +++ image_effects.services.yml | 6 ++ ...fects.jquery_colorpicker_color_selector.js | 22 +++++ src/ImageEffectsUninstallValidator.php | 50 +++++++++++ .../ColorSelector/JqueryColorPicker.php | 41 +++++++++ .../ImageEffectsUninstallValidator.php | 88 +++++++++++++++++++ src/Tests/ImageEffectsSettingsFormTest.php | 33 ++++++- 9 files changed, 261 insertions(+), 4 deletions(-) create mode 100644 css/image_effects.jquery_colorpicker_color_selector.css create mode 100644 js/image_effects.jquery_colorpicker_color_selector.js create mode 100644 src/ImageEffectsUninstallValidator.php create mode 100644 src/Plugin/image_effects/ColorSelector/JqueryColorPicker.php create mode 100644 src/ProxyClass/ImageEffectsUninstallValidator.php diff --git a/README.md b/README.md index 4f86b63..6373c66 100644 --- a/README.md +++ b/README.md @@ -83,9 +83,11 @@ for further information. - Check Image Effects configuration page (_Manage > Configuration > Media > Image Effects_), and choose the UI components that effects provided by this module should use: - - _Color selector_ - allows to use either a 'color' HTML element for selecting - colors, or a color picker provided by the Farbtastic library. Alternative - selectors may be added by other modules. + - _Color selector_ - allows to specify a UI component to select colors in the + image effects. It can use a 'color' HTML element, or a color picker + provided by the Farbtastic library, or a JQuery Colorpicker (if the _JQuery + Colorpicker_ module is installed). Alternative selectors may be added by + other modules. - _Image selector_ - some effects (e.g. Watermark) require to define an image file to be used. This setting allows to use either a basic text field where the URI/path to the image can be entered, or a 'dropdown' select that will diff --git a/css/image_effects.jquery_colorpicker_color_selector.css b/css/image_effects.jquery_colorpicker_color_selector.css new file mode 100644 index 0000000..51ae125 --- /dev/null +++ b/css/image_effects.jquery_colorpicker_color_selector.css @@ -0,0 +1,7 @@ +/* JQuery Colorpicker Color selector */ + +div.image-effects-jquery-colorpicker-color-selector span.image-effects-color-suffix { + left: 40px; + position: absolute; + top: 6px; +} diff --git a/image_effects.libraries.yml b/image_effects.libraries.yml index 3026301..3ef5c04 100644 --- a/image_effects.libraries.yml +++ b/image_effects.libraries.yml @@ -24,6 +24,16 @@ image_effects.html_color_selector: component: css/image_effects.html_color_selector.css: {} +image_effects.jquery_colorpicker_color_selector: + version: VERSION + js: + js/image_effects.jquery_colorpicker_color_selector.js: {} + css: + component: + css/image_effects.jquery_colorpicker_color_selector.css: {} + dependencies: + - jquery_colorpicker/element + image_effects.text_overlay_preview: version: VERSION css: diff --git a/image_effects.services.yml b/image_effects.services.yml index c418361..ad8be8f 100644 --- a/image_effects.services.yml +++ b/image_effects.services.yml @@ -14,3 +14,9 @@ services: class: Drupal\image_effects\Plugin\ImageEffectsPluginManager parent: default_plugin_manager arguments: ['font_selector', '@config.factory'] + image_effects.uninstall_validator: + class: Drupal\image_effects\ImageEffectsUninstallValidator + tags: + - { name: module_install.uninstall_validator } + arguments: ['@config.factory', '@string_translation'] + lazy: true diff --git a/js/image_effects.jquery_colorpicker_color_selector.js b/js/image_effects.jquery_colorpicker_color_selector.js new file mode 100644 index 0000000..cdb33a4 --- /dev/null +++ b/js/image_effects.jquery_colorpicker_color_selector.js @@ -0,0 +1,22 @@ +/** + * @file + * + * JQuery Colorpicker color selector. + * + * Alters field_suffix form element after change to the color field. + */ + +(function ($) { + 'use strict'; + Drupal.behaviors.imageEffectsJqueryColorpickerColorSelector = { + attach: function (context, settings) { + $('.image-effects-jquery-colorpicker-color-selector .image-effects-jquery-colorpicker', context).once('image-effects-jquery-colorpicker-color-selector').each(function (index) { + $(this).parent().append('#' + this.value.toUpperCase() + ''); + $(this).on('change', function (event) { + var suffix = $(this).parent().find('.image-effects-color-suffix').get(0); + $(suffix).text('#' + this.value.toUpperCase()); + }); + }); + } + }; +})(jQuery); diff --git a/src/ImageEffectsUninstallValidator.php b/src/ImageEffectsUninstallValidator.php new file mode 100644 index 0000000..c0bd793 --- /dev/null +++ b/src/ImageEffectsUninstallValidator.php @@ -0,0 +1,50 @@ +configFactory = $config_factory; + $this->stringTranslation = $string_translation; + } + + /** + * {@inheritdoc} + */ + public function validate($module) { + $reasons = []; + // Prevents uninstalling 'jquery_colorpicker' if its color selector plugin + // is in use. + if ($module == 'jquery_colorpicker' && $this->configFactory->get('image_effects.settings')->get('color_selector.plugin_id') === 'jquery_colorpicker') { + $reasons[] = $this->t('The Image Effects module is using the JQuery Colorpicker color selector'); + } + return $reasons; + } + +} diff --git a/src/Plugin/image_effects/ColorSelector/JqueryColorPicker.php b/src/Plugin/image_effects/ColorSelector/JqueryColorPicker.php new file mode 100644 index 0000000..20470a2 --- /dev/null +++ b/src/Plugin/image_effects/ColorSelector/JqueryColorPicker.php @@ -0,0 +1,41 @@ + 'jquery_colorpicker', + '#title' => isset($options['#title']) ? $options['#title'] : $this->t('Color'), + '#default_value' => Unicode::substr($options['#default_value'], -6), + '#attributes' => ['class' => ['image-effects-jquery-colorpicker']], + '#wrapper_attributes' => ['class' => ['image-effects-jquery-colorpicker-color-selector']], + '#attached' => ['library' => ['image_effects/image_effects.jquery_colorpicker_color_selector']], + ]; + } + + /** + * {@inheritdoc} + */ + public static function isAvailable() { + return \Drupal::service('module_handler')->moduleExists('jquery_colorpicker'); + } + +} diff --git a/src/ProxyClass/ImageEffectsUninstallValidator.php b/src/ProxyClass/ImageEffectsUninstallValidator.php new file mode 100644 index 0000000..5ef0e98 --- /dev/null +++ b/src/ProxyClass/ImageEffectsUninstallValidator.php @@ -0,0 +1,88 @@ +container = $container; + $this->drupalProxyOriginalServiceId = $drupal_proxy_original_service_id; + } + + /** + * Lazy loads the real service from the container. + * + * @return object + * Returns the constructed real service. + */ + protected function lazyLoadItself() + { + if (!isset($this->service)) { + $this->service = $this->container->get($this->drupalProxyOriginalServiceId); + } + + return $this->service; + } + + /** + * {@inheritdoc} + */ + public function validate($module) + { + return $this->lazyLoadItself()->validate($module); + } + + /** + * {@inheritdoc} + */ + public function setStringTranslation(\Drupal\Core\StringTranslation\TranslationInterface $translation) + { + return $this->lazyLoadItself()->setStringTranslation($translation); + } + + } + +} diff --git a/src/Tests/ImageEffectsSettingsFormTest.php b/src/Tests/ImageEffectsSettingsFormTest.php index eb25ac7..e372a21 100644 --- a/src/Tests/ImageEffectsSettingsFormTest.php +++ b/src/Tests/ImageEffectsSettingsFormTest.php @@ -11,7 +11,7 @@ */ class ImageEffectsSettingsFormTest extends WebTestBase { - public static $modules = ['image_effects']; + public static $modules = ['image_effects', 'jquery_colorpicker']; /** * {@inheritdoc} @@ -69,4 +69,35 @@ public function testSettingsForm() { $this->assertEqual(['path' => 'public://'], \Drupal::config('image_effects.settings')->get('font_selector.plugin_settings.dropdown')); } + /** + * Test JQuery Colorpicker color selector. + */ + public function testJqueryColorpickerSelector() { + $admin_path = '/admin/config/media/image_effects'; + + // Get the settings form. + $this->drupalGet($admin_path); + + // Change the default color selector. + $edit = [ + 'settings[color_selector][plugin_id]' => 'jquery_colorpicker', + ]; + $this->drupalPostForm(NULL, $edit, t('Save configuration')); + + // Check config changed. + $this->assertEqual('jquery_colorpicker', \Drupal::config('image_effects.settings')->get('color_selector.plugin_id')); + + // Verify that the 'jquery_colorpicker' module cannot be uninstalled. + $this->assertNotEqual([], \Drupal::service('module_installer')->validateUninstall(['jquery_colorpicker'])); + + // Back to the default color selector. + $edit = [ + 'settings[color_selector][plugin_id]' => 'html_color', + ]; + $this->drupalPostForm(NULL, $edit, t('Save configuration')); + + // Verify that the 'jquery_colorpicker' module can be uninstalled now. + $this->assertTrue(\Drupal::service('module_installer')->uninstall(['jquery_colorpicker'])); + } + }