nakedobjects.spa
Version:
Single Page Application client for a Naked Objects application.
200 lines • 8.18 kB
JavaScript
import { MenuItemViewModel } from './menu-item-view-model';
import { ChoiceViewModel } from './choice-view-model';
import * as Models from '../models';
import * as Msg from '../user-messages';
import each from 'lodash/each';
import filter from 'lodash/filter';
import find from 'lodash/find';
import map from 'lodash/map';
import reduce from 'lodash/reduce';
import uniqWith from 'lodash/uniqWith';
import * as Validate from '../validate';
export function copy(event, item, context) {
var cKeyCode = 67;
if (event && (event.keyCode === cKeyCode && event.ctrlKey)) {
context.setCopyViewModel(item);
event.preventDefault();
}
}
export function tooltip(onWhat, fields) {
if (onWhat.clientValid()) {
return "";
}
var missingMandatoryFields = filter(fields, function (p) { return !p.clientValid && !p.getMessage(); });
if (missingMandatoryFields.length > 0) {
return reduce(missingMandatoryFields, function (s, t) { return s + t.title + "; "; }, Msg.mandatoryFieldsPrefix);
}
var invalidFields = filter(fields, function (p) { return !p.clientValid; });
if (invalidFields.length > 0) {
return reduce(invalidFields, function (s, t) { return s + t.title + "; "; }, Msg.invalidFieldsPrefix);
}
return "";
}
function getMenuNameForLevel(menupath, level) {
var menu = "";
if (menupath && menupath.length > 0) {
var menus = menupath.split("_");
if (menus.length > level) {
menu = menus[level];
}
}
return menu || "";
}
function removeDuplicateMenuNames(menus) {
return uniqWith(menus, function (m1, m2) {
if (m1.name && m2.name) {
return m1.name === m2.name;
}
return false;
});
}
export function createSubmenuItems(avms, menuSlot, level) {
// if not root menu aggregate all actions with same name
var menuActions;
var menuItems;
if (menuSlot.name) {
var actions = filter(avms, function (a) { return getMenuNameForLevel(a.menuPath, level) === menuSlot.name && !getMenuNameForLevel(a.menuPath, level + 1); });
menuActions = actions;
//then collate submenus
var submenuActions_1 = filter(avms, function (a) { return getMenuNameForLevel(a.menuPath, level) === menuSlot.name && getMenuNameForLevel(a.menuPath, level + 1); });
var menuSubSlots = map(submenuActions_1, function (a) { return ({ name: getMenuNameForLevel(a.menuPath, level + 1), action: a }); });
menuSubSlots = removeDuplicateMenuNames(menuSubSlots);
menuItems = map(menuSubSlots, function (slot) { return createSubmenuItems(submenuActions_1, slot, level + 1); });
}
else {
menuActions = [menuSlot.action];
menuItems = null;
}
return new MenuItemViewModel(menuSlot.name, menuActions, menuItems);
}
export function createMenuItems(avms) {
// first create a top level menu for each action
// note at top level we leave 'un-menued' actions
// use an anonymous object locally so we can construct objects with readonly properties
var menuSlots = map(avms, function (a) { return ({ name: getMenuNameForLevel(a.menuPath, 0), action: a }); });
// remove non unique submenus
menuSlots = removeDuplicateMenuNames(menuSlots);
// update submenus with all actions under same submenu
return map(menuSlots, function (slot) { return createSubmenuItems(avms, slot, 0); });
}
export function actionsTooltip(onWhat, actionsOpen) {
if (actionsOpen) {
return Msg.closeActions;
}
return onWhat.noActions() ? Msg.noActions : Msg.openActions;
}
export function getCollectionDetails(count) {
if (count == null) {
return Msg.unknownCollectionSize;
}
if (count === 0) {
return Msg.emptyCollectionSize;
}
var postfix = count === 1 ? "Item" : "Items";
return count + " " + postfix;
}
export function drop(context, error, vm, newValue) {
return context.isSubTypeOf(newValue.draggableType, vm.returnType).
then(function (canDrop) {
if (canDrop) {
vm.setNewValue(newValue);
return true;
}
return false;
}).
catch(function (reject) { return error.handleError(reject); });
}
;
function validateAgainstType(model, modelValue, viewValue, filter) {
// first check
var mandatory = Validate.validateMandatory(model, viewValue);
if (mandatory) {
return mandatory;
}
// if optional but empty always valid
if (modelValue == null || modelValue === "") {
return "";
}
return Validate.validateMandatoryAgainstType(model, viewValue, filter);
}
export function validate(rep, vm, modelValue, viewValue, mandatoryOnly) {
var message = mandatoryOnly ? Validate.validateMandatory(rep, viewValue) : validateAgainstType(rep, modelValue, viewValue, vm.localFilter);
if (message !== Msg.mandatory) {
vm.setMessage(message);
}
else {
vm.resetMessage();
}
vm.clientValid = !message;
return vm.clientValid;
}
;
export function setScalarValueInView(vm, propertyRep, value) {
if (Models.isDate(propertyRep)) {
var date = Models.toUtcDate(value);
vm.value = date ? Models.toDateString(date) : "";
}
else if (Models.isDateTime(propertyRep)) {
var date = Models.toUtcDate(value);
vm.value = date ? Models.toDateTimeString(date) : "";
}
else if (Models.isTime(propertyRep)) {
var time = Models.toTime(value);
vm.value = time ? Models.toTimeString(time) : "";
}
else {
vm.value = value.scalar();
}
}
export function dirtyMarker(context, configService, oid) {
return (configService.config.showDirtyFlag && context.getIsDirty(oid)) ? "*" : "";
}
export function createChoiceViewModels(id, searchTerm, choices) {
return Promise.resolve(map(choices, function (v, k) { return new ChoiceViewModel(v, id, k, searchTerm); }));
}
export function handleErrorResponse(err, messageViewModel, valueViewModels) {
var requiredFieldsMissing = false; // only show warning message if we have nothing else
var fieldValidationErrors = false;
var contributedParameterErrorMsg = "";
each(err.valuesMap(), function (errorValue, k) {
var valueViewModel = find(valueViewModels, function (vvm) { return vvm.id === k; });
if (valueViewModel) {
var reason = errorValue.invalidReason;
if (reason) {
if (reason === "Mandatory") {
var r = "REQUIRED";
requiredFieldsMissing = true;
valueViewModel.description = valueViewModel.description.indexOf(r) === 0 ? valueViewModel.description : r + " " + valueViewModel.description;
}
else {
valueViewModel.setMessage(reason);
fieldValidationErrors = true;
}
}
}
else {
// no matching parm for message - this can happen in contributed actions
// make the message a dialog level warning.
contributedParameterErrorMsg = errorValue.invalidReason || "";
}
});
var msg = contributedParameterErrorMsg || err.invalidReason() || "";
if (requiredFieldsMissing)
msg = msg + " Please complete REQUIRED fields. ";
if (fieldValidationErrors)
msg = msg + " See field validation message(s). ";
if (!msg)
msg = err.warningMessage;
messageViewModel.setMessage(msg);
}
export function incrementPendingPotentAction(context, invokableaction, paneId) {
if (invokableaction.isPotent()) {
context.incPendingPotentActionOrReload(paneId);
}
}
export function decrementPendingPotentAction(context, invokableaction, paneId) {
if (invokableaction.isPotent()) {
context.decPendingPotentActionOrReload(paneId);
}
}
//# sourceMappingURL=helpers-view-models.js.map