1 | const EventEmitter = require('events').EventEmitter
|
2 | const inherits = require('inherits')
|
3 | const format = require('param-case')
|
4 | const extend = require('just-extend');
|
5 |
|
6 | module.exports = Checkbox
|
7 | inherits(Checkbox, EventEmitter)
|
8 |
|
9 | function Checkbox (opts) {
|
10 | if (!(this instanceof Checkbox)) return new Checkbox(opts)
|
11 |
|
12 | var self = this;
|
13 |
|
14 | if (!this.group) {
|
15 | this.group = document.createElement('fieldset');
|
16 | this.group.className = 'settings-panel-checkbox-group';
|
17 | opts.container.appendChild(this.group);
|
18 | }
|
19 |
|
20 |
|
21 | if (!opts.options) {
|
22 | let input = this.group.querySelector('.settings-panel-checkbox');
|
23 | let label = this.group.querySelector('.settings-panel-checkbox-label');
|
24 | if (!input) {
|
25 | this.element = input = this.group.appendChild(document.createElement('input'));
|
26 | input.className = 'settings-panel-checkbox';
|
27 | this.labelEl = label = this.group.appendChild(document.createElement('label'));
|
28 | this.labelEl.innerHTML = ' ';
|
29 | label.className = 'settings-panel-checkbox-label';
|
30 | input.onchange = function (data) {
|
31 | self.emit('input', data.target.checked)
|
32 | }
|
33 | setTimeout(function () {
|
34 | self.emit('init', input.checked)
|
35 | })
|
36 | }
|
37 | }
|
38 |
|
39 | else {
|
40 | var html = '';
|
41 |
|
42 | if (Array.isArray(opts.options)) {
|
43 | for (i = 0; i < opts.options.length; i++) {
|
44 | let option = opts.options[i]
|
45 | html += createOption(option, option);
|
46 | }
|
47 | } else {
|
48 | for (let key in opts.options) {
|
49 | html += createOption(opts.options[key], key);
|
50 | }
|
51 | }
|
52 |
|
53 | this.group.innerHTML = html;
|
54 |
|
55 | this.group.addEventListener('change', () => {
|
56 | let v = [];
|
57 | [].slice.call(this.group.querySelectorAll('.settings-panel-checkbox')).forEach(el => {
|
58 | if (el.checked) v.push(el.getAttribute('data-value'));
|
59 | });
|
60 |
|
61 | this.emit('input', v);
|
62 | });
|
63 | setTimeout(() => {
|
64 | this.emit('init')
|
65 | });
|
66 | }
|
67 |
|
68 | function createOption (label, value) {
|
69 | let htmlFor = `settings-panel-${format(opts.panel.id)}-${format(opts.id)}-input-${format(value)}`;
|
70 |
|
71 | let html = `<input type="checkbox" class="settings-panel-checkbox" ${value === opts.value ? 'checked' : ''} id="${htmlFor}" name="${format(opts.id)}" data-value="${value}" title="${value}"/><label for="${htmlFor}" class="settings-panel-checkbox-label" title="${value}">${label}</label>`;
|
72 | return html;
|
73 | }
|
74 |
|
75 | this.update(opts);
|
76 | }
|
77 |
|
78 | Checkbox.prototype.update = function (opts) {
|
79 | extend(this, opts);
|
80 |
|
81 | if (!this.options) {
|
82 | this.labelEl.htmlFor = this.id
|
83 | this.element.id = this.id
|
84 | this.element.type = 'checkbox';
|
85 | this.element.checked = !!this.value;
|
86 | }
|
87 | else {
|
88 | if (!Array.isArray(this.value)) this.value = [this.value];
|
89 | let els = [].slice.call(this.group.querySelectorAll('.settings-panel-checkbox'));
|
90 | els.forEach(el => {
|
91 | if (this.value.indexOf(el.getAttribute('data-value')) >= 0) {
|
92 | el.checked = true;
|
93 | }
|
94 | else {
|
95 | el.checked = false;
|
96 | }
|
97 | });
|
98 | }
|
99 |
|
100 | this.group.disabled = !!this.disabled;
|
101 |
|
102 | return this;
|
103 | }
|