{"version":3,"file":"../../javascript/downlight-selector.js","names":["DownlightSelector","selector","step","steps","data","validData","getParams","Object","fromEntries","URLSearchParams","window","location","search","entries","toQueryString","toString","getStepElement","Array","from","document","querySelectorAll","find","item","dataset","getStepElementByName","name","getChoiceElement","choice","showElement","element","style","display","getComputedStyle","hideElement","setStep","pushUrl","updateUrl","forEach","classList","remove","getImagesForStep","setAttribute","currentStep","add","fetch","method","headers","credentials","then","response","ok","Error","status","json","undefined","payload","redirect","skipStep","setSkipped","incStep","keys","options","key","option","figure","querySelector","innerHTML","image","closest","toggle","valid","assign","catch","error","console","log","finally","parseInt","getPreviousStep","previousStep","previousElementSibling","setData","value","previousChoiceParams","label","previousChoices","JSON","stringify","trimData","validParams","parsedParams","parse","params","history","pushState","pathname","installation","load","param","init","getElementById","getAttribute","popup","body","appendChild","delegateDownlightEvent","type","callback","addEventListener","event","target","documentElement","contains","initDownlightSelector","button","preventDefault","link","choiceValue","pair","split","popupSelector","overlay","createElement","className","popupEvent","once","readyState"],"sources":["downlight-selector.js"],"mappings":"AAAA,IAAIA,kBAAoB,CACpBC,SAAU,KACVC,KAAM,EACNC,MAAO,EACPC,KAAM,CAAC,EACPC,UAAW,CAAC,EAEZC,UAAW,WACP,OAAOC,OAAOC,YAAY,IAAIC,gBAAgBC,OAAOC,SAASC,QAAQC,UAC1E,EAEAC,cAAe,SAAUV,GACrB,OAAO,IAAIK,gBAAgBL,GAAMW,UACrC,EAEAC,eAAgB,SAAUd,GACtB,OAAOe,MAAMC,KAAKC,SAASC,iBAAiB,uBAAuBC,KAAK,SAAUC,GAC9E,OAAOA,EAAKC,QAAQrB,OAASA,EAAKa,UACtC,IAAM,IACV,EAEAS,qBAAsB,SAAUC,GAC5B,OAAOR,MAAMC,KAAKC,SAASC,iBAAiB,uBAAuBC,KAAK,SAAUC,GAC9E,OAAOA,EAAKC,QAAQE,OAASA,CACjC,IAAM,IACV,EAEAC,iBAAkB,SAAUC,GACxB,OAAOV,MAAMC,KAAKC,SAAS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AC7BvH,kBAAkBiF,QAAQqC,EAAK,GAAIA,EAAK,IAExC,IAAIE,EAAgB7F,EAAOJ,QAAQ+E,MACnC,QAAsBhD,IAAlBkE,EAA6B,CAC7B,IAAIC,EAAUtG,SAASuG,cAAc,OACrCD,EAAQE,UAAY,oBACpBxG,SAASoF,KAAKC,YAAYiB,GAE1B,IAAInB,EAAQnF,SAAS8C,cAAcuD,GACnCxH,kBAAkB4B,YAAY0E,GAE1BA,GACAA,EAAMM,iBAAiB,QAAS,SAA2BgB,GAClDA,EAAWd,OAAO1C,QAAQ,4BAI/BpE,kBAAkBiC,YAAYqE,GAC9BmB,EAAQlF,SACZ,EAAG,CAACsF,MAAM,GAElB,MACI7H,kBAAkB2D,SAzBtB,CALA,CAgCJ,EACJ,CAE4B,YAAxBxC,SAAS2G,WACT3G,SAASyF,iBAAiB,mBAAoBK,uBAE9CA,wBAGJvG,OAAOkG,iBAAiB,WAAY,WAChC5G,kBAAkBiG,KAAKjG,kBAAkBM,YAC7C","ignoreList":[],"sourceRoot":"../../../../resources/javascript","sourcesContent":["var DownlightSelector = {\n    selector: null,\n    step: 1,\n    steps: 0,\n    data: {},\n    validData: {},\n\n    getParams: function () {\n        return Object.fromEntries(new URLSearchParams(window.location.search).entries());\n    },\n\n    toQueryString: function (data) {\n        return new URLSearchParams(data).toString();\n    },\n\n    getStepElement: function (step) {\n        return Array.from(document.querySelectorAll('.js-downlight-step')).find(function (item) {\n            return item.dataset.step === step.toString();\n        }) || null;\n    },\n\n    getStepElementByName: function (name) {\n        return Array.from(document.querySelectorAll('.js-downlight-step')).find(function (item) {\n            return item.dataset.name === name;\n        }) || null;\n    },\n\n    getChoiceElement: function (choice) {\n        return Array.from(document.querySelectorAll('.js-downlight-choice')).find(function (item) {\n            return item.dataset.choice === choice;\n        }) || null;\n    },\n\n    showElement: function (element) {\n        if (!element) {\n            return;\n        }\n\n        element.style.display = '';\n\n        if (window.getComputedStyle(element).display === 'none') {\n            element.style.display = 'block';\n        }\n    },\n\n    hideElement: function (element) {\n        if (!element) {\n            return;\n        }\n\n        element.style.display = 'none';\n    },\n\n    setStep: function (step, pushUrl) {\n        DownlightSelector.step = step;\n        DownlightSelector.data.step = step;\n\n        if (pushUrl) {\n            DownlightSelector.updateUrl();\n        }\n\n        document.querySelectorAll('.js-downlight-step').forEach(function (item) {\n            item.classList.remove('downlight-step--active');\n        });\n\n        if (DownlightSelector.step != 1 && DownlightSelector.step != DownlightSelector.steps) {\n            DownlightSelector.getImagesForStep();\n        } else {\n            if (DownlightSelector.selector) {\n                DownlightSelector.selector.setAttribute('data-current-step', step);\n            }\n\n            var currentStep = DownlightSelector.getStepElement(DownlightSelector.step);\n            if (currentStep) {\n                currentStep.classList.add('downlight-step--active');\n            }\n        }\n    },\n\n    getImagesForStep: function () {\n        document.querySelectorAll('.loadable-overlay').forEach(function (item) {\n            item.classList.add('loadable-overlay--loading');\n        });\n\n        return fetch('/downlight-selector/images?' + DownlightSelector.toQueryString(DownlightSelector.data), {\n            method: 'GET',\n            headers: {\n                'X-Requested-With': 'XMLHttpRequest',\n            },\n            credentials: 'same-origin',\n        })\n            .then(function (response) {\n                if (!response.ok) {\n                    throw new Error('Request failed with status ' + response.status);\n                }\n\n                return response.json();\n            })\n            .then(function (data) {\n                if (data.payload.redirect !== undefined) {\n                    window.location.assign(data.payload.redirect);\n                    return;\n                }\n\n                var step = DownlightSelector.getStepElement(DownlightSelector.step);\n\n                if (!step) {\n                    return;\n                }\n\n                if (data.payload.skipStep !== undefined) {\n                    DownlightSelector.setSkipped(step.dataset.name);\n                    DownlightSelector.incStep();\n                    return;\n                }\n\n                Object.keys(data.payload.options).forEach(function (key) {\n                    var option = data.payload.options[key];\n                    var figure = step.querySelector('.js-downlight-choice[data-' + key + '] figure');\n\n                    if (!figure) {\n                        return;\n                    }\n\n                    figure.innerHTML = option.image;\n                    figure.closest('.downlight-choice')?.classList.toggle('downlight-choice--disabled', !option.valid);\n                });\n\n                if (DownlightSelector.selector) {\n                    DownlightSelector.selector.setAttribute('data-current-step', DownlightSelector.step);\n                }\n\n                step.classList.add('downlight-step--active');\n            })\n            .catch(function (error) {\n                console.log('downlight-selector.js getImagesForStep failed', error);\n            })\n            .finally(function () {\n                document.querySelectorAll('.loadable-overlay').forEach(function (item) {\n                    item.classList.remove('loadable-overlay--loading');\n                });\n            });\n    },\n\n    incStep: function () {\n        DownlightSelector.setStep(parseInt(DownlightSelector.step, 10) + 1, true);\n    },\n\n    setSkipped: function (name) {\n        DownlightSelector.data[name] = 'skip';\n    },\n\n    getPreviousStep: function (currentStep) {\n        var previousStep = currentStep ? currentStep.previousElementSibling : null;\n\n        if (!previousStep) {\n            return null;\n        }\n\n        var name = previousStep.dataset.name;\n        if (DownlightSelector.data[name] === 'skip') {\n            return DownlightSelector.getPreviousStep(previousStep);\n        }\n\n        return previousStep;\n    },\n\n    setData: function (name, value) {\n        var label = '';\n        var previousChoiceParams = {};\n\n        if (value === undefined) {\n            delete DownlightSelector.data[name];\n        } else {\n            previousChoiceParams = Object.assign({}, DownlightSelector.data);\n            if (previousChoiceParams[name] !== undefined) {\n                delete previousChoiceParams[name];\n            }\n\n            DownlightSelector.data[name] = value;\n\n            var choice = DownlightSelector.getChoiceElement(name + '=' + value);\n            label = choice ? choice.dataset.label || '' : '';\n        }\n\n        var step = DownlightSelector.getStepElementByName(name);\n        if (step) {\n            step.dataset.previousChoices = JSON.stringify(previousChoiceParams);\n        }\n    },\n\n    trimData: function (validParams) {\n        var parsedParams = validParams ? JSON.parse(validParams) : {};\n        DownlightSelector.validData = parsedParams;\n\n        Object.keys(DownlightSelector.data).forEach(function (key) {\n            if (DownlightSelector.validData[key] === undefined) {\n                delete DownlightSelector.data[key];\n\n                var step = DownlightSelector.getStepElementByName(key);\n                if (step) {\n                    delete step.dataset.previousChoices;\n                }\n            }\n        });\n    },\n\n    updateUrl: function () {\n        var params;\n\n        if (DownlightSelector.step < DownlightSelector.steps) {\n            params = DownlightSelector.toQueryString(DownlightSelector.data);\n            history.pushState(DownlightSelector.data, '', window.location.pathname + '?' + params);\n        } else {\n            if (DownlightSelector.data.installation == 'yes') {\n                delete DownlightSelector.data.installation;\n            }\n\n            Object.keys(DownlightSelector.data).forEach(function (key) {\n                if (DownlightSelector.data[key] === 'any') {\n                    delete DownlightSelector.data[key];\n                }\n            });\n\n            params = DownlightSelector.toQueryString(DownlightSelector.data);\n            window.location.assign('/downlight-selector/results?' + params);\n        }\n    },\n\n    load: function (params) {\n        if (!DownlightSelector.selector) {\n            return;\n        }\n\n        if (params === null) {\n            params = {};\n        }\n\n        if (params.step === undefined) {\n            params.step = 1;\n        }\n\n        Object.keys(DownlightSelector.data).forEach(function (param) {\n            if (!params[param]) {\n                DownlightSelector.setData(param, undefined);\n            }\n        });\n\n        Object.keys(params).forEach(function (param) {\n            DownlightSelector.setData(param, params[param]);\n        });\n\n        if (DownlightSelector.data.step >= DownlightSelector.steps) {\n            DownlightSelector.data.step--;\n        }\n\n        DownlightSelector.setStep(DownlightSelector.data.step);\n    },\n\n    init: function () {\n        DownlightSelector.selector = document.getElementById('js-downlight-selector');\n\n        if (!DownlightSelector.selector) {\n            return;\n        }\n\n        DownlightSelector.steps = parseInt(DownlightSelector.selector.getAttribute('data-max-steps'), 10);\n\n        document.querySelectorAll('.js-downlight-popup').forEach(function (popup) {\n            document.body.appendChild(popup);\n        });\n\n        DownlightSelector.load(DownlightSelector.getParams());\n    },\n};\n\nfunction delegateDownlightEvent(type, selector, callback) {\n    document.addEventListener(type, function (event) {\n        var target = event.target.closest(selector);\n\n        if (!target || !document.documentElement.contains(target)) {\n            return;\n        }\n\n        callback(event, target);\n    });\n}\n\nfunction initDownlightSelector() {\n    DownlightSelector.init();\n\n    delegateDownlightEvent('click', '.js-downlight-previous', function (event, button) {\n        event.preventDefault();\n\n        var currentStep = button.closest('.js-downlight-step');\n        var previousStep = DownlightSelector.getPreviousStep(currentStep);\n\n        if (!previousStep) {\n            return;\n        }\n\n        DownlightSelector.trimData(previousStep.dataset.previousChoices);\n        DownlightSelector.setStep(previousStep.dataset.step, true);\n    });\n\n    delegateDownlightEvent('click', '.js-downlight-choice a', function (event, link) {\n        event.preventDefault();\n\n        var choice = link.closest('.js-downlight-choice');\n        if (!choice) {\n            return;\n        }\n\n        var choiceValue = choice.dataset.choice;\n        if (!choiceValue) {\n            return;\n        }\n\n        var pair = choiceValue.split('=');\n        DownlightSelector.setData(pair[0], pair[1]);\n\n        var popupSelector = choice.dataset.popup;\n        if (popupSelector !== undefined) {\n            var overlay = document.createElement('div');\n            overlay.className = 'downlight-overlay';\n            document.body.appendChild(overlay);\n\n            var popup = document.querySelector(popupSelector);\n            DownlightSelector.showElement(popup);\n\n            if (popup) {\n                popup.addEventListener('click', function handlePopupChoice(popupEvent) {\n                    if (!popupEvent.target.closest('.js-downlight-choice a')) {\n                        return;\n                    }\n\n                    DownlightSelector.hideElement(popup);\n                    overlay.remove();\n                }, {once: true});\n            }\n        } else {\n            DownlightSelector.incStep();\n        }\n    });\n}\n\nif (document.readyState === 'loading') {\n    document.addEventListener('DOMContentLoaded', initDownlightSelector);\n} else {\n    initDownlightSelector();\n}\n\nwindow.addEventListener('popstate', function () {\n    DownlightSelector.load(DownlightSelector.getParams());\n});\n"]}