UNPKG

@pilotlab/lux-attributes

Version:

A luxurious user experience framework, developed by your friends at Pilot.

290 lines 13.8 kB
"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_ids_1 = require("@pilotlab/lux-ids"); var lux_numbers_1 = require("@pilotlab/lux-numbers"); var lux_result_1 = require("@pilotlab/lux-result"); var lux_nodes_1 = require("@pilotlab/lux-nodes"); var attributeEnums_1 = require("./attributeEnums"); var lux_animation_1 = require("@pilotlab/lux-animation"); var attributeTools_1 = require("./attributeTools"); var attributesBase_1 = require("./attributesBase"); var attributeSetReturn_1 = require("./attributeSetReturn"); var attributeChangeOptions_1 = require("./attributeChangeOptions"); var attributeEventArgs_1 = require("./attributeEventArgs"); var AttributeBase = (function (_super) { __extends(AttributeBase, _super); function AttributeBase(factory, key, value, dataType, label, isRoot, isInitialize) { if (key === void 0) { key = ''; } if (isRoot === void 0) { isRoot = false; } if (isInitialize === void 0) { isInitialize = true; } var _this = _super.call(this, null, null, null, null, false) || this; _this.p_dataType = attributeEnums_1.DataType.STRING; if (isInitialize) _this.initialize(factory, key, value, dataType, label, isRoot); return _this; } AttributeBase.prototype.p_onInitializeStarted = function (result, args) { var _this = this; var factory = args[0]; var key = args[1]; var value = args[2]; var dataType = args[3]; var label = args[4]; var isRoot = args[5]; _super.prototype.p_onInitializeStarted.call(this, new lux_result_1.Result(), [factory, isRoot ? lux_nodes_1.NodeType.ROOT : lux_nodes_1.NodeType.BASIC, key, label]).then(function () { if (lux_is_1.default.notEmpty(dataType)) _this.p_dataType = dataType; else { if (lux_is_1.default.notEmpty(value)) _this.p_dataType = attributeTools_1.default.getDataType(value); else _this.p_dataType = attributeEnums_1.DataType.OBJECT; } if (_this.p_dataType === attributeEnums_1.DataType.COLLECTION) { _this.p_wrapAttributes(value, attributeEnums_1.AttributesWrapType.CLEAR_BASE); } else if (lux_is_1.default.notEmpty(value)) _this.p_value = value; result.resolve(value); }); return result; }; AttributeBase.prototype.p_wrapAttributes = function (value, wrapType) { if (wrapType === void 0) { wrapType = attributeEnums_1.AttributesWrapType.OVERWRITE_BASE; } var changeOptions = attributeChangeOptions_1.default.noSaveOrSignal; var attributes; if (lux_is_1.default.notEmpty(value)) { attributes = this.create.collection.fromAny(value, this); } if (lux_is_1.default.empty(attributes)) attributes = this.create.collection.instance(this); if (lux_is_1.default.notEmpty(this.p_value)) { if (wrapType === attributeEnums_1.AttributesWrapType.CLEAR_BASE) { } else if (wrapType === attributeEnums_1.AttributesWrapType.CLEAR_WRAPPED) { attributes.clear(changeOptions); attributes.update(this.p_value, changeOptions); } else if (wrapType === attributeEnums_1.AttributesWrapType.OVERWRITE_BASE) { this.p_value.update(attributes, changeOptions); attributes.clear(attributeChangeOptions_1.default.noSaveOrSignal); attributes.update(this.p_value, changeOptions); } else if (wrapType === attributeEnums_1.AttributesWrapType.OVERWRITE_WRAPPED) { attributes.update(this.p_value, changeOptions); } } this.p_value = attributes; this.p_value.internalParent = this; this.p_children = this.p_value; }; Object.defineProperty(AttributeBase.prototype, "create", { get: function () { return this.p_factory; }, enumerable: true, configurable: true }); Object.defineProperty(AttributeBase.prototype, "value", { get: function () { return this.p_value; }, set: function (value) { this.set(value, attributeChangeOptions_1.default.default.durationZero); }, enumerable: true, configurable: true }); Object.defineProperty(AttributeBase.prototype, "valueTarget", { get: function () { return this.p_valueTarget; }, enumerable: true, configurable: true }); Object.defineProperty(AttributeBase.prototype, "valuePrevious", { get: function () { return this.p_valuePrevious; }, enumerable: true, configurable: true }); Object.defineProperty(AttributeBase.prototype, "dataType", { get: function () { return this.p_dataType; }, set: function (value) { this.p_dataType = value; }, enumerable: true, configurable: true }); Object.defineProperty(AttributeBase.prototype, "attributes", { get: function () { return this.p_children; }, enumerable: true, configurable: true }); Object.defineProperty(AttributeBase.prototype, "isEmpty", { get: function () { return lux_is_1.default.empty(this.key) || (lux_is_1.default.empty(this.p_value) && lux_is_1.default.empty(this.p_valueTarget)); }, enumerable: true, configurable: true }); Object.defineProperty(AttributeBase.prototype, "omit", { get: function () { return this.p_omit; }, set: function (fn) { this.p_omit = fn; }, enumerable: true, configurable: true }); Object.defineProperty(AttributeBase.prototype, "validate", { set: function (fn) { this.p_validate = fn; }, enumerable: true, configurable: true }); Object.defineProperty(AttributeBase.prototype, "copy", { get: function () { return null; }, enumerable: true, configurable: true }); Object.defineProperty(AttributeBase.prototype, "animationKey", { get: function () { if (lux_is_1.default.empty(this._animationKey)) this._animationKey = this.key + '_' + lux_ids_1.Identifier.getSessionUniqueInteger(); return this._animationKey; }, enumerable: true, configurable: true }); AttributeBase.prototype.internalChanged = function (args) { if ((args.changeOptions.isSignalChange || args.changeOptions.isSave) && this.isSignalChange) this.changed.dispatch(args); if (lux_is_1.default.notEmpty(this.parentCollection) && (lux_is_1.default.notEmpty(this.parentCollection.internalChanged))) { this.parentCollection.internalChanged(args); } }; AttributeBase.prototype.set = function (value, changeOptions) { var _this = this; if (changeOptions === void 0) { changeOptions = attributeChangeOptions_1.default.default; } var result = new lux_result_1.Result(); var returnValue = new attributeSetReturn_1.default(this, false); this.p_valuePrevious = this.p_value; if (lux_is_1.default.notEmpty(this.p_validate)) value = this.p_validate(value); if (typeof value === 'number') { var rounded = lux_numbers_1.Numbers.round(value, 4); value = rounded; } if (lux_is_1.default.notEmpty(this.p_animation)) { this.p_animation.interrupt(); this.p_animation = null; } var setValue = function () { if (_this.p_value !== value) { _this.p_value = value; _this.p_valueTarget = value; returnValue.isChanged = true; _this.doChanged(changeOptions.action); } else if (changeOptions.action === attributeEnums_1.AttributeChangeActions.SAVE) { _this.doChanged(changeOptions.action); } result.resolve(returnValue); }; if (this.p_value === value) { if (changeOptions.action === attributeEnums_1.AttributeChangeActions.SAVE) { this.doChanged(changeOptions.action); result.resolve(returnValue); } } else if (this.p_dataType === attributeEnums_1.DataType.COLLECTION) { if (lux_is_1.default.notEmpty(this.p_children) && (this.p_children instanceof attributesBase_1.default)) { this.p_children.update(value, changeOptions).then(function (tracker) { returnValue.isChanged = tracker.isChanged; result.resolve(returnValue); }); } } else if (lux_is_1.default.notEmpty(changeOptions.durationSpeed) && changeOptions.durationSpeed !== 0) { var duration = lux_animation_1.Animation.getDuration(changeOptions.durationSpeed); var easeFinal = lux_animation_1.Animation.validateSpeed(changeOptions.durationSpeed).ease; switch (this.dataType) { case attributeEnums_1.DataType.NUMBER: case attributeEnums_1.DataType.NUMBER_INT: case attributeEnums_1.DataType.NUMBER_DOUBLE: if (duration > 0) { if (this.p_value !== value) { this.p_valueTarget = value; returnValue.isChanged = true; var animation = lux_animation_1.Animation.go(this.p_value, value, duration, easeFinal, changeOptions.repeatCount, this.animationKey); animation.ticked.listen(function (args) { _this.p_value = args.values[0].current; var action = changeOptions.action === attributeEnums_1.AttributeChangeActions.SAVE ? attributeEnums_1.AttributeChangeActions.SIGNAL_CHANGE : changeOptions.action; _this.doChanged(action); }, this); animation.completed.listenOnce(function () { _this.p_value = value; _this.doChanged(changeOptions.action); result.resolve(returnValue); }); returnValue.animation.animations.add(animation); this.p_animation = returnValue.animation; result.resolve(returnValue); } else if (changeOptions.action === attributeEnums_1.AttributeChangeActions.SAVE) { this.doChanged(changeOptions.action); } } else setValue(); break; default: setValue(); } } else setValue(); return result; }; AttributeBase.prototype.interrupt = function () { if (lux_is_1.default.notEmpty(this.p_animation)) this.p_animation.interrupt(); }; AttributeBase.prototype.doChanged = function (action) { var args = new attributeEventArgs_1.default(this, lux_nodes_1.NodeChangeType.UPDATED, new attributeChangeOptions_1.default(action).durationZero); this.internalChanged(args); }; AttributeBase.prototype.toObject = function (isIncludeDataTypes, appendToObject, isForceInclude) { if (isIncludeDataTypes === void 0) { isIncludeDataTypes = false; } if (isForceInclude === void 0) { isForceInclude = false; } if (!isForceInclude && (lux_is_1.default.notEmpty(this.p_omit) && this.p_omit(this.p_value))) return appendToObject; var object = {}; if (isIncludeDataTypes) { object['dataType'] = this.dataType; if (lux_is_1.default.notEmpty(this.label) && this.label != this.key) object['label'] = this.label; if (this.p_value instanceof attributesBase_1.default) { object['value'] = this.p_value.toObject(isIncludeDataTypes, isForceInclude); } else object['value'] = this.p_value; } else { if (this.p_value instanceof attributesBase_1.default) { object = this.p_value.toObject(isIncludeDataTypes, isForceInclude); } else object = this.p_value; } if (lux_is_1.default.notEmpty(appendToObject)) { appendToObject[this.key] = object; return appendToObject; } else return object; }; AttributeBase.prototype.toJson = function (isIncludeDataTypes) { if (isIncludeDataTypes === void 0) { isIncludeDataTypes = false; } return JSON.stringify(this.toObject(isIncludeDataTypes)); }; return AttributeBase; }(lux_nodes_1.NodeBase)); exports.AttributeBase = AttributeBase; exports.default = AttributeBase; //# sourceMappingURL=attributeBase.js.map