Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions modules/backend/formwidgets/ColorPicker.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ class ColorPicker extends FormWidgetBase
*/
public $allowEmpty = false;

/**
* @var string Preferred color format [rgb|prgb|hex|hsv|hsl]
*/
public $format = 'hex';

/**
* @var bool Show opacity slider
*/
Expand Down Expand Up @@ -68,6 +73,7 @@ class ColorPicker extends FormWidgetBase
public function init()
{
$this->fillFromConfig([
'format',
'availableColors',
'allowEmpty',
'showAlpha',
Expand All @@ -93,6 +99,7 @@ public function prepareVars()
$this->vars['name'] = $this->getFieldName();
$this->vars['value'] = $value = $this->getLoadValue();
$this->vars['availableColors'] = $availableColors = $this->getAvailableColors();
$this->vars['format'] = $this->format;
$this->vars['allowEmpty'] = $this->allowEmpty;
$this->vars['showAlpha'] = $this->showAlpha;
$this->vars['readOnly'] = $this->readOnly;
Expand Down
58 changes: 51 additions & 7 deletions modules/backend/formwidgets/colorpicker/assets/js/colorpicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,57 @@
}

ColorPicker.DEFAULTS = {
preferredFormat: 'hex',
showAlpha: false,
allowEmpty: false,
dataLocker: null,
disabled: false
}

ColorPicker.prototype.init = function() {

var self = this

var retriveColorFormat = function() {
var format = (
(
self.options.showAlpha &&
self.options.preferredFormat != 'hex'
) ||
(
!self.options.showAlpha &&
self.options.preferredFormat == 'hex'
)
) ? self.options.preferredFormat : 'rgb'
return format
}

var getColorStringFormat = function(color, format) {
var colorFormat = ''
if (color) {
switch(format) {
case 'rgb':
colorFormat = color.toRgbString()
break;
case 'prgb':
colorFormat = color.toPercentageRgbString()
break;
case 'hsv':
colorFormat = color.toHsvString()
break;
case 'hsl':
colorFormat = color.toHslString()
break;
case 'hex':
default:
colorFormat = color.toHexString()
break;
}
}
return colorFormat;
}

this.$preferredFormat = retriveColorFormat()
this.$dataLocker = $(this.options.dataLocker, this.$el)
this.$colorList = $('>ul', this.$el)
this.$customColor = $('[data-custom-color]', this.$el)
Expand All @@ -52,29 +95,30 @@
*/
if (this.$customColor.length) {
this.$customColor.spectrum({
preferredFormat: 'hex',
showInput: true,
showAlpha: this.options.showAlpha,
preferredFormat: this.$preferredFormat,
allowEmpty: this.options.allowEmpty,
color: this.$customColor.data('hexColor'),
chooseText: $.wn.lang.get('colorpicker.choose', 'Ok'),
cancelText: '⨯',
appendTo: 'parent',
disabled: this.options.disabled,
hide: function(color) {
var hex = color ? color.toHexString() : ''
self.$customColorSpan.css('background', hex)
var colorStringFormat = getColorStringFormat(color, self.$preferredFormat)
self.$customColorSpan.css('background', colorStringFormat)
},
show: function(color) {
self.selectColor(self.$customColor)
},
move: function(color) {
var hex = color ? color.toHexString() : ''
self.$customColorSpan.css('background', hex)
var colorStringFormat = getColorStringFormat(color, self.$preferredFormat)
self.$customColorSpan.css('background', colorStringFormat)
},
change: function(color) {
var hex = color ? color.toHexString() : ''
self.setCustomColor(hex)
var colorStringFormat = getColorStringFormat(color, self.$preferredFormat)
self.$customColorSpan.css('background', colorStringFormat)
self.setCustomColor(colorFormat)
}
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
id="<?= $this->getId() ?>"
class="field-colorpicker <?php if($readOnly || $disabled): ?>disabled<?php endif; ?>"
data-control="colorpicker"
data-preferred-format="<?= $format ?>"
<?php if ($showAlpha): ?>data-show-alpha="<?= $showAlpha ?>"<?php endif ?>
<?php if ($allowEmpty): ?>data-allow-empty="<?= $allowEmpty ?>"<?php endif ?>
data-data-locker="#<?= $this->getId('input') ?>"
Expand Down