snappymail/dev/Knoin/Knoin.js

333 lines
8.2 KiB
JavaScript
Raw Normal View History

2016-07-08 07:22:58 +08:00
import ko from 'ko';
import { koComputable } from 'External/ko';
import { doc, $htmlCL, elementById, fireEvent } from 'Common/Globals';
import { isFunction, forEachObjectValue, forEachObjectEntry } from 'Common/Utils';
2016-07-08 07:22:58 +08:00
let
SCREENS = {},
currentScreen = null,
defaultScreenName = '';
2016-07-08 07:22:58 +08:00
const
autofocus = dom => {
const af = dom.querySelector('[autofocus]');
af && af.focus();
2021-08-20 21:40:07 +08:00
},
visiblePopups = new Set,
2021-08-20 21:40:07 +08:00
/**
* @param {string} screenName
* @returns {?Object}
*/
screen = screenName => screenName && null != SCREENS[screenName] ? SCREENS[screenName] : null,
/**
* @param {Function} ViewModelClass
* @param {Object=} vmScreen
* @returns {*}
*/
buildViewModel = (ViewModelClass, vmScreen) => {
if (ViewModelClass && !ViewModelClass.__builded) {
let vmDom = null;
const vm = new ViewModelClass(vmScreen),
position = vm.viewType || '',
dialog = ViewType.Popup === position,
2021-08-20 21:40:07 +08:00
vmPlace = position ? doc.getElementById('rl-' + position.toLowerCase()) : null;
ViewModelClass.__builded = true;
ViewModelClass.__vm = vm;
if (vmPlace) {
vmDom = dialog
? Element.fromHTML('<dialog id="V-'+ vm.viewModelTemplateID + '"></dialog>')
: Element.fromHTML('<div id="V-'+ vm.viewModelTemplateID + '" hidden=""></div>');
2021-08-20 21:40:07 +08:00
vmPlace.append(vmDom);
vm.viewModelDom = vmDom;
ViewModelClass.__dom = vmDom;
if (ViewType.Popup === position) {
vm.closeCommand = createCommand(() => hideScreenPopup(ViewModelClass));
2021-08-20 21:40:07 +08:00
// Firefox / Safari HTMLDialogElement not defined
if (!vmDom.showModal) {
vmDom.classList.add('polyfill');
vmDom.showModal = () => {
if (!vmDom.backdrop) {
vmDom.before(vmDom.backdrop = Element.fromHTML('<div class="dialog-backdrop"></div>'));
}
vmDom.setAttribute('open','');
vmDom.open = true;
vmDom.returnValue = null;
vmDom.backdrop.hidden = false;
};
vmDom.close = v => {
vmDom.backdrop.hidden = true;
vmDom.returnValue = v;
vmDom.removeAttribute('open', null);
vmDom.open = false;
};
}
2021-08-20 21:40:07 +08:00
// show/hide popup/modal
const endShowHide = e => {
if (e.target === vmDom) {
if (vmDom.classList.contains('animate')) {
2021-08-20 21:40:07 +08:00
autofocus(vmDom);
vm.afterShow && vm.afterShow();
2021-08-20 21:40:07 +08:00
} else {
vmDom.close();
vm.afterHide && vm.afterHide();
2021-08-20 21:40:07 +08:00
}
}
};
2022-02-26 08:06:18 +08:00
vm.modalVisible.subscribe(value => {
2021-08-20 21:40:07 +08:00
if (value) {
visiblePopups.add(vm);
vmDom.style.zIndex = 3000 + (visiblePopups.size * 2);
vmDom.showModal();
if (vmDom.backdrop) {
vmDom.backdrop.style.zIndex = 3000 + visiblePopups.size;
}
vm.keyScope.set();
2021-08-20 21:40:07 +08:00
requestAnimationFrame(() => { // wait just before the next paint
vmDom.offsetHeight; // force a reflow
vmDom.classList.add('animate'); // trigger the transitions
2021-08-20 21:40:07 +08:00
});
} else {
visiblePopups.delete(vm);
2021-08-20 21:40:07 +08:00
vm.onHide && vm.onHide();
vm.keyScope.unset();
vmDom.classList.remove('animate'); // trigger the transitions
}
arePopupsVisible(0 < visiblePopups.size);
2021-08-20 21:40:07 +08:00
});
vmDom.addEventListener('transitionend', endShowHide);
}
2016-07-08 07:22:58 +08:00
2021-08-20 21:40:07 +08:00
ko.applyBindingAccessorsToNode(
vmDom,
{
i18nInit: true,
template: () => ({ name: vm.viewModelTemplateID })
2021-08-20 21:40:07 +08:00
},
vm
);
2021-05-31 22:19:01 +08:00
2021-08-20 21:40:07 +08:00
vm.onBuild && vm.onBuild(vmDom);
fireEvent('rl-view-model', vm);
2021-08-20 21:40:07 +08:00
} else {
console.log('Cannot find view model position: ' + position);
2016-07-08 07:22:58 +08:00
}
}
2021-08-20 21:40:07 +08:00
return ViewModelClass && ViewModelClass.__vm;
},
2016-07-08 07:22:58 +08:00
forEachViewModel = (screen, fn) => {
screen.viewModels.forEach(ViewModelClass => {
if (
ViewModelClass.__vm &&
ViewModelClass.__dom &&
ViewType.Popup !== ViewModelClass.__vm.viewType
) {
fn(ViewModelClass.__vm, ViewModelClass.__dom);
}
});
},
hideScreen = (screenToHide, destroy) => {
screenToHide.onHide && screenToHide.onHide();
forEachViewModel(screenToHide, (vm, dom) => {
dom.hidden = true;
vm.onHide && vm.onHide();
destroy && vm.viewModelDom.remove();
});
},
/**
* @param {Function} ViewModelClassToHide
* @returns {void}
*/
hideScreenPopup = ViewModelClassToHide => {
if (ViewModelClassToHide && ViewModelClassToHide.__vm && ViewModelClassToHide.__dom) {
2022-02-26 08:06:18 +08:00
ViewModelClassToHide.__vm.modalVisible(false);
}
},
2021-08-20 21:40:07 +08:00
/**
* @param {string} screenName
* @param {string} subPart
* @returns {void}
*/
screenOnRoute = (screenName, subPart) => {
let vmScreen = null,
isSameScreen = false;
2016-07-08 07:22:58 +08:00
2021-08-20 21:40:07 +08:00
if (null == screenName || '' == screenName) {
screenName = defaultScreenName;
}
2016-07-08 07:22:58 +08:00
// Close all popups
for (let vm of visiblePopups) {
vm.closeCommand();
}
2021-08-20 21:40:07 +08:00
if (screenName) {
vmScreen = screen(screenName);
if (!vmScreen) {
vmScreen = screen(defaultScreenName);
if (vmScreen) {
subPart = screenName + '/' + subPart;
screenName = defaultScreenName;
}
2016-07-08 07:22:58 +08:00
}
2021-08-20 21:40:07 +08:00
if (vmScreen && vmScreen.__started) {
isSameScreen = currentScreen && vmScreen === currentScreen;
if (!vmScreen.__builded) {
vmScreen.__builded = true;
vmScreen.viewModels.forEach(ViewModelClass =>
buildViewModel(ViewModelClass, vmScreen)
);
vmScreen.onBuild && vmScreen.onBuild();
2016-07-08 07:22:58 +08:00
}
2021-08-20 21:40:07 +08:00
setTimeout(() => {
// hide screen
if (currentScreen && !isSameScreen) {
hideScreen(currentScreen);
2021-08-20 21:40:07 +08:00
}
// --
currentScreen = vmScreen;
2016-07-08 07:22:58 +08:00
2021-08-20 21:40:07 +08:00
// show screen
if (!isSameScreen) {
vmScreen.onShow && vmScreen.onShow();
forEachViewModel(vmScreen, (vm, dom) => {
vm.onBeforeShow && vm.onBeforeShow();
dom.hidden = false;
vm.onShow && vm.onShow();
autofocus(dom);
});
2016-07-08 07:22:58 +08:00
}
2021-08-20 21:40:07 +08:00
// --
vmScreen.__cross && vmScreen.__cross.parse(subPart);
2021-08-20 21:40:07 +08:00
}, 1);
}
}
};
2021-08-20 21:40:07 +08:00
export const
ViewType = {
Popup: 'Popups',
Left: 'Left',
Right: 'Right',
Content: 'Content'
},
/**
* @param {Function} fExecute
* @param {(Function|boolean|null)=} fCanExecute = true
* @returns {Function}
*/
2021-12-31 20:50:33 +08:00
createCommand = (fExecute, fCanExecute) => {
let fResult = () => {
fResult.canExecute() && fExecute.call(null);
2021-08-20 21:40:07 +08:00
return false;
2021-12-31 20:50:33 +08:00
};
2021-08-20 21:40:07 +08:00
fResult.enabled = ko.observable(true);
2021-12-31 20:50:33 +08:00
fResult.canExecute = isFunction(fCanExecute)
? koComputable(() => fResult.enabled() && fCanExecute())
: fResult.enabled;
2021-08-20 21:40:07 +08:00
return fResult;
},
/**
* @param {Function} ViewModelClassToShow
* @param {Array=} params
* @returns {void}
*/
showScreenPopup = (ViewModelClassToShow, params = []) => {
const vm = buildViewModel(ViewModelClassToShow) && ViewModelClassToShow.__dom && ViewModelClassToShow.__vm;
2021-08-20 21:40:07 +08:00
if (vm) {
params = params || [];
vm.onBeforeShow && vm.onBeforeShow(...params);
2022-02-26 08:06:18 +08:00
vm.modalVisible(true);
2021-08-20 21:40:07 +08:00
vm.onShow && vm.onShow(...params);
}
},
arePopupsVisible = ko.observable(false),
2021-08-20 21:40:07 +08:00
/**
* @param {Array} screensClasses
* @returns {void}
*/
startScreens = screensClasses => {
hasher.clear();
forEachObjectValue(SCREENS, screen => hideScreen(screen, 1));
SCREENS = {};
currentScreen = null,
defaultScreenName = '';
2021-08-20 21:40:07 +08:00
screensClasses.forEach(CScreen => {
if (CScreen) {
const vmScreen = new CScreen(),
screenName = vmScreen.screenName;
defaultScreenName || (defaultScreenName = screenName);
SCREENS[screenName] = vmScreen;
2021-08-20 21:40:07 +08:00
}
});
forEachObjectValue(SCREENS, vmScreen => vmScreen.onStart());
2016-07-08 07:22:58 +08:00
2021-08-20 21:40:07 +08:00
const cross = new Crossroads();
cross.addRoute(/^([a-zA-Z0-9-]*)\/?(.*)$/, screenOnRoute);
2016-07-08 07:22:58 +08:00
2021-08-20 21:40:07 +08:00
hasher.add(cross.parse.bind(cross));
hasher.init();
2016-07-08 07:22:58 +08:00
2021-08-20 21:40:07 +08:00
setTimeout(() => $htmlCL.remove('rl-started-trigger'), 100);
const c = elementById('rl-content'), l = elementById('rl-loading');
c && (c.hidden = false);
l && l.remove();
2021-08-20 21:40:07 +08:00
},
2016-07-08 07:22:58 +08:00
2022-02-21 22:36:34 +08:00
/**
* Used by ko.bindingHandlers.command (template data-bind="command: ")
* to enable/disable click/submit action.
*/
2021-08-20 21:40:07 +08:00
decorateKoCommands = (thisArg, commands) =>
forEachObjectEntry(commands, (key, canExecute) => {
2021-08-20 21:40:07 +08:00
let command = thisArg[key],
fn = (...args) => fn.enabled() && fn.canExecute() && command.apply(thisArg, args);
2016-07-08 07:22:58 +08:00
2021-08-20 21:40:07 +08:00
// fn.__realCanExecute = canExecute;
2021-08-20 21:40:07 +08:00
fn.enabled = ko.observable(true);
2021-12-31 20:50:33 +08:00
fn.canExecute = isFunction(canExecute)
? koComputable(() => fn.enabled() && canExecute.call(thisArg, thisArg))
: koComputable(() => fn.enabled());
2021-08-20 21:40:07 +08:00
thisArg[key] = fn;
});
ko.decorateCommands = decorateKoCommands;