Core.UI = Core.UI || {}; Core.UI.MainMenu = class extends _core { /** * Get main menu object. * @return {jQuery} */ static getElement() { let $stickyMenu = $('body > .pusher > .sticky > .menu.main-menu'); if ($stickyMenu.exists()) { return $stickyMenu; } return $('body > .pusher > .menu.main-menu'); } /** * Set page menu. * @param {object} menuData * @param {string} menuData.menu_html */ static setMenu(menuData) { this.parameters = {}; this.$menu = null; this.parameterChangeId = 0; let $menu = this.getElement(); if (!menuData.menu_html || !$menu.exists()) { return; } let $home = $menu.find('.home.item'); if ($home.exists()) { $home.remove(); } $menu.children('.dropdown.item').remove(); $menu.prepend(menuData.menu_html); this.bind(); } /** * Bind menu item. * @param {jQuery} $item * @param {jQuery} $dropdown */ static bindMenuItem($item, $dropdown) { let me = this; $item.rebind('contextmenu', function (e) { e.preventDefault(); return false; }); $item.rebind('mousedown', function (e) { if (e.which !== 1) { return; } $item.data('pressed-time', (new Date()).getTime()); $item.find('.progress-bar').remove(); $item.html(`
${$item.html()}`); var animate = setInterval( function () { let width = (((new Date()).getTime() - parseInt($item.data('pressed-time'))) / parseInt($item.data('long-press-time'))) * 100; $item.find('.progress-bar').css('width', `${width}%`); }, parseInt($item.data('long-press-time')) / 100 ); var clear = function () { clearTimeout(timeout); clearInterval(animate); $item.find('.progress-bar').remove(); }; $item.rebind('mouseout', function () { if ($item.find('.progress-bar:hover').length > 0) { return; } else if ($item.filter(':hover').length > 0) { return; } clear(); }); $('body').rebind('mouseup', clear); var timeout = setTimeout( function () { if ($dropdown) { $dropdown.dropdown('hide'); } me.executeSelected($item.data('js-func'), $item); clear(); }, parseInt($item.data('long-press-time')) ); }); } /** * Bind menu. */ static bind() { let me = this; me.getElement().children('.ui.dropdown.item, .home.item').each(function () { let $menuItem = $(this); if ($menuItem.hasClass('confirm-input')) { me.bindMenuItem( $menuItem ); } $menuItem.dropdown({ action: function (text, value, $item) { $item = $($item); if ($item.hasClass('confirm-input')) { return false; } me.executeSelected( $item.data('js-func'), $item ); $menuItem.dropdown('hide'); return true; } }); $menuItem.find('.item.confirm-input').each(function () { me.bindMenuItem( $(this), $menuItem ); }); }); } /** * Execute selected menu item. * @param {string} jsFunc * @param {jQuery} $item */ static executeSelected(jsFunc, $item) { let data = jsFunc.split(':'); let action = data.shift(); let object = data.shift(); let parameters = data.join(':'); switch (action) { case 'page': eval(`${object}.open(${parameters})`); break; case 'pageCall': eval(`${object}.open()`); eval(`${object}.${parameters}()`); break; case 'activePage': let call = `Page.activePage.${object}`; if (parameters) { call = `${call}('${parameters}')`; } else if (!call.endsWith(')')) { call = `${call}()`; } eval(call); break; case 'code': eval(object); break; case 'url': window.open(object); break; case 'url-external': let u = jsFunc.slice(13); alert(u); window.open(u); break; case 'modal': eval(`new ${object}()`); break; } } /** * Set menu parameter. * @param {string} name * @param {string} value */ static setParameter(name, value) { this.parameterChangeId++; this.parameters[name] = value; this.refresh(this.parameterChangeId); } /** * Reset menu and refresh it. */ static reset() { this.parameterChangeId++; this.parameters = {}; this.refresh(this.parameterChangeId); } /** * Refresh menu. This is called after setting a parameter but can be called independently. * @param {number} parameterChangeId */ static refresh(parameterChangeId) { if (!parameterChangeId) { this.parameterChangeId++; parameterChangeId = this.parameterChangeId; } let me = this; Server.call( this.uri, { func: 'apiGetPageMenu', configuration: $.extend(Page.getParameters(), me.parameters) }, function (data) { if (me.parameterChangeId != parameterChangeId) { me.refresh(me.parameterChangeId); } else { me.setMenu(data); } }, me.getElement(), '' ); } /** * Set page title. * @param {string} title */ static setTitle(title) { this.getElement().parent().find('span.title').html(title); } };