@aurigma/design-atoms-model
Version:
Design Atoms is a part of Customer's Canvas SDK which allows for manipulating individual design elements through your code.
164 lines • 7.87 kB
JavaScript
var __read = (this && this.__read) || function (o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator];
if (!m) return o;
var i = m.call(o), r, ar = [], e;
try {
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
}
catch (error) { e = { error: error }; }
finally {
try {
if (r && !r.done && (m = i["return"])) m.call(i);
}
finally { if (e) throw e.error; }
}
return ar;
};
import { BarcodeSubType } from "./BarcodeSubType";
import { BarcodeFormat } from "./BarcodeFormat";
import * as _ from "underscore";
import * as vcard from "vcard-parser";
import { arrayEquals } from "../../Utils";
import { VCardAddressType } from "../Interfaces/IBarcodeData";
var VCardAddress = /** @class */ (function () {
function VCardAddress(address) {
if (address === void 0) { address = {}; }
this.type = [VCardAddressType.none];
Object.assign(this, address);
}
Object.defineProperty(VCardAddress.prototype, "isEmpty", {
get: function () {
return !this.POBox &&
!this.extendedAddress &&
!this.address &&
!this.locality &&
!this.state &&
!this.zipCode &&
!this.country;
},
enumerable: true,
configurable: true
});
VCardAddress.prototype.equals = function (other) {
return this.POBox === other.POBox &&
this.extendedAddress === other.extendedAddress &&
this.address === other.address &&
this.locality === other.locality &&
this.state === other.state &&
this.zipCode === other.zipCode &&
this.country === other.country;
};
VCardAddress.prototype.toVCardValue = function () {
return [
this.POBox,
this.extendedAddress,
this.address,
this.locality,
this.state,
this.zipCode,
this.country,
];
};
VCardAddress.fromVCardValue = function (vCardAddress, meta) {
var _a = __read(vCardAddress, 7), POBox = _a[0], extendedAddress = _a[1], address = _a[2], locality = _a[3], state = _a[4], zipCode = _a[5], country = _a[6];
var type = Object.keys(meta).map(function (key) { return VCardAddressType[key]; });
return new VCardAddress({ POBox: POBox, extendedAddress: extendedAddress, address: address, state: state, zipCode: zipCode, locality: locality, country: country, type: type });
};
return VCardAddress;
}());
export { VCardAddress };
var BarcodeData = /** @class */ (function () {
function BarcodeData(rawData) {
this.barcodeFormat = BarcodeFormat.QR_CODE;
this.barcodeSubType = BarcodeSubType.None;
this.addresses = [];
if (typeof rawData === "string") {
try {
rawData = JSON.parse(rawData);
}
catch (e) {
console.warn("Unable to parse BarcodeData from string ", rawData, e);
rawData = null;
}
}
if (typeof rawData === "object" && rawData != null) {
//TODO Add a validation of raw objects.
Object.assign(this, rawData);
this.addresses = Array.isArray(rawData.addresses)
? rawData.addresses.map(function (address) { return new VCardAddress(address); }) : [];
}
}
Object.defineProperty(BarcodeData.prototype, "isEmpty", {
get: function () {
return _.isEmpty(this.firstName) &&
_.isEmpty(this.lastName) &&
_.isEmpty(this.organization) &&
_.isEmpty(this.position) &&
_.isEmpty(this.email) &&
_.isEmpty(this.mobilePhone) &&
_.isEmpty(this.url) &&
_.isEmpty(this.phone) &&
_.isEmpty(this.data) &&
_.isEmpty(this.barcodeValue) &&
_.isEmpty(this.fax) &&
(this.addresses == null || this.addresses.length === 0);
},
enumerable: true,
configurable: true
});
BarcodeData.prototype.equals = function (other) {
return other != null
&& this.barcodeFormat === other.barcodeFormat
&& this.barcodeSubType === other.barcodeSubType
&& this.firstName === other.firstName
&& this.lastName === other.lastName
&& this.organization === other.organization
&& this.position === other.position
&& this.email === other.email
&& this.mobilePhone === other.mobilePhone
&& this.url === other.url
&& this.data === other.data
&& this.phone === other.phone
&& this.barcodeValue === other.barcodeValue
&& this.fax === other.fax
&& arrayEquals(this.addresses, other.addresses);
};
BarcodeData.prototype.toVCardString = function () {
var vCard = {
n: [{ value: [this.lastName, this.firstName, "", "", ""] }],
"email;internet": [{ value: this.email }],
org: [{ value: this.organization }],
"tel;cell": [{ value: this.mobilePhone }],
"tel;work": [{ value: this.phone }],
"tel;fax": [{ value: this.fax }],
title: [{ value: this.position }],
url: [{ value: this.url }],
};
this.addresses.forEach(function (address) {
var types = address.type.map(function (type) { return VCardAddressType[type]; }).join(";");
vCard["adr;" + types] = [{ value: address.toVCardValue() }];
});
return vcard.generate(vCard);
};
BarcodeData.fromVCardString = function (value) {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v;
var vCard = value != null ? vcard.parse(value) : {};
var result = new BarcodeData();
result.barcodeFormat = BarcodeFormat.QR_CODE;
result.barcodeSubType = BarcodeSubType.VCard;
result.email = (_b = (_a = vCard.email) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.value;
result.lastName = (_d = (_c = vCard.n) === null || _c === void 0 ? void 0 : _c[0]) === null || _d === void 0 ? void 0 : _d.value[0];
result.firstName = (_f = (_e = vCard.n) === null || _e === void 0 ? void 0 : _e[0]) === null || _f === void 0 ? void 0 : _f.value[1];
result.organization = (_h = (_g = vCard.org) === null || _g === void 0 ? void 0 : _g[0]) === null || _h === void 0 ? void 0 : _h.value;
result.mobilePhone = (_k = (_j = vCard.tel) === null || _j === void 0 ? void 0 : _j[0]) === null || _k === void 0 ? void 0 : _k.value;
result.phone = (_m = (_l = vCard.tel) === null || _l === void 0 ? void 0 : _l[1]) === null || _m === void 0 ? void 0 : _m.value;
result.fax = (_p = (_o = vCard.tel) === null || _o === void 0 ? void 0 : _o[2]) === null || _p === void 0 ? void 0 : _p.value;
result.position = (_r = (_q = vCard.title) === null || _q === void 0 ? void 0 : _q[0]) === null || _r === void 0 ? void 0 : _r.value;
result.url = (_t = (_s = vCard.url) === null || _s === void 0 ? void 0 : _s[0]) === null || _t === void 0 ? void 0 : _t.value;
result.addresses = (_v = (_u = vCard.adr) === null || _u === void 0 ? void 0 : _u.map(function (adr) { return VCardAddress.fromVCardValue(adr.value, adr.meta); })) !== null && _v !== void 0 ? _v : [];
return result;
};
return BarcodeData;
}());
export { BarcodeData };
//# sourceMappingURL=BarcodeData.js.map