class UIFactory { /* Display a toast based on provided options */ toast(options) { const theToast = new Toaster(options); theToast.makeToast() theToast.show() return theToast } /* Display a modal based on provided options */ modal (options) { const theModal = new ModalFactory(options); theModal.makeModal() theModal.show() return theModal } /* Display a modal based on provided options */ modalFromURL (url, successCallback, failCallback) { return AJAXApi.quickFetchURL(url).then((modalHTML) => { const theModal = new ModalFactory({ rawHTML: modalHTML, replaceFormSubmissionByAjax: true, successCallback: successCallback !== undefined ? successCallback : () => {}, failCallback: failCallback !== undefined ? failCallback : (errorMessage) => {}, }); theModal.makeModal(modalHTML) theModal.show() theModal.$modal.data('modalObject', theModal) return theModal }) } /* Fetch HTML from the provided URL and override content of $container. $statusNode allows to specify another HTML node to display the loading */ reload (url, $container, $statusNode=null) { $container = $($container) $statusNode = $($statusNode) if (!$statusNode) { $statusNode = $container } AJAXApi.quickFetchURL(url, { statusNode: $statusNode[0] }).then((data) => { $container.replaceWith(data) }) } } class Toaster { constructor(options) { this.options = Object.assign({}, Toaster.defaultOptions, options) this.bsToastOptions = { autohide: this.options.autohide, delay: this.options.delay, } } static defaultOptions = { id: false, title: false, muted: false, body: false, variant: 'default', autohide: true, delay: 5000, titleHtml: false, mutedHtml: false, bodyHtml: false, closeButton: true, } makeToast() { if (this.isValid()) { this.$toast = Toaster.buildToast(this.options) $('#mainToastContainer').append(this.$toast) } } show() { if (this.isValid()) { var that = this this.$toast.toast(this.bsToastOptions) .toast('show') .on('hidden.bs.toast', function () { that.removeToast() }) } } removeToast() { this.$toast.remove(); } isValid() { return this.options.title !== false || this.options.muted !== false || this.options.body !== false || this.options.titleHtml !== false || this.options.mutedHtml !== false || this.options.bodyHtml !== false } static buildToast(options) { var $toast = $('