{"version":3,"sources":["webpack:///./app/javascript/helpers/form.ts","webpack:///./app/javascript/schedules/elements/patient_attendance_element.ts"],"names":["createHiddenInput","name","value","input","document","createElement","type","PatientAttendanceElement","registerElement","property","Boolean","Array","target","targets","handleDropdownShown","bind","handleDropdownHidden","$","this","dropdown","on","active","button","disabled","off","event","attendanceTypeId","detail","relatedTarget","getAttribute","updateDialogTitle","hiddenInput","attendanceTypeIdName","form","append","sameReasonContainer","hidden","selectedPatientIds","length","showPatientTextareas","resetForm","removeAttendanceIds","removePatientIds","title","getElementById","randomId","attendanceName","getAttendanceTypeNameById","textContent","id","attendanceType","attendanceTypes","find","at","checkbox","HTMLInputElement","checked","hidePatientTextareas","sameReasonTextAreaActive","appendPatientIds","dialog","hideWithoutEmitting","emit","presentForm","clearForm","reset","textareaContainers","container","textarea","querySelector","patientId","dataset","isSelected","includes","required","sameReasonTextareaContainer","patientIdsName","from","querySelectorAll","forEach","remove","ImpulseElement"],"mappings":"2FAEO,SAASA,EAAkBC,EAAcC,GAC9C,IAAMC,EAAQC,SAASC,cAAc,SAIrC,OAHAF,EAAMG,KAAO,SACbH,EAAMF,KAAOA,EACbE,EAAMD,MAAQA,EACPC,EAPT,mC,u6FCKqBI,G,EADpBC,YAAgB,sB,EAKdC,YAAS,CAAEH,KAAMI,U,EAKjBD,YAAS,CAAEH,KAAMK,Q,EAKjBF,c,EAKAA,c,EAKAA,YAAS,CAAEH,KAAMK,Q,EAKjBF,c,EAEAG,c,EACAA,c,EACAA,c,EACAA,c,EACAA,c,EACAA,c,EACAA,c,EACAA,c,EACAC,c,+TAED,aAAe,IAAD,S,4FAAA,WACZ,eADY,0ZAEZ,EAAKC,oBAAsB,EAAKA,oBAAoBC,KAAzB,MAC3B,EAAKC,qBAAuB,EAAKA,qBAAqBD,KAA1B,MAHhB,E,sCASd,WACEE,EAAEC,KAAKC,UAAUC,GAAG,oBAAqBF,KAAKJ,qBAC9CG,EAAEC,KAAKC,UAAUC,GAAG,qBAAsBF,KAA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C,KAAK6C,aAC3B7C,KAAKyC,iBAAiBzC,KAAK8C,a,kCAG7B,WACE9C,KAAKwB,qB,uBAGP,WACExB,KAAKe,KAAKgC,QACV/C,KAAKwC,0BAAyB,K,kCAMhC,WAAgC,IAAD,MACLxC,KAAKgD,oBADA,IAC7B,2BAAiD,CAAC,IAAvCC,EAAsC,QACzCC,EAAWD,EAAUE,cAAc,YACjCC,EAAcH,EAAUI,QAAxBD,UACR,IAAKF,IAAaE,EAAW,OAE7B,IAAME,EAAatD,KAAKmB,mBAAmBoC,SAASH,GACpDH,EAAU/B,QAAUoC,EACpBJ,EAASM,SAAWF,GARO,iC,kCAY/B,WAAgC,IAAD,MACLtD,KAAKgD,oBADA,IAC7B,2BAAiD,CAAC,IAAvCC,EAAsC,QACzCC,EAAWD,EAAUE,cAAc,YACzC,IAAKD,EAAU,OACfD,EAAU/B,QAAS,EACnBgC,EAASM,UAAW,GALO,iC,sCAS/B,SAAiCxE,GAC/B,IAAMkE,EAAWlD,KAAKyD,4BAA4BN,cAAc,YAC3DD,IAELlD,KAAKyD,4BAA4BvC,QAAUlC,EAC3CkE,EAASM,SAAWxE,K,8BAGtB,SAAyB+B,GAAwB,IAAD,MAC7Bf,KAAKmB,oBADwB,IAC9C,2BAA0C,CAAC,IAAhCY,EAA+B,QAClClB,EAAc/B,YAAkBkB,KAAK0D,eAAgB3B,GAC3DhB,EAAKC,OAAOH,IAHgC,iC,iCAOhD,WACuBpB,MAAMkE,KAAK3D,KAAK4D,iBAAL,iBAAgC5D,KAAKc,qBAArC,QACnB+C,SAAQ,SAAC5E,GAAD,OAAWA,EAAM6E,c,8BAGxC,WACuBrE,MAAMkE,KAAK3D,KAAK4D,iBAAL,iBAAgC5D,KAAK0D,eAArC,QACnBG,SAAQ,SAAC5E,GAAD,OAAWA,EAAM6E,iB,iFA7MYC,K,sGAId,K,iHAKoB,M","file":"js/67-814c4c73c2329deb6fc7.chunk.js","sourcesContent":["/* eslint-disable import/prefer-default-export */\n\nexport function createHiddenInput(name: string, value: string) {\n const input = document.createElement('input');\n input.type = 'hidden';\n input.name = name;\n input.value = value;\n return input;\n}\n","import { ImpulseElement, property, registerElement, target, targets } from '@ambiki/impulse';\nimport type AwcDialogElement from '@ambiki/impulse-view-components/dist/elements/dialog';\nimport { createHiddenInput } from 'helpers/form';\n\n@registerElement('patient-attendance')\nexport default class PatientAttendanceElement extends ImpulseElement {\n /**\n * Whether the element is active or not. If active, then attendance can be marked.\n */\n @property({ type: Boolean }) active = false;\n\n /**\n * An array of patient ids that were selected from the checkboxes.\n */\n @property({ type: Array }) selectedPatientIds: string[] = [];\n\n /**\n * The form name of the attendance type.\n */\n @property() attendanceTypeIdName: string;\n\n /**\n * The form name of the patient ids.\n */\n @property() patientIdsName: string;\n\n /**\n * JSON for all attendance types.\n */\n @property({ type: Array }) attendanceTypes: Array<{ id: string; name: string }>;\n\n /**\n * DOM ID for the attendance dialog.\n */\n @property() randomId: string;\n\n @target() dialog: AwcDialogElement;\n @target() dropdown: HTMLElement;\n @target() button: HTMLButtonElement;\n @target() form: HTMLFormElement;\n @target() presentForm: HTMLFormElement;\n @target() clearForm: HTMLFormElement;\n @target() sameReasonContainer: HTMLElement;\n @target() sameReasonTextareaContainer: HTMLElement;\n @targets() textareaContainers: HTMLElement[];\n\n constructor() {\n super();\n this.handleDropdownShown = this.handleDropdownShown.bind(this);\n this.handleDropdownHidden = this.handleDropdownHidden.bind(this);\n }\n\n /**\n * Called when the element is added to the DOM.\n */\n connected() {\n $(this.dropdown).on('shown.bs.dropdown', this.handleDropdownShown);\n $(this.dropdown).on('hidden.bs.dropdown', this.handleDropdownHidden);\n if (this.active) {\n this.button.disabled = false;\n }\n }\n\n /**\n * Called when the element is removed from the DOM.\n */\n disconnected() {\n $(this.dropdown).off('shown.bs.dropdown', this.handleDropdownShown);\n $(this.dropdown).off('hidden.bs.dropdown', this.handleDropdownHidden);\n }\n\n /**\n * Called when the `active` property changes it's state.\n */\n activeChanged(active: boolean) {\n this.button.disabled = !active;\n }\n\n /**\n * Called when the dialog is shown. This is a great place to initialize things.\n */\n dialogShown(event: CustomEvent<{ dialog: AwcDialogElement; relatedTarget: HTMLButtonElement }>) {\n const invoker = event.detail.relatedTarget;\n const attendanceTypeId = invoker.getAttribute('data-attendance-type-id');\n if (!attendanceTypeId) return;\n\n this.updateDialogTitle(attendanceTypeId);\n const hiddenInput = createHiddenInput(this.attendanceTypeIdName, attendanceTypeId);\n this.form.append(hiddenInput);\n // Hide if there is only patient that is selected.\n this.sameReasonContainer.hidden = this.selectedPatientIds.length === 1;\n this.showPatientTextareas();\n }\n\n /**\n * Called when the dialog is hidden. Clean up task can be performed here.\n */\n dialogHidden() {\n this.resetForm();\n this.removeAttendanceIds();\n this.removePatientIds();\n }\n\n /**\n * Update the title of the attendance dialog to indicate the attendance type.\n */\n updateDialogTitle(attendanceTypeId: string) {\n const title = document.getElementById(`${this.randomId}_title`);\n if (!title) return;\n\n const attendanceName = this.getAttendanceTypeNameById(attendanceTypeId);\n if (attendanceName) {\n title.textContent = `${attendanceName} reason`;\n }\n }\n\n /**\n * Returns the the attendance type name based on the attendance id.\n */\n getAttendanceTypeNameById(id: string): string | null {\n const attendanceType = this.attendanceTypes.find((at) => at.id === id);\n return attendanceType ? attendanceType.name : null; // Returns null if ID is not found\n }\n\n /**\n * Called when the \"Same reason\" checkbox is toggled.\n */\n toggleSameReason({ target: checkbox }: Event) {\n if (!(checkbox instanceof HTMLInputElement)) return;\n\n if (checkbox.checked) {\n this.hidePatientTextareas();\n this.sameReasonTextAreaActive(true);\n this.appendPatientIds(this.form);\n } else {\n this.showPatientTextareas();\n this.sameReasonTextAreaActive(false);\n this.removePatientIds();\n }\n }\n\n /**\n * Called when the attendance has been marked successfully.\n */\n attendanceMarked(event: CustomEvent) {\n this.dialog.hideWithoutEmitting();\n $(this.dropdown).dropdown('hide');\n this.emit('marked', { detail: event.detail[0] });\n }\n\n private handleDropdownShown() {\n this.appendPatientIds(this.presentForm);\n this.appendPatientIds(this.clearForm);\n }\n\n private handleDropdownHidden() {\n this.removePatientIds();\n }\n\n private resetForm() {\n this.form.reset();\n this.sameReasonTextAreaActive(false);\n }\n\n /**\n * Shows the textarea of the patients that were selected from the checkboxes.\n */\n private showPatientTextareas() {\n for (const container of this.textareaContainers) {\n const textarea = container.querySelector('textarea');\n const { patientId } = container.dataset;\n if (!textarea || !patientId) return;\n\n const isSelected = this.selectedPatientIds.includes(patientId);\n container.hidden = !isSelected;\n textarea.required = isSelected;\n }\n }\n\n private hidePatientTextareas() {\n for (const container of this.textareaContainers) {\n const textarea = container.querySelector('textarea');\n if (!textarea) return;\n container.hidden = true;\n textarea.required = false;\n }\n }\n\n private sameReasonTextAreaActive(value: boolean) {\n const textarea = this.sameReasonTextareaContainer.querySelector('textarea');\n if (!textarea) return;\n\n this.sameReasonTextareaContainer.hidden = !value;\n textarea.required = value;\n }\n\n private appendPatientIds(form: HTMLFormElement) {\n for (const id of this.selectedPatientIds) {\n const hiddenInput = createHiddenInput(this.patientIdsName, id);\n form.append(hiddenInput);\n }\n }\n\n private removeAttendanceIds() {\n const hiddenInputs = Array.from(this.querySelectorAll(`[name=\"${this.attendanceTypeIdName}\"]`));\n hiddenInputs.forEach((input) => input.remove());\n }\n\n private removePatientIds() {\n const hiddenInputs = Array.from(this.querySelectorAll(`[name=\"${this.patientIdsName}\"]`));\n hiddenInputs.forEach((input) => input.remove());\n }\n}\n"],"sourceRoot":""}