@startinblox/core 0.17.21-beta.7 → 0.17.21-beta.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["solid-form-search.ts"],"names":["Sib","WidgetMixin","AttributeBinderMixin","ContextMixin","newWidgetFactory","html","render","ifDefined","uniqID","SolidFormSearch","name","use","attributes","defaultWidget","type","String","default","submitButton","callback","newValue","oldValue","noRender","populate","submitWidget","undefined","classSubmitButton","value","initialState","error","created","element","closest","autoRangeValues","rangeId","attachedElements","Set","defaultMultipleWidget","defaultSetWidget","values","widgets","forEach","widget","component","getValue","JSON","parse","list","multiple","getWidget","field","isSet","tagName","hasAttribute","idField","setAttribute","widgetAttribute","getAttribute","customElements","get","startsWith","widgetFromTagName","attach","elm","has","add","updateAutoRanges","detach","delete","attr","replace","getValuesOfField","id","ldpContains","Array","from","map","data","context","sibStore","setLocalData","change","resource","dispatchEvent","CustomEvent","bubbles","detail","inputChange","getSubmitTemplate","empty","replaceAttributesData","addEventListener","e","preventDefault","fields","getFields","widgetTemplates","Promise","all","createWidgetTemplate","template","register"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,SAASA,GAAT,QAAoB,aAApB;AACA,SAASC,WAAT,QAA4B,uBAA5B;AACA,SAASC,oBAAT,QAAqC,gCAArC;AACA,SAASC,YAAT,QAA6B,wBAA7B;AAEA,SAASC,gBAAT,QAAiC,mCAAjC;AAEA,SAASC,IAAT,EAAeC,MAAf,QAA6B,UAA7B;AACA,SAASC,SAAT,QAA0B,gCAA1B;AACA,SAASC,MAAT,QAAuB,iBAAvB;AAEA,OAAO,MAAMC,eAAe,GAAG;AAC7BC,EAAAA,IAAI,EAAE,mBADuB;AAE7BC,EAAAA,GAAG,EAAE,CAACV,WAAD,EAAcC,oBAAd,EAAoCC,YAApC,CAFwB;AAG7BS,EAAAA,UAAU,EAAE;A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EAEM,KAAKF,YAAL,KAAsB,QAAtB,GAAiClB,IAAjC,qBACwB,KAAKY,YAAL,IAAqB,EAD7C,IAEEZ,IAFF,qBAG6BE,SAAS,CAAC,KAAKU,YAAL,IAAqBO,SAAtB,CAHtC,CAFN;AASD,GA5I4B;;AA6I7BgE,EAAAA,KAAK,GAAS,CAAE,CA7Ia;;AA8I7B,QAAMlE,QAAN,GAAgC;AAC9B,UAAM,KAAKmE,qBAAL,EAAN;;AACA,QAAG,KAAKxE,YAAL,IAAqB,IAAxB,EAA8B;AAC5B,WAAKa,OAAL,CAAa4D,gBAAb,CAA8B,OAA9B,EAAuC,MAAM,KAAKJ,WAAL,EAA7C;AACD,KAFD,MAEO;AACL,WAAKxD,OAAL,CAAa4D,gBAAb,CAA8B,QAA9B,EAAyCC,CAAD,IAAc;AACpDA,QAAAA,CAAC,CAACC,cAAF;AACA,aAAKN,WAAL;AACD,OAHD;AAID;;AACD,UAAMO,MAAM,GAAG,MAAM,KAAKC,SAAL,EAArB;AACA,UAAMC,eAAe,GAAG,MAAMC,OAAO,CAACC,GAAR,CAAYJ,MAAM,CAAClB,GAAP,CAAY1B,KAAD,IAAmB,KAAKiD,oBAAL,CAA0BjD,KAA1B,CAA9B,CAAZ,CAA9B;AACA,UAAMkD,QAAQ,GAAG9F,IAAH,qBAER0F,eAFQ,EAGR,KAAK9E,YAAL,IAAqB,IAArB,GAA4B,EAA5B,GAAiC,KAAKsE,iBAAL,EAHzB,CAAd;AAMAjF,IAAAA,MAAM,CAAC6F,QAAD,EAAW,KAAKrE,OAAhB,CAAN;AACD;;AAjK4B,CAAxB;AAoKP9B,GAAG,CAACoG,QAAJ,CAAa3F,eAAb","sourcesContent":["import { Sib } from '../libs/Sib';\nimport { WidgetMixin } from '../mixins/widgetMixin';\nimport { AttributeBinderMixin } from '../mixins/attributeBinderMixin';\nimport { ContextMixin } from '../mixins/contextMixin';\nimport type { WidgetInterface } from '../mixins/interfaces';\nimport { newWidgetFactory } from '../new-widgets/new-widget-factory';\n\nimport { html, render } from 'lit-html';\nimport { ifDefined } from 'lit-html/directives/if-defined';\nimport { uniqID } from '../libs/helpers';\n\nexport const SolidFormSearch = {\n name: 'solid-form-search',\n use: [WidgetMixin, AttributeBinderMixin, ContextMixin],\n attributes: {\n defaultWidget: {\n type: String,\n default: 'solid-form-label-text',\n },\n submitButton: {\n type: String,\n default: null,\n callback: function (newValue: string, oldValue: string) {\n if (this.noRender == null && newValue !== oldValue) this.populate();\n },\n },\n submitWidget: {\n type: String,\n default: undefined\n },\n classSubmitButton: {\n type: String,\n default: undefined,\n },\n noRender: {\n type: String,\n default: null,\n callback: function (value: boolean) {\n if (value === null) this.populate()\n }\n },\n },\n initialState: {\n error: '',\n },\n created() {\n if (this.element.closest('[no-render]')) this.noRender = ''; // if embedded in no-render, apply no-render to himself\n this.autoRangeValues = {};\n this.rangeId = uniqID();\n this.attachedElements = new Set();\n },\n get defaultMultipleWidget(): string {\n return 'solid-multiple-form';\n },\n get defaultSetWidget(): string {\n return 'solid-set-default';\n },\n\n get value(): object {\n const values = {};\n this.widgets.forEach((widget) => {\n const name = (widget.component || widget).name;\n if (name == null) return;\n let value = widget.component ? widget.component.getValue() : widget.value;\n try {\n value = JSON.parse(value);\n } catch {}\n value = {\n type: widget.component.type,\n list: !!widget.component.multiple,\n value: value,\n }\n values[name] = value;\n });\n return values;\n },\n getWidget(field: string, isSet: boolean = false): WidgetInterface {\n let tagName = '';\n // If auto-range-[field] exists, create range-[field] and sets its value\n if(this.element.hasAttribute('auto-range-' + field) && !this.element.hasAttribute('range-' + field)) {\n const idField = `${this.rangeId}_${field}`;\n this.element.setAttribute('range-' + field, 'store://local.' + idField);\n this.populate();\n }\n \n const widgetAttribute = this.element.getAttribute('widget-' + field);\n // Choose widget\n if (!widgetAttribute && (this.element.hasAttribute('range-' + field) || this.element.hasAttribute('enum-' + field))) {\n tagName = 'solid-form-dropdown'\n } else {\n tagName = widgetAttribute || (!isSet ? this.defaultWidget : this.defaultSetWidget);\n }\n // Create widget\n if (!customElements.get(tagName)) { // component does not exist\n if (tagName.startsWith('solid')) newWidgetFactory(tagName); // solid- -> create it\n }\n\n return this.widgetFromTagName(tagName);\n },\n async attach(elm: any) {\n if(this.attachedElements.has(elm)) return;\n this.attachedElements.add(elm);\n await this.updateAutoRanges();\n },\n async detach(elm: any) {\n if(!this.attachedElements.has(elm)) return;\n this.attachedElements.delete(elm);\n await this.updateAutoRanges();\n },\n async updateAutoRanges() {\n for(const attr of (this.element as Element).attributes) {\n if(!attr['name'].startsWith('auto-range-')) continue;\n const field = attr['name'].replace('auto-range-', '');\n const autoRangeValues = new Set();\n for(const elm of this.attachedElements) {\n for(const value of await elm.getValuesOfField(field)) {\n autoRangeValues.add(value);\n }\n }\n const idField = `${this.rangeId}_${field}`;\n const id = `store://local.${idField}`;\n const ldpContains = Array.from(autoRangeValues).map(id => ({'@id' : id}));\n const data = {\n \"@type\": \"ldp:Container\",\n \"@context\": this.context,\n \"ldp:contains\" : ldpContains,\n };\n sibStore.setLocalData(data, id);\n }\n },\n change(resource: object): void {\n this.element.dispatchEvent(\n new CustomEvent('formChange', {\n bubbles: true,\n detail: { resource },\n }),\n );\n },\n async inputChange(): Promise<void> {\n this.change(this.value);\n },\n getSubmitTemplate() {\n return html`\n <div class=${ifDefined(this.classSubmitButton)}>\n ${this.submitWidget === 'button' ? html`\n <button type=\"submit\">${this.submitButton || ''}</button>\n ` : html`\n <input type=\"submit\" value=${ifDefined(this.submitButton || undefined)}>\n `}\n </div>\n ` \n },\n empty(): void {},\n async populate(): Promise<void> {\n await this.replaceAttributesData();\n if(this.submitButton == null) {\n this.element.addEventListener('input', () => this.inputChange());\n } else {\n this.element.addEventListener('submit', (e: Event) => {\n e.preventDefault();\n this.inputChange();\n });\n }\n const fields = await this.getFields();\n const widgetTemplates = await Promise.all(fields.map((field: string) => this.createWidgetTemplate(field)));\n const template = html`\n <form>\n ${widgetTemplates}\n ${this.submitButton == null ? '' : this.getSubmitTemplate()}\n </form>\n `;\n render(template, this.element);\n }\n};\n\nSib.register(SolidFormSearch);"]}
|
|
1
|
+
{"version":3,"sources":["solid-form-search.ts"],"names":["Sib","WidgetMixin","AttributeBinderMixin","ContextMixin","newWidgetFactory","html","render","ifDefined","uniqID","SolidFormSearch","name","use","attributes","defaultWidget","type","String","default","submitButton","callback","newValue","oldValue","noRender","populate","submitWidget","undefined","classSubmitButton","value","initialState","error","created","element","closest","autoRangeValues","rangeId","attachedElements","Set","defaultMultipleWidget","defaultSetWidget","values","widgets","forEach","widget","component","getValue","JSON","parse","list","multiple","getWidget","field","isSet","tagName","hasAttribute","idField","setAttribute","widgetAttribute","getAttribute","customElements","get","startsWith","widgetFromTagName","attach","elm","has","add","updateAutoRanges","detach","delete","attr","replace","getValuesOfField","id","ldpContains","Array","from","map","data","context","sibStore","setLocalData","change","resource","dispatchEvent","CustomEvent","bubbles","detail","inputChange","getSubmitTemplate","empty","replaceAttributesData","addEventListener","e","preventDefault","fields","getFields","widgetTemplates","Promise","all","createWidgetTemplate","template","register"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,SAASA,GAAT,QAAoB,aAApB;AACA,SAASC,WAAT,QAA4B,uBAA5B;AACA,SAASC,oBAAT,QAAqC,gCAArC;AACA,SAASC,YAAT,QAA6B,wBAA7B;AAEA,SAASC,gBAAT,QAAiC,mCAAjC;AAEA,SAASC,IAAT,EAAeC,MAAf,QAA6B,UAA7B;AACA,SAASC,SAAT,QAA0B,gCAA1B;AACA,SAASC,MAAT,QAAuB,iBAAvB;AAGA,OAAO,MAAMC,eAAe,GAAG;AAC7BC,EAAAA,IAAI,EAAE,mBADuB;AAE7BC,EAAAA,GAAG,EAAE,CAACV,WAAD,EAAcC,oBAAd,EAAoCC,YAApC,CAFwB;AAG7BS,EAAAA,UAAU,EAAE;AACVC,IAAAA,aAAa,EAAE;AACbC,MAAAA,IAAI,EAAEC,MADO;AAEbC,MAAAA,OAAO,EAAE;AAFI,K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EEZ,IAFF,qBAG6BE,SAAS,CAAC,KAAKU,YAAL,IAAqBO,SAAtB,CAHtC,CAFN;AASD,GA5I4B;;AA6I7BgE,EAAAA,KAAK,GAAS,CAAE,CA7Ia;;AA8I7B,QAAMlE,QAAN,GAAgC;AAC9B,UAAM,KAAKmE,qBAAL,EAAN;;AACA,QAAG,KAAKxE,YAAL,IAAqB,IAAxB,EAA8B;AAC5B,WAAKa,OAAL,CAAa4D,gBAAb,CAA8B,OAA9B,EAAuC,MAAM,KAAKJ,WAAL,EAA7C;AACD,KAFD,MAEO;AACL,WAAKxD,OAAL,CAAa4D,gBAAb,CAA8B,QAA9B,EAAyCC,CAAD,IAAc;AACpDA,QAAAA,CAAC,CAACC,cAAF;AACA,aAAKN,WAAL;AACD,OAHD;AAID;;AACD,UAAMO,MAAM,GAAG,MAAM,KAAKC,SAAL,EAArB;AACA,UAAMC,eAAe,GAAG,MAAMC,OAAO,CAACC,GAAR,CAAYJ,MAAM,CAAClB,GAAP,CAAY1B,KAAD,IAAmB,KAAKiD,oBAAL,CAA0BjD,KAA1B,CAA9B,CAAZ,CAA9B;AACA,UAAMkD,QAAQ,GAAG9F,IAAH,qBAER0F,eAFQ,EAGR,KAAK9E,YAAL,IAAqB,IAArB,GAA4B,EAA5B,GAAiC,KAAKsE,iBAAL,EAHzB,CAAd;AAMAjF,IAAAA,MAAM,CAAC6F,QAAD,EAAW,KAAKrE,OAAhB,CAAN;AACD;;AAjK4B,CAAxB;AAoKP9B,GAAG,CAACoG,QAAJ,CAAa3F,eAAb","sourcesContent":["import { Sib } from '../libs/Sib';\nimport { WidgetMixin } from '../mixins/widgetMixin';\nimport { AttributeBinderMixin } from '../mixins/attributeBinderMixin';\nimport { ContextMixin } from '../mixins/contextMixin';\nimport type { WidgetInterface } from '../mixins/interfaces';\nimport { newWidgetFactory } from '../new-widgets/new-widget-factory';\n\nimport { html, render } from 'lit-html';\nimport { ifDefined } from 'lit-html/directives/if-defined';\nimport { uniqID } from '../libs/helpers';\nimport type { SearchQuery } from '../libs/interfaces';\n\nexport const SolidFormSearch = {\n name: 'solid-form-search',\n use: [WidgetMixin, AttributeBinderMixin, ContextMixin],\n attributes: {\n defaultWidget: {\n type: String,\n default: 'solid-form-label-text',\n },\n submitButton: {\n type: String,\n default: null,\n callback: function (newValue: string, oldValue: string) {\n if (this.noRender == null && newValue !== oldValue) this.populate();\n },\n },\n submitWidget: {\n type: String,\n default: undefined\n },\n classSubmitButton: {\n type: String,\n default: undefined,\n },\n noRender: {\n type: String,\n default: null,\n callback: function (value: boolean) {\n if (value === null) this.populate()\n }\n },\n },\n initialState: {\n error: '',\n },\n created() {\n if (this.element.closest('[no-render]')) this.noRender = ''; // if embedded in no-render, apply no-render to himself\n this.autoRangeValues = {};\n this.rangeId = uniqID();\n this.attachedElements = new Set();\n },\n get defaultMultipleWidget(): string {\n return 'solid-multiple-form';\n },\n get defaultSetWidget(): string {\n return 'solid-set-default';\n },\n\n get value(): SearchQuery {\n const values: SearchQuery = {};\n this.widgets.forEach((widget) => {\n const name = (widget.component || widget).name;\n if (name == null) return;\n let value = widget.component ? widget.component.getValue() : widget.value;\n try {\n value = JSON.parse(value);\n } catch {}\n value = {\n type: widget.component.type,\n list: !!widget.component.multiple,\n value: value,\n }\n values[name] = value;\n });\n return values;\n },\n getWidget(field: string, isSet: boolean = false): WidgetInterface {\n let tagName = '';\n // If auto-range-[field] exists, create range-[field] and sets its value\n if(this.element.hasAttribute('auto-range-' + field) && !this.element.hasAttribute('range-' + field)) {\n const idField = `${this.rangeId}_${field}`;\n this.element.setAttribute('range-' + field, 'store://local.' + idField);\n this.populate();\n }\n \n const widgetAttribute = this.element.getAttribute('widget-' + field);\n // Choose widget\n if (!widgetAttribute && (this.element.hasAttribute('range-' + field) || this.element.hasAttribute('enum-' + field))) {\n tagName = 'solid-form-dropdown'\n } else {\n tagName = widgetAttribute || (!isSet ? this.defaultWidget : this.defaultSetWidget);\n }\n // Create widget\n if (!customElements.get(tagName)) { // component does not exist\n if (tagName.startsWith('solid')) newWidgetFactory(tagName); // solid- -> create it\n }\n\n return this.widgetFromTagName(tagName);\n },\n async attach(elm: any) {\n if(this.attachedElements.has(elm)) return;\n this.attachedElements.add(elm);\n await this.updateAutoRanges();\n },\n async detach(elm: any) {\n if(!this.attachedElements.has(elm)) return;\n this.attachedElements.delete(elm);\n await this.updateAutoRanges();\n },\n async updateAutoRanges() {\n for(const attr of (this.element as Element).attributes) {\n if(!attr['name'].startsWith('auto-range-')) continue;\n const field = attr['name'].replace('auto-range-', '');\n const autoRangeValues = new Set();\n for(const elm of this.attachedElements) {\n for(const value of await elm.getValuesOfField(field)) {\n autoRangeValues.add(value);\n }\n }\n const idField = `${this.rangeId}_${field}`;\n const id = `store://local.${idField}`;\n const ldpContains = Array.from(autoRangeValues).map(id => ({'@id' : id}));\n const data = {\n \"@type\": \"ldp:Container\",\n \"@context\": this.context,\n \"ldp:contains\" : ldpContains,\n };\n sibStore.setLocalData(data, id);\n }\n },\n change(resource: object): void {\n this.element.dispatchEvent(\n new CustomEvent('formChange', {\n bubbles: true,\n detail: { resource },\n }),\n );\n },\n async inputChange(): Promise<void> {\n this.change(this.value);\n },\n getSubmitTemplate() {\n return html`\n <div class=${ifDefined(this.classSubmitButton)}>\n ${this.submitWidget === 'button' ? html`\n <button type=\"submit\">${this.submitButton || ''}</button>\n ` : html`\n <input type=\"submit\" value=${ifDefined(this.submitButton || undefined)}>\n `}\n </div>\n ` \n },\n empty(): void {},\n async populate(): Promise<void> {\n await this.replaceAttributesData();\n if(this.submitButton == null) {\n this.element.addEventListener('input', () => this.inputChange());\n } else {\n this.element.addEventListener('submit', (e: Event) => {\n e.preventDefault();\n this.inputChange();\n });\n }\n const fields = await this.getFields();\n const widgetTemplates = await Promise.all(fields.map((field: string) => this.createWidgetTemplate(field)));\n const template = html`\n <form>\n ${widgetTemplates}\n ${this.submitButton == null ? '' : this.getSubmitTemplate()}\n </form>\n `;\n render(template, this.element);\n }\n};\n\nSib.register(SolidFormSearch);"]}
|
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
import { parseFieldsString, findClosingBracketMatchIndex, compare, uniqID } from "./helpers.js";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Check if the field is a set
|
|
5
|
+
* @param field - field to test
|
|
6
|
+
* @param fields - list of fields
|
|
7
|
+
* @returns true if the field is a set, false otherwise
|
|
8
|
+
*/
|
|
9
|
+
const isSet = (field, fields) => {
|
|
10
|
+
if (!fields) return false;
|
|
11
|
+
let foundSets = fields.match(getSetRegexp(field));
|
|
12
|
+
return foundSets ? foundSets.length > 0 : false;
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* Get the list of fields in the set
|
|
16
|
+
* @param field - set name to get
|
|
17
|
+
* @param fields - list of fields
|
|
18
|
+
* @returns a list of fields in the set
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
const getSet = (field, fields) => {
|
|
23
|
+
const setString = fields.match(getSetRegexp(field));
|
|
24
|
+
if (!setString) return [];
|
|
25
|
+
const firstSetBracket = fields.indexOf(setString[0]) + setString[0].length - 1;
|
|
26
|
+
const lastSetBracket = findClosingBracketMatchIndex(fields, firstSetBracket);
|
|
27
|
+
const set = fields.substring(firstSetBracket + 1, lastSetBracket);
|
|
28
|
+
return parseFieldsString(set);
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
const getSetRegexp = field => {
|
|
32
|
+
return new RegExp("(^|\\,|\\(|\\s)\\s*".concat(field, "\\s*\\("), 'g');
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
* Check if the field is a special search field
|
|
36
|
+
* @param field - field to test
|
|
37
|
+
* @param searchForm - current search form
|
|
38
|
+
* @returns true if the field is a special search field
|
|
39
|
+
*/
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
const isSearchField = (field, searchForm) => {
|
|
43
|
+
return searchForm.hasAttribute('search-' + field);
|
|
44
|
+
};
|
|
45
|
+
/**
|
|
46
|
+
* Get the fields targetted by a search field
|
|
47
|
+
* @param field - search field to get
|
|
48
|
+
* @param searchForm - current search form
|
|
49
|
+
* @returns a list of fields targetted by the current search field
|
|
50
|
+
*/
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
const getSearchField = (field, searchForm) => {
|
|
54
|
+
return parseFieldsString(searchForm.getAttribute('search-' + field));
|
|
55
|
+
};
|
|
56
|
+
/**
|
|
57
|
+
* Throw or simply return value
|
|
58
|
+
* @param throwOn - should throw on True or False
|
|
59
|
+
* @param ret - value to return
|
|
60
|
+
* @returns
|
|
61
|
+
*/
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
const orThrow = (throwOn, ret) => {
|
|
65
|
+
if (throwOn === true && ret) throw true;
|
|
66
|
+
if (throwOn === false && !ret) throw false;
|
|
67
|
+
return ret;
|
|
68
|
+
};
|
|
69
|
+
/**
|
|
70
|
+
* Compare a value to the query
|
|
71
|
+
* @param val - value to compare
|
|
72
|
+
* @param query - object to know how and what value to compare
|
|
73
|
+
* @param throwOn - should function throw error on True or False
|
|
74
|
+
* @returns true if value matches, throw error otherwise
|
|
75
|
+
*/
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
const matchValue = async (val, query, throwOn) => {
|
|
79
|
+
var _subject$isContainer;
|
|
80
|
+
|
|
81
|
+
const subject = await val;
|
|
82
|
+
if (subject == null && query.value === '') return orThrow(throwOn, true); // filter not set and subject not existing -> ignore filter
|
|
83
|
+
|
|
84
|
+
if (subject == null) return orThrow(throwOn, false); // return false; // property does not exist on resource
|
|
85
|
+
|
|
86
|
+
if (query.list) {
|
|
87
|
+
// Filter on a container
|
|
88
|
+
if (query.value.length === 0) return orThrow(throwOn, true);
|
|
89
|
+
|
|
90
|
+
for (const v of query.value) {
|
|
91
|
+
if (await matchValue(subject, {
|
|
92
|
+
type: query.type,
|
|
93
|
+
value: v,
|
|
94
|
+
list: false
|
|
95
|
+
})) {
|
|
96
|
+
// do not throw here, we need the result
|
|
97
|
+
return orThrow(throwOn, true);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
return orThrow(throwOn, false);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
if ((_subject$isContainer = subject.isContainer) !== null && _subject$isContainer !== void 0 && _subject$isContainer.call(subject)) {
|
|
105
|
+
let ret = Promise.resolve(query.value === ''); // if no query, return a match
|
|
106
|
+
|
|
107
|
+
for (const value of subject['ldp:contains']) {
|
|
108
|
+
ret = (await ret) || (await matchValue(value, query)); // do not throw here, we need the result
|
|
109
|
+
|
|
110
|
+
if (ret) return orThrow(throwOn, true);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
return orThrow(throwOn, await ret);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
return orThrow(throwOn, compare[query.type](subject, query.value));
|
|
117
|
+
};
|
|
118
|
+
/**
|
|
119
|
+
* Cache properties of a filter to avoid repeated parsing
|
|
120
|
+
* @param cacheKey - uniq key
|
|
121
|
+
* @param filter - filter to check
|
|
122
|
+
* @param fields - fields attribute of the element
|
|
123
|
+
* @param searchForm - current search form
|
|
124
|
+
*/
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
const cacheFieldsProps = (cacheKey, filter, fields, searchForm) => {
|
|
128
|
+
if (!window.cachePropsSearchFilter[cacheKey]) {
|
|
129
|
+
window.cachePropsSearchFilter[cacheKey] = {
|
|
130
|
+
setFields: isSet(filter, fields) ? getSet(filter, fields) : null,
|
|
131
|
+
setSearchFields: isSearchField(filter, searchForm) ? getSearchField(filter, searchForm) : null
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
/**
|
|
136
|
+
* Check if one resource match one filter
|
|
137
|
+
* @param resource - resource to test
|
|
138
|
+
* @param filter - name of the property tested
|
|
139
|
+
* @param query - query object to know how to compare values
|
|
140
|
+
* @param fields - fields attribute of the element
|
|
141
|
+
* @param searchForm - searchForm component
|
|
142
|
+
* @param filterId - uniqId used to fill the cache
|
|
143
|
+
* @param throwOn - should function throw error on True or False
|
|
144
|
+
* @returns return true if the resource matches the filter, throw error otherwise
|
|
145
|
+
*/
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
const matchFilter = async (resource, filter, query, fieldsAttr, searchForm, filterId, throwOn) => {
|
|
149
|
+
let fields = null;
|
|
150
|
+
const cacheKey = "".concat(filter, "_").concat(filterId);
|
|
151
|
+
cacheFieldsProps(cacheKey, filter, fieldsAttr, searchForm);
|
|
152
|
+
|
|
153
|
+
if (window.cachePropsSearchFilter[cacheKey].setFields !== null) {
|
|
154
|
+
fields = window.cachePropsSearchFilter[cacheKey].setFields;
|
|
155
|
+
} else if (window.cachePropsSearchFilter[cacheKey].setSearchFields !== null) {
|
|
156
|
+
fields = window.cachePropsSearchFilter[cacheKey].setSearchFields;
|
|
157
|
+
} else {
|
|
158
|
+
// search on 1 field
|
|
159
|
+
return matchValue(resource[filter], query, throwOn);
|
|
160
|
+
} // search on multiple fields
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
try {
|
|
164
|
+
await Promise.all((fields || []).map(field => matchFilter(resource, field, query, fieldsAttr, searchForm, filterId, true // stop searching when 1 filter is true (= OR)
|
|
165
|
+
)));
|
|
166
|
+
} catch (e) {
|
|
167
|
+
return true;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
throw false;
|
|
171
|
+
};
|
|
172
|
+
/**
|
|
173
|
+
* Check if one resource match all the filters
|
|
174
|
+
* @param resource - resource to test
|
|
175
|
+
* @param filters - current filters
|
|
176
|
+
* @param filterNames - names of the filters
|
|
177
|
+
* @param fields - fields attribute of the element
|
|
178
|
+
* @param searchForm - searchForm component
|
|
179
|
+
* @param filterId - uniqId used to fill the cache
|
|
180
|
+
* @returns true if resource match, false otherwise
|
|
181
|
+
*/
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
const matchFilters = async (resource, filters, filterNames, fields, searchForm, filterId) => {
|
|
185
|
+
// return true if all filters values are contained in the corresponding field of the resource
|
|
186
|
+
try {
|
|
187
|
+
await Promise.all(filterNames.map(filter => matchFilter(resource, filter, filters[filter], fields, searchForm, filterId, false // stop searching when 1 filter is false (= AND)
|
|
188
|
+
)));
|
|
189
|
+
} catch (e) {
|
|
190
|
+
return false;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
return true;
|
|
194
|
+
};
|
|
195
|
+
/**
|
|
196
|
+
* Check which resources match the filters
|
|
197
|
+
* @param resources - list of resources to filter
|
|
198
|
+
* @param filters - current filters
|
|
199
|
+
* @param fields - fields attribute of the element
|
|
200
|
+
* @param searchForm - searchForm component
|
|
201
|
+
* @returns resources filtered
|
|
202
|
+
*/
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
const searchInResources = async (resources, filters, fields, searchForm) => {
|
|
206
|
+
// Optim: use cache to do these things only once
|
|
207
|
+
const filterNames = Object.keys(filters);
|
|
208
|
+
const filterId = uniqID();
|
|
209
|
+
window.cachePropsSearchFilter = {};
|
|
210
|
+
return Promise.all(resources.map(resource => matchFilters(resource, filters, filterNames, fields, searchForm, filterId)));
|
|
211
|
+
};
|
|
212
|
+
|
|
213
|
+
export { searchInResources };
|
|
214
|
+
//# sourceMappingURL=filter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["filter.ts"],"names":["parseFieldsString","findClosingBracketMatchIndex","compare","uniqID","isSet","field","fields","foundSets","match","getSetRegexp","length","getSet","setString","firstSetBracket","indexOf","lastSetBracket","set","substring","RegExp","isSearchField","searchForm","hasAttribute","getSearchField","getAttribute","orThrow","throwOn","ret","matchValue","val","query","subject","value","list","v","type","isContainer","Promise","resolve","cacheFieldsProps","cacheKey","filter","window","cachePropsSearchFilter","setFields","setSearchFields","matchFilter","resource","fieldsAttr","filterId","all","map","e","matchFilters","filters","filterNames","searchInResources","resources","Object","keys"],"mappings":"AAAA,SAASA,iBAAT,EAA4BC,4BAA5B,EAA0DC,OAA1D,EAAmEC,MAAnE,QAAiF,WAAjF;;AAGA;AACA;AACA;AACA;AACA;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AAG,OACnBN,QADmB,EAEnBO,OAFmB,EAGnBC,WAHmB,EAInBhD,MAJmB,EAKnBc,UALmB,EAMnB4B,QANmB,KAOE;AACrB;AACA,MAAI;AACF,UAAMZ,OAAO,CAACa,GAAR,CAAYK,WAAW,CAACJ,GAAZ,CAAgBV,MAAM,IACtCK,WAAW,CACTC,QADS,EAETN,MAFS,EAGTa,OAAO,CAACb,MAAD,CAHE,EAITlC,MAJS,EAKTc,UALS,EAMT4B,QANS,EAOT,KAPS,CAOH;AAPG,KADK,CAAZ,CAAN;AAWD,GAZD,CAYE,OAAOG,CAAP,EAAU;AAAE,WAAO,KAAP;AAAc;;AAC5B,SAAO,IAAP;AACD,CAvBD;AAyBA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;AACA,MAAMI,iBAAiB,GAAG,OACxBC,SADwB,EAExBH,OAFwB,EAGxB/C,MAHwB,EAIxBc,UAJwB,KAKrB;AACH;AACA,QAAMkC,WAAW,GAAGG,MAAM,CAACC,IAAP,CAAYL,OAAZ,CAApB;AACA,QAAML,QAAQ,GAAG7C,MAAM,EAAvB;AACAsC,EAAAA,MAAM,CAACC,sBAAP,GAAgC,EAAhC;AAEA,SAAON,OAAO,CAACa,GAAR,CAAYO,SAAS,CAACN,GAAV,CAAcJ,QAAQ,IACvCM,YAAY,CACVN,QADU,EAEVO,OAFU,EAGVC,WAHU,EAIVhD,MAJU,EAKVc,UALU,EAMV4B,QANU,CADK,CAAZ,CAAP;AAUD,CArBD;;AAuBA,SACEO,iBADF","sourcesContent":["import { parseFieldsString, findClosingBracketMatchIndex, compare, uniqID } from \"./helpers\";\nimport type { Query, SearchQuery } from \"./interfaces\";\n\n/**\n * Check if the field is a set\n * @param field - field to test\n * @param fields - list of fields\n * @returns true if the field is a set, false otherwise\n */\nconst isSet = (field: string, fields: string): boolean => {\n if (!fields) return false;\n let foundSets = fields.match(getSetRegexp(field));\n return foundSets ? foundSets.length > 0 : false;\n}\n/**\n * Get the list of fields in the set\n * @param field - set name to get\n * @param fields - list of fields\n * @returns a list of fields in the set\n */\nconst getSet = (field: string, fields: string): string[] => {\n const setString = fields.match(getSetRegexp(field));\n if (!setString) return [];\n const firstSetBracket = fields.indexOf(setString[0]) + (setString[0].length) - 1;\n const lastSetBracket = findClosingBracketMatchIndex(fields, firstSetBracket);\n const set = fields.substring(firstSetBracket + 1, lastSetBracket);\n return parseFieldsString(set);\n}\nconst getSetRegexp = (field: string) => {\n return new RegExp(`(^|\\\\,|\\\\(|\\\\s)\\\\s*${field}\\\\s*\\\\(`, 'g')\n}\n/**\n * Check if the field is a special search field\n * @param field - field to test\n * @param searchForm - current search form\n * @returns true if the field is a special search field\n */\nconst isSearchField = (field: string, searchForm: any) => {\n return searchForm.hasAttribute('search-' + field);\n}\n/**\n * Get the fields targetted by a search field\n * @param field - search field to get\n * @param searchForm - current search form\n * @returns a list of fields targetted by the current search field\n */\nconst getSearchField = (field: string, searchForm: any): string[] => {\n return parseFieldsString(searchForm.getAttribute('search-' + field));\n}\n\n/**\n * Throw or simply return value\n * @param throwOn - should throw on True or False\n * @param ret - value to return\n * @returns\n */\nconst orThrow = (throwOn: boolean | undefined, ret: boolean) => {\n if (throwOn === true && ret) throw true;\n if (throwOn === false && !ret) throw false;\n return ret;\n}\n\n/**\n * Compare a value to the query\n * @param val - value to compare\n * @param query - object to know how and what value to compare\n * @param throwOn - should function throw error on True or False\n * @returns true if value matches, throw error otherwise\n */\nconst matchValue = async (val: any, query: Query, throwOn?: boolean): Promise<boolean> => {\n const subject = await val;\n if (subject == null && query.value === '') return orThrow(throwOn, true); // filter not set and subject not existing -> ignore filter\n if (subject == null) return orThrow(throwOn, false); // return false; // property does not exist on resource\n if (query.list) { // Filter on a container\n if(query.value.length === 0) return orThrow(throwOn, true);\n for(const v of query.value) {\n if (await matchValue(subject, { type: query.type, value: v, list: false })) { // do not throw here, we need the result\n return orThrow(throwOn, true);\n }\n }\n return orThrow(throwOn, false);\n }\n if (subject.isContainer?.()) {\n let ret: any = Promise.resolve(query.value === ''); // if no query, return a match\n for (const value of subject['ldp:contains']) {\n ret = await ret || await matchValue(value, query); // do not throw here, we need the result\n if (ret) return orThrow(throwOn, true);\n }\n return orThrow(throwOn, await ret);\n }\n\n return orThrow(throwOn, compare[query.type](subject, query.value));\n}\n\n/**\n * Cache properties of a filter to avoid repeated parsing\n * @param cacheKey - uniq key\n * @param filter - filter to check\n * @param fields - fields attribute of the element\n * @param searchForm - current search form\n */\nconst cacheFieldsProps = (\n cacheKey: string,\n filter: string,\n fields: string,\n searchForm: any\n) => {\n if (!window.cachePropsSearchFilter[cacheKey]) {\n window.cachePropsSearchFilter[cacheKey] = {\n setFields: isSet(filter, fields) ? getSet(filter, fields) : null,\n setSearchFields: isSearchField(filter, searchForm) ? getSearchField(filter, searchForm) : null\n }\n }\n}\n\n/**\n * Check if one resource match one filter\n * @param resource - resource to test\n * @param filter - name of the property tested\n * @param query - query object to know how to compare values\n * @param fields - fields attribute of the element\n * @param searchForm - searchForm component\n * @param filterId - uniqId used to fill the cache\n * @param throwOn - should function throw error on True or False\n * @returns return true if the resource matches the filter, throw error otherwise\n */\nconst matchFilter = async (\n resource: object,\n filter: string,\n query: Query,\n fieldsAttr: string,\n searchForm: any,\n filterId: string,\n throwOn: boolean\n) => {\n let fields: string[] | null = null;\n\n const cacheKey = `${filter}_${filterId}`;\n cacheFieldsProps(cacheKey, filter, fieldsAttr, searchForm);\n\n if (window.cachePropsSearchFilter[cacheKey].setFields !== null) {\n fields = window.cachePropsSearchFilter[cacheKey].setFields;\n } else if (window.cachePropsSearchFilter[cacheKey].setSearchFields !== null) {\n fields = window.cachePropsSearchFilter[cacheKey].setSearchFields;\n } else { // search on 1 field\n return matchValue(resource[filter], query, throwOn);\n }\n\n // search on multiple fields\n try {\n await Promise.all((fields || []).map(field =>\n matchFilter(\n resource,\n field,\n query,\n fieldsAttr,\n searchForm,\n filterId,\n true // stop searching when 1 filter is true (= OR)\n )\n ))\n } catch (e) { return true }\n throw false;\n}\n\n/**\n * Check if one resource match all the filters\n * @param resource - resource to test\n * @param filters - current filters\n * @param filterNames - names of the filters\n * @param fields - fields attribute of the element\n * @param searchForm - searchForm component\n * @param filterId - uniqId used to fill the cache\n * @returns true if resource match, false otherwise\n */\nconst matchFilters = async (\n resource: object,\n filters: SearchQuery,\n filterNames: string[],\n fields: string,\n searchForm: any,\n filterId: string\n): Promise<boolean> => {\n // return true if all filters values are contained in the corresponding field of the resource\n try {\n await Promise.all(filterNames.map(filter =>\n matchFilter(\n resource,\n filter,\n filters[filter],\n fields,\n searchForm,\n filterId,\n false // stop searching when 1 filter is false (= AND)\n )\n ));\n } catch (e) { return false }\n return true;\n}\n\n/**\n * Check which resources match the filters\n * @param resources - list of resources to filter\n * @param filters - current filters\n * @param fields - fields attribute of the element\n * @param searchForm - searchForm component\n * @returns resources filtered\n */\nconst searchInResources = async (\n resources: object[],\n filters: SearchQuery,\n fields: string,\n searchForm: any\n) => {\n // Optim: use cache to do these things only once\n const filterNames = Object.keys(filters);\n const filterId = uniqID();\n window.cachePropsSearchFilter = {};\n\n return Promise.all(resources.map(resource =>\n matchFilters(\n resource,\n filters,\n filterNames,\n fields,\n searchForm,\n filterId\n )\n ));\n}\n\nexport {\n searchInResources\n};\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["interfaces.ts"],"names":[],"mappings":"AAKC;AAUA;AA8BA;AAsBA;AAIA","sourcesContent":["export interface AccessorStaticInterface {\n [key: string]: {\n get: Function;\n set: Function;\n }\n};\n\nexport interface ArrayOfHooksInterface {\n created: HookInterface[];\n attached: HookInterface[];\n detached: HookInterface[];\n}\n\nexport interface AttributeChangedCallbackInterface {\n (newValue: any, oldValue: any): void;\n};\n\nexport interface AttributeDefinitionInterface {\n type?: any;\n default?: any;\n required?: boolean;\n callback?: AttributeChangedCallbackInterface;\n}\n\nexport interface AttributesDefinitionInterface {\n [key: string]: AttributeDefinitionInterface;\n}\n\nexport type ComponentConstructorInterface = new (element: HTMLElement) => ComponentInterface;\n\nexport interface ComponentInterface {\n element: HTMLElement;\n created():void;\n attached():void;\n detached():void;\n\n attributesCallback(key: string, value: any, oldValue: any):void;\n}\n\nexport interface ComponentStaticInterface extends HasAttributesDefinitionInterface, HasInitialStateInterface {\n name: String;\n\n hooks: ArrayOfHooksInterface;\n methods: Map<string, Function>;\n accessors: AccessorStaticInterface;\n};\n\nexport interface HasAttributesDefinitionInterface {\n attributes?: AttributesDefinitionInterface | undefined;\n}\n\nexport interface HasHooksInterface {\n created?: HookInterface;\n attached?: HookInterface;\n detached?: HookInterface;\n}\n\nexport interface HasInitialStateInterface {\n initialState?: object | undefined;\n}\n\nexport interface HasMixinsInterface {\n use?: MixinStaticInterface[] | undefined;\n}\n\nexport interface HookInterface {\n (): void;\n};\n\nexport interface MixinStaticInterface extends HasAttributesDefinitionInterface, HasMixinsInterface, HasInitialStateInterface, HasHooksInterface {\n name: String;\n};\n\nexport interface LocationResourceInterface {\n lat?: HookInterface;\n lng?: HookInterface;\n}\n"]}
|
|
1
|
+
{"version":3,"sources":["interfaces.ts"],"names":[],"mappings":"AAKC;AAUA;AA8BA;AAsBA;AAIA","sourcesContent":["export interface AccessorStaticInterface {\n [key: string]: {\n get: Function;\n set: Function;\n }\n};\n\nexport interface ArrayOfHooksInterface {\n created: HookInterface[];\n attached: HookInterface[];\n detached: HookInterface[];\n}\n\nexport interface AttributeChangedCallbackInterface {\n (newValue: any, oldValue: any): void;\n};\n\nexport interface AttributeDefinitionInterface {\n type?: any;\n default?: any;\n required?: boolean;\n callback?: AttributeChangedCallbackInterface;\n}\n\nexport interface AttributesDefinitionInterface {\n [key: string]: AttributeDefinitionInterface;\n}\n\nexport type ComponentConstructorInterface = new (element: HTMLElement) => ComponentInterface;\n\nexport interface ComponentInterface {\n element: HTMLElement;\n created():void;\n attached():void;\n detached():void;\n\n attributesCallback(key: string, value: any, oldValue: any):void;\n}\n\nexport interface ComponentStaticInterface extends HasAttributesDefinitionInterface, HasInitialStateInterface {\n name: String;\n\n hooks: ArrayOfHooksInterface;\n methods: Map<string, Function>;\n accessors: AccessorStaticInterface;\n};\n\nexport interface HasAttributesDefinitionInterface {\n attributes?: AttributesDefinitionInterface | undefined;\n}\n\nexport interface HasHooksInterface {\n created?: HookInterface;\n attached?: HookInterface;\n detached?: HookInterface;\n}\n\nexport interface HasInitialStateInterface {\n initialState?: object | undefined;\n}\n\nexport interface HasMixinsInterface {\n use?: MixinStaticInterface[] | undefined;\n}\n\nexport interface HookInterface {\n (): void;\n};\n\nexport interface MixinStaticInterface extends HasAttributesDefinitionInterface, HasMixinsInterface, HasInitialStateInterface, HasHooksInterface {\n name: String;\n};\n\nexport interface LocationResourceInterface {\n lat?: HookInterface;\n lng?: HookInterface;\n}\nexport interface Query {\n value: any\n type: \"boolean\" | \"number\" | \"range\" | \"resource\" | \"string\";\n list: boolean\n}\n\nexport interface SearchQuery {\n [key: string]: Query\n}"]}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { searchInResources } from '../libs/filter.js';
|
|
2
2
|
const FilterMixin = {
|
|
3
3
|
name: 'filter-mixin',
|
|
4
4
|
use: [],
|
|
@@ -55,7 +55,7 @@ const FilterMixin = {
|
|
|
55
55
|
if (this.filteredBy || this.searchFields) {
|
|
56
56
|
if (!this.searchCount.has(context)) this.searchCount.set(context, 1);
|
|
57
57
|
if (!this.searchForm) await this.createFilter(context);
|
|
58
|
-
const filteredResources = await
|
|
58
|
+
const filteredResources = await searchInResources(resources, this.filters, this.fields, this.searchForm);
|
|
59
59
|
resources = resources.filter((_v, index) => filteredResources[index]);
|
|
60
60
|
}
|
|
61
61
|
|
|
@@ -70,56 +70,6 @@ const FilterMixin = {
|
|
|
70
70
|
await this.populate();
|
|
71
71
|
},
|
|
72
72
|
|
|
73
|
-
async matchValue(subject, query) {
|
|
74
|
-
var _subject$isContainer;
|
|
75
|
-
|
|
76
|
-
if (subject == null && query.value === '') return true; // filter not set and subject not existing -> ignore filter
|
|
77
|
-
|
|
78
|
-
if (subject == null) return false; // property does not exist on resource
|
|
79
|
-
// Filter on a container
|
|
80
|
-
|
|
81
|
-
if (query.list) {
|
|
82
|
-
if (query.value.length === 0) return true;
|
|
83
|
-
|
|
84
|
-
for (const v of query.value) {
|
|
85
|
-
const q = {
|
|
86
|
-
type: query.type,
|
|
87
|
-
value: v
|
|
88
|
-
};
|
|
89
|
-
if (await this.matchValue(subject, q)) return true;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
return false;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
if ((_subject$isContainer = subject.isContainer) !== null && _subject$isContainer !== void 0 && _subject$isContainer.call(subject)) {
|
|
96
|
-
let ret = Promise.resolve(query.value === ''); // if no query, return a match
|
|
97
|
-
|
|
98
|
-
for (const value of subject['ldp:contains']) {
|
|
99
|
-
ret = (await ret) || (await this.matchValue(value, query));
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
return ret;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
return compare[query.type](subject, query.value);
|
|
106
|
-
},
|
|
107
|
-
|
|
108
|
-
async matchFilter(resource, filter, query) {
|
|
109
|
-
let fields = [];
|
|
110
|
-
if (this.isSet(filter)) fields = this.getSet(filter);else if (this.isSearchField(filter)) fields = this.getSearchField(filter); // search on 1 field
|
|
111
|
-
|
|
112
|
-
if (fields.length == 0) return this.matchValue(await resource[filter], query); // search on multiple fields
|
|
113
|
-
|
|
114
|
-
return fields.reduce( // return true if it matches at least one of the fields
|
|
115
|
-
async (initial, field) => (await initial) || (await this.matchFilter(resource, field, query)), Promise.resolve(false));
|
|
116
|
-
},
|
|
117
|
-
|
|
118
|
-
async matchFilters(resource) {
|
|
119
|
-
//return true if all filters values are contained in the corresponding field of the resource
|
|
120
|
-
return Object.keys(this.filters).reduce(async (initial, filter) => (await initial) && (await this.matchFilter(resource, filter, this.filters[filter])), Promise.resolve(true));
|
|
121
|
-
},
|
|
122
|
-
|
|
123
73
|
async getValuesOfField(field) {
|
|
124
74
|
const arrayOfDataObjects = this.resource['ldp:contains'];
|
|
125
75
|
const arrayOfDataIds = [];
|
|
@@ -179,15 +129,6 @@ const FilterMixin = {
|
|
|
179
129
|
});
|
|
180
130
|
this.element.insertBefore(this.searchForm, this.element.firstChild);
|
|
181
131
|
await this.searchForm.component.populate();
|
|
182
|
-
},
|
|
183
|
-
|
|
184
|
-
// Search fields
|
|
185
|
-
isSearchField(field) {
|
|
186
|
-
return this.searchForm.hasAttribute('search-' + field);
|
|
187
|
-
},
|
|
188
|
-
|
|
189
|
-
getSearchField(field) {
|
|
190
|
-
return parseFieldsString(this.searchForm.getAttribute('search-' + field));
|
|
191
132
|
}
|
|
192
133
|
|
|
193
134
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["filterMixin.ts"],"names":["compare","parseFieldsString","FilterMixin","name","use","initialState","searchCount","attributes","searchFields","type","String","default","filteredBy","callback","newValue","searchForm","getAttribute","component","detach","populate","created","Map","element","addEventListener","window","document","contains","updateAutoRanges","attached","listPostProcessors","push","filterCallback","bind","filters","value","filterList","resources","div","context","has","set","createFilter","filteredResources","Promise","all","map","matchFilters","filter","_v","index","nextProcessor","shift","get","resource","empty","matchValue","subject","query","list","length","v","q","isContainer","ret","resolve","matchFilter","fields","isSet","getSet","isSearchField","getSearchField","reduce","initial","field","Object","keys","getValuesOfField","arrayOfDataObjects","arrayOfDataIds","obj","nextArrayOfObjects","console","warn","nextArrayOfIds","getElementById","createElement","attach","toggleAttribute","searchAttributes","Array","from","attr","startsWith","replace","forEach","setAttribute","insertBefore","firstChild","hasAttribute"],"mappings":"AAAA,SAASA,OAAT,EAAkBC,iBAAlB,QAA2C,iBAA3C;AAEA,MAAMC,WAAW,GAAG;AAClBC,EAAAA,IAAI,EAAE,cADY;AAElBC,EAAAA,GAAG,EAAE,EAFa;AAGlBC,EAAAA,YAAY,EAAE;AACZC,IAAAA,WAAW,EAAE;AADD,GAHI;AAMlBC,EAAAA,UAAU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,CAAaC,UAAb,CAAwB,SAAxB,CADS,EAExB/C,GAFwB,CAEpB8C,IAAI,KAAK;AACZxF,MAAAA,IAAI,EAAEwF,IAAI,CAAC,MAAD,CAAJ,CAAaE,OAAb,CAAqB,SAArB,EAAgC,EAAhC,CADM;AAEZ3D,MAAAA,KAAK,EAAEyD,IAAI,CAAC,OAAD;AAFC,KAAL,CAFgB,CAAzB;AAOAH,IAAAA,gBAAgB,CAACM,OAAjB,CAAyB,CAAC;AAAC3F,MAAAA,IAAD;AAAO+B,MAAAA;AAAP,KAAD,KAAmB;AAC1C,WAAKnB,UAAL,CAAgBgF,YAAhB,CAA6B5F,IAA7B,EAAmC+B,KAAnC;AACD,KAFD;AAIA,SAAKZ,OAAL,CAAa0E,YAAb,CAA0B,KAAKjF,UAA/B,EAA2C,KAAKO,OAAL,CAAa2E,UAAxD;AACA,UAAM,KAAKlF,UAAL,CAAgBE,SAAhB,CAA0BE,QAA1B,EAAN;AACD,GAhKiB;;AAiKlB;AACAkD,EAAAA,aAAa,CAACI,KAAD,EAAgB;AAC3B,WAAO,KAAK1D,UAAL,CAAgBmF,YAAhB,CAA6B,YAAYzB,KAAzC,CAAP;AACD,GApKiB;;AAqKlBH,EAAAA,cAAc,CAACG,KAAD,EAA0B;AACtC,WAAOxE,iBAAiB,CAAC,KAAKc,UAAL,CAAgBC,YAAhB,CAA6B,YAAYyD,KAAzC,CAAD,CAAxB;AACD;;AAvKiB,CAApB;AA0KA,SACEvE,WADF","sourcesContent":["import { compare, parseFieldsString } from '../libs/helpers';\n\nconst FilterMixin = {\n name: 'filter-mixin',\n use: [],\n initialState: {\n searchCount: null,\n },\n attributes: {\n searchFields: {\n type: String,\n default: null\n },\n filteredBy: {\n type: String,\n default: null,\n callback(newValue: string) {\n // if we change search form, re-populate\n if (newValue && this.searchForm && newValue !== this.searchForm.getAttribute('id')) {\n this.searchForm.component.detach(this);\n this.searchForm = null;\n this.populate();\n }\n }\n }\n },\n created() {\n this.searchCount = new Map();\n this.element.addEventListener('populate', () => {\n if (!window.document.contains(this.element)) return;\n this.searchForm?.component.updateAutoRanges();\n })\n },\n attached(): void {\n this.listPostProcessors.push(this.filterCallback.bind(this));\n },\n get filters(): object {\n return this.searchForm?.component.value ?? {};\n },\n set filters(filters) {\n this.searchForm.component.value = filters;\n this.filterList();\n },\n async filterCallback(resources: object[], listPostProcessors: Function[], div: HTMLElement, context: string): Promise<void> {\n if (this.filteredBy || this.searchFields) {\n if (!this.searchCount.has(context)) this.searchCount.set(context, 1);\n if (!this.searchForm) await this.createFilter(context);\n const filteredResources = await Promise.all(resources.map(this.matchFilters.bind(this)));\n resources =\tresources.filter((_v, index) => filteredResources[index]);\n }\n\n const nextProcessor = listPostProcessors.shift();\n if(nextProcessor) await nextProcessor(resources, listPostProcessors, div, context + (this.searchCount.get(context) || ''));\n },\n async filterList(context: string): Promise<void> {\n this.searchCount.set(context, this.searchCount.get(context) + 1);\n if (!this.resource) return;\n this.empty();\n await this.populate();\n },\n async matchValue(subject, query): Promise<boolean> {\n if (subject == null && query.value === '') return true; // filter not set and subject not existing -> ignore filter\n if (subject == null) return false; // property does not exist on resource\n // Filter on a container\n if (query.list) {\n if(query.value.length === 0) return true;\n for(const v of query.value) {\n const q = {\n type: query.type,\n value: v,\n }\n if(await this.matchValue(subject, q)) return true;\n }\n return false;\n }\n if (subject.isContainer?.()) {\n let ret = Promise.resolve(query.value === ''); // if no query, return a match\n for (const value of subject['ldp:contains']) {\n ret = await ret || await this.matchValue(value, query)\n }\n return ret;\n }\n return compare[query.type](subject, query.value);\n },\n async matchFilter(resource: object, filter: string, query: any): Promise<boolean> {\n let fields: string[] = [];\n if (this.isSet(filter)) fields = this.getSet(filter);\n else if (this.isSearchField(filter)) fields = this.getSearchField(filter);\n\n // search on 1 field\n if (fields.length == 0)\n return this.matchValue(await resource[filter], query);\n\n // search on multiple fields\n return fields.reduce( // return true if it matches at least one of the fields\n async (initial, field) => await initial || await this.matchFilter(resource, field, query),\n Promise.resolve(false),\n );\n },\n async matchFilters(resource: object): Promise<boolean> {\n //return true if all filters values are contained in the corresponding field of the resource\n return Object.keys(this.filters).reduce(\n async (initial, filter) =>\n await initial && await this.matchFilter(resource, filter, this.filters[filter]),\n Promise.resolve(true)\n );\n },\n async getValuesOfField(field: string) {\n const arrayOfDataObjects = this.resource['ldp:contains'];\n const arrayOfDataIds: string[] = [];\n for (const obj of arrayOfDataObjects) {\n // for each element, if it's an object, catch all elements in 'ldp:contains' key\n const nextArrayOfObjects = await obj[field];\n if (!nextArrayOfObjects) continue;\n\n if (typeof nextArrayOfObjects !== \"object\") {\n console.warn(`The format value of ${field} is not suitable with auto-range-[field] attribute`);\n continue;\n }\n\n const nextArrayOfIds = nextArrayOfObjects['ldp:contains'];\n for (const obj of nextArrayOfIds) {\n // catch each element id\n arrayOfDataIds.push(obj['@id']);\n }\n if (nextArrayOfObjects['@type'] !== 'ldp:Container') {\n // if no element in 'ldp:contains', catch object id\n arrayOfDataIds.push(nextArrayOfObjects['@id']);\n }\n }\n return arrayOfDataIds;\n },\n async createFilter(context: string): Promise<void> {\n const filteredBy = this.filteredBy;\n if (filteredBy != null) {\n this.searchForm = document.getElementById(filteredBy)\n if (!this.searchForm) throw `#${filteredBy} is not in DOM`;\n } else {\n this.searchForm = document.createElement(`solid-form-search`);\n }\n this.searchForm.component.attach(this);\n this.searchForm.addEventListener('formChange', () => {\n this.filterList(context);\n });\n this.searchForm.toggleAttribute('naked', true);\n\n if (filteredBy) return;\n\n //pass attributes to search form\n const searchAttributes = Array.from((this.element as Element).attributes)\n .filter(attr => attr['name'].startsWith('search-'))\n .map(attr => ({\n name: attr['name'].replace('search-', ''),\n value: attr['value'],\n }));\n\n searchAttributes.forEach(({name, value}) => {\n this.searchForm.setAttribute(name, value);\n });\n\n this.element.insertBefore(this.searchForm, this.element.firstChild);\n await this.searchForm.component.populate();\n },\n // Search fields\n isSearchField(field: string) {\n return this.searchForm.hasAttribute('search-' + field);\n },\n getSearchField(field: string): string[] {\n return parseFieldsString(this.searchForm.getAttribute('search-' + field));\n },\n}\n\nexport {\n FilterMixin\n}"]}
|
|
1
|
+
{"version":3,"sources":["filterMixin.ts"],"names":["searchInResources","FilterMixin","name","use","initialState","searchCount","attributes","searchFields","type","String","default","filteredBy","callback","newValue","searchForm","getAttribute","component","detach","populate","created","Map","element","addEventListener","window","document","contains","updateAutoRanges","attached","listPostProcessors","push","filterCallback","bind","filters","value","filterList","resources","div","context","has","set","createFilter","filteredResources","fields","filter","_v","index","nextProcessor","shift","get","resource","empty","getValuesOfField","field","arrayOfDataObjects","arrayOfDataIds","obj","nextArrayOfObjects","console","warn","nextArrayOfIds","getElementById","createElement","attach","toggleAttribute","searchAttributes","Array","from","attr","startsWith","map","replace","forEach","setAttribute","insertBefore","firstChild"],"mappings":"AACA,SAASA,iBAAT,QAAkC,gBAAlC;AAEA,MAAMC,WAAW,GAAG;AAClBC,EAAAA,IAAI,EAAE,cADY;AAElBC,EAAAA,GAAG,EAAE,EAFa;AAGlBC,EAAAA,YAAY,EAAE;AACZC,IAAAA,WAAW,EAAE;AADD,GAHI;AAMlBC,EAAAA,UAAU,EAAE;AACVC,IAAAA,YAAY,EAAE;AACZC,MAAAA,IAAI,EAAEC,MADM;AAEZC,MAAAA,OAAO,EAAE;AAFG,KADJ;AAKVC,IAAAA,UAAU,EAAE;AACVH,MAAAA,IAAI,EAAEC,MADI;AAEVC,MAAAA,OAAO,EAAE,IAFC;;AAGVE,MAAAA,QAAQ,CAACC,QAAD,EAAmB;AACzB;AACA,YAAIA,QAAQ,IAAI,KAAKC,UAAjB,IAA+BD,QAAQ,KAAK,KAAKC,UAAL,CAAgBC,YAAhB,CAA6B,IAA7B,CAAhD,EAAoF;AAClF,eAAKD,UAAL,CAAgBE,SAAhB,CAA0BC,MAA1B,CAAiC,IAAjC;AACA,eAAKH,UAAL,GAAkB,IAAlB;AACA,eAAKI,QAAL;AACD;AACF;;AAVS;AALF,GANM;;AAwBlBC,EAAAA,OAAO,GAAG;AACR,S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D,KAAmB;AAC1C,WAAKnB,UAAL,CAAgB0D,YAAhB,CAA6BtE,IAA7B,EAAmC+B,KAAnC;AACD,KAFD;AAIA,SAAKZ,OAAL,CAAaoD,YAAb,CAA0B,KAAK3D,UAA/B,EAA2C,KAAKO,OAAL,CAAaqD,UAAxD;AACA,UAAM,KAAK5D,UAAL,CAAgBE,SAAhB,CAA0BE,QAA1B,EAAN;AACD;;AAtHiB,CAApB;AAyHA,SACEjB,WADF","sourcesContent":["import type { SearchQuery } from '../libs/interfaces';\nimport { searchInResources } from '../libs/filter';\n\nconst FilterMixin = {\n name: 'filter-mixin',\n use: [],\n initialState: {\n searchCount: null,\n },\n attributes: {\n searchFields: {\n type: String,\n default: null\n },\n filteredBy: {\n type: String,\n default: null,\n callback(newValue: string) {\n // if we change search form, re-populate\n if (newValue && this.searchForm && newValue !== this.searchForm.getAttribute('id')) {\n this.searchForm.component.detach(this);\n this.searchForm = null;\n this.populate();\n }\n }\n }\n },\n created() {\n this.searchCount = new Map();\n this.element.addEventListener('populate', () => {\n if (!window.document.contains(this.element)) return;\n this.searchForm?.component.updateAutoRanges();\n })\n },\n attached(): void {\n this.listPostProcessors.push(this.filterCallback.bind(this));\n },\n get filters(): SearchQuery {\n return this.searchForm?.component.value ?? {};\n },\n set filters(filters) {\n this.searchForm.component.value = filters;\n this.filterList();\n },\n async filterCallback(resources: object[], listPostProcessors: Function[], div: HTMLElement, context: string): Promise<void> {\n if (this.filteredBy || this.searchFields) {\n if (!this.searchCount.has(context)) this.searchCount.set(context, 1);\n if (!this.searchForm) await this.createFilter(context);\n const filteredResources = await searchInResources(\n resources,\n this.filters,\n this.fields,\n this.searchForm\n );\n resources =\tresources.filter((_v, index) => filteredResources[index]);\n }\n\n const nextProcessor = listPostProcessors.shift();\n if(nextProcessor) await nextProcessor(resources, listPostProcessors, div, context + (this.searchCount.get(context) || ''));\n },\n async filterList(context: string): Promise<void> {\n this.searchCount.set(context, this.searchCount.get(context) + 1);\n if (!this.resource) return;\n this.empty();\n await this.populate();\n },\n async getValuesOfField(field: string) {\n const arrayOfDataObjects = this.resource['ldp:contains'];\n const arrayOfDataIds: string[] = [];\n for (const obj of arrayOfDataObjects) {\n // for each element, if it's an object, catch all elements in 'ldp:contains' key\n const nextArrayOfObjects = await obj[field];\n if (!nextArrayOfObjects) continue;\n\n if (typeof nextArrayOfObjects !== \"object\") {\n console.warn(`The format value of ${field} is not suitable with auto-range-[field] attribute`);\n continue;\n }\n\n const nextArrayOfIds = nextArrayOfObjects['ldp:contains'];\n for (const obj of nextArrayOfIds) {\n // catch each element id\n arrayOfDataIds.push(obj['@id']);\n }\n if (nextArrayOfObjects['@type'] !== 'ldp:Container') {\n // if no element in 'ldp:contains', catch object id\n arrayOfDataIds.push(nextArrayOfObjects['@id']);\n }\n }\n return arrayOfDataIds;\n },\n async createFilter(context: string): Promise<void> {\n const filteredBy = this.filteredBy;\n if (filteredBy != null) {\n this.searchForm = document.getElementById(filteredBy)\n if (!this.searchForm) throw `#${filteredBy} is not in DOM`;\n } else {\n this.searchForm = document.createElement(`solid-form-search`);\n }\n this.searchForm.component.attach(this);\n this.searchForm.addEventListener('formChange', () => {\n this.filterList(context);\n });\n this.searchForm.toggleAttribute('naked', true);\n\n if (filteredBy) return;\n\n //pass attributes to search form\n const searchAttributes = Array.from((this.element as Element).attributes)\n .filter(attr => attr['name'].startsWith('search-'))\n .map(attr => ({\n name: attr['name'].replace('search-', ''),\n value: attr['value'],\n }));\n\n searchAttributes.forEach(({name, value}) => {\n this.searchForm.setAttribute(name, value);\n });\n\n this.element.insertBefore(this.searchForm, this.element.firstChild);\n await this.searchForm.component.populate();\n },\n}\n\nexport {\n FilterMixin\n}"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@startinblox/core",
|
|
3
|
-
"version": "0.17.21-beta.
|
|
3
|
+
"version": "0.17.21-beta.8",
|
|
4
4
|
"description": "This is a series of web component respecting both the web components standards and the Linked Data Platform convention.",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"module": "./dist/index.js",
|