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 60 61 62 63 64 65 66 67 68 69 70 71 72 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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 | import Component from '@ember/component'; import layout from '../templates/components/el-checkbox'; import {computed, set} from "@ember/object"; export default Component.extend({ layout, // todo: focus tagName: 'label', classNames: ['el-checkbox'], classNameBindings: [ 'isChecked:is-checked', 'isDisabled:is-disabled', 'border:is-bordered', 'checkboxSize', ], attributeBindings: [ 'role', 'isChecked:aria-checked' ], role: 'checkbox', name: null, change: null, border: false, size: '', label: null, trueLabel: undefined, falseLabel: undefined, value: null, model: null, isLabelProvided: computed.or('falseLabel', 'trueLabel'), init() { this._super(...arguments); set(this, 'value', this.modelGet()); }, isChecked: computed('model', 'label', 'trueLabel', 'checked', function () { if (typeof this.checked === "boolean") { return this.checked; } if (typeof this.model === "boolean") { return this.model; } else if (Array.isArray(this.model)) { return this.model.indexOf(this.label) > -1; } else if (this.model !== null && this.model !== undefined) { return this.model === this.trueLabel; } return !!this.model; }), checkboxSize: computed('size', 'parent', 'parent.size', function () { let size = this.parent ? this.parent.size || this.size : this.size; return size ? 'el-checkbox-button--' + size : ''; }), compTabIndex: computed("indeterminate", function () { return this.indeterminate ? 0 : false; }), compRole: computed("indeterminate", function () { return this.indeterminate ? 'checkbox' : false; }), compAriaChecked: computed("indeterminate", function () { return this.indeterminate ? 'mixed' : false; }), isLimitDisabled: computed('min', 'max', 'parent', 'isChecked', function () { if (this.parent) { return !!(this.parent.max || this.parent.min) && (this.model.length >= this.parent.max && !this.isChecked) || (this.model.length <= this.parent.min && this.isChecked); } return false }), isDisabled: computed('parent', 'disabled', 'parent.disabled', 'isLimitDisabled', function () { return this.parent ? this.parent.disabled || this.disabled || this.isLimitDisabled : this.disabled; }), actions: { changed(value, name) { if (this.get('action')) { this.get('action')(value, name); } }, handleChange(ev) { if (this.isLimitExceeded) return; let value; if (typeof this.checked === "boolean") { value = !this.checked; } else { if (ev.target.checked) { value = this.trueLabel === undefined ? true : this.trueLabel; } else { value = this.falseLabel === undefined ? false : this.falseLabel; } } set(this, 'value', value); this.modelSet(this.value); if (this.action) { this.action(this.value); } } }, modelGet() { if (this.parent) { const model = this.model; if (typeof model === "boolean") { return model; } else if (Array.isArray(model)) { return model.indexOf(this.label) > -1; } else if (model !== null && model !== undefined) { return model === this.trueLabel; } } return this.model; }, modelSet(val) { if (this.parent) { set(this, 'isLimitExceeded', false); (this.parent.min !== undefined && val.length < this.parent.min && (set(this, 'isLimitExceeded', true))); (this.parent.max !== undefined && val.length > this.parent.max && (set(this, 'isLimitExceeded', true))); if (Array.isArray(this.model) && this.isLimitExceeded === false && !this.model.includes(this.label)) { set(this, 'model', [...this.model, this.label]); } else { const index = this.model.indexOf(this.label); this.model.splice(index, 1); set(this, 'model', [...this.model]); } } else { set(this, 'value', val); set(this, 'model', val); } }, }); |