/
Color.php
59 lines (54 loc) · 1.74 KB
/
Color.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
52
53
54
55
56
57
58
59
<?php
namespace Concrete\Core\Form\Service\Widget;
use Concrete\Core\Http\Request;
use Concrete\Core\View\View;
class Color
{
/**
* Creates form fields and JavaScript includes to add a color picker widget.
* <code>
* $dh->output('background-color', '#f00');
* </code>.
*
* @param string $inputName
* @param string|null $value
* @param array $options
*/
public function output($inputName, $value = null, $options = [])
{
$r = Request::getInstance();
if ($r->request->has($inputName)) {
$value = h($r->request->get($inputName));
}
$strOptions = '';
$defaults = [
'value' => $value,
'type' => 'color',
'className' => 'ccm-widget-colorpicker',
'showInitial' => true,
'showInput' => true,
'allowEmpty' => true,
'cancelText' => t('Cancel'),
'chooseText' => t('Choose'),
'togglePaletteMoreText' => t('more'),
'togglePaletteLessText' => t('less'),
'noColorSelectedText' => t('No Color Selected'),
'preferredFormat' => 'rgb',
'showAlpha' => false,
'clearText' => t('Clear Color Selection'),
'appendTo' => '.ui-dialog',
];
$strOptions = json_encode(array_merge($defaults, $options));
$identifier = new \Concrete\Core\Utility\Service\Identifier();
$identifier = $identifier->getString(32);
echo <<<EOT
<input type="text" data-color-picker="{$identifier}" name="{$inputName}" value="{$value}" id="ccm-colorpicker-{$inputName}" />
<script>
$(function () {
$('[data-color-picker={$identifier}]').spectrum({$strOptions});
});
</script>
EOT
;
}
}