1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | var util_1 = require("./util");
|
4 | var cache_1 = require("./cache");
|
5 | var cacheUtil_1 = require("./cacheUtil");
|
6 | var getActualUid = function (uidOrEntity) {
|
7 | if (typeof uidOrEntity === 'string') {
|
8 | return uidOrEntity;
|
9 | }
|
10 | else if (typeof uidOrEntity === 'number') {
|
11 | return String(uidOrEntity);
|
12 | }
|
13 | else if (util_1.isObject(uidOrEntity)) {
|
14 | if (util_1.hasUid(uidOrEntity)) {
|
15 | return uidOrEntity[cache_1.config.uidName];
|
16 | }
|
17 | }
|
18 | };
|
19 | var getObject = function (uidOrEntity, instance) {
|
20 | var realUid = getActualUid(uidOrEntity);
|
21 | if (!realUid) {
|
22 | return;
|
23 | }
|
24 | var item = cacheUtil_1.getCachedItem(realUid, instance);
|
25 | return item ? item.entity : undefined;
|
26 | };
|
27 | exports.getItem = function (entity, instance, nodeId) {
|
28 | if (!entity) {
|
29 | throw new TypeError('One get(): requires a uid to retrieve an item from the cache.');
|
30 | }
|
31 | if (util_1.isArray(entity)) {
|
32 | return entity
|
33 | .map(function (item) { return getObject(item, instance); })
|
34 | .filter(function (item) { return (item !== null && item !== undefined); });
|
35 | }
|
36 | return getObject(entity, instance);
|
37 | };
|
38 | var getEditableObject = function (uidOrEntity, instance) {
|
39 | var realUid = getActualUid(uidOrEntity);
|
40 | var existing = exports.getItem(realUid, instance);
|
41 | return existing ? util_1.deepClone(existing, undefined, false) : undefined;
|
42 | };
|
43 | exports.getEditItem = function (obj, instance, nodeId) {
|
44 | if (util_1.isArray(obj)) {
|
45 | return obj
|
46 | .map(function (item) { return getEditableObject(item, instance); })
|
47 | .filter(function (item) { return (item !== null && item !== undefined); });
|
48 | }
|
49 | return getEditableObject(obj, instance);
|
50 | };
|