@aurigma/design-atoms
Version:
Design Atoms is a part of Customer's Canvas SDK which allows for manipulating individual design elements through your code.
97 lines • 3.14 kB
JavaScript
var SelectorType;
(function (SelectorType) {
SelectorType[SelectorType["Class"] = 0] = "Class";
SelectorType[SelectorType["Name"] = 1] = "Name";
SelectorType[SelectorType["Id"] = 2] = "Id";
})(SelectorType || (SelectorType = {}));
;
class Selector {
constructor(value, type) {
this._type = type;
this._value = value;
}
static fromString(selectorItem) {
if (selectorItem == null || selectorItem == '')
return null;
if (selectorItem.startsWith('#')) {
if (selectorItem.length > 1)
return new Selector(selectorItem.substring(1), SelectorType.Id);
else
return null;
}
if (selectorItem.startsWith('.')) {
if (selectorItem.length > 1)
return new Selector(selectorItem.substring(1), SelectorType.Class);
else
return null;
}
return new Selector(selectorItem, SelectorType.Name);
}
match(item) {
switch (this._type) {
case SelectorType.Class:
const mC = this._matchClass(item.class);
return mC;
case SelectorType.Id:
const mId = this._matchId(item.id);
return mId;
case SelectorType.Name:
const mName = this._matchName(item.name);
return mName;
default:
throw new Error(`Unexpected selector type: ${SelectorType[this._type]}`);
}
}
_matchClass(itemRawClass) {
if (itemRawClass == null)
return false;
const classes = itemRawClass.split(" ");
if (classes.length == 0)
return false;
return classes.includes(this._value);
}
_matchId(itemId) {
return itemId == this._value;
}
_matchName(itemName) {
return itemName.toLowerCase() == this._value.toLowerCase();
}
}
class SelectorsModel {
constructor(selectors) {
this._selectors = selectors;
}
match(item) {
if (this._selectors == null)
return false;
for (let selector of this._selectors) {
if (selector.match(item))
return true;
}
return false;
}
}
export class QuerySelector {
process(items, selectors) {
const result = [];
const selectorsModel = this._parseSelectors(selectors);
items.forEach(item => {
if (selectorsModel.match(item))
result.push(item);
});
return result;
}
_parseSelectors(selectors) {
const parsedSelectors = [];
const splitItems = selectors.split(' ');
for (let item of splitItems) {
if (item == null || item == "")
continue;
const selector = Selector.fromString(item);
if (selector)
parsedSelectors.push(selector);
}
return new SelectorsModel(parsedSelectors);
}
}
//# sourceMappingURL=QuerySelector.js.map