var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) { if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it"); return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver); }; var _OptionsController_instances, _OptionsController_shouldChangeState, _OptionsController_activateKey, _OptionsController_deactivateKey, _OptionsController_getElementKey; import SyncedBooleanAttributesController from '../synced_boolean_attributes_controller/synced_boolean_attributes_controller'; class OptionsController extends SyncedBooleanAttributesController { constructor() { super(...arguments); _OptionsController_instances.add(this); this.outletUpdate = this.select; } select(event, updateTo = {}) { var _a; const activeOptions = updateTo.data; for (const index in this.optionTargets) { const target = this.optionTargets[index]; const wasSelected = target === event.currentTarget; const isCurrentlyActive = (_a = this.getValueForElement(target)) !== null && _a !== void 0 ? _a : false; const optionKey = __classPrivateFieldGet(this, _OptionsController_instances, "m", _OptionsController_getElementKey).call(this, target); const shouldChangeState = activeOptions !== undefined ? !!activeOptions[optionKey] !== isCurrentlyActive : __classPrivateFieldGet(this, _OptionsController_instances, "m", _OptionsController_shouldChangeState).call(this, isCurrentlyActive, wasSelected); if (shouldChangeState) { const willBeActive = !isCurrentlyActive; this.updateAttributesForElement(target, willBeActive); willBeActive ? __classPrivateFieldGet(this, _OptionsController_instances, "m", _OptionsController_activateKey).call(this, optionKey) : __classPrivateFieldGet(this, _OptionsController_instances, "m", _OptionsController_deactivateKey).call(this, optionKey); } } this.sendToOutlets(event, Object.assign(Object.assign({}, updateTo), { data: this.activeOptionsValue })); } optionTargetConnected(element) { const key = __classPrivateFieldGet(this, _OptionsController_instances, "m", _OptionsController_getElementKey).call(this, element); const isActive = this.activeOptionsValue[key] || this.doesElementHaveOnAttrs(element); if (isActive) { __classPrivateFieldGet(this, _OptionsController_instances, "m", _OptionsController_activateKey).call(this, key); } else { __classPrivateFieldGet(this, _OptionsController_instances, "m", _OptionsController_deactivateKey).call(this, key); } this.updateAttributesForElement(element, isActive); } getValueForElement(element) { var _a; const optionKey = __classPrivateFieldGet(this, _OptionsController_instances, "m", _OptionsController_getElementKey).call(this, element); return (_a = this.activeOptionsValue[optionKey]) !== null && _a !== void 0 ? _a : false; } getState() { return this.activeOptionsValue; } } _OptionsController_instances = new WeakSet(), _OptionsController_shouldChangeState = function _OptionsController_shouldChangeState(isCurrentlyActive, wasSelected) { if (!wasSelected && !isCurrentlyActive) { // Not currently on and wasn't selected, no reason to change return false; } if (!wasSelected && isCurrentlyActive && this.isMultiValue) { // It wasn't selected and it is on. However, multi value is on so just leave it return false; } if (wasSelected && isCurrentlyActive && !this.toggleableValue) { // It was selected and it's on. But toggle isn't on so just leave it return false; } // Wasn't selected and it's active but only one can be active // Was selected and it's active but toggling behavior means it should deactivate // Was selected and it's not on, so it just needs to be turned on return true; }, _OptionsController_activateKey = function _OptionsController_activateKey(key) { this.activeOptionsValue = Object.assign(Object.assign({}, this.activeOptionsValue), { [key]: true }); }, _OptionsController_deactivateKey = function _OptionsController_deactivateKey(key) { const copy = Object.assign({}, this.activeOptionsValue); delete copy[key]; this.activeOptionsValue = copy; }, _OptionsController_getElementKey = function _OptionsController_getElementKey(element) { const elementValue = element === null || element === void 0 ? void 0 : element.getAttribute('data-option-value'); if (elementValue) return elementValue; const content = element === null || element === void 0 ? void 0 : element.textContent; if (content === null) { throw new Error(`${element.tagName} was given as an options target without a data-option-value or textContent. One must be provided to identify the target.`); } return content.trim(); }; OptionsController.outlets = SyncedBooleanAttributesController.outlets; OptionsController.targets = ['option']; OptionsController.values = Object.assign(Object.assign({}, SyncedBooleanAttributesController.values), { activeOptions: Object, isMulti: { type: Boolean, default: false }, toggleable: { type: Boolean, default: false } }); export default OptionsController;