{"version":3,"file":"group_controller-BYLgrId_.js","sources":["../../../app/javascript/controllers/filter/group_controller.js"],"sourcesContent":["import ApplicationController from '../application_controller'\n\nexport default class extends ApplicationController {\n static values = {\n active: Boolean,\n max: Number,\n }\n\n static targets = [\n 'list',\n 'item',\n 'template',\n 'orValueButton',\n ]\n\n connect() {\n super.connect()\n\n if(!this.activeValue) {\n this.element.querySelectorAll('input, select').forEach(this.disable)\n }\n\n this.itemTargets[0].querySelector('input').focus()\n }\n\n onFilterItemChange({ target }) {\n if(this.maxValue && this.itemTargets.length >= this.maxValue) {\n return this.hide(this.orValueButtonTarget)\n }\n\n const parent = target.closest('[data-filter--group-target=\"item\"]')\n const lastItem = this.itemTargets[this.itemTargets.length - 1]\n\n if(parent === lastItem) {\n if(target.value) {\n this.show(this.orValueButtonTarget)\n } else {\n this.hide(this.orValueButtonTarget)\n }\n }\n }\n\n onInputEnter({ target }) {\n if(!target.value) return\n\n const parent = target.closest('[data-filter--group-target=\"item\"]')\n const lastItem = this.itemTargets[this.itemTargets.length - 1]\n\n if(parent === lastItem) {\n this.addNewItem()\n }\n }\n\n addNewItem() {\n const lastItem = this.itemTargets[this.itemTargets.length - 1]\n const newItem = this.templateTarget.cloneNode(true)\n\n newItem.setAttribute('data-filter--group-target', 'item')\n newItem.setAttribute('data-controller', 'filter--group-item')\n newItem.classList.remove('hidden')\n newItem.querySelectorAll('input, select').forEach(this.enable)\n\n this.listTarget.insertBefore(newItem, this.templateTarget)\n\n this.nextTick(() => {\n this.focus(newItem.querySelector('input'))\n this.hide(this.orValueButtonTarget)\n })\n\n lastItem.querySelector(\"[data-filter--group-item-target='removeButtonContainer']\").classList.remove('hidden')\n }\n\n removeItem({ currentTarget }) {\n const item = currentTarget.closest('[data-filter--group-target=\"item\"]')\n item.remove()\n }\n\n itemTargetDisconnected() {\n const lastItem = this.itemTargets[this.itemTargets.length - 1]\n\n const input = lastItem.querySelector('input')\n this.focus(input, { moveCursorToEnd: true })\n\n if(input.value) {\n this.show(this.orValueButtonTarget)\n }\n\n if(this.itemTargets.length > 1) return\n\n this.hide(lastItem.querySelector('[data-filter--group-item-target=\"removeButtonContainer\"]'))\n this.hide(lastItem.querySelector('[data-filter--group-item-target=\"orLabel\"]'))\n }\n\n showAndEnable() {\n this.show()\n\n this.itemTargets.forEach((item) => {\n item.querySelectorAll('input, select').forEach(this.enable)\n this.focusFirstItem()\n })\n\n this.focusFirstItem()\n }\n\n hideAndDisable() {\n this.hide()\n this.element.querySelectorAll('input, select').forEach(this.disable)\n }\n\n focusFirstItem() {\n this.nextTick(() => {\n this.focus(this.itemTargets[0].querySelector('input'))\n })\n }\n\n saveState() {\n this.itemTargets.forEach((item) => {\n this.dispatch('state:save', {\n target: item\n })\n })\n }\n\n restoreState() {\n this.itemTargets.forEach((item) => {\n this.dispatch('state:restore', {\n target: item\n })\n })\n }\n\n get limitReached() {\n return this.maxValue && this.itemTargets.length >= this.maxValue\n }\n}\n"],"names":["GroupController","ApplicationController","target","parent","lastItem","newItem","currentTarget","input","item","__publicField"],"mappings":"uNAEe,MAAKA,UAASC,CAAsB,CAajD,SAAU,CACR,MAAM,QAAO,EAET,KAAK,aACP,KAAK,QAAQ,iBAAiB,eAAe,EAAE,QAAQ,KAAK,OAAO,EAGrE,KAAK,YAAY,CAAC,EAAE,cAAc,OAAO,EAAE,MAAK,CACpD,CAEE,mBAAmB,CAAE,OAAAC,GAAU,CAC7B,GAAG,KAAK,UAAY,KAAK,YAAY,QAAU,KAAK,SAClD,OAAO,KAAK,KAAK,KAAK,mBAAmB,EAG3C,MAAMC,EAASD,EAAO,QAAQ,oCAAoC,EAC5DE,EAAW,KAAK,YAAY,KAAK,YAAY,OAAS,CAAC,EAE1DD,IAAWC,IACTF,EAAO,MACR,KAAK,KAAK,KAAK,mBAAmB,EAElC,KAAK,KAAK,KAAK,mBAAmB,EAG1C,CAEE,aAAa,CAAE,OAAAA,GAAU,CACvB,GAAG,CAACA,EAAO,MAAO,OAElB,MAAMC,EAASD,EAAO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