diff --git a/static/js/app.js b/static/js/app.js new file mode 100644 index 0000000..4964e9b --- /dev/null +++ b/static/js/app.js @@ -0,0 +1,197 @@ +// notie +function notify(msg, msgType) { + notie.alert({ + type: msgType, + text: msg, + }); +} + +// sweetalert2 +function notifyModal(title, text, icon, confirmButtonText) { + Swal.fire({ + title: title, + text: text, + icon: icon, + confirmButtonText: confirmButtonText + }) +} + +// Prompt is out Javascript module for all alerts, notifications, and custom popup dialogs +function Prompt() { + let toast = function (c) { + const { + msg = "", + icon = "success", + position = "top-end", + } = c + + const Toast = Swal.mixin({ + toast: true, + title: msg, + position: position, + icon: icon, + showConfirmButton: false, + timer: 3000, + timerProgressBar: true, + didOpen: (toast) => { + toast.addEventListener('mouseenter', Swal.stopTimer) + toast.addEventListener('mouseleave', Swal.resumeTimer) + } + }) + Toast.fire({}) + } + + let success = function (c) { + const { + msg = "", + title = "", + footer = "", + } = c + + Swal.fire({ + icon: 'success', + title: title, + text: msg, + footer: footer, + }) + } + + let error = function (c) { + const { + msg = "", + title = "", + footer = "", + } = c + + Swal.fire({ + icon: 'error', + title: title, + text: msg, + footer: footer, + }) + } + + async function custom(c) { + const { + icon = "", + msg = "", + title = "", + showConfirmButton = true, + } = c; + + const {value: result} = await Swal.fire({ + icon: icon, + title: title, + html: msg, + backdrop: false, + focusConfirm: false, + showCancelButton: true, + showConfirmButton: showConfirmButton, + willOpen: () => { + if (c.willOpen !== undefined) { + c.willOpen(); + } + }, + didOpen: () => { + if (c.didOpen !== undefined) { + c.didOpen(); + } + }, + preConfirm: () => { + return [ + document.getElementById('start').value, + document.getElementById('end').value, + ] + }, + }) + + if (c.callback !== undefined) { + if (result && + result.dismiss !== Swal.DismissReason.cancel && + result !== "") { + c.callback(result); + } else { + c.callback(false); + } + } + } + + return { + toast: toast, + success: success, + error: error, + custom: custom, + } +} + +function roomReservation(id) { + document.getElementById("check-availability").addEventListener('click', () => { + let html = ` +
+ `; + attention.custom({ + title: "Choose your dates", + msg: html, + willOpen: () => { + const elem = document.getElementById('reservation-dates-modal') + const rp = new DateRangePicker(elem, { + "format": "yyyy-mm-dd", + showOnFocus: true, + minDate: new Date(), + }); + }, + didOpen: () => { + return [ + document.getElementById('start').removeAttribute("disabled"), + document.getElementById('end').removeAttribute("disabled"), + ] + }, + callback: (result) => { + const formElem = document.getElementById("check-availability-form"); + let formData = new FormData(formElem); + formData.append("csrf_token", "{{.CSRFToken}}"); + formData.append("room_id", id) + + fetch('/availability-json', { + method: "post", + body: formData, + }) + .then(response => response.json()) + .then(data => { + if (data.ok) { + console.log("room is available") + attention.custom({ + icon: "success", + // TODO: use the default ok button instead + msg: 'Room is available
' + + '', + showConfirmButton: false, + }); + } else { + console.log("room is not available") + attention.error({ + msg: "No availability" + }); + } + }) + }, + }); + }); + +} diff --git a/templates/base.layout.tmpl b/templates/base.layout.tmpl index 43e0be0..682919c 100644 --- a/templates/base.layout.tmpl +++ b/templates/base.layout.tmpl @@ -77,44 +77,10 @@ + {{block "js" .}} diff --git a/templates/generals.page.tmpl b/templates/generals.page.tmpl index 3d2644e..fd040d2 100644 --- a/templates/generals.page.tmpl +++ b/templates/generals.page.tmpl @@ -30,75 +30,6 @@ {{end}} {{define "js"}} {{end}} diff --git a/templates/majors.page.tmpl b/templates/majors.page.tmpl index 46a5959..2bd8b81 100644 --- a/templates/majors.page.tmpl +++ b/templates/majors.page.tmpl @@ -30,75 +30,6 @@ {{end}} {{define "js"}} {{end}}