├── _config └── .gitkeep ├── .gitattributes ├── screenshot.png ├── .idea ├── codeStyles │ ├── codeStyleConfig.xml │ └── Project.xml ├── vcs.xml ├── silverstripe-color-field.iml ├── modules.xml └── workspace.xml ├── templates └── RyanPotter │ └── SilverStripeColorField │ └── Forms │ └── ColorField.ss ├── .editorconfig ├── composer.json ├── client ├── css │ └── color-picker.css └── javascript │ ├── color-picker.js │ └── lib │ └── color-picker.min.js ├── README.md └── src └── Forms └── ColorField.php /_config/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | /.gitignore export-ignore 2 | /.scrutinizer.yml export-ignore 3 | -------------------------------------------------------------------------------- /screenshot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Rhym/silverstripe-color-field/HEAD/screenshot.png -------------------------------------------------------------------------------- /.idea/codeStyles/codeStyleConfig.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/silverstripe-color-field.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /templates/RyanPotter/SilverStripeColorField/Forms/ColorField.ss: -------------------------------------------------------------------------------- 1 |
2 | 5 | 6 | 7 | 8 |
9 | -------------------------------------------------------------------------------- /.idea/codeStyles/Project.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 11 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # This file is for unifying the coding style for different editors and IDEs 2 | # editorconfig.org 3 | 4 | root = true 5 | 6 | [*] 7 | end_of_line = lf 8 | charset = utf-8 9 | trim_trailing_whitespace = true 10 | insert_final_newline = true 11 | indent_style = space 12 | indent_size = 2 13 | 14 | [**.js] 15 | indent_style = space 16 | indent_size = 2 17 | 18 | [**.scss] 19 | indent_style = space 20 | indent_size = 2 21 | 22 | [**.css] 23 | indent_style = space 24 | indent_size = 2 25 | 26 | [**.php] 27 | indent_style = space 28 | indent_size = 2 29 | 30 | [**.html] 31 | indent_style = space 32 | indent_size = 2 33 | 34 | [*.md] 35 | trim_trailing_whitespace = false 36 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ryanpotter/silverstripe-color-field", 3 | "description": "Silverstripe Color Field", 4 | "type": "silverstripe-vendormodule", 5 | "keywords": [ 6 | "silverstripe" 7 | ], 8 | "license": "BSD-3-Clause", 9 | "authors": [ 10 | { 11 | "name": "Ryan Potter", 12 | "homepage": "http://ryanpotter.co.nz/", 13 | "role": "Developer" 14 | } 15 | ], 16 | "require": { 17 | "silverstripe/framework": "^4.0 || ^5.0" 18 | }, 19 | "support": { 20 | "issues": "https://github.com/Rhym/silverstripe-color-field/issues" 21 | }, 22 | "extra": { 23 | "installer-name": "silverstripe-color-field", 24 | "expose": [ 25 | "client" 26 | ] 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /client/css/color-picker.css: -------------------------------------------------------------------------------- 1 | .c-color-picker { 2 | position: relative; 3 | font-size: 0; 4 | } 5 | 6 | .c-color-picker__label { 7 | font-size: 13px; 8 | padding: 0.5385rem 1em; 9 | border: 1px solid #cbd3df; 10 | border-right-width: 0; 11 | margin: 0; 12 | display: inline-block; 13 | cursor: pointer; 14 | border-top-left-radius: 0.23rem; 15 | border-bottom-left-radius: 0.23rem; 16 | } 17 | 18 | .c-color-picker__text { 19 | 20 | } 21 | 22 | .c-color-picker__wrapper { 23 | display: inline-block; 24 | border-top-right-radius: 0.23rem; 25 | border-bottom-right-radius: 0.23rem; 26 | } 27 | 28 | .color.field .c-color-picker__input { 29 | border-color: rgba(0, 0, 0, 0.25); 30 | display: inline-block; 31 | width: auto; 32 | border-top-left-radius: 0; 33 | border-bottom-left-radius: 0; 34 | } 35 | 36 | .color.field .c-color-picker__input:focus { 37 | border-color: rgba(0, 0, 0, 0.25); 38 | } 39 | 40 | .iris-picker { 41 | position: absolute !important; 42 | z-index: 99999; 43 | } 44 | -------------------------------------------------------------------------------- /client/javascript/color-picker.js: -------------------------------------------------------------------------------- 1 | ; 2 | (function ($) { 3 | $.entwine('color-picker', function ($) { 4 | $(document).on('click', function (e) { 5 | if (!$(e.target).is('.js-color-picker, .iris-picker, .iris-picker-inner')) { 6 | $('.js-color-picker').iris('hide'); 7 | } 8 | }); 9 | $('.js-color-picker').entwine({ 10 | onmatch: function () { 11 | var palettes = $('.js-color-picker').data('palette'); 12 | $('.js-color-picker').iris({ 13 | palettes: palettes, 14 | change: function (event, ui) { 15 | var $c, $r, $g, $b, $mid; 16 | $(this).css('backgroundColor', ui.color.toString()); 17 | $(this).parent().css('backgroundColor', ui.color.toString()); 18 | } 19 | }); 20 | this.on('click', function (e) { 21 | $('.js-color-picker').iris('hide'); 22 | $(this).iris('show'); 23 | }); 24 | this._super(); 25 | }, 26 | onunmatch: function () { 27 | this._super(); 28 | } 29 | }); 30 | }); 31 | })(jQuery); 32 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SilverStripe Color Field 2 | 3 | [![Latest Stable Version](https://poser.pugx.org/ryanpotter/silverstripe-color-field/v/stable)](https://packagist.org/packages/ryanpotter/silverstripe-color-field) 4 | [![Total Downloads](https://poser.pugx.org/ryanpotter/silverstripe-color-field/downloads)](https://packagist.org/packages/ryanpotter/silverstripe-color-field) 5 | [![Monthly Downloads](https://poser.pugx.org/ryanpotter/silverstripe-color-field/d/monthly)](https://packagist.org/packages/ryanpotter/silverstripe-color-field) 6 | 7 | A colour picker field for the Silverstripe CMS. 8 | 9 | Uses [Iris Colour Picker](https://github.com/Automattic/Iris). 10 | 11 | ### Installation 12 | 13 | Installation with composer: 14 | 15 | ```bash 16 | composer require ryanpotter/silverstripe-color-field 17 | ``` 18 | 19 | ### Usage 20 | 21 | ```php 22 | use RyanPotter\SilverStripeColorField\Forms\ColorField; 23 | ``` 24 | 25 | ```php 26 | private static $db = [ 27 | 'MyColor' => DBVarchar::class . '(7)', 28 | ]; 29 | ``` 30 | 31 | ```php 32 | ColorField::create('MyColor') 33 | ``` 34 | 35 | ### Screenshot 36 | 37 | ![Screenshot](https://github.com/Rhym/silverstripe-color-field/blob/master/screenshot.png) 38 | 39 | ### Configuration 40 | 41 | Add as many colours as you want to the palettes. 42 | 43 | ```yml 44 | RyanPotter\SilverStripeColorField\Forms\ColorField: 45 | colors: 46 | - '#1976D2' 47 | - '#2196F3' 48 | - '#BBDEFB' 49 | - '#FFFFFF' 50 | - '#FF4081' 51 | - '#212121' 52 | - '#727272' 53 | - '#B6B6B6' 54 | ``` 55 | -------------------------------------------------------------------------------- /src/Forms/ColorField.php: -------------------------------------------------------------------------------- 1 | addExtraClass('text'); 47 | } 48 | 49 | /** 50 | * @param array $properties 51 | * @return DBHTMLText 52 | */ 53 | public function Field($properties = []): DBHTMLText 54 | { 55 | $this->addExtraClass('c-color-picker__input js-color-picker'); 56 | 57 | /** 58 | * Assign a background color to the field. 59 | */ 60 | $style = sprintf( 61 | 'background-image: none; background-color: %s; color: %s;', 62 | $this->value ? $this->value : '#ffffff', 63 | $this->getTextColor() 64 | ); 65 | $this->setAttribute('style', $style); 66 | 67 | /** 68 | * Apply custom data attributes for the Iris Color Picker. 69 | */ 70 | if ($colors = $this->config()->colors) { 71 | $pallets = '"' . implode('", "', $colors) . '"'; 72 | $this->setAttribute('data-palette', '[' . $pallets . ']'); 73 | } 74 | 75 | $properties['type'] = 'text'; 76 | $properties['tabindex'] = $this->getAttribute('tabindex'); 77 | $properties['maxlength'] = ($this->maxLength) ? $this->maxLength : null; 78 | $properties['size'] = ($this->maxLength) ? min($this->maxLength, 30) : null; 79 | 80 | /** 81 | * Check if the field is disabled. 82 | */ 83 | if ($this->disabled) { 84 | $properties['disabled'] = 'disabled'; 85 | } 86 | 87 | $obj = ($properties) ? $this->customise($properties) : $this; 88 | 89 | return $obj->renderWith($this->getTemplates()); 90 | } 91 | 92 | /** 93 | * @param \SilverStripe\Forms\Validator $validator 94 | * @return bool 95 | */ 96 | function validate($validator): bool 97 | { 98 | return true; 99 | } 100 | 101 | /** 102 | * @desc Set the colour to be white or black depending on the shade of the background colour of the Field. 103 | * @return string 104 | */ 105 | protected function getTextColor(): string 106 | { 107 | if ($this->value) { 108 | $c = intval(str_replace("#", "", $this->value), 16); 109 | $r = $c >> 16; 110 | $g = ($c >> 8) & 0xff; 111 | $b = $c & 0xff; 112 | $mid = ($r + $g + $b) / 3; 113 | 114 | return ($mid > 127) ? '#000000' : '#ffffff'; 115 | } else { 116 | return '#000000'; 117 | } 118 | } 119 | } 120 | -------------------------------------------------------------------------------- /.idea/workspace.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 14 | 15 | $PROJECT_DIR$/composer.json 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 53 | 54 | 59 | 60 | 61 | 62 | 63 | true 64 | DEFINITION_ORDER 65 | 66 | 67 | 68 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 |