SpectrumColorpickerHelper::_generateJs()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 1
eloc 7
nc 1
nop 2
dl 0
loc 14
rs 10
c 2
b 1
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * Saito - The Threaded Web Forum
7
 *
8
 * @copyright Copyright (c) the Saito Project Developers
9
 * @link https://github.com/Schlaefer/Saito
10
 * @license http://opensource.org/licenses/MIT
11
 */
12
13
namespace SpectrumColorpicker\View\Helper;
14
15
use Cake\View\Helper;
16
use Cake\View\Helper\FormHelper;
17
use Cake\View\Helper\HtmlHelper;
18
19
/**
20
 * @property FormHelper $Form
21
 * @property HtmlHelper $Html
22
 */
23
class SpectrumColorpickerHelper extends Helper
24
{
25
    use Helper\IdGeneratorTrait;
26
27
    public $helpers = ['Form', 'Html', 'Url'];
28
29
    protected $_included = false;
30
31
    /**
32
     * Get Spectrum picker
33
     *
34
     * @param string $field model field name
35
     * @param array $options picker options
36
     * @return string picker form HTML
37
     */
38
    public function input($field, array $options = [])
39
    {
40
        $defaults = [
41
            'color-picker' => [
42
                'allowEmpty' => true,
43
                'preferredFormat' => 'hex',
44
                'showButtons' => false,
45
                'showInput' => true,
46
            ],
47
            'text' => [
48
                'label' => false,
49
                'maxlength' => '7',
50
                'type' => 'text',
51
            ],
52
        ];
53
54
        foreach (['color-picker', 'text'] as $key) {
55
            if (empty($options[$key])) {
56
                $options[$key] = [];
57
            }
58
            $options[$key] += $defaults[$key];
59
        }
60
61
        $html = $this->Form->control($field, $options['text']);
62
        $this->_generateJs($field, $options['color-picker']);
63
64
        return $html;
65
    }
66
67
    /**
68
     * Generate JS for picker
69
     *
70
     * @param string $field model field name to derive id-HTML-tag
71
     * @param array $options Spectrum picker options
72
     * @return void
73
     */
74
    protected function _generateJs($field, array $options = [])
75
    {
76
        $this->_includeAssets();
77
78
        $id = $this->_domId($field);
79
        $options = json_encode($options);
80
        // "hide.spectrum"-event: Spectrum doesn't apply color value if
81
        // the dialog is closed by pressing the open button again
82
        $js = "$(function() {
83
            let el = $('input#{$id}');
84
            el.spectrum({$options});
85
            el.on('hide.spectrum', function(e, color) { e.currentTarget.value = color === null ? '' : '#' + color.toHex(); });
86
        });";
87
        $this->Html->scriptBlock($js, ['block' => 'script']);
88
    }
89
90
    /**
91
     * Include assets (CSS, JS)
92
     *
93
     * @return void
94
     */
95
    protected function _includeAssets()
96
    {
97
        if ($this->_included) {
98
            return;
99
        }
100
        $this->_included = true;
101
        $this->Html->script(
102
            'SpectrumColorpicker.spectrum.js',
103
            ['block' => 'script-head']
104
        );
105
        $this->Html->css(
106
            'SpectrumColorpicker.spectrum.css',
107
            ['block' => 'css']
108
        );
109
    }
110
}
111