Core.UI = Core.UI || {}; Core.UI.Sidebar = class extends _core { /** * Set page sidebar. * @param {string} sidebarHtml * @param {function()} onOpen * @param {function()} onClose */ static init(sidebarHtml, onOpen, onClose) { this.onOpen = onOpen || function() {}; this.onClose = onClose || function() {}; this.forms = {}; this.$sidebar = this.getElement(); if (!sidebarHtml || !this.$sidebar.exists()) { return; } this.$sidebar.html(sidebarHtml); this.bind(); this.open(); } /** * Get sidebar element * @return {jQuery} */ static getElement() { return $('body > .ui.sidebar'); } /** * Determine if sidebar is open. * @return {bool} */ static isOpen() { let scrollPosition = $(window).scrollTop(); let isVisible = this.getElement().sidebar('is visible'); window.scrollTo(0, scrollPosition); return isVisible; } /** * Bind sidebar. */ static bind() { Core.UI.Bind.get(this.getElement(), this); } /** * Open sidebar. */ static open(parameters) { let sidebarParameters = { closable: false, scrollLock: true, returnScroll: true, dimPage: false, onShow: () => { this.onOpen(this.getElement()); Core.UI.Bind.get(this.getElement()); if (sidebarParameters.closable) { $('.pusher').rebind( 'click', () => this.close() ); } }, onHide: () => { Page.getPageSegment().fade('hide', this.dimmerId); this.dimmerId = null; }, onHidden: () => { if ($.isFunction(this.onClose)) { this.onClose(); } } }; $.extend( true, sidebarParameters, parameters ); this.dimmerId = Page.getPageSegment().fade( 'show', { textOnly: true, opacity: (sidebarParameters.closable ? 0 : 0.25) } ); this.getElement() .sidebar(sidebarParameters) .sidebar('show'); } /** * Close sidebar. * @param {function()} callback */ static close(callback) { this.onClose = callback; // this.getElement().sidebar({ // onHidden: () => { // Page.getPageSegment().fade('hide', this.dimmerId); // this.dimmerId = null; // this.onClose(); // if ($.isFunction(callback)) { // callback(); // } // } // }); this.getElement().sidebar('hide'); } /** * Apply action to form. * @param {string|null} formName * @param {string} action * @param {string} value1 * @param {string} value2 */ static form(formName, action, value1, value2) { let forms = {}; this.$sidebar.find('form').each(function () { let $form = $(this); let classes = []; $form[0].classList.forEach(function (className) { classes.push(className); }); classes.splice(0, 2); let className = classes[0]; if (!className) { className = ''; } forms[className] = $form; }); if (!formName) { formName = Object.keys(forms)[0]; } let $form = forms[formName]; if (action && $form && $form.exists()) { return $form.form(action, value1, value2); } return $form; } };