From 05189326f0bdc8dd6b6d48580ed242cc652a0388 Mon Sep 17 00:00:00 2001 From: Stefanie Gevaert Date: Mon, 7 Sep 2020 13:29:50 +0200 Subject: [PATCH 01/15] #36 added migration to updated older version to new version --- src/migrations/Install.php | 48 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 src/migrations/Install.php diff --git a/src/migrations/Install.php b/src/migrations/Install.php new file mode 100644 index 0000000..058ec17 --- /dev/null +++ b/src/migrations/Install.php @@ -0,0 +1,48 @@ +updateTables(); + + return true; + } + + /** + * @inheritdoc + */ + public function safeDown() + { + echo "Install cannot be reverted.\n"; + return true; + } + + // Protected Methods + // ========================================================================= + + protected function updateTables() + { + $this->update( + '{{%fields}}', + [ + 'type' => 'percipioglobal\\colourswatches\\fields\\ColourSwatches' + ], + 'type = :riastype', + [ + ':riastype' => 'rias\\colourswatches\\fields\\ColourSwatches' + ] + ); + } +} \ No newline at end of file From e0bbffbe2dbd7235236b58b759fadafbd2c57d2b Mon Sep 17 00:00:00 2001 From: Stefanie Gevaert Date: Mon, 7 Sep 2020 16:58:10 +0200 Subject: [PATCH 02/15] #36 debug migrate --- src/migrations/Install.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/migrations/Install.php b/src/migrations/Install.php index 058ec17..7828e2e 100644 --- a/src/migrations/Install.php +++ b/src/migrations/Install.php @@ -15,6 +15,9 @@ class Install extends Migration public function safeUp() { + echo "migrate"; + die(); + $this->updateTables(); return true; From c5a8050a375760bea02a8dc674477a52c8333642 Mon Sep 17 00:00:00 2001 From: Stefanie Gevaert Date: Mon, 7 Sep 2020 17:04:46 +0200 Subject: [PATCH 03/15] #36 debug upgrade --- src/migrations/Install.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/migrations/Install.php b/src/migrations/Install.php index 7828e2e..e1f06a8 100644 --- a/src/migrations/Install.php +++ b/src/migrations/Install.php @@ -10,14 +10,15 @@ */ class Install extends Migration { + + echo "migrate"; + die(); + // Public Methods // ========================================================================= public function safeUp() { - echo "migrate"; - die(); - $this->updateTables(); return true; From f6bd9ab418db0b8a8c78fc6eb1a906b6a87c1a63 Mon Sep 17 00:00:00 2001 From: Stefanie Gevaert Date: Mon, 7 Sep 2020 17:08:42 +0200 Subject: [PATCH 04/15] #36 debug upgrade --- src/migrations/Install.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/migrations/Install.php b/src/migrations/Install.php index e1f06a8..445a1b9 100644 --- a/src/migrations/Install.php +++ b/src/migrations/Install.php @@ -10,15 +10,13 @@ */ class Install extends Migration { - - echo "migrate"; - die(); - // Public Methods // ========================================================================= public function safeUp() { + echo "migrate"; + $this->updateTables(); return true; From 31588c8079921774377382ca4d8405506c8c9515 Mon Sep 17 00:00:00 2001 From: Stefanie Gevaert Date: Thu, 10 Sep 2020 15:52:31 +0200 Subject: [PATCH 05/15] upgrade composer version --- composer.json | 2 +- src/ColourSwatches.php | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 182d6af..3ad6258 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "percipioglobal/craft-colour-swatches", "description": "Let clients choose from a predefined set of colours.", "type": "craft-plugin", - "version": "1.3.3", + "version": "1.4.0", "keywords": [ "craft", "cms", diff --git a/src/ColourSwatches.php b/src/ColourSwatches.php index ab0965e..d21e848 100644 --- a/src/ColourSwatches.php +++ b/src/ColourSwatches.php @@ -27,6 +27,11 @@ */ class ColourSwatches extends Plugin { + // Public Properties + // ========================================================================= + + public $schemaVersion = '1.4.0'; + // Static Properties // ========================================================================= From 7a2b3a5617aeab63f30ef96496ba233319eba871 Mon Sep 17 00:00:00 2001 From: Stefanie Gevaert Date: Thu, 10 Sep 2020 16:24:39 +0200 Subject: [PATCH 06/15] #36 removed install migration and made an update namespace migration --- src/migrations/{Install.php => update_namespace.php} | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) rename src/migrations/{Install.php => update_namespace.php} (92%) diff --git a/src/migrations/Install.php b/src/migrations/update_namespace.php similarity index 92% rename from src/migrations/Install.php rename to src/migrations/update_namespace.php index 445a1b9..2a49408 100644 --- a/src/migrations/Install.php +++ b/src/migrations/update_namespace.php @@ -15,8 +15,6 @@ class Install extends Migration public function safeUp() { - echo "migrate"; - $this->updateTables(); return true; @@ -27,7 +25,7 @@ public function safeUp() */ public function safeDown() { - echo "Install cannot be reverted.\n"; + echo "Cannot update namespace.\n"; return true; } From 1235f832796cf53b30e9784ee42449d24f3fca02 Mon Sep 17 00:00:00 2001 From: Stefanie Gevaert Date: Thu, 10 Sep 2020 18:44:00 +0200 Subject: [PATCH 07/15] #36 added versioning to the extra in composer.json --- composer.json | 2 +- src/ColourSwatches.php | 5 ----- 2 files changed, 1 insertion(+), 6 deletions(-) diff --git a/composer.json b/composer.json index 3ad6258..f24be15 100644 --- a/composer.json +++ b/composer.json @@ -32,7 +32,7 @@ "extra": { "name": "Colour Swatches", "handle": "colour-swatches", - "schemaVersion": "1.0.0", + "schemaVersion": "1.4.0", "hasCpSettings": false, "hasCpSection": false, "changelogUrl": "https://raw.githubusercontent.com/percipioglobal/craft-colour-swatches/master/CHANGELOG.md", diff --git a/src/ColourSwatches.php b/src/ColourSwatches.php index d21e848..ab0965e 100644 --- a/src/ColourSwatches.php +++ b/src/ColourSwatches.php @@ -27,11 +27,6 @@ */ class ColourSwatches extends Plugin { - // Public Properties - // ========================================================================= - - public $schemaVersion = '1.4.0'; - // Static Properties // ========================================================================= From d8ea407bfc9a24138d92603adfd7289f83afe551 Mon Sep 17 00:00:00 2001 From: Stefanie Gevaert Date: Fri, 11 Sep 2020 11:35:12 +0200 Subject: [PATCH 08/15] updated namespace --- src/migrations/update_namespace.php | 32 ++++++++++++----------------- 1 file changed, 13 insertions(+), 19 deletions(-) diff --git a/src/migrations/update_namespace.php b/src/migrations/update_namespace.php index 2a49408..59b5022 100644 --- a/src/migrations/update_namespace.php +++ b/src/migrations/update_namespace.php @@ -8,32 +8,15 @@ /** * Install migration. */ -class Install extends Migration +class update_namespace extends Migration { // Public Methods // ========================================================================= public function safeUp() { - $this->updateTables(); + echo "update_namespace updating .\n"; - return true; - } - - /** - * @inheritdoc - */ - public function safeDown() - { - echo "Cannot update namespace.\n"; - return true; - } - - // Protected Methods - // ========================================================================= - - protected function updateTables() - { $this->update( '{{%fields}}', [ @@ -44,5 +27,16 @@ protected function updateTables() ':riastype' => 'rias\\colourswatches\\fields\\ColourSwatches' ] ); + + return true; + } + + /** + * @inheritdoc + */ + public function safeDown() + { + echo "update_namespace cannot be reverted.\n"; + return true; } } \ No newline at end of file From e89f51b2acc2c4dc1766be853a47a83835355c17 Mon Sep 17 00:00:00 2001 From: Stefanie Gevaert Date: Fri, 11 Sep 2020 16:22:42 +0200 Subject: [PATCH 09/15] create migration from craft --- ...e.php => m200911_142127_update_namespace.php} | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) rename src/migrations/{update_namespace.php => m200911_142127_update_namespace.php} (71%) diff --git a/src/migrations/update_namespace.php b/src/migrations/m200911_142127_update_namespace.php similarity index 71% rename from src/migrations/update_namespace.php rename to src/migrations/m200911_142127_update_namespace.php index 59b5022..038bce0 100644 --- a/src/migrations/update_namespace.php +++ b/src/migrations/m200911_142127_update_namespace.php @@ -6,13 +6,13 @@ use craft\db\Migration; /** - * Install migration. + * m200911_142127_update_namespace migration. */ -class update_namespace extends Migration +class m200911_142127_update_namespace extends Migration { - // Public Methods - // ========================================================================= - + /** + * @inheritdoc + */ public function safeUp() { echo "update_namespace updating .\n"; @@ -36,7 +36,7 @@ public function safeUp() */ public function safeDown() { - echo "update_namespace cannot be reverted.\n"; - return true; + echo "m200911_142127_update_namespace cannot be reverted.\n"; + return false; } -} \ No newline at end of file +} From 44684d017d75a78637e5f7eed6f87d4912c21411 Mon Sep 17 00:00:00 2001 From: Stefanie Gevaert Date: Fri, 11 Sep 2020 16:40:20 +0200 Subject: [PATCH 10/15] #36 updated CHANGELOG.md --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index fba8e0c..90de1a1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,9 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +## 1.4.0 - 2020-9-11 +Migrate namespace from rias to percipioglobal after upgrade +Fixes issues (#36) ## 1.3.3 - 2020-8-19 Bugfix Fixed bugs introduced in last release by not checking the default configuration settings - Apologies. New features coming in next release! From 05ac9cdcb5fc84cb09322cf896bcb89485920857 Mon Sep 17 00:00:00 2001 From: Stefanie Gevaert Date: Mon, 14 Sep 2020 16:09:55 +0200 Subject: [PATCH 11/15] #50 check for color vs colour properties in config. updated to default color everywhere --- README.md | 8 ++++---- src/config.php | 20 ++++++++++---------- src/fields/ColourSwatches.php | 6 +++--- src/models/ColourSwatches.php | 30 ++++++++++++++++++++++++------ src/models/Settings.php | 1 + src/templates/colourOption.twig | 12 ++++++------ src/templates/input.twig | 2 +- 7 files changed, 49 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index 5ab9362..1301894 100644 --- a/README.md +++ b/README.md @@ -47,9 +47,9 @@ Take a look at the [config file](https://github.com/percipioglobal/craft-colour- ```php return [ // Predefined colours, fixed options [label, colour, default (boolean)] - 'colours' => [ + 'colors' => [ [ - 'label' => 'red', + 'label' => 'red', 'color' => '#F56565', 'default' => true, ], @@ -134,7 +134,7 @@ return [ ], 'Gradients' => [ [ - 'label' => 'primary', + 'label' => 'primary', 'default' => false, 'color' => [ [ @@ -176,7 +176,7 @@ You can access both the label and color in your template. By default, the label {{ fieldName.label }} {{ fieldName.labels }} {# Returns an array of all labels #} {{ fieldName.color }} -{{ fieldName.colours }} {# Returns an array of all colours #} +{{ fieldName.colors }} {# Returns an array of all colours #} ``` OR to access custom attributes from the config file diff --git a/src/config.php b/src/config.php index 7799338..1dec4bb 100644 --- a/src/config.php +++ b/src/config.php @@ -26,9 +26,9 @@ return [ // Predefined colours - 'colours' => [ + 'colors' => [ [ - 'label' => 'red', + 'label' => 'red', 'color' => '#F56565', 'class' => 'red-500', // custom attribute 'default' => true, @@ -90,7 +90,7 @@ 'default' => true, 'color' => [ [ - 'color' => '#38B2AC', // the colour shown in the fieldtype (required) + 'color' => '#38B2AC', // the color shown in the fieldtype (required) 'class' => 'teal-500', // custom attribute 'btnClass' => 'blue-500', // custom attribute, ], @@ -101,7 +101,7 @@ 'default' => false, 'color' => [ [ - 'color' => '#4299E1', // the colour shown in the fieldtype (required) + 'color' => '#4299E1', // the color shown in the fieldtype (required) 'class' => 'blue-500', // custom attribute 'btnClass' => 'teal-500', // custom attribute ], @@ -112,7 +112,7 @@ 'default' => false, 'color' => [ [ - 'color' => '#ED64A6', // the colour shown in the fieldtype (required) + 'color' => '#ED64A6', // the color shown in the fieldtype (required) 'class' => 'pink-500', // custom attribute 'btnClass' => 'blue-500', // custom attribute ], @@ -121,14 +121,14 @@ ], 'Gradients' => [ [ - 'label' => 'primary', + 'label' => 'primary', 'default' => true, 'color' => [ [ - 'color' => '#38B2AC', // the colour shown in the fieldtype (required) + 'color' => '#38B2AC', // the color shown in the fieldtype (required) ], [ - 'color' => '#434190', // the next colour in this loop + 'color' => '#434190', // the next color in this loop ], ] ], @@ -137,10 +137,10 @@ 'default' => false, 'color' => [ [ - 'color' => '#434190', // the colour shown in the fieldtype (required) + 'color' => '#434190', // the color shown in the fieldtype (required) ], [ - 'color' => '#ED64A6', // the next colour in this loop + 'color' => '#ED64A6', // the next color in this loop ], ] ], diff --git a/src/fields/ColourSwatches.php b/src/fields/ColourSwatches.php index e68b937..77998b0 100644 --- a/src/fields/ColourSwatches.php +++ b/src/fields/ColourSwatches.php @@ -114,7 +114,7 @@ public function getSettingsHtml() } elseif ($this->palette) { $rows = Plugin::$plugin->settings->palettes[$this->palette]; } else { - $rows = Plugin::$plugin->settings->colours; + $rows = Plugin::$plugin->settings->colors ? Plugin::$plugin->settings->colors : Plugin::$plugin->settings->colours; } $config = [ @@ -172,7 +172,7 @@ public function getSettingsHtml() [ 'field' => $this, 'config' => $config, - 'configOptions' => Plugin::$plugin->settings->colours, + 'configOptions' => Plugin::$plugin->settings->colors ? Plugin::$plugin->settings->colors : Plugin::$plugin->settings->colours, 'paletteOptions' => $paletteOptions, 'palettes' => Plugin::$plugin->settings->palettes, ] @@ -202,7 +202,7 @@ public function getInputHtml($value, ElementInterface $element = null): string 'field' => $this, 'id' => $id, 'namespacedId' => $namespacedId, - 'configOptions'=> Plugin::$plugin->settings->colours, + 'configOptions'=> Plugin::$plugin->settings->colors ? Plugin::$plugin->settings->colors : Plugin::$plugin->settings->colours, 'palettes' => Plugin::$plugin->settings->palettes, ] ); diff --git a/src/models/ColourSwatches.php b/src/models/ColourSwatches.php index 1c6b607..497f6a6 100644 --- a/src/models/ColourSwatches.php +++ b/src/models/ColourSwatches.php @@ -13,17 +13,20 @@ class ColourSwatches * @var string */ public $color = ''; + public $colour = ''; public function __construct($value) { if (!empty($value)) { if (is_array($value)) { $this->label = $value['label']; - $this->color = $value['color']; + $this->color = $value['color'] ? $value['color'] : $value['colour']; + $this->colour = $this->color; } else { $value = json_decode($value); $this->label = $value->label; - $this->color = $value->color; + $this->color = $value->color ? $value->color : $value->colour; + $this->colour = $this->color; } } } @@ -35,15 +38,30 @@ public function __toString() public function colours() { - if (strstr($this->color, ';') !== false) { - return explode(';', $this->color); - } + return $this->colorsToArray(); + } - return explode(',', $this->color); + public function colors() + { + return $this->colorsToArray(); } public function labels() { return explode(',', $this->label); } + + protected function colorsToArray() + { + if(is_array($this->color)) + { + return $this->color; + } + + if (strstr($this->color, ';') !== false) { + return explode(';', $this->color); + } + + return explode(',', $this->color); + } } diff --git a/src/models/Settings.php b/src/models/Settings.php index 4b6ea82..4564989 100644 --- a/src/models/Settings.php +++ b/src/models/Settings.php @@ -7,5 +7,6 @@ class Settings extends Model { public $colours = []; + public $colors = []; public $palettes = []; } diff --git a/src/templates/colourOption.twig b/src/templates/colourOption.twig index 2c7b0a6..be2c218 100644 --- a/src/templates/colourOption.twig +++ b/src/templates/colourOption.twig @@ -1,20 +1,20 @@ -{% set colours = option.color %} +{% set colours = option.color is defined ? option.color : option.colour %} {% set isDefault = option.default %} {% set fieldValue = fieldValue ?? null %} -{% if option.color is iterable %} +{% if colours is iterable %} {# option.color is an array #} {% set value = { label: option.label, - color: option.color, + color: colours, } %} @@ -22,7 +22,7 @@ {# option.color is a string #} {% set value = { label: option.label, - color: option.color, + color: colours, } %} {% if ';' in value.color %} {% set colours = value.color | split(';') %} diff --git a/src/templates/input.twig b/src/templates/input.twig index fd95343..8d0285a 100644 --- a/src/templates/input.twig +++ b/src/templates/input.twig @@ -28,7 +28,7 @@ {% if option.default %} {% set value = { label: option.label, - color: option.color, + color: option.color is defined ? option.color : option.colour, } %} {% set defaultValue = value|json_encode %} From 3cf897a2870095b0fc8a80ff8c9bc8e9a9442f78 Mon Sep 17 00:00:00 2001 From: Stefanie Gevaert Date: Mon, 14 Sep 2020 16:17:21 +0200 Subject: [PATCH 12/15] #50 updated changelog --- CHANGELOG.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 90de1a1..02e79d0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,8 +5,8 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). ## 1.4.0 - 2020-9-11 -Migrate namespace from rias to percipioglobal after upgrade -Fixes issues (#36) +Migrate namespace from rias to percipioglobal after upgrade. Also converted config php to 'colors' instead of 'colours'. Still supports 'colours' for older versions. +Fixes issues (#36, #50) ## 1.3.3 - 2020-8-19 Bugfix Fixed bugs introduced in last release by not checking the default configuration settings - Apologies. New features coming in next release! From e80311379fc7fe4c4edd9e7e8af72fba650990fa Mon Sep 17 00:00:00 2001 From: Stefanie Gevaert Date: Mon, 14 Sep 2020 16:21:18 +0200 Subject: [PATCH 13/15] #50 updated changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 02e79d0..2267575 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). -## 1.4.0 - 2020-9-11 +## 1.4.0 - 2020-9-11 Bugfix Migrate namespace from rias to percipioglobal after upgrade. Also converted config php to 'colors' instead of 'colours'. Still supports 'colours' for older versions. Fixes issues (#36, #50) From 7102d266391fa9aa4b4ca956b1de174ea46c3a29 Mon Sep 17 00:00:00 2001 From: Stefanie Gevaert Date: Thu, 17 Sep 2020 12:43:50 +0200 Subject: [PATCH 14/15] #50 added extra check for colour values --- src/models/ColourSwatches.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/models/ColourSwatches.php b/src/models/ColourSwatches.php index 497f6a6..443fd41 100644 --- a/src/models/ColourSwatches.php +++ b/src/models/ColourSwatches.php @@ -25,7 +25,7 @@ public function __construct($value) } else { $value = json_decode($value); $this->label = $value->label; - $this->color = $value->color ? $value->color : $value->colour; + $this->color = $this->fetchColorOrColour($value); $this->colour = $this->color; } } @@ -51,6 +51,14 @@ public function labels() return explode(',', $this->label); } + protected function fetchColorOrColour($value){ + if(property_exists($value, "colour") && "" === $value->color ){ + return $value->colour; + } + + return $value->color; + } + protected function colorsToArray() { if(is_array($this->color)) From 1813ad226589b8925c80c7cb1cda046a915bc20e Mon Sep 17 00:00:00 2001 From: Jamie Taylor Date: Thu, 26 Nov 2020 21:51:44 +0000 Subject: [PATCH 15/15] Required field fix fixes #56 fixes #32 Added support for gradient previews --- CHANGELOG.md | 4 +- README.md | 6 +- src/ColourSwatches.php | 1 + .../dist/css/ColourSwatches.css | 5 ++ .../dist/js/ColourSwatches.js | 1 + src/config.php | 12 +-- src/fields/ColourSwatches.php | 57 +++++++++----- src/models/ColourSwatches.php | 78 ++++++------------- src/models/Settings.php | 1 - src/templates/colourOption.twig | 9 ++- src/templates/input.twig | 46 +++++++---- 11 files changed, 113 insertions(+), 107 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2267575..8aa8c11 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,12 +14,12 @@ Fixes issues (#44, #45, #46, #47) ## 1.3.2 - 2020-8-13 Bugfix ElementInterface updated to allow colour preview for custom arrays. Fixes issue #42 -fieldValue conditions for standard colour strings udpated to reflect null update in version 1.3.1. Fixes issue #43 +fieldValue conditions for standard colour strings updated to reflect null update in version 1.3.1. Fixes issue #43 ## 1.3.1 - 2020-8-03 Bugfix Updated save function to ensure custom array fields remain selected on resave / edit -Allowed swtch between config or palette option in field settings +Allowed switch between config or palette option in field settings ## 1.3.0.1 - 2020-7-31 HOTFIX temp fixed broken settings page, expect more robust repair next week :) diff --git a/README.md b/README.md index 1301894..3d579ba 100644 --- a/README.md +++ b/README.md @@ -116,7 +116,7 @@ return [ [ 'color' => '#4299E1', // the colour shown in the fieldtype (required) 'class' => 'blue-500', // custom attribute - 'btnClass' => 'teal-500', // custom attribute + 'btnClass' => 'teal-500', // custom attribute ], ] ], @@ -127,12 +127,12 @@ return [ [ 'color' => '#ED64A6', // the colour shown in the fieldtype (required) 'class' => 'pink-500', // custom attribute - 'btnClass' => 'blue-500', // custom attribute + 'btnClass' => 'blue-500', // custom attribute ], ] ], ], - 'Gradients' => [ + 'Gradients' => [ // custom label [ 'label' => 'primary', 'default' => false, diff --git a/src/ColourSwatches.php b/src/ColourSwatches.php index ab0965e..be06613 100644 --- a/src/ColourSwatches.php +++ b/src/ColourSwatches.php @@ -14,6 +14,7 @@ use craft\base\Plugin; use craft\events\RegisterComponentTypesEvent; use craft\services\Fields; +use craft\services\Plugins; use percipioglobal\colourswatches\fields\ColourSwatches as ColourSwatchesField; use percipioglobal\colourswatches\models\Settings; use yii\base\Event; diff --git a/src/assetbundles/colourswatchesfield/dist/css/ColourSwatches.css b/src/assetbundles/colourswatchesfield/dist/css/ColourSwatches.css index 7b4e24f..99f2cfb 100644 --- a/src/assetbundles/colourswatchesfield/dist/css/ColourSwatches.css +++ b/src/assetbundles/colourswatchesfield/dist/css/ColourSwatches.css @@ -57,4 +57,9 @@ .color-swatches button.active { box-shadow: 0 0 0 3px #F2842D; +} + +.color-swatches pre { + font-size: 0.75rem; + font-family: courier; } \ No newline at end of file diff --git a/src/assetbundles/colourswatchesfield/dist/js/ColourSwatches.js b/src/assetbundles/colourswatchesfield/dist/js/ColourSwatches.js index c5683cf..47d1ab6 100644 --- a/src/assetbundles/colourswatchesfield/dist/js/ColourSwatches.js +++ b/src/assetbundles/colourswatchesfield/dist/js/ColourSwatches.js @@ -19,6 +19,7 @@ ColourSelectInput = Garnish.Base.extend({ { var $option = $(ev.currentTarget); + // enables toggle on/off function of color buttons if ($option.hasClass('active')) { $option.removeClass('active'); diff --git a/src/config.php b/src/config.php index 1dec4bb..234ca22 100644 --- a/src/config.php +++ b/src/config.php @@ -90,9 +90,9 @@ 'default' => true, 'color' => [ [ - 'color' => '#38B2AC', // the color shown in the fieldtype (required) - 'class' => 'teal-500', // custom attribute - 'btnClass' => 'blue-500', // custom attribute, + 'color' => '#38B2AC', // the color shown in the fieldtype (required) + 'class' => 'teal-500', // custom attribute + 'btnClass' => 'blue-500', // custom attribute, ], ] ], @@ -103,7 +103,7 @@ [ 'color' => '#4299E1', // the color shown in the fieldtype (required) 'class' => 'blue-500', // custom attribute - 'btnClass' => 'teal-500', // custom attribute + 'btnClass' => 'teal-500', // custom attribute ], ] ], @@ -114,12 +114,12 @@ [ 'color' => '#ED64A6', // the color shown in the fieldtype (required) 'class' => 'pink-500', // custom attribute - 'btnClass' => 'blue-500', // custom attribute + 'btnClass' => 'blue-500', // custom attribute ], ] ], ], - 'Gradients' => [ + 'Gradients' => [ // custom label [ 'label' => 'primary', 'default' => true, diff --git a/src/fields/ColourSwatches.php b/src/fields/ColourSwatches.php index 77998b0..8fbf9da 100644 --- a/src/fields/ColourSwatches.php +++ b/src/fields/ColourSwatches.php @@ -51,7 +51,7 @@ class ColourSwatches extends Field implements PreviewableFieldInterface */ public static function displayName(): string { - return Craft::t('colour-swatches', 'Colour Swatches'); + return Craft::t('colour-swatches', 'Color Swatches'); } // Public Methods @@ -83,11 +83,16 @@ public function getContentColumnType(): string */ public function normalizeValue($value, ElementInterface $element = null) { + if ($value instanceof ColourSwatchesModel) { return $value; } - - return new ColourSwatchesModel($value); + // quick array transform so that we can ensure and `required fields` fire an error + $valueData = (array)json_decode($value); + // if we have actual data return model + if(count($valueData) > 0) { + return new ColourSwatchesModel($value); + } } /** @@ -95,11 +100,7 @@ public function normalizeValue($value, ElementInterface $element = null) */ public function serializeValue($value, ElementInterface $element = null) { - if ($value instanceof ColourSwatchesModel) { - return $value; - } - - return new ColourSwatchesModel($value); + return $value; } /** @@ -185,6 +186,7 @@ public function getSettingsHtml() public function getInputHtml($value, ElementInterface $element = null): string { // Register our asset bundle + // print_r($value); Craft::$app->getView()->registerAssetBundle(ColourSwatchesFieldAsset::class); // Get our id and namespace @@ -212,19 +214,32 @@ public function getInputHtml($value, ElementInterface $element = null): string * {@inheritdoc} */ public function getTableAttributeHtml($value, ElementInterface $element): string - { - /** @var ColorData|null $value */ - $fieldValue; - if (!$value) { - return '
'; + { + // our preview no data value + $color = null; + $style="background-color: transparent"; + // if we have data + if(!empty($value)){ + $fieldValue = get_object_vars($value); + $gradients = array(); + // if we have a custom color config + if (is_array($value->color)) { + foreach ($value->color as $color){ + $gradients[] = $color->color; + } + // set a fallback if we only have one colour + $style="background-color:$gradients[0]"; + // else build the gradient + if(count($gradients) > 1){ + $gradients = implode(",", $gradients); + $style="background: linear-gradient(to bottom right, $gradients);"; + } + // if we're using the CP values + } else { + $color = $value->color; + $style="background-color:$color"; + } } - if (is_array($value->color)) { - $fieldValue = $value->color[0]->color; - } else { - $fieldValue = $value->color; - } - return "
"; - // . "
{$value->label}
"; - + return '
'; } } diff --git a/src/models/ColourSwatches.php b/src/models/ColourSwatches.php index 443fd41..9999d62 100644 --- a/src/models/ColourSwatches.php +++ b/src/models/ColourSwatches.php @@ -7,69 +7,35 @@ class ColourSwatches /** * @var string */ - public $label = ''; - - /** - * @var string - */ - public $color = ''; - public $colour = ''; public function __construct($value) { - if (!empty($value)) { - if (is_array($value)) { - $this->label = $value['label']; - $this->color = $value['color'] ? $value['color'] : $value['colour']; - $this->colour = $this->color; - } else { - $value = json_decode($value); - $this->label = $value->label; - $this->color = $this->fetchColorOrColour($value); - $this->colour = $this->color; - } + if($this->validateJson($value)){ + // typecast our object to an array + $colorData = (array)json_decode($value); + $value = null; + $color = array_filter($colorData); + // if our array has usable data + if (!empty($colorData['label'])) + { + $this->label = $colorData['label']; + $this->color = $colorData['color']; + } + // else ensure we return a null value (only really needed for old data stores) + else { + $value = null; + } + // else ensure we return a null value + } else { + $value = null; } } - public function __toString() - { - return $this->label; - } - - public function colours() - { - return $this->colorsToArray(); - } - - public function colors() + // making sure we have json data, returns boolean(true) if this is the case + public function validateJson($value) { - return $this->colorsToArray(); + $json = json_decode($value); + return $json && $value != $json; } - public function labels() - { - return explode(',', $this->label); - } - - protected function fetchColorOrColour($value){ - if(property_exists($value, "colour") && "" === $value->color ){ - return $value->colour; - } - - return $value->color; - } - - protected function colorsToArray() - { - if(is_array($this->color)) - { - return $this->color; - } - - if (strstr($this->color, ';') !== false) { - return explode(';', $this->color); - } - - return explode(',', $this->color); - } } diff --git a/src/models/Settings.php b/src/models/Settings.php index 4564989..b394cdb 100644 --- a/src/models/Settings.php +++ b/src/models/Settings.php @@ -6,7 +6,6 @@ class Settings extends Model { - public $colours = []; public $colors = []; public $palettes = []; } diff --git a/src/templates/colourOption.twig b/src/templates/colourOption.twig index be2c218..d7aebec 100644 --- a/src/templates/colourOption.twig +++ b/src/templates/colourOption.twig @@ -1,14 +1,19 @@ {% set colours = option.color is defined ? option.color : option.colour %} {% set isDefault = option.default %} +{% set active = null %} {% set fieldValue = fieldValue ?? null %} +{% if fieldValue|length %} + {% set active = fieldValue.label ?? null %} +{% endif %} + {% if colours is iterable %} {# option.color is an array #} {% set value = { label: option.label, color: colours, } %} -