{"version":3,"file":"search_controller-s6EMQ9Oy.js","sources":["../../../app/javascript/controllers/audience/search_controller.js"],"sourcesContent":["import { get } from '@rails/request.js'\n\nimport ApplicationController from '../application_controller'\n\nexport default class extends ApplicationController {\n static values = {\n turboControl: { type: Boolean, default: true },\n search: { type: String, default: '' },\n url: String,\n }\n\n static targets = [\n 'input',\n 'url',\n 'skeletonList',\n 'content',\n 'clearButton',\n 'sortOption',\n ]\n\n initialize() {\n super.initialize()\n\n this.search = this.search.bind(this)\n this.popStateListener = this.popStateListener.bind(this)\n\n this.urlValue ||= window.location.href\n }\n\n connect() {\n if(this.turboControlValue) {\n window.addEventListener('popstate', this.popStateListener)\n this.nextTick(this.popStateListener)\n }\n\n super.connect()\n }\n\n disconnect() {\n if(this.turboControlValue) {\n window.removeEventListener('popstate', this.popStateListener)\n }\n\n super.disconnect()\n }\n\n popStateListener() {\n const url = new URL(this.urlValue)\n const search = url.searchParams.get('search') || ''\n\n if(search !== this.searchValue) {\n this.inputTarget.value = search\n this.searchValue = search\n\n this.search({ action: 'replace' })\n }\n\n if(this.didSearch && !search) {\n this.clear()\n }\n\n const selected = new URLSearchParams(window.location.search).get('order') || 'activity'\n\n this.sortOptionTargets.forEach(option => {\n if(option.dataset.value === selected) {\n this.addClass(option, 'bg-lavender-light')\n } else {\n this.removeClass(option, 'bg-lavender-light')\n }\n })\n }\n\n scheduleTimeout() {\n if(this.inputTarget.value) {\n this.show(this.clearButtonTarget)\n this.debounce(this.search)\n } else {\n clearTimeout(this.debounceTimeout)\n\n this.search()\n this.clear()\n }\n }\n\n search({ action } = {}) {\n if(!this.hasContentTarget) return\n\n this.show(this.skeletonListTarget)\n this.hide(this.contentTarget)\n\n this.searchValue = this.inputTarget.value\n\n const url = new URL(this.urlValue)\n\n url.searchParams.delete('viewed')\n url.searchParams.delete('search')\n\n if(this.inputTarget.value) {\n url.searchParams.append('search', this.inputTarget.value)\n }\n\n if(this.turboControlValue) {\n Turbo.visit(url.toString(), { frame: 'main-content', action: action || 'advance' })\n } else {\n get(url.toString(), { responseKind: 'turbo-stream' })\n }\n }\n\n clear() {\n this.inputTarget.value = ''\n\n this.hide(this.clearButtonTarget)\n this.hide(this.skeletonListTarget)\n\n this.show(this.contentTarget)\n\n this.searchValue = ''\n this.search({ action: 'replace' })\n }\n\n clearByEscape() {\n if(this.inputTarget.value) {\n this.clear()\n }\n }\n\n reset() {\n this.inputTarget.value = ''\n this.searchValue = ''\n\n this.sortOptionTargets.forEach(option => {\n if(option.dataset.value === 'activity') {\n this.addClass(option, 'bg-lavender-light')\n } else {\n this.removeClass(option, 'bg-lavender-light')\n }\n })\n }\n\n sort({ currentTarget }) {\n const { value } = currentTarget.dataset\n this.sortValue = value\n\n this.sortOptionTargets.forEach((target) => {\n if(target === currentTarget) {\n this.addClass(target, 'bg-lavender-light')\n } else {\n this.removeClass(target, 'bg-lavender-light')\n }\n })\n\n currentTarget.blur()\n\n const url = new URL(this.urlValue)\n url.searchParams.set('order', value)\n\n Turbo.visit(url.toString(), { frame: 'main-content', action: 'advance' })\n }\n\n // private\n\n searchValueChanged() {\n if(this.searchValue) {\n this.show(this.clearButtonTarget)\n } else {\n this.hide(this.clearButtonTarget)\n }\n }\n\n get didSearch() {\n return this.searchValue !== ''\n }\n}\n"],"names":["search_controller","ApplicationController","search","selected","option","action","url","get","currentTarget","value","target","__publicField"],"mappings":"+PAIe,MAAKA,UAASC,CAAsB,CAgBjD,YAAa,CACX,MAAM,WAAU,EAEhB,KAAK,OAAS,KAAK,OAAO,KAAK,IAAI,EACnC,KAAK,iBAAmB,KAAK,iBAAiB,KAAK,IAAI,EAEvD,KAAK,WAAL,KAAK,SAAa,OAAO,SAAS,KACtC,CAEE,SAAU,CACL,KAAK,oBACN,OAAO,iBAAiB,WAAY,KAAK,gBAAgB,EACzD,KAAK,SAAS,KAAK,gBAAgB,GAGrC,MAAM,QAAO,CACjB,CAEE,YAAa,CACR,KAAK,mBACN,OAAO,oBAAoB,WAAY,KAAK,gBAAgB,EA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