nakedobjects.spa
Version:
Single Page Application client for a Naked Objects application.
838 lines • 41.6 kB
JavaScript
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
import { InteractionMode, Pane, CollectionViewState } from './route-data';
import { UrlManagerService } from './url-manager.service';
import { RepLoaderService } from './rep-loader.service';
import { Injectable } from '@angular/core';
import * as Constants from './constants';
import * as Models from './models';
import { Subject } from 'rxjs/Subject';
import { ConfigService } from './config.service';
import { LoggerService } from './logger.service';
import each from 'lodash/each';
import find from 'lodash/find';
import filter from 'lodash/filter';
import map from 'lodash/map';
import forEach from 'lodash/forEach';
import keys from 'lodash/keys';
import findKey from 'lodash/findKey';
import first from 'lodash/first';
import omit from 'lodash/omit';
import remove from 'lodash/remove';
import sortBy from 'lodash/sortBy';
var DirtyState;
(function (DirtyState) {
DirtyState[DirtyState["DirtyMustReload"] = 1] = "DirtyMustReload";
DirtyState[DirtyState["DirtyMayReload"] = 2] = "DirtyMayReload";
DirtyState[DirtyState["Clean"] = 3] = "Clean";
})(DirtyState || (DirtyState = {}));
var DirtyList = (function () {
function DirtyList() {
this.dirtyObjects = {};
}
DirtyList.prototype.setDirty = function (oid, alwaysReload) {
if (alwaysReload === void 0) { alwaysReload = false; }
this.setDirtyInternal(oid, alwaysReload ? DirtyState.DirtyMustReload : DirtyState.DirtyMayReload);
};
DirtyList.prototype.setDirtyInternal = function (oid, dirtyState) {
var key = oid.getKey();
this.dirtyObjects[key] = dirtyState;
};
DirtyList.prototype.getDirty = function (oid) {
var key = oid.getKey();
return this.dirtyObjects[key] || DirtyState.Clean;
};
DirtyList.prototype.clearDirty = function (oid) {
var key = oid.getKey();
this.dirtyObjects = omit(this.dirtyObjects, key);
};
DirtyList.prototype.clear = function () {
this.dirtyObjects = {};
};
return DirtyList;
}());
function isSameObject(object, type, id) {
if (object) {
var sid = object.serviceId();
return sid ? sid === type : (object.domainType() === type && object.instanceId() === Models.withNull(id));
}
return false;
}
var TransientCache = (function () {
function TransientCache(depth) {
this.depth = depth;
this.transientCache = [undefined, [], []]; // per pane
}
TransientCache.prototype.add = function (paneId, obj) {
var paneObjects = this.transientCache[paneId];
if (paneObjects.length >= this.depth) {
paneObjects = paneObjects.slice(-(this.depth - 1));
}
paneObjects.push(obj);
this.transientCache[paneId] = paneObjects;
};
TransientCache.prototype.get = function (paneId, type, id) {
var paneObjects = this.transientCache[paneId];
return find(paneObjects, function (o) { return isSameObject(o, type, id); }) || null;
};
TransientCache.prototype.remove = function (paneId, type, id) {
var paneObjects = this.transientCache[paneId];
paneObjects = remove(paneObjects, function (o) { return isSameObject(o, type, id); });
this.transientCache[paneId] = paneObjects;
};
TransientCache.prototype.clear = function () {
this.transientCache = [undefined, [], []];
};
TransientCache.prototype.swap = function () {
var _a = this.transientCache, t1 = _a[1], t2 = _a[2];
this.transientCache[1] = t2;
this.transientCache[2] = t1;
};
return TransientCache;
}());
var RecentCache = (function () {
function RecentCache(keySeparator, depth) {
this.keySeparator = keySeparator;
this.depth = depth;
this.recentCache = [];
}
RecentCache.prototype.add = function (obj) {
// find any matching entries and remove them - should only be one
remove(this.recentCache, function (i) { return i.id() === obj.id(); });
// push obj on top of array
this.recentCache = [obj].concat(this.recentCache);
// drop oldest if we're full
if (this.recentCache.length > this.depth) {
this.recentCache = this.recentCache.slice(0, this.depth);
}
};
RecentCache.prototype.items = function () {
return this.recentCache;
};
RecentCache.prototype.clear = function () {
this.recentCache = [];
};
return RecentCache;
}());
var ValueCache = (function () {
function ValueCache() {
this.currentValues = [undefined, {}, {}];
this.currentId = [undefined, "", ""];
}
ValueCache.prototype.addValue = function (id, valueId, value, paneId) {
if (this.currentId[paneId] !== id) {
this.currentId[paneId] = id;
this.currentValues[paneId] = {};
}
this.currentValues[paneId][valueId] = new Models.Value(value);
};
ValueCache.prototype.getValue = function (id, valueId, paneId) {
if (this.currentId[paneId] !== id) {
this.currentId[paneId] = id;
this.currentValues[paneId] = {};
}
return this.currentValues[paneId][valueId];
};
ValueCache.prototype.getValues = function (id, paneId) {
if (id && this.currentId[paneId] !== id) {
this.currentId[paneId] = id;
this.currentValues[paneId] = {};
}
return this.currentValues[paneId];
};
ValueCache.prototype.clear = function (paneId) {
this.currentId[paneId] = "";
this.currentValues[paneId] = {};
};
ValueCache.prototype.swap = function () {
var _a = this.currentId, i1 = _a[1], i2 = _a[2];
this.currentId[Pane.Pane1] = i2;
this.currentId[Pane.Pane2] = i1;
var _b = this.currentValues, v1 = _b[1], v2 = _b[2];
this.currentValues[Pane.Pane1] = v2;
this.currentValues[Pane.Pane2] = v1;
};
return ValueCache;
}());
var ContextService = (function () {
function ContextService(urlManager, repLoader, configService, loggerService) {
var _this = this;
this.urlManager = urlManager;
this.repLoader = repLoader;
this.configService = configService;
this.loggerService = loggerService;
this.clearingDataFlag = false;
// cached values
this.currentObjects = [undefined, null, null]; // per pane
this.transientCache = new TransientCache(this.configService.config.transientCacheDepth);
this.currentMenuList = {};
this.currentServices = null;
this.currentMenus = null;
this.currentVersion = null;
this.currentUser = null;
this.recentcache = new RecentCache(this.keySeparator, this.configService.config.recentCacheDepth);
this.dirtyList = new DirtyList();
this.currentLists = {};
this.parameterCache = new ValueCache();
this.objectEditCache = new ValueCache();
this.getFile = function (object, url, mt) {
var isDirty = _this.getIsDirty(object.getOid());
return _this.repLoader.getFile(url, mt, isDirty);
};
this.setFile = function (object, url, mt, file) { return _this.repLoader.uploadFile(url, mt, file); };
this.clearCachedFile = function (url) { return _this.repLoader.clearCache(url); };
// exposed for test mocking
this.getDomainObject = function (paneId, oid, interactionMode) {
var type = oid.domainType;
var id = oid.instanceId;
var dirtyState = _this.dirtyList.getDirty(oid);
// no need to reload forms
var forceReload = interactionMode !== InteractionMode.Form &&
((dirtyState === DirtyState.DirtyMustReload) ||
((dirtyState === DirtyState.DirtyMayReload) && _this.configService.config.autoLoadDirty));
if (!forceReload && isSameObject(_this.currentObjects[paneId], type, id)) {
return Promise.resolve(_this.currentObjects[paneId]);
}
// deeper cache for transients
if (interactionMode === InteractionMode.Transient) {
var transientObj = _this.transientCache.get(paneId, type, id);
var p = transientObj
? Promise.resolve(transientObj)
: Promise.reject(new Models.ErrorWrapper(Models.ErrorCategory.ClientError, Models.ClientErrorCode.ExpiredTransient, ""));
return p;
}
var object = new Models.DomainObjectRepresentation();
object.hateoasUrl = _this.configService.config.appPath + "/objects/" + type + "/" + id;
object.setInlinePropertyDetails(interactionMode === InteractionMode.Edit);
_this.incPendingPotentActionOrReload(paneId);
return _this.repLoader.populate(object, forceReload)
.then(function (obj) {
_this.currentObjects[paneId] = obj;
if (forceReload) {
_this.dirtyList.clearDirty(oid);
_this.clearCachedCollections(obj);
}
_this.cacheRecentlyViewed(obj);
_this.decPendingPotentActionOrReload(paneId);
return Promise.resolve(obj);
})
.catch(function (e) {
_this.decPendingPotentActionOrReload(paneId);
throw e;
});
};
this.getIsDirty = function (oid) { return !oid.isService && _this.dirtyList.getDirty(oid) !== DirtyState.Clean; };
this.mustReload = function (oid) {
var dirtyState = _this.dirtyList.getDirty(oid);
return (dirtyState === DirtyState.DirtyMustReload) || ((dirtyState === DirtyState.DirtyMayReload) && _this.configService.config.autoLoadDirty);
};
this.getObjectForEdit = function (paneId, object) { return _this.editOrReloadObject(paneId, object, true); };
this.reloadObject = function (paneId, object) { return _this.editOrReloadObject(paneId, object, false); };
this.getService = function (paneId, serviceType) {
if (isSameObject(_this.currentObjects[paneId], serviceType)) {
return Promise.resolve(_this.currentObjects[paneId]);
}
return _this.getServices()
.then(function (services) {
var service = services.getService(serviceType);
if (service) {
return _this.repLoader.populate(service);
}
return Promise.reject("unknown service " + serviceType);
})
.then(function (service) {
_this.currentObjects[paneId] = service;
return Promise.resolve(service);
});
};
this.getActionDetails = function (actionMember) {
var details = actionMember.getDetails();
if (details) {
return _this.repLoader.populate(details, true);
}
return Promise.reject("Couldn't find details on " + actionMember.actionId());
};
this.getCollectionDetails = function (collectionMember, state, ignoreCache) {
var details = collectionMember.getDetails();
if (details) {
if (state === CollectionViewState.Table) {
details.setUrlParameter(Constants.roInlineCollectionItems, true);
}
var parent_1 = collectionMember.parent;
var isDirty = false;
if (parent_1 instanceof Models.DomainObjectRepresentation) {
var oid = parent_1.getOid();
isDirty = _this.dirtyList.getDirty(oid) !== DirtyState.Clean;
}
return _this.repLoader.populate(details, isDirty || ignoreCache);
}
return Promise.reject("Couldn't find details on " + collectionMember.collectionId());
};
this.getInvokableAction = function (action) {
if (action instanceof Models.InvokableActionMember || action instanceof Models.ActionRepresentation) {
return Promise.resolve(action);
}
return _this.getActionDetails(action);
};
this.getMenu = function (menuId) {
if (_this.currentMenuList[menuId]) {
return Promise.resolve(_this.currentMenuList[menuId]);
}
return _this.getMenus()
.then(function (menus) {
var menu = menus.getMenu(menuId);
if (menu) {
return _this.repLoader.populate(menu);
}
return Promise.reject("couldn't find menu " + menuId);
})
.then(function (menu) {
_this.currentMenuList[menuId] = menu;
return Promise.resolve(menu);
});
};
this.pendingClearMessages = false;
this.pendingClearWarnings = false;
this.clearMessages = function () {
if (_this.pendingClearMessages) {
_this.messagesSource.next([]);
}
_this.pendingClearMessages = !_this.pendingClearMessages;
};
this.clearWarnings = function () {
if (_this.pendingClearWarnings) {
_this.warningsSource.next([]);
}
_this.pendingClearWarnings = !_this.pendingClearWarnings;
};
this.broadcastMessage = function (m) {
_this.pendingClearMessages = false;
_this.messagesSource.next([m]);
};
this.broadcastWarning = function (w) {
_this.pendingClearWarnings = false;
_this.warningsSource.next([w]);
};
this.getHome = function () {
// for moment don't bother caching only called on startup and for whatever resaon cache doesn't work.
// once version cached no longer called.
return _this.repLoader.populate(new Models.HomePageRepresentation({}, _this.configService.config.appPath));
};
this.getServices = function () {
if (_this.currentServices) {
return Promise.resolve(_this.currentServices);
}
return _this.getHome()
.then(function (home) {
var ds = home.getDomainServices();
return _this.repLoader.populate(ds);
})
.then(function (services) {
_this.currentServices = services;
return Promise.resolve(services);
});
};
this.getMenus = function () {
if (_this.currentMenus) {
return Promise.resolve(_this.currentMenus);
}
return _this.getHome()
.then(function (home) {
var ds = home.getMenus();
return _this.repLoader.populate(ds);
})
.then(function (menus) {
_this.currentMenus = menus;
return Promise.resolve(_this.currentMenus);
});
};
this.getVersion = function () {
if (_this.currentVersion) {
return Promise.resolve(_this.currentVersion);
}
return _this.getHome()
.then(function (home) {
var v = home.getVersion();
return _this.repLoader.populate(v);
})
.then(function (version) {
_this.currentVersion = version;
return Promise.resolve(version);
});
};
this.getUser = function () {
if (_this.currentUser) {
return Promise.resolve(_this.currentUser);
}
return _this.getHome()
.then(function (home) {
var u = home.getUser();
return _this.repLoader.populate(u);
})
.then(function (user) {
_this.currentUser = user;
return Promise.resolve(user);
});
};
this.getObject = function (paneId, oid, interactionMode) {
return oid.isService ? _this.getService(paneId, oid.domainType) : _this.getDomainObject(paneId, oid, interactionMode);
};
this.getCachedList = function (paneId, page, pageSize) {
var index = _this.urlManager.getListCacheIndex(paneId, page, pageSize);
var entry = _this.currentLists[index];
return entry ? entry.list : null;
};
this.clearCachedList = function (paneId, page, pageSize) {
var index = _this.urlManager.getListCacheIndex(paneId, page, pageSize);
delete _this.currentLists[index];
};
this.handleResult = function (paneId, result, page, pageSize) {
if (result.resultType() === "list") {
var resultList = result.result().list(); // not null
var index = _this.urlManager.getListCacheIndex(paneId, page, pageSize);
_this.cacheList(resultList, index);
return Promise.resolve(resultList);
}
else {
return Promise.reject(new Models.ErrorWrapper(Models.ErrorCategory.ClientError, Models.ClientErrorCode.WrongType, "expect list"));
}
};
this.getList = function (paneId, resultPromise, page, pageSize) {
return resultPromise().then(function (result) { return _this.handleResult(paneId, result, page, pageSize); });
};
this.getActionExtensionsFromMenu = function (menuId, actionId) {
return _this.getMenu(menuId).then(function (menu) { return Promise.resolve(menu.actionMember(actionId).extensions()); });
};
this.getActionExtensionsFromObject = function (paneId, oid, actionId) {
return _this.getObject(paneId, oid, InteractionMode.View).then(function (object) { return Promise.resolve(object.actionMember(actionId).extensions()); });
};
this.getListFromMenu = function (routeData, page, pageSize) {
var menuId = routeData.menuId;
var actionId = routeData.actionId;
var parms = routeData.actionParams;
var state = routeData.state;
var paneId = routeData.paneId;
var newPage = page || routeData.page;
var newPageSize = pageSize || routeData.pageSize;
var urlParms = _this.getPagingParms(newPage, newPageSize);
if (state === CollectionViewState.Table) {
urlParms[Constants.roInlineCollectionItems] = true;
}
var promise = function () { return _this.getMenu(menuId).then(function (menu) { return _this.getInvokableAction(menu.actionMember(actionId)); }).then(function (details) { return _this.repLoader.invoke(details, parms, urlParms); }); };
return _this.getList(paneId, promise, newPage, newPageSize);
};
this.getListFromObject = function (routeData, page, pageSize) {
var objectId = routeData.objectId;
var actionId = routeData.actionId;
var parms = routeData.actionParams;
var state = routeData.state;
var oid = Models.ObjectIdWrapper.fromObjectId(objectId, _this.keySeparator);
var paneId = routeData.paneId;
var newPage = page || routeData.page;
var newPageSize = pageSize || routeData.pageSize;
var urlParms = _this.getPagingParms(newPage, newPageSize);
if (state === CollectionViewState.Table) {
urlParms[Constants.roInlineCollectionItems] = true;
}
var promise = function () { return _this.getObject(paneId, oid, InteractionMode.View)
.then(function (object) { return _this.getInvokableAction(object.actionMember(actionId)); })
.then(function (details) { return _this.repLoader.invoke(details, parms, urlParms); }); };
return _this.getList(paneId, promise, newPage, newPageSize);
};
this.setObject = function (paneId, co) { return _this.currentObjects[paneId] = co; };
this.swapCurrentObjects = function () {
_this.parameterCache.swap();
_this.objectEditCache.swap();
_this.transientCache.swap();
var _a = _this.currentObjects, p1 = _a[1], p2 = _a[2];
_this.currentObjects[1] = p2;
_this.currentObjects[2] = p1;
};
this.currentError = null;
this.getError = function () { return _this.currentError; };
this.setError = function (e) { return _this.currentError = e; };
this.previousUrl = null;
this.getPreviousUrl = function () { return _this.previousUrl; };
this.setPreviousUrl = function (url) { return _this.previousUrl = url; };
this.doPrompt = function (field, id, searchTerm, setupPrompt, objectValues, digest) {
var map = field.getPromptMap(); // not null
map.setMembers(objectValues);
setupPrompt(map);
var addEmptyOption = field.entryType() !== Models.EntryType.AutoComplete && field.extensions().optional();
return _this.repLoader.retrieve(map, Models.PromptRepresentation, digest).then(function (p) { return p.choices(addEmptyOption); });
};
this.autoComplete = function (field, id, objectValues, searchTerm, digest) {
return _this.doPrompt(field, id, searchTerm, function (map) { return map.setSearchTerm(searchTerm); }, objectValues, digest);
};
this.conditionalChoices = function (field, id, objectValues, args, digest) {
return _this.doPrompt(field, id, null, function (map) { return map.setArguments(args); }, objectValues, digest);
};
this.nextTransientId = 0;
this.setResult = function (action, result, fromPaneId, toPaneId, page, pageSize) {
if (!result.result().isNull()) {
if (result.resultType() === "object") {
var resultObject = result.result().object();
resultObject.keySeparator = _this.keySeparator;
if (resultObject.persistLink()) {
// transient object
var domainType = resultObject.extensions().domainType();
resultObject.wrapped().domainType = domainType;
resultObject.wrapped().instanceId = (_this.nextTransientId++).toString();
resultObject.hateoasUrl = "/" + domainType + "/" + _this.nextTransientId;
// copy the etag down into the object
resultObject.etagDigest = result.etagDigest;
_this.setObject(toPaneId, resultObject);
_this.transientCache.add(toPaneId, resultObject);
_this.urlManager.pushUrlState(toPaneId);
var interactionMode = resultObject.extensions().interactionMode() === "transient"
? InteractionMode.Transient
: InteractionMode.NotPersistent;
_this.urlManager.setObjectWithMode(resultObject, interactionMode, toPaneId);
}
else if (resultObject.selfLink()) {
var selfLink = resultObject.selfLink();
// persistent object
// set the object here and then update the url. That should reload the page but pick up this object
// so we don't hit the server again.
// copy the etag down into the object
resultObject.etagDigest = result.etagDigest;
_this.setObject(toPaneId, resultObject);
// update angular cache
var url = selfLink.href() + "?" + Constants.roInlinePropertyDetails + "=false";
_this.repLoader.addToCache(url, resultObject.wrapped());
// if render in edit must be a form
if (resultObject.extensions().interactionMode() === "form") {
_this.urlManager.pushUrlState(toPaneId);
_this.urlManager.setObjectWithMode(resultObject, InteractionMode.Form, toPaneId);
}
else {
_this.cacheRecentlyViewed(resultObject);
_this.urlManager.setObject(resultObject, toPaneId);
}
}
else {
_this.loggerService.throw("ContextService:setResult result object without self or persist link");
}
}
else if (result.resultType() === "list") {
var resultList = result.result().list();
var parms = _this.parameterCache.getValues(action.actionId(), fromPaneId);
var search = _this.urlManager.setList(action, parms, fromPaneId, toPaneId);
var index = _this.urlManager.getListCacheIndexFromSearch(search, toPaneId, page, pageSize);
_this.cacheList(resultList, index);
}
}
else if (result.resultType() === "void") {
_this.urlManager.triggerPageReloadByFlippingReloadFlagInUrl(fromPaneId);
}
};
this.pendingPotentActionCount = [undefined, 0, 0];
this.warningsSource = new Subject();
this.messagesSource = new Subject();
this.warning$ = this.warningsSource.asObservable();
this.messages$ = this.messagesSource.asObservable();
this.copiedViewModelSource = new Subject();
this.copiedViewModel$ = this.copiedViewModelSource.asObservable();
this.concurrencyErrorSource = new Subject();
this.concurrencyError$ = this.concurrencyErrorSource.asObservable();
this.invokeAction = function (action, parms, fromPaneId, toPaneId, gotoResult) {
if (fromPaneId === void 0) { fromPaneId = 1; }
if (toPaneId === void 0) { toPaneId = 1; }
if (gotoResult === void 0) { gotoResult = true; }
var invokeOnMap = function (iAction) {
var im = iAction.getInvokeMap();
each(parms, function (parm, k) { return im.setParameter(k, parm); });
var setDirty = _this.getSetDirtyFunction(iAction, parms);
return _this.invokeActionInternal(im, iAction, fromPaneId, toPaneId, setDirty, gotoResult);
};
return invokeOnMap(action);
};
this.updateObject = function (object, props, paneId, viewSavedObject) {
var update = object.getUpdateMap();
each(props, function (v, k) { return update.setProperty(k, v); });
return _this.repLoader.retrieve(update, Models.DomainObjectRepresentation, object.etagDigest)
.then(function (updatedObject) {
// This is a kludge because updated object has no self link.
var rawLinks = object.wrapped().links;
updatedObject.wrapped().links = rawLinks;
_this.setNewObject(updatedObject, paneId, viewSavedObject);
return Promise.resolve(updatedObject);
});
};
this.saveObject = function (object, props, paneId, viewSavedObject) {
var persist = object.getPersistMap();
each(props, function (v, k) { return persist.setMember(k, v); });
return _this.repLoader.retrieve(persist, Models.DomainObjectRepresentation, object.etagDigest)
.then(function (updatedObject) {
_this.transientCache.remove(paneId, object.domainType(), object.id());
_this.setNewObject(updatedObject, paneId, viewSavedObject);
return Promise.resolve(updatedObject);
});
};
this.validateUpdateObject = function (object, props) {
var update = object.getUpdateMap();
update.setValidateOnly();
each(props, function (v, k) { return update.setProperty(k, v); });
return _this.repLoader.validate(update, object.etagDigest);
};
this.validateSaveObject = function (object, props) {
var persist = object.getPersistMap();
persist.setValidateOnly();
each(props, function (v, k) { return persist.setMember(k, v); });
return _this.repLoader.validate(persist, object.etagDigest);
};
this.subTypeCache = {};
this.isSubTypeOf = function (toCheckType, againstType) {
if (_this.subTypeCache[toCheckType] && typeof _this.subTypeCache[toCheckType][againstType] !== "undefined") {
return _this.subTypeCache[toCheckType][againstType];
}
var isSubTypeOf = new Models.DomainTypeActionInvokeRepresentation(againstType, toCheckType, _this.configService.config.appPath);
var promise = _this.repLoader.populate(isSubTypeOf, true)
.then(function (updatedObject) {
return updatedObject.value();
})
.catch(function (reject) {
return false;
});
var entry = {};
entry[againstType] = promise;
_this.subTypeCache[toCheckType] = entry;
return promise;
};
this.getRecentlyViewed = function () { return _this.recentcache.items(); };
this.clearRecentlyViewed = function () {
// clear both recent view and cached objects
each(_this.recentcache.items(), function (i) { return _this.dirtyList.setDirty(i.getOid()); });
_this.recentcache.clear();
};
this.cacheFieldValue = function (dialogId, pid, pv, paneId) {
if (paneId === void 0) { paneId = Pane.Pane1; }
_this.parameterCache.addValue(dialogId, pid, pv, paneId);
};
this.getDialogCachedValues = function (dialogId, paneId) {
if (dialogId === void 0) { dialogId = null; }
if (paneId === void 0) { paneId = Pane.Pane1; }
return _this.parameterCache.getValues(dialogId, paneId);
};
this.getObjectCachedValues = function (objectId, paneId) {
if (objectId === void 0) { objectId = null; }
if (paneId === void 0) { paneId = Pane.Pane1; }
return _this.objectEditCache.getValues(objectId, paneId);
};
this.clearDialogCachedValues = function (paneId) {
if (paneId === void 0) { paneId = Pane.Pane1; }
_this.parameterCache.clear(paneId);
};
this.clearObjectCachedValues = function (paneId) {
if (paneId === void 0) { paneId = Pane.Pane1; }
_this.objectEditCache.clear(paneId);
};
this.cachePropertyValue = function (obj, p, pv, paneId) {
if (paneId === void 0) { paneId = Pane.Pane1; }
_this.dirtyList.setDirty(obj.getOid());
_this.objectEditCache.addValue(obj.id(), p.id(), pv, paneId);
};
this.keySeparator = this.configService.config.keySeparator;
}
ContextService.prototype.clearCachedCollections = function (obj) {
var _this = this;
each(obj.collectionMembers(), function (cm) {
var details = cm.getDetails();
if (details) {
var baseUrl = details.getUrl();
details.setUrlParameter(Constants.roInlineCollectionItems, true);
var inlineUrl = details.getUrl();
_this.repLoader.clearCache(baseUrl);
_this.repLoader.clearCache(inlineUrl);
}
});
};
ContextService.prototype.editOrReloadObject = function (paneId, object, inlineDetails) {
var _this = this;
var parms = {};
parms[Constants.roInlinePropertyDetails] = inlineDetails;
return this.repLoader.retrieveFromLink(object.selfLink(), parms)
.then(function (obj) {
_this.currentObjects[paneId] = obj;
var oid = obj.getOid();
_this.dirtyList.clearDirty(oid);
_this.cacheRecentlyViewed(obj);
return Promise.resolve(obj);
});
};
ContextService.prototype.cacheList = function (list, index) {
var entry = this.currentLists[index];
if (entry) {
entry.list = list;
entry.added = Date.now();
}
else {
if (keys(this.currentLists).length >= this.configService.config.listCacheSize) {
//delete oldest;
var oldest_1 = first(sortBy(this.currentLists, "e.added")).added;
var oldestIndex = findKey(this.currentLists, function (e) { return e.added === oldest_1; });
if (oldestIndex) {
delete this.currentLists[oldestIndex];
}
}
this.currentLists[index] = { list: list, added: Date.now() };
}
};
ContextService.prototype.getPagingParms = function (page, pageSize) {
return (page && pageSize) ? { "x-ro-page": page, "x-ro-pageSize": pageSize } : {};
};
ContextService.prototype.incPendingPotentActionOrReload = function (paneId) {
var count = this.pendingPotentActionCount[paneId] + 1;
this.pendingPotentActionCount[paneId] = count;
};
ContextService.prototype.decPendingPotentActionOrReload = function (paneId) {
var count = this.pendingPotentActionCount[paneId] - 1;
if (count < 0) {
count = 0;
this.loggerService.warn("ContextService:decPendingPotentActionOrReload count less than 0");
}
this.pendingPotentActionCount[paneId] = count;
};
ContextService.prototype.isPendingPotentActionOrReload = function (paneId) {
return this.pendingPotentActionCount[paneId] > 0;
};
ContextService.prototype.setMessages = function (result) {
this.pendingClearMessages = this.pendingClearWarnings = false;
var warnings = result.extensions().warnings() || [];
var messages = result.extensions().messages() || [];
this.warningsSource.next(warnings);
this.messagesSource.next(messages);
};
ContextService.prototype.setCopyViewModel = function (dvm) {
this.copiedViewModel = dvm;
this.copiedViewModelSource.next(Models.withUndefined(dvm));
};
ContextService.prototype.getCopyViewModel = function () {
return this.copiedViewModel;
};
ContextService.prototype.setConcurrencyError = function (oid) {
this.concurrencyErrorSource.next(oid);
};
ContextService.prototype.invokeActionInternal = function (invokeMap, action, fromPaneId, toPaneId, setDirty, gotoResult) {
var _this = this;
if (gotoResult === void 0) { gotoResult = false; }
invokeMap.setUrlParameter(Constants.roInlinePropertyDetails, false);
if (action.extensions().returnType() === "list" && action.extensions().renderEagerly()) {
invokeMap.setUrlParameter(Constants.roInlineCollectionItems, true);
}
return this.repLoader.retrieve(invokeMap, Models.ActionResultRepresentation, action.parent.etagDigest)
.then(function (result) {
setDirty();
_this.setMessages(result);
if (gotoResult) {
_this.setResult(action, result, fromPaneId, toPaneId, 1, _this.configService.config.defaultPageSize);
}
return result;
});
};
ContextService.prototype.getSetDirtyFunction = function (action, parms) {
var _this = this;
var parent = action.parent;
if (action.isNotQueryOnly()) {
var setCurrentObjectsDirty_1 = function () {
var pane1Obj = _this.currentObjects[Pane.Pane1];
var pane2Obj = _this.currentObjects[Pane.Pane2];
var setDirty = function (m) {
if (m) {
_this.dirtyList.setDirty(m.getOid());
}
};
setDirty(pane1Obj);
setDirty(pane2Obj);
};
if (parent instanceof Models.DomainObjectRepresentation) {
return function () {
_this.dirtyList.setDirty(parent.getOid());
setCurrentObjectsDirty_1();
};
}
if (parent instanceof Models.CollectionRepresentation) {
return function () {
var selfLink = parent.selfLink();
var oid = Models.ObjectIdWrapper.fromLink(selfLink, _this.keySeparator);
_this.dirtyList.setDirty(oid);
setCurrentObjectsDirty_1();
};
}
if (parent instanceof Models.CollectionMember) {
return function () {
var memberParent = parent.parent;
if (memberParent instanceof Models.DomainObjectRepresentation) {
_this.dirtyList.setDirty(memberParent.getOid());
}
setCurrentObjectsDirty_1();
};
}
if (parent instanceof Models.ListRepresentation && parms) {
var ccaParm = find(action.parameters(), function (p) { return p.isCollectionContributed(); });
var ccaId = ccaParm ? ccaParm.id() : null;
var ccaValue = ccaId ? parms[ccaId] : null;
// this should always be true
if (ccaValue && ccaValue.isList()) {
var refValues = filter(ccaValue.list(), function (v) { return v.isReference(); });
var links_1 = map(refValues, function (v) { return v.link(); });
return function () {
forEach(links_1, function (l) { return _this.dirtyList.setDirty(l.getOid(_this.keySeparator)); });
setCurrentObjectsDirty_1();
};
}
}
return setCurrentObjectsDirty_1;
}
return function () { };
};
ContextService.prototype.setNewObject = function (updatedObject, paneId, viewSavedObject) {
this.setObject(paneId, updatedObject);
this.dirtyList.clearDirty(updatedObject.getOid());
if (viewSavedObject) {
this.urlManager.setObject(updatedObject, paneId);
}
else {
this.urlManager.popUrlState(paneId);
}
};
ContextService.prototype.cacheRecentlyViewed = function (obj) {
// never cache forms
if (obj.extensions().interactionMode() !== "form") {
this.recentcache.add(obj);
}
};
ContextService.prototype.logoff = function () {
var _this = this;
for (var pane = 1; pane <= 2; pane++) {
delete this.currentObjects[pane];
}
this.currentServices = null;
this.currentMenus = null;
this.currentVersion = null;
this.currentUser = null;
this.transientCache.clear();
this.recentcache.clear();
this.dirtyList.clear();
// k will always be defined
forEach(this.currentMenuList, function (v, k) { return delete _this.currentMenuList[k]; });
forEach(this.currentLists, function (v, k) { return delete _this.currentLists[k]; });
};
return ContextService;
}());
ContextService = __decorate([
Injectable(),
__metadata("design:paramtypes", [UrlManagerService,
RepLoaderService,
ConfigService,
LoggerService])
], ContextService);
export { ContextService };
//# sourceMappingURL=context.service.js.map