@pilotlab/lux-attributes
Version:
A luxurious user experience framework, developed by your friends at Pilot.
169 lines • 8.61 kB
JavaScript
;
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_debug_1 = require("@pilotlab/lux-debug");
var lux_strings_1 = require("@pilotlab/lux-strings");
var lux_nodes_1 = require("@pilotlab/lux-nodes");
var attributeBase_1 = require("./attributeBase");
var attributesBase_1 = require("./attributesBase");
var attributeCreateOptions_1 = require("./attributeCreateOptions");
var attributeTools_1 = require("./attributeTools");
var attributeEnums_1 = require("./attributeEnums");
var AttributeFactoryBase = (function (_super) {
__extends(AttributeFactoryBase, _super);
function AttributeFactoryBase() {
return _super !== null && _super.apply(this, arguments) || this;
}
Object.defineProperty(AttributeFactoryBase.prototype, "collection", {
get: function () {
return null;
},
enumerable: true,
configurable: true
});
AttributeFactoryBase.prototype.fromObject = function (obj) {
try {
if (obj instanceof attributeBase_1.default)
return obj;
else if (lux_is_1.default.notEmpty(obj['dataType']))
return this.fromStrings(obj['key'], obj['value'], obj['dataType'].toLocaleLowerCase());
else if (lux_is_1.default.notEmpty(obj['value'])) {
var dataType = attributeTools_1.default.getDataType(obj['value']);
var value = obj['value'];
if (dataType === 'object' && !(value instanceof attributesBase_1.default)) {
dataType = 'collection';
value = this.collection.fromObject(value);
}
return this.fromStrings(obj['key'], value, dataType);
}
else {
var attribute = void 0;
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
var value = obj[key];
var dataType = attributeTools_1.default.getDataType(value);
attribute = this.fromStrings(key, obj[key], dataType);
break;
}
}
return attribute;
}
}
catch (e) {
lux_debug_1.default.error(e, 'Attribute.create.fromObject(...)');
}
};
AttributeFactoryBase.prototype.fromStrings = function (key, value, dataType) {
try {
var node = void 0;
if (lux_is_1.default.empty(dataType))
dataType = attributeTools_1.default.getDataType(value);
switch (dataType.toLowerCase()) {
case 'bool':
case 'boolean':
if (typeof value === 'string')
value = (value.toLowerCase() === 'true');
node = this.instance(new attributeCreateOptions_1.default(value, attributeEnums_1.DataType.BOOLEAN), key);
break;
case 'collection':
if (lux_is_1.default.notEmpty(value)) {
if (typeof value === 'string')
value = JSON.parse(value);
if (!(value instanceof attributesBase_1.default)) {
value = this.collection.fromAny(value);
}
}
node = this.instance(new attributeCreateOptions_1.default(value, attributeEnums_1.DataType.COLLECTION), key);
break;
case 'date_time':
if (typeof value === 'string')
value = Date.parse(value);
node = this.instance(new attributeCreateOptions_1.default(value, attributeEnums_1.DataType.DATE_TIME), key);
break;
case 'number':
if (typeof value === 'string')
value = parseFloat(value);
node = this.instance(new attributeCreateOptions_1.default(value, attributeEnums_1.DataType.NUMBER), key);
break;
case 'double':
case 'number_double':
if (typeof value === 'string')
value = parseFloat(value);
node = this.instance(new attributeCreateOptions_1.default(value, attributeEnums_1.DataType.NUMBER_DOUBLE), key);
break;
case 'int':
case 'number_int':
if (typeof value === 'string')
value = parseInt(value);
node = this.instance(new attributeCreateOptions_1.default(value, attributeEnums_1.DataType.NUMBER_INT), key);
break;
case 'function':
if (typeof value === 'string')
value = lux_strings_1.default.getFunctionFromString(value);
node = this.instance(new attributeCreateOptions_1.default(value, attributeEnums_1.DataType.FUNCTION), key);
break;
case 'hex_value':
node = this.instance(new attributeCreateOptions_1.default(value, attributeEnums_1.DataType.STRING_HEX_VALUE), key);
break;
case 'html':
if (typeof value === 'string')
value = lux_strings_1.default.stringToHtmlDocument(value);
node = this.instance(new attributeCreateOptions_1.default(value, attributeEnums_1.DataType.STRING_HTML), key);
break;
case 'json':
case 'string_json':
node = this.instance(new attributeCreateOptions_1.default(value, attributeEnums_1.DataType.STRING_JSON), key);
break;
case 'reg_exp':
case 'string_reg_exp':
value = new RegExp(value);
node = this.instance(new attributeCreateOptions_1.default(value, attributeEnums_1.DataType.REG_EXP), key);
break;
case 'string':
node = this.instance(new attributeCreateOptions_1.default(value, attributeEnums_1.DataType.STRING), key);
break;
case 'svg':
case 'string_svg':
if (typeof value === 'string')
value = lux_strings_1.default.stringToSvgDocument(value);
node = this.instance(new attributeCreateOptions_1.default(value, attributeEnums_1.DataType.STRING_SVG), key);
break;
case 'uri':
case 'string_uri':
node = this.instance(new attributeCreateOptions_1.default(value, attributeEnums_1.DataType.STRING_URI), key);
break;
case 'xml':
case 'string_xml':
if (typeof value === 'string')
value = lux_strings_1.default.stringToXmlDocument(value);
node = this.instance(new attributeCreateOptions_1.default(value, attributeEnums_1.DataType.STRING_XML), key);
break;
case 'object':
if (typeof value === 'string')
value = JSON.parse(value);
node = this.instance(new attributeCreateOptions_1.default(value, attributeEnums_1.DataType.OBJECT), key);
case 'any':
default:
node = this.instance(new attributeCreateOptions_1.default(value, attributeEnums_1.DataType.ANY), key);
}
return node;
}
catch (e) {
lux_debug_1.default.error(e + " + ', key: " + key + ", value: " + value + ", dataType: " + dataType, 'Attribute.fromStrings(...)');
}
};
return AttributeFactoryBase;
}(lux_nodes_1.NodeFactoryBase));
exports.AttributeFactoryBase = AttributeFactoryBase;
exports.default = AttributeFactoryBase;
//# sourceMappingURL=attributeFactoryBase.js.map