@hashuplabs/omni-filter
Version:
OmniFilter Search component
167 lines • 5.62 kB
JavaScript
var OmniToken = /** @class */ (function () {
function OmniToken(parent) {
this._entityName = null;
this.entity = null;
this.marked = false;
this.hasValue = false;
this.operator = null;
this.values = null;
this.id = 'omni-' + String(OmniToken._nextId++);
this.parent = parent;
}
OmniToken.prototype.serialize = function () {
if (!this.hasValues)
return;
var buffer = [];
buffer.push(this.entityName);
buffer.push(this.operator.op);
var x = Array.isArray(this.values) ? this.values : [this.values];
var r = {};
var key = buffer.join('.');
r[key] = x.map(function (v) { return v.values.join('|'); }).join(',');
return r;
};
OmniToken.fromString = function (parent, serialized) {
var x = new OmniToken(parent);
if (serialized[0] === '#')
serialized = serialized.substring(1);
var qs = serialized.split('='); // FIXME: handle this better for values that may contain '='
var partKey = qs.shift();
var partVal = qs.join('=');
if (!partKey)
return;
if (!partVal)
return;
var keyParts = partKey.split('.');
var opKey = keyParts.pop();
if (!opKey)
return;
var entKey = keyParts.join('.');
if (!entKey)
return;
if (!opKey)
return;
// TODO: Implement potential other keyParts
var ent = parent._entities.find(function (v) { return v.entityName === entKey; });
if (!ent) {
// console.warn('OmniToken::fromString --> Unable to find entity: ', entKey);
return;
}
var op = ent.type.operators.find(function (_op) { return _op.op === opKey; });
if (!op) {
// console.warn('OmniTOken::fromString ==> Unable to find operator by key ==> ', opKey);
return;
}
x.entity = ent;
x.entityName = entKey;
x.operator = op;
x.values = partVal.split('|').map(function (p) {
var r = {
id: p,
label: p.toString(),
values: [p],
};
return r;
});
return x;
};
Object.defineProperty(OmniToken.prototype, "hasEntityName", {
get: function () {
return this._entityName !== null && this._entityName !== '';
},
enumerable: false,
configurable: true
});
Object.defineProperty(OmniToken.prototype, "hasOperator", {
get: function () {
return this.operator !== null;
},
enumerable: false,
configurable: true
});
Object.defineProperty(OmniToken.prototype, "hasValues", {
get: function () {
return !(this.values == null || this.values === []);
},
enumerable: false,
configurable: true
});
Object.defineProperty(OmniToken.prototype, "isEmpty", {
get: function () {
return (((this._entityName == null || this._entityName === '') && this.operator == null && this.values == null) ||
this.values === []);
},
enumerable: false,
configurable: true
});
Object.defineProperty(OmniToken.prototype, "isComplete", {
get: function () {
return !this.isEmpty && this.values !== [] && this.values != null;
},
enumerable: false,
configurable: true
});
Object.defineProperty(OmniToken.prototype, "entityName", {
get: function () {
return this._entityName;
},
set: function (value) {
this._entityName = value;
},
enumerable: false,
configurable: true
});
Object.defineProperty(OmniToken.prototype, "oneValue", {
get: function () {
return Array.isArray(this.values) ? this.values[0] : this.values;
},
enumerable: false,
configurable: true
});
Object.defineProperty(OmniToken.prototype, "filterValue", {
get: function () {
if (Array.isArray(this.values) && this.values.length > 1) {
return this.values.flatMap(function (_v) { return _v.values; });
}
var v = this.oneValue.values;
if (this.operator && this.operator.apply) {
return this.operator.apply(v);
}
return v.length > 1 ? v : v[0];
// if( !v ) return null;
// return v.id.replaceAll('"','');
},
enumerable: false,
configurable: true
});
OmniToken.prototype.mark = function () {
this.marked = true;
};
OmniToken.prototype.clear = function () {
this.values = null;
this.hasValue = false;
this.operator = null;
this.entityName = null;
};
OmniToken.prototype.back = function () {
if (this.hasValues) {
var values = this.values;
this.previousValue = values;
this.values = null;
return Array.isArray(values) ? values[0].label.replaceAll('"', '') : values.label.replaceAll('"', '');
}
else if (this.hasOperator) {
var label = this.operator.label;
this.operator = null;
return label;
}
else {
this.entityName = null;
return this.entityName;
}
};
OmniToken._nextId = 1;
return OmniToken;
}());
export { OmniToken };
//# sourceMappingURL=OmniToken.js.map