/* eslint-disable unicorn/prefer-module */ import type * as globalTypes from "../types/globalTypes"; import type * as recordTypes from "../types/recordTypes"; import type { cityssmGlobal } from "@cityssm/bulma-webapp-js/src/types"; import type { BulmaJS } from "@cityssm/bulma-js/types"; declare const cityssm: cityssmGlobal; declare const bulmaJS: BulmaJS; (() => { const urlPrefix = document.querySelector("main").dataset.urlPrefix; const containerElement = document.querySelector( "#container--occupancyTypes" ) as HTMLElement; let occupancyTypes: recordTypes.OccupancyType[] = exports.occupancyTypes; delete exports.occupancyTypes; const expandedOccupancyTypes = new Set(); const toggleOccupancyTypeFields = (clickEvent: Event) => { const toggleButtonElement = clickEvent.currentTarget as HTMLButtonElement; const occupancyTypeElement = toggleButtonElement.closest( ".container--occupancyType" ) as HTMLElement; const occupancyTypeId = Number.parseInt( occupancyTypeElement.dataset.occupancyTypeId, 10 ); if (expandedOccupancyTypes.has(occupancyTypeId)) { expandedOccupancyTypes.delete(occupancyTypeId); } else { expandedOccupancyTypes.add(occupancyTypeId); } toggleButtonElement.innerHTML = expandedOccupancyTypes.has( occupancyTypeId ) ? '' : ''; const panelBlockElements = occupancyTypeElement.querySelectorAll(".panel-block"); for (const panelBlockElement of panelBlockElements) { panelBlockElement.classList.toggle("is-hidden"); } }; const moveOccupancyTypeUp = (clickEvent: Event) => { clickEvent.preventDefault(); const occupancyTypeId = ( (clickEvent.currentTarget as HTMLElement).closest( ".container--occupancyType" ) as HTMLElement ).dataset.occupancyTypeId; cityssm.postJSON( urlPrefix + "/admin/doMoveOccupancyTypeUp", { occupancyTypeId }, (responseJSON: { success: boolean; errorMessage?: string; occupancyTypes?: recordTypes.OccupancyType[]; }) => { if (responseJSON.success) { occupancyTypes = responseJSON.occupancyTypes; renderOccupancyTypes(); } else { bulmaJS.alert({ title: "Error Moving Occupancy Type", message: responseJSON.errorMessage, contextualColorName: "danger" }); } } ); }; const moveOccupancyTypeDown = (clickEvent: Event) => { clickEvent.preventDefault(); const occupancyTypeId = ( (clickEvent.currentTarget as HTMLElement).closest( ".container--occupancyType" ) as HTMLElement ).dataset.occupancyTypeId; cityssm.postJSON( urlPrefix + "/admin/doMoveOccupancyTypeDown", { occupancyTypeId }, (responseJSON: { success: boolean; errorMessage?: string; occupancyTypes?: recordTypes.OccupancyType[]; }) => { if (responseJSON.success) { occupancyTypes = responseJSON.occupancyTypes; renderOccupancyTypes(); } else { bulmaJS.alert({ title: "Error Moving Occupancy Type", message: responseJSON.errorMessage, contextualColorName: "danger" }); } } ); }; const renderOccupancyTypes = () => { if (occupancyTypes.length === 0) { containerElement.innerHTML = '
There are no active ' + exports.aliases.occupancy.toLowerCase() + " types.

" + "
"; return; } containerElement.innerHTML = ""; for (const occupancyType of occupancyTypes) { const occupancyTypeContainer = document.createElement("div"); occupancyTypeContainer.className = "panel container--occupancyType"; occupancyTypeContainer.dataset.occupancyTypeId = occupancyType.occupancyTypeId.toString(); occupancyTypeContainer.innerHTML = '
' + '
' + ('
' + '
' + '" + "
" + '
' + '

' + cityssm.escapeHTML(occupancyType.occupancyType) + "

" + "
" + "
") + ('
' + '
' + '" + "
" + '
' + '" + "
" + ('
' + '
' + '
' + '" + "
" + '
' + '" + "
" + "
" + "
") + "
") + "
" + "
"; if (occupancyType.occupancyTypeFields.length === 0) { occupancyTypeContainer.insertAdjacentHTML( "beforeend", '
' + '
' + '

There are no additional fields.

' + "
" + "
" ); } else { for (const occupancyTypeField of occupancyType.occupancyTypeFields) { const panelBlockElement = document.createElement("div"); panelBlockElement.className = "panel-block is-block container--occupancyTypeField"; if ( !expandedOccupancyTypes.has( occupancyType.occupancyTypeId ) ) { panelBlockElement.classList.add("is-hidden"); } panelBlockElement.dataset.occupancyTypeFieldId = occupancyTypeField.occupancyTypeFieldId.toString(); panelBlockElement.innerHTML = '
' + '" + '
' + ('
' + '
' + '
' + '" + "
" + '
' + '" + "
" + "
" + "
") + "
" + "
"; occupancyTypeContainer.append(panelBlockElement); } } occupancyTypeContainer .querySelector(".button--toggleOccupancyTypeFields") .addEventListener("click", toggleOccupancyTypeFields); occupancyTypeContainer .querySelector(".button--moveOccupancyTypeUp") .addEventListener("click", moveOccupancyTypeUp); occupancyTypeContainer .querySelector(".button--moveOccupancyTypeDown") .addEventListener("click", moveOccupancyTypeDown); containerElement.append(occupancyTypeContainer); } }; renderOccupancyTypes(); })();