From 3374a2d2d3776ee59626a53a1aab94927dbc510c Mon Sep 17 00:00:00 2001 From: djmaze Date: Sat, 22 Aug 2020 09:35:24 +0200 Subject: [PATCH] Put createCommandLegacy code in createCommand --- dev/Knoin/Knoin.js | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/dev/Knoin/Knoin.js b/dev/Knoin/Knoin.js index fd2b260f1..2153b5b28 100644 --- a/dev/Knoin/Knoin.js +++ b/dev/Knoin/Knoin.js @@ -2,14 +2,13 @@ import ko from 'ko'; import { $htmlCL, VIEW_MODELS, popupVisibilityNames } from 'Common/Globals'; -import { pString, createCommandLegacy, isNonEmptyArray } from 'Common/Utils'; - //import { bMobileDevice } from 'Common/Globals'; let currentScreen = null, defaultScreenName = ''; -const SCREENS = {}, $ = jQuery; +const SCREENS = {}, $ = jQuery, + isNonEmptyArray = values => Array.isArray(values) && values.length; export const ViewType = { Popup: 'Popups', @@ -34,7 +33,25 @@ export function hideLoading() { * @returns {Function} */ export function createCommand(fExecute, fCanExecute = true) { - return createCommandLegacy(null, fExecute, fCanExecute); + let fResult = null; + const fNonEmpty = (...args) => { + if (fResult && fResult.canExecute && fResult.canExecute()) { + fExecute.apply(null, args); + } + return false; + }; + + fResult = fExecute ? fNonEmpty : ()=>{}; + fResult.enabled = ko.observable(true); + fResult.isCommand = true; + + if (typeof fCanExecute === 'function') { + fResult.canExecute = ko.computed(() => fResult && fResult.enabled() && fCanExecute.call(null)); + } else { + fResult.canExecute = ko.computed(() => fResult && fResult.enabled() && !!fCanExecute); + } + + return fResult; } /** @@ -260,7 +277,7 @@ export function screenOnRoute(screenName, subPart) { isSameScreen = false, cross = null; - if (!pString(screenName)) { + if (null == screenName || '' == screenName) { screenName = defaultScreenName; }