@pilotlab/lux-attributes
Version:
A luxurious user experience framework, developed by your friends at Pilot.
195 lines • 9.17 kB
JavaScript
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
var lux_is_1 = require("@pilotlab/lux-is");
var lux_collections_1 = require("@pilotlab/lux-collections");
var lux_nodes_1 = require("@pilotlab/lux-nodes");
var lux_result_1 = require("@pilotlab/lux-result");
var lux_signals_1 = require("@pilotlab/lux-signals");
var attributeCreateOptions_1 = require("./attributeCreateOptions");
var attributeSetReturn_1 = require("./attributeSetReturn");
var attributeEnums_1 = require("./attributeEnums");
var attributeChangeOptions_1 = require("./attributeChangeOptions");
var attributeUpdateTracker_1 = require("./attributeUpdateTracker");
var AttributesBase = (function (_super) {
__extends(AttributesBase, _super);
function AttributesBase(factory, parent, pathSegment) {
if (pathSegment === void 0) { pathSegment = '.'; }
var _this = _super.call(this, factory, parent, pathSegment) || this;
_this.saveTriggered = new lux_signals_1.Signal();
_this.p_factory = factory;
return _this;
}
Object.defineProperty(AttributesBase.prototype, "create", {
get: function () { return this.p_factory; },
enumerable: true,
configurable: true
});
Object.defineProperty(AttributesBase.prototype, "copy", {
get: function () {
return this.create.collection.fromObject(this.toObject());
},
enumerable: true,
configurable: true
});
AttributesBase.prototype.getOrCreate = function (path, createOptions, segmentCreateOptions) {
if (lux_is_1.default.empty(createOptions))
createOptions = new attributeCreateOptions_1.default(null, attributeEnums_1.DataType.COLLECTION);
if (lux_is_1.default.empty(segmentCreateOptions))
segmentCreateOptions = new attributeCreateOptions_1.default(null, attributeEnums_1.DataType.COLLECTION);
return _super.prototype.getOrCreate.call(this, path, createOptions, segmentCreateOptions);
};
AttributesBase.prototype.get = function (path, value, dataType, label, changeOptions) {
if (changeOptions === void 0) { changeOptions = attributeChangeOptions_1.default.default; }
var createOptions = new attributeCreateOptions_1.default(value, dataType, label, changeOptions);
var segmentCreateOptions = new attributeCreateOptions_1.default(null, attributeEnums_1.DataType.COLLECTION, null, changeOptions);
return this.getOrCreate(path, createOptions, segmentCreateOptions);
};
AttributesBase.prototype.getOnly = function (paths) {
var _this = this;
var collection = this.create.collection.instance();
paths.forEach(function (path) {
var child = _this.get(path);
if (lux_is_1.default.notEmpty(child) && !child.isEmpty)
collection.add(child, null, attributeChangeOptions_1.default.zero);
});
return collection;
};
AttributesBase.prototype.getAll = function (key, options, filter, sort) {
var collectionIn;
var collectionOut = this.create.collection.instance();
if (lux_is_1.default.notEmpty(key)) {
collectionIn = this.create.collection.instance();
this.forEach(function (childLevel1) {
if (childLevel1.dataType === attributeEnums_1.DataType.COLLECTION) {
childLevel1.value.forEach(function (nodeLevel2) {
if (nodeLevel2.key === key) {
var isInclude = true;
if (lux_is_1.default.notEmpty(options)) {
if (lux_is_1.default.notEmpty(options.key))
if (nodeLevel2.value !== options.key)
isInclude = false;
}
if (isInclude)
collectionIn.set(childLevel1.key, childLevel1.value);
}
});
return true;
}
return true;
});
}
else
collectionIn = this;
if (lux_is_1.default.notEmpty(filter)) {
collectionIn.list.forEach(function (child) {
if (filter(child.toObject()))
collectionOut.set(child.key, child.value);
return true;
});
}
else
collectionOut = collectionIn;
if (lux_is_1.default.notEmpty(sort))
collectionOut.sort(sort);
else {
collectionOut.list.sort(function (a, b) {
if (a.value.get(key).value > b.value.get(key).value)
return 1;
else if (a.value.get(key).value < b.value.get(key).value)
return -1;
return 0;
});
}
return collectionOut;
};
AttributesBase.prototype.set = function (path, value, changeOptions) {
var result = new lux_result_1.Result();
if (lux_is_1.default.empty(path))
return result.resolve(new attributeSetReturn_1.default(null, false));
var createOptions = new attributeCreateOptions_1.default(value, null, null, changeOptions);
var segmentCreateOptions = new attributeCreateOptions_1.default(null, attributeEnums_1.DataType.COLLECTION, null, changeOptions);
var node = _super.prototype.getOrCreate.call(this, path, createOptions, segmentCreateOptions);
if (lux_is_1.default.notEmpty(node)) {
node.set(value, changeOptions).then(function (returnValue) {
result.resolve(returnValue);
});
}
return result;
};
AttributesBase.prototype.deleteByKey = function (key, changeOptions) {
if (changeOptions === void 0) { changeOptions = attributeChangeOptions_1.default.default; }
return this.deleteByKey(key, changeOptions);
};
AttributesBase.prototype.interruptAll = function () {
this.p_list.forEach(function (child) {
if (lux_is_1.default.empty(child))
return true;
child.interrupt();
return true;
});
};
AttributesBase.prototype.toMap = function (isIncludeDataTypes) {
if (isIncludeDataTypes === void 0) { isIncludeDataTypes = false; }
var map = new lux_collections_1.MapList();
this.p_list.forEach(function (child) {
if (lux_is_1.default.empty(child))
return true;
map.set(child.key, child.toObject(isIncludeDataTypes));
return true;
});
return map;
};
AttributesBase.prototype.toObject = function (isIncludeDataTypes, isForceIncludeAll) {
if (isIncludeDataTypes === void 0) { isIncludeDataTypes = false; }
if (isForceIncludeAll === void 0) { isForceIncludeAll = false; }
var object = {};
this.p_list.forEach(function (node) {
if (lux_is_1.default.empty(node))
return true;
node.toObject(isIncludeDataTypes, object, isForceIncludeAll);
return true;
});
return object;
};
AttributesBase.prototype.toArray = function () {
var array = [];
for (var i = 0; i < this.p_list.size; i++) {
if (lux_is_1.default.notEmpty(this.p_list.item(i)))
array[i] = this.p_list.item(i).toObject(false);
}
return array;
};
AttributesBase.prototype.toJson = function (isIncludeDataTypes) {
if (isIncludeDataTypes === void 0) { isIncludeDataTypes = false; }
return JSON.stringify(this.toObject(isIncludeDataTypes));
};
AttributesBase.prototype.update = function (data, changeOptions, progressTracker) {
if (changeOptions === void 0) { changeOptions = attributeChangeOptions_1.default.default; }
var attributesNew = this.create.collection.fromAny(data);
var updateTracker = new attributeUpdateTracker_1.default(attributesNew, changeOptions, progressTracker);
if (lux_is_1.default.empty(attributesNew) || attributesNew.size === 0) {
updateTracker.progressTracker.run();
return updateTracker;
}
this.updateTracked(updateTracker);
return updateTracker;
};
AttributesBase.prototype.updateTracked = function (updateTracker) {
updateTracker.update(this);
updateTracker.progressTracker.run();
};
return AttributesBase;
}(lux_nodes_1.NodesBase));
exports.AttributesBase = AttributesBase;
exports.default = AttributesBase;
//# sourceMappingURL=attributesBase.js.map