2020-11-27 19:17:04 +03:00
|
|
|
import { importModule } from '@uupaa/dynamic-import-polyfill';
|
2021-01-26 16:25:38 -05:00
|
|
|
import './viewManager/viewContainer.scss';
|
2022-04-10 02:22:13 -04:00
|
|
|
import Dashboard from '../utils/dashboard';
|
2020-10-12 23:08:55 +01:00
|
|
|
|
2022-11-18 18:58:11 -05:00
|
|
|
const getMainAnimatedPages = () => {
|
2023-09-29 01:30:57 -04:00
|
|
|
return document.querySelector('.mainAnimatedPages');
|
2022-11-18 18:58:11 -05:00
|
|
|
};
|
|
|
|
|
2023-04-19 01:56:05 -04:00
|
|
|
function setControllerClass(view, options) {
|
|
|
|
if (options.controllerFactory) {
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
2019-04-02 22:10:22 +01:00
|
|
|
|
2023-04-19 01:56:05 -04:00
|
|
|
let controllerUrl = view.getAttribute('data-controller');
|
2019-04-02 22:10:22 +01:00
|
|
|
|
2023-04-19 01:56:05 -04:00
|
|
|
if (controllerUrl) {
|
|
|
|
if (controllerUrl.indexOf('__plugin/') === 0) {
|
|
|
|
controllerUrl = controllerUrl.substring('__plugin/'.length);
|
2019-04-02 22:10:22 +01:00
|
|
|
}
|
|
|
|
|
2023-04-19 01:56:05 -04:00
|
|
|
controllerUrl = Dashboard.getPluginUrl(controllerUrl);
|
|
|
|
const apiUrl = ApiClient.getUrl('/web/' + controllerUrl);
|
|
|
|
return importModule(apiUrl).then((ControllerFactory) => {
|
|
|
|
options.controllerFactory = ControllerFactory;
|
|
|
|
});
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
2023-04-19 01:56:05 -04:00
|
|
|
return Promise.resolve();
|
|
|
|
}
|
2019-04-02 22:10:22 +01:00
|
|
|
|
2023-04-19 01:56:05 -04:00
|
|
|
export function loadView(options) {
|
|
|
|
if (!options.cancel) {
|
|
|
|
const selected = selectedPageIndex;
|
|
|
|
const previousAnimatable = selected === -1 ? null : allPages[selected];
|
|
|
|
let pageIndex = selected + 1;
|
2019-04-02 22:10:22 +01:00
|
|
|
|
2023-04-19 01:56:05 -04:00
|
|
|
if (pageIndex >= pageContainerCount) {
|
|
|
|
pageIndex = 0;
|
|
|
|
}
|
2019-04-03 00:20:09 +01:00
|
|
|
|
2023-04-19 01:56:05 -04:00
|
|
|
const isPluginpage = options.url.includes('configurationpage');
|
|
|
|
const newViewInfo = normalizeNewView(options, isPluginpage);
|
|
|
|
const newView = newViewInfo.elem;
|
2019-04-02 22:10:22 +01:00
|
|
|
|
2023-04-19 01:56:05 -04:00
|
|
|
const currentPage = allPages[pageIndex];
|
2019-04-02 22:10:22 +01:00
|
|
|
|
2023-04-19 01:56:05 -04:00
|
|
|
if (currentPage) {
|
|
|
|
triggerDestroy(currentPage);
|
|
|
|
}
|
2019-04-02 22:10:22 +01:00
|
|
|
|
2023-04-19 01:56:05 -04:00
|
|
|
let view = newView;
|
2019-04-02 22:10:22 +01:00
|
|
|
|
2023-04-19 01:56:05 -04:00
|
|
|
if (typeof view == 'string') {
|
|
|
|
view = document.createElement('div');
|
|
|
|
view.innerHTML = newView;
|
|
|
|
}
|
2019-04-02 22:10:22 +01:00
|
|
|
|
2023-04-19 01:56:05 -04:00
|
|
|
view.classList.add('mainAnimatedPage');
|
2022-11-18 18:58:11 -05:00
|
|
|
|
2023-09-29 01:30:57 -04:00
|
|
|
const mainAnimatedPages = getMainAnimatedPages();
|
|
|
|
|
|
|
|
if (!mainAnimatedPages) {
|
2023-04-19 01:56:05 -04:00
|
|
|
console.warn('[viewContainer] main animated pages element is not present');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (currentPage) {
|
|
|
|
if (newViewInfo.hasScript && window.$) {
|
|
|
|
mainAnimatedPages.removeChild(currentPage);
|
|
|
|
view = $(view).appendTo(mainAnimatedPages)[0];
|
2020-09-02 01:06:07 +03:00
|
|
|
} else {
|
2023-04-19 01:56:05 -04:00
|
|
|
mainAnimatedPages.replaceChild(view, currentPage);
|
2020-09-02 01:06:07 +03:00
|
|
|
}
|
2023-09-12 17:02:06 -04:00
|
|
|
} else if (newViewInfo.hasScript && window.$) {
|
|
|
|
view = $(view).appendTo(mainAnimatedPages)[0];
|
2023-04-19 01:56:05 -04:00
|
|
|
} else {
|
2023-09-12 17:02:06 -04:00
|
|
|
mainAnimatedPages.appendChild(view);
|
2023-04-19 01:56:05 -04:00
|
|
|
}
|
2019-04-02 22:10:22 +01:00
|
|
|
|
2023-04-19 01:56:05 -04:00
|
|
|
if (options.type) {
|
|
|
|
view.setAttribute('data-type', options.type);
|
|
|
|
}
|
2020-08-01 19:18:03 +02:00
|
|
|
|
2023-04-19 01:56:05 -04:00
|
|
|
const properties = [];
|
2019-04-02 22:10:22 +01:00
|
|
|
|
2023-04-19 01:56:05 -04:00
|
|
|
if (options.fullscreen) {
|
|
|
|
properties.push('fullscreen');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (properties.length) {
|
|
|
|
view.setAttribute('data-properties', properties.join(','));
|
|
|
|
}
|
2020-09-02 01:06:07 +03:00
|
|
|
|
2023-04-19 01:56:05 -04:00
|
|
|
allPages[pageIndex] = view;
|
2020-09-02 01:06:07 +03:00
|
|
|
|
2023-04-19 01:56:05 -04:00
|
|
|
return setControllerClass(view, options)
|
|
|
|
// Timeout for polyfilled CustomElements (webOS 1.2)
|
|
|
|
.then(() => new Promise((resolve) => setTimeout(resolve, 0)))
|
|
|
|
.then(() => {
|
|
|
|
if (onBeforeChange) {
|
|
|
|
onBeforeChange(view, false, options);
|
|
|
|
}
|
2019-04-02 22:10:22 +01:00
|
|
|
|
2023-04-19 01:56:05 -04:00
|
|
|
beforeAnimate(allPages, pageIndex, selected);
|
|
|
|
selectedPageIndex = pageIndex;
|
|
|
|
currentUrls[pageIndex] = options.url;
|
2019-04-02 22:10:22 +01:00
|
|
|
|
2023-04-19 01:56:05 -04:00
|
|
|
if (!options.cancel && previousAnimatable) {
|
|
|
|
afterAnimate(allPages, pageIndex);
|
|
|
|
}
|
2020-01-04 01:31:35 +01:00
|
|
|
|
2023-04-19 01:56:05 -04:00
|
|
|
if (window.$) {
|
|
|
|
$.mobile = $.mobile || {};
|
|
|
|
$.mobile.activePage = view;
|
|
|
|
}
|
2019-04-02 22:10:22 +01:00
|
|
|
|
2023-04-19 01:56:05 -04:00
|
|
|
return view;
|
|
|
|
});
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
2023-04-19 01:56:05 -04:00
|
|
|
}
|
2018-10-23 01:05:09 +03:00
|
|
|
|
2023-04-19 01:56:05 -04:00
|
|
|
function parseHtml(html, hasScript) {
|
|
|
|
if (hasScript) {
|
|
|
|
html = html
|
|
|
|
.replaceAll('\x3c!--<script', '<script')
|
|
|
|
.replaceAll('</script>--\x3e', '</script>');
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
2023-04-19 01:56:05 -04:00
|
|
|
const wrapper = document.createElement('div');
|
|
|
|
wrapper.innerHTML = html;
|
|
|
|
return wrapper.querySelector('div[data-role="page"]');
|
|
|
|
}
|
2019-04-02 22:10:22 +01:00
|
|
|
|
2023-04-19 01:56:05 -04:00
|
|
|
function normalizeNewView(options, isPluginpage) {
|
|
|
|
const viewHtml = options.view;
|
2019-04-02 22:10:22 +01:00
|
|
|
|
2023-04-19 01:56:05 -04:00
|
|
|
if (viewHtml.indexOf('data-role="page"') === -1) {
|
|
|
|
return viewHtml;
|
|
|
|
}
|
2019-04-02 22:10:22 +01:00
|
|
|
|
2023-04-19 01:56:05 -04:00
|
|
|
let hasScript = viewHtml.indexOf('<script') !== -1;
|
|
|
|
const elem = parseHtml(viewHtml, hasScript);
|
2019-04-02 22:10:22 +01:00
|
|
|
|
2023-04-19 01:56:05 -04:00
|
|
|
if (hasScript) {
|
|
|
|
hasScript = elem.querySelector('script') != null;
|
|
|
|
}
|
2019-04-02 22:10:22 +01:00
|
|
|
|
2023-04-19 01:56:05 -04:00
|
|
|
let hasjQuery = false;
|
|
|
|
let hasjQuerySelect = false;
|
|
|
|
let hasjQueryChecked = false;
|
2019-04-02 22:10:22 +01:00
|
|
|
|
2023-04-19 01:56:05 -04:00
|
|
|
if (isPluginpage) {
|
|
|
|
hasjQuery = viewHtml.indexOf('jQuery') != -1 || viewHtml.indexOf('$(') != -1 || viewHtml.indexOf('$.') != -1;
|
|
|
|
hasjQueryChecked = viewHtml.indexOf('.checked(') != -1;
|
|
|
|
hasjQuerySelect = viewHtml.indexOf('.selectmenu(') != -1;
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
2023-04-19 01:56:05 -04:00
|
|
|
return {
|
|
|
|
elem: elem,
|
|
|
|
hasScript: hasScript,
|
|
|
|
hasjQuerySelect: hasjQuerySelect,
|
|
|
|
hasjQueryChecked: hasjQueryChecked,
|
|
|
|
hasjQuery: hasjQuery
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function beforeAnimate(allPages, newPageIndex, oldPageIndex) {
|
|
|
|
for (let index = 0, length = allPages.length; index < length; index++) {
|
|
|
|
if (newPageIndex !== index && oldPageIndex !== index) {
|
|
|
|
allPages[index].classList.add('hide');
|
2019-04-02 22:10:22 +01:00
|
|
|
}
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
2023-04-19 01:56:05 -04:00
|
|
|
}
|
2018-10-23 01:05:09 +03:00
|
|
|
|
2023-04-19 01:56:05 -04:00
|
|
|
function afterAnimate(allPages, newPageIndex) {
|
|
|
|
for (let index = 0, length = allPages.length; index < length; index++) {
|
|
|
|
if (newPageIndex !== index) {
|
|
|
|
allPages[index].classList.add('hide');
|
2019-04-02 22:10:22 +01:00
|
|
|
}
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
2023-04-19 01:56:05 -04:00
|
|
|
}
|
2018-10-23 01:05:09 +03:00
|
|
|
|
2023-04-19 01:56:05 -04:00
|
|
|
export function setOnBeforeChange(fn) {
|
|
|
|
onBeforeChange = fn;
|
|
|
|
}
|
2018-10-23 01:05:09 +03:00
|
|
|
|
2023-04-19 01:56:05 -04:00
|
|
|
export function tryRestoreView(options) {
|
2023-09-29 01:30:57 -04:00
|
|
|
console.debug('[viewContainer] tryRestoreView', options);
|
2023-04-19 01:56:05 -04:00
|
|
|
const url = options.url;
|
|
|
|
const index = currentUrls.indexOf(url);
|
2019-04-02 22:10:22 +01:00
|
|
|
|
2023-04-19 01:56:05 -04:00
|
|
|
if (index !== -1) {
|
|
|
|
const animatable = allPages[index];
|
|
|
|
const view = animatable;
|
2019-04-02 22:10:22 +01:00
|
|
|
|
2023-04-19 01:56:05 -04:00
|
|
|
if (view) {
|
|
|
|
if (options.cancel) {
|
|
|
|
return;
|
|
|
|
}
|
2019-04-02 22:10:22 +01:00
|
|
|
|
2023-04-19 01:56:05 -04:00
|
|
|
const selected = selectedPageIndex;
|
|
|
|
const previousAnimatable = selected === -1 ? null : allPages[selected];
|
|
|
|
return setControllerClass(view, options).then(() => {
|
|
|
|
if (onBeforeChange) {
|
|
|
|
onBeforeChange(view, true, options);
|
|
|
|
}
|
2019-04-02 22:10:22 +01:00
|
|
|
|
2023-04-19 01:56:05 -04:00
|
|
|
beforeAnimate(allPages, index, selected);
|
|
|
|
animatable.classList.remove('hide');
|
|
|
|
selectedPageIndex = index;
|
2019-04-02 22:10:22 +01:00
|
|
|
|
2023-04-19 01:56:05 -04:00
|
|
|
if (!options.cancel && previousAnimatable) {
|
|
|
|
afterAnimate(allPages, index);
|
|
|
|
}
|
2019-04-02 22:10:22 +01:00
|
|
|
|
2023-04-19 01:56:05 -04:00
|
|
|
if (window.$) {
|
|
|
|
$.mobile = $.mobile || {};
|
|
|
|
$.mobile.activePage = view;
|
|
|
|
}
|
2019-04-02 22:10:22 +01:00
|
|
|
|
2023-04-19 01:56:05 -04:00
|
|
|
return view;
|
|
|
|
});
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-19 01:56:05 -04:00
|
|
|
return Promise.reject();
|
|
|
|
}
|
|
|
|
|
|
|
|
function triggerDestroy(view) {
|
|
|
|
view.dispatchEvent(new CustomEvent('viewdestroy', {}));
|
|
|
|
}
|
|
|
|
|
|
|
|
export function reset() {
|
2023-09-29 01:30:57 -04:00
|
|
|
console.debug('[viewContainer] resetting view cache');
|
2023-04-19 01:56:05 -04:00
|
|
|
allPages = [];
|
|
|
|
currentUrls = [];
|
2023-09-29 01:30:57 -04:00
|
|
|
const mainAnimatedPages = getMainAnimatedPages();
|
2023-04-19 01:56:05 -04:00
|
|
|
if (mainAnimatedPages) mainAnimatedPages.innerHTML = '';
|
|
|
|
selectedPageIndex = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
let onBeforeChange;
|
|
|
|
let allPages = [];
|
|
|
|
let currentUrls = [];
|
|
|
|
const pageContainerCount = 3;
|
|
|
|
let selectedPageIndex = -1;
|
|
|
|
reset();
|
|
|
|
getMainAnimatedPages()?.classList.remove('hide');
|
2020-08-01 05:36:36 +02:00
|
|
|
|
|
|
|
export default {
|
2023-09-29 01:30:57 -04:00
|
|
|
loadView,
|
|
|
|
tryRestoreView,
|
|
|
|
reset,
|
|
|
|
setOnBeforeChange
|
2020-08-01 05:36:36 +02:00
|
|
|
};
|