class _page { /** * @param {string} property * @return {*} */ static _get(property) { return this.hasOwnProperty(property) ? this[property] : null; } /** * @param {string} property * @param {*} value */ static _set(property, value) { this[property] = value; } /** * Setup class static variables. Called once when module is loaded. */ static setup() { this.error_return_code = 2; this.force_login = 1; } /** * Get class name for URI. */ static getClassName() { let className = this.name; return className; } /** * Get page URI. * * @return {string} */ static getURI() { let dir = ''; let parts = this.getClassName().split('.'); if (parts[0] === 'Modules') { parts[0] = 'Module'; } for (let i = 0; i < parts.length; i++) { dir = `${dir}/${_.kebabCase(parts[i])}`; } return `/api/${Page.Meta.api_version}${dir}/index.php`; } /** * Open page. * * @param {Object} parameters * @param {Object} options */ static open(parameters, options) { if (!parameters) { parameters = {}; } if (!options) { options = {}; } let className = this.getClassName(); let uri = this.getURI(); Page.activePage = this; Core.API.call({ url: uri, data: { func: 'apiGetPage', parameters: $.extend(options, Page.getParameters(parameters, false)), error_return_code: eval(`${className}.error_return_code`), force_login: eval(`${className}.force_login`) }, callback: (data) => { if (typeof hotkeys !== 'undefined') { hotkeys.setScope(className); } if (data.return == data.error_return_code) { Page.getContext().fade('dialog', data.message); return; } data.uri = uri; data.class_name = className; let pageObject = eval(className); Page.open(new Page.Structure(data), pageObject, parameters); pageObject.init(data); if (parameters.method) { pageObject[parameters.method](); } }, fadeParameters: 'Loading page...' }); } /** * Refresh page. */ static refresh() { this.open(Page.getParameters(null, true)); } /** * Close page and go home. */ static close() { Page.Home.open(); Core.UI.MainMenu.reset(); } /** * Apply action to form. * @param {string} formName * @param {string} action * @param {string} value1 * @param {string} value2 */ static form(formName, action, value1, value2) { let forms = {}; Page.getPageSegment().find('form').each(function () { let $form = $(this); let classes = []; $form[0].classList.forEach(function (className) { classes.push(className); }); classes.splice(0, 2); forms[classes[0]] = $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; } /** * Reload an element on the page. * @param {jQuery} selector * @param {function()} callback * @param {string} url * @param {boolean} showLoader */ static reloadElement(selector, callback, url, showLoader) { let idx = null; if (showLoader) { if ($(selector).fade('instance') === undefined) { $(selector).fade(); } idx = $(selector).fade('show', { text: '' }); } $.get((url ? url : window.location.href), {}, function(data) { if (showLoader) { $(selector).fade('hide', idx); } $(selector).replaceWith($(data).find(selector).prop('outerHTML')); if ($.isFunction(callback)) { callback(); } }); } /** * Subscribe to MQTT topic. * @param {string} topic * @param {function()} callback */ static subscribeToMQTTTopic(topic, callback) { Mqtt.subscribe( this.getURI(), topic, callback ); } /** * Override this. */ static bind() { } /** * Override this. */ static bindTab() { } /** * Override this. */ static bindTable() { } /** * Override this. */ static bindRow() { } /** * Override this. */ static refreshTabs() { } } _page.setup();