auto-refresh data when updated
Should be evident when the name of the filter has been changed
This commit is contained in:
parent
eedd53aba4
commit
3e2723d58b
1 changed files with 120 additions and 110 deletions
|
@ -15,57 +15,57 @@ class Search {
|
||||||
};
|
};
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Include all relevant DOM elements into this object.
|
Include all relevant DOM elements into this object.
|
||||||
*/
|
*/
|
||||||
#get() {
|
#get() {
|
||||||
document.querySelectorAll(`[data-result]`).forEach((ELEMENT) => {
|
document.querySelectorAll(`[data-result]`).forEach((ELEMENT) => {
|
||||||
let SOURCE = ELEMENT.getAttribute(`data-result`);
|
let SOURCE = ELEMENT.getAttribute(`data-result`);
|
||||||
|
|
||||||
if (SOURCE != `state`) {
|
if (SOURCE != `state`) {
|
||||||
this[SOURCE] = (!this[SOURCE])
|
this[SOURCE] = (!this[SOURCE])
|
||||||
? {}
|
? {}
|
||||||
: this[SOURCE];
|
: this[SOURCE];
|
||||||
|
|
||||||
const elements = () => {
|
const elements = () => {
|
||||||
this[SOURCE][`elements`] = (this[SOURCE][`elements`]) ? this[SOURCE][`elements`] : {};
|
this[SOURCE][`elements`] = (this[SOURCE][`elements`]) ? this[SOURCE][`elements`] : {};
|
||||||
|
|
||||||
// First, add the search box.
|
// First, add the search box.
|
||||||
this[SOURCE][`elements`][`search box`] = (this[SOURCE][`elements`][`search box`])
|
this[SOURCE][`elements`][`search box`] = (this[SOURCE][`elements`][`search box`])
|
||||||
? this[SOURCE][`elements`][`search box`].push(ELEMENT)
|
? this[SOURCE][`elements`][`search box`].push(ELEMENT)
|
||||||
: [ELEMENT];
|
: [ELEMENT];
|
||||||
|
|
||||||
let SOURCES = {
|
let SOURCES = {
|
||||||
"results list": `[data-results-list="${SOURCE}"]`,
|
"results list": `[data-results-list="${SOURCE}"]`,
|
||||||
"container": `[data-result-linked="${SOURCE}"]`,
|
"container": `[data-result-linked="${SOURCE}"]`,
|
||||||
"enable": `[data-result-enable]`
|
"enable": `[data-result-enable]`
|
||||||
};
|
};
|
||||||
|
|
||||||
const linked = () => {
|
const linked = () => {
|
||||||
let LINKED_SOURCES = {
|
let LINKED_SOURCES = {
|
||||||
"content": "data-result-content",
|
"content": "data-result-content",
|
||||||
"fields": "data-result-store"
|
"fields": "data-result-store"
|
||||||
};
|
};
|
||||||
|
|
||||||
(Object.keys(LINKED_SOURCES)).forEach((COMPONENT) => {
|
(Object.keys(LINKED_SOURCES)).forEach((COMPONENT) => {
|
||||||
(document.querySelector(SOURCES[`container`].concat(` [`, LINKED_SOURCES[COMPONENT], `]`)))
|
(document.querySelector(SOURCES[`container`].concat(` [`, LINKED_SOURCES[COMPONENT], `]`)))
|
||||||
? (document.querySelectorAll(SOURCES[`container`].concat(` [`, LINKED_SOURCES[COMPONENT], `]`))).forEach((ELEMENT) => {
|
? (document.querySelectorAll(SOURCES[`container`].concat(` [`, LINKED_SOURCES[COMPONENT], `]`))).forEach((ELEMENT) => {
|
||||||
this[SOURCE][`elements`][COMPONENT] = (this[SOURCE][`elements`][COMPONENT] && !(Array.isArray(this[SOURCE][`elements`][COMPONENT])) && (typeof this[SOURCE][`elements`][COMPONENT]).includes(`obj`)) ? this[SOURCE][`elements`][COMPONENT] : {};
|
this[SOURCE][`elements`][COMPONENT] = (this[SOURCE][`elements`][COMPONENT] && !(Array.isArray(this[SOURCE][`elements`][COMPONENT])) && (typeof this[SOURCE][`elements`][COMPONENT]).includes(`obj`)) ? this[SOURCE][`elements`][COMPONENT] : {};
|
||||||
|
|
||||||
// Get the name of the element.
|
// Get the name of the element.
|
||||||
let NAME = ELEMENT.getAttribute(LINKED_SOURCES[COMPONENT]);
|
let NAME = ELEMENT.getAttribute(LINKED_SOURCES[COMPONENT]);
|
||||||
(ELEMENT.getAttribute(`data-store-location`))
|
(ELEMENT.getAttribute(`data-store-location`))
|
||||||
? ELEMENT[`data store location`] = ELEMENT.getAttribute(`data-store-location`)
|
? ELEMENT[`data store location`] = ELEMENT.getAttribute(`data-store-location`)
|
||||||
: false;
|
: false;
|
||||||
|
|
||||||
// Set the element.
|
// Set the element.
|
||||||
this[SOURCE][`elements`][COMPONENT][NAME] = (this[SOURCE][`elements`][COMPONENT][NAME] ? this[SOURCE][`elements`][COMPONENT][NAME].length : false)
|
this[SOURCE][`elements`][COMPONENT][NAME] = (this[SOURCE][`elements`][COMPONENT][NAME] ? this[SOURCE][`elements`][COMPONENT][NAME].length : false)
|
||||||
? (this[SOURCE][`elements`][COMPONENT][NAME].includes(ELEMENT)
|
? (this[SOURCE][`elements`][COMPONENT][NAME].includes(ELEMENT)
|
||||||
? false
|
? false
|
||||||
: [...this[SOURCE][`elements`][COMPONENT][NAME], ELEMENT])
|
: [...this[SOURCE][`elements`][COMPONENT][NAME], ELEMENT])
|
||||||
: [ELEMENT];
|
: [ELEMENT];
|
||||||
|
|
||||||
|
|
||||||
// Remove the attribute.
|
// Remove the attribute.
|
||||||
[LINKED_SOURCES[COMPONENT], `data-store-location`].forEach((ATTRIBUTE) => {
|
[LINKED_SOURCES[COMPONENT], `data-store-location`].forEach((ATTRIBUTE) => {
|
||||||
ELEMENT.removeAttribute(ATTRIBUTE);
|
ELEMENT.removeAttribute(ATTRIBUTE);
|
||||||
})
|
})
|
||||||
|
@ -73,7 +73,7 @@ class Search {
|
||||||
: false;
|
: false;
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
if (SOURCES ? Object.keys(SOURCES) : false) {
|
if (SOURCES ? Object.keys(SOURCES) : false) {
|
||||||
(Object.keys(SOURCES)).forEach((COMPONENT) => {
|
(Object.keys(SOURCES)).forEach((COMPONENT) => {
|
||||||
(document.querySelector(SOURCES[COMPONENT]))
|
(document.querySelector(SOURCES[COMPONENT]))
|
||||||
|
@ -83,23 +83,23 @@ class Search {
|
||||||
linked();
|
linked();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get relevant data.
|
// Get relevant data.
|
||||||
const attributes = () => {
|
const attributes = () => {
|
||||||
// Accumulate all search criteria where possible.
|
// Accumulate all search criteria where possible.
|
||||||
(ELEMENT.hasAttribute(`data-results-filters`))
|
(ELEMENT.hasAttribute(`data-results-filters`))
|
||||||
? this[SOURCE][`additional criteria`] = (this[SOURCE][`additional criteria`]) ? [...this[SOURCE][`additional criteria`], ...ELEMENT.getAttribute(`data-results-filters`).split(`,`)] : ELEMENT.getAttribute(`data-results-filters`).split(`,`)
|
? this[SOURCE][`additional criteria`] = (this[SOURCE][`additional criteria`]) ? [...this[SOURCE][`additional criteria`], ...ELEMENT.getAttribute(`data-results-filters`).split(`,`)] : ELEMENT.getAttribute(`data-results-filters`).split(`,`)
|
||||||
: false;
|
: false;
|
||||||
(ELEMENT.hasAttribute(`data-show`))
|
(ELEMENT.hasAttribute(`data-show`))
|
||||||
? this[SOURCE][`preview`] = ELEMENT.getAttribute(`data-show`)
|
? this[SOURCE][`preview`] = ELEMENT.getAttribute(`data-show`)
|
||||||
: false;
|
: false;
|
||||||
|
|
||||||
// Remove attributes only used during construction, simultaneously protecting against edited HTML from the debugger.
|
// Remove attributes only used during construction, simultaneously protecting against edited HTML from the debugger.
|
||||||
[`data-result`, `data-results-filters`, `data-show`].forEach((ATTRIBUTE) => {
|
[`data-result`, `data-results-filters`, `data-show`].forEach((ATTRIBUTE) => {
|
||||||
ELEMENT.removeAttribute(ATTRIBUTE);
|
ELEMENT.removeAttribute(ATTRIBUTE);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
elements();
|
elements();
|
||||||
attributes();
|
attributes();
|
||||||
}
|
}
|
||||||
|
@ -108,7 +108,7 @@ class Search {
|
||||||
};
|
};
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Set the functions of the relevant elements.
|
Set the functions of the relevant elements.
|
||||||
*/
|
*/
|
||||||
#set() {
|
#set() {
|
||||||
(Object.keys(this)).forEach((SOURCE) => {
|
(Object.keys(this)).forEach((SOURCE) => {
|
||||||
|
@ -117,9 +117,9 @@ class Search {
|
||||||
ELEMENT.addEventListener(`change`, () => {this.run({"name": SOURCE, "element": ELEMENT}, null, {"auto sync": true});});
|
ELEMENT.addEventListener(`change`, () => {this.run({"name": SOURCE, "element": ELEMENT}, null, {"auto sync": true});});
|
||||||
});
|
});
|
||||||
|
|
||||||
// Set the state.
|
// Set the state.
|
||||||
this[SOURCE][`scripts`] = {"background": {}};
|
this[SOURCE][`scripts`] = {"background": {}};
|
||||||
|
|
||||||
// Find the data.
|
// Find the data.
|
||||||
this.run({"name": SOURCE}, `*`, {"auto sync": true});
|
this.run({"name": SOURCE}, `*`, {"auto sync": true});
|
||||||
this.pick(SOURCE, null);
|
this.pick(SOURCE, null);
|
||||||
|
@ -128,7 +128,7 @@ class Search {
|
||||||
};
|
};
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Run a search.
|
Run a search.
|
||||||
|
|
||||||
@param {object} source the source data
|
@param {object} source the source data
|
||||||
@param {object} data the data to find for
|
@param {object} data the data to find for
|
||||||
|
@ -148,13 +148,15 @@ class Search {
|
||||||
show().then(() => {
|
show().then(() => {
|
||||||
if (((typeof options).includes(`obj`) && options) ? options[`auto sync`] : false) {
|
if (((typeof options).includes(`obj`) && options) ? options[`auto sync`] : false) {
|
||||||
// Set the refresh function.
|
// Set the refresh function.
|
||||||
let item = this[source[`name`]][`selected`];
|
let EXISTING_DATA = {};
|
||||||
|
EXISTING_DATA[`item`] = this[source[`name`]][`selected`];
|
||||||
|
EXISTING_DATA[`criteria`] = this[source[`name`]][`criteria`];
|
||||||
|
|
||||||
this[source[`name`]][`scripts`][`refresh`] = () => {
|
this[source[`name`]][`scripts`][`refresh`] = () => {
|
||||||
wait((this[`state`][`read/write`] ? this[`state`][`read/write`] >= 0 : true)).then(
|
wait((this[`state`][`read/write`] ? this[`state`][`read/write`] >= 0 : true)).then(
|
||||||
() => {
|
() => {
|
||||||
if (this[source][`selected`] == item) {
|
if (this[source[`name`]][`selected`] == EXISTING_DATA[`item`] || EXISTING_DATA[`criteria`] == this[source[`name`]][`criteria`]) {
|
||||||
show()
|
show();
|
||||||
} else if (this[source[`name`]][`scripts`][`background`][`refresh`]) {
|
} else if (this[source[`name`]][`scripts`][`background`][`refresh`]) {
|
||||||
this[source[`name`]][`scripts`][`background`][`refresh`].cancel();
|
this[source[`name`]][`scripts`][`background`][`refresh`].cancel();
|
||||||
};
|
};
|
||||||
|
@ -162,7 +164,7 @@ class Search {
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
this[source[`name`]][`scripts`][`background`][`refresh`] = new background(() => {this[source[`name`]][`scripts`][`refresh`]});
|
this[source[`name`]][`scripts`][`background`][`refresh`] = new background(() => {this[source[`name`]][`scripts`][`refresh`]()});
|
||||||
};
|
};
|
||||||
}).catch((err) => {
|
}).catch((err) => {
|
||||||
logging.error(err);
|
logging.error(err);
|
||||||
|
@ -170,7 +172,7 @@ class Search {
|
||||||
};
|
};
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Find the data.
|
Find the data.
|
||||||
|
|
||||||
@param {object} source the source data
|
@param {object} source the source data
|
||||||
@param {string} data the data to find for
|
@param {string} data the data to find for
|
||||||
|
@ -181,15 +183,15 @@ class Search {
|
||||||
? source = {"name": source}
|
? source = {"name": source}
|
||||||
: false;
|
: false;
|
||||||
|
|
||||||
// Set the primary search criteria.
|
// Set the primary search criteria.
|
||||||
if (data && data != `*`) {
|
if (data && data != `*`) {
|
||||||
// Having data filled means an override.
|
// Having data filled means an override.
|
||||||
this[source[`name`]][`criteria`] = ((typeof data).includes(`str`)) ? data.trim() : data;
|
this[source[`name`]][`criteria`] = ((typeof data).includes(`str`)) ? data.trim() : data;
|
||||||
} else if ((source[`element`]) ? source[`element`].value.trim() : false) {
|
} else if ((source[`element`]) ? source[`element`].value.trim() : false) {
|
||||||
// There is an element to use.
|
// There is an element to use.
|
||||||
this[source[`name`]][`criteria`] = source[`element`].value.trim();
|
this[source[`name`]][`criteria`] = source[`element`].value.trim();
|
||||||
} else if (this[source[`name`]][`elements`][`search box`] ? this[source[`name`]][`elements`][`search box`].length : false) {
|
} else if (this[source[`name`]][`elements`][`search box`] ? this[source[`name`]][`elements`][`search box`].length : false) {
|
||||||
// No element defined, look for every box.
|
// No element defined, look for every box.
|
||||||
(this[source[`name`]][`elements`][`search box`]).forEach((ELEMENT) => {
|
(this[source[`name`]][`elements`][`search box`]).forEach((ELEMENT) => {
|
||||||
this[source[`name`]][`criteria`] = (ELEMENT.type.includes(`num`) || ELEMENT.type.includes(`range`))
|
this[source[`name`]][`criteria`] = (ELEMENT.type.includes(`num`) || ELEMENT.type.includes(`range`))
|
||||||
? ((parseFloat(ELEMENT.value.trim()) != parseInt(ELEMENT.value.trim()))
|
? ((parseFloat(ELEMENT.value.trim()) != parseInt(ELEMENT.value.trim()))
|
||||||
|
@ -203,19 +205,19 @@ class Search {
|
||||||
this[source[`name`]][`criteria`] = null;
|
this[source[`name`]][`criteria`] = null;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Find.
|
// Find.
|
||||||
this[source[`name`]][`results`] = await ((this[source[`name`]][`criteria`] != null)
|
this[source[`name`]][`results`] = await ((this[source[`name`]][`criteria`] != null)
|
||||||
? ((this[source[`name`]][`additional criteria`] ? this[source[`name`]][`additional criteria`].length : false)
|
? ((this[source[`name`]][`additional criteria`] ? this[source[`name`]][`additional criteria`].length : false)
|
||||||
? global.search(source[`name`], this[source[`name`]][`criteria`], this[source[`name`]][`additional criteria`])
|
? global.search(source[`name`], this[source[`name`]][`criteria`], this[source[`name`]][`additional criteria`])
|
||||||
: global.search(source[`name`], this[source[`name`]][`criteria`]))
|
: global.search(source[`name`], this[source[`name`]][`criteria`]))
|
||||||
: global.read(source[`name`]));
|
: global.read(source[`name`]));
|
||||||
|
|
||||||
// Return the data.
|
// Return the data.
|
||||||
return (this[source[`name`]][`results`]);
|
return (this[source[`name`]][`results`]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Display the search results.
|
Display the search results.
|
||||||
|
|
||||||
@param {string} source the source data
|
@param {string} source the source data
|
||||||
@param {object} data the data to display
|
@param {object} data the data to display
|
||||||
|
@ -225,43 +227,43 @@ class Search {
|
||||||
if (source ? (Array.isArray(source) ? source.length : String(source)) : false) {
|
if (source ? (Array.isArray(source) ? source.length : String(source)) : false) {
|
||||||
source = (Array.isArray(source)) ? source.join(`,`) : String(source);
|
source = (Array.isArray(source)) ? source.join(`,`) : String(source);
|
||||||
|
|
||||||
// Get the data.
|
// Get the data.
|
||||||
data = (data && ((typeof data).includes(`obj`))) ? data : this[source][`results`];
|
data = (data && ((typeof data).includes(`obj`))) ? data : this[source][`results`];
|
||||||
|
|
||||||
const gui_output = () => {
|
const gui_output = () => {
|
||||||
// Prepare the elements we will need.
|
// Prepare the elements we will need.
|
||||||
if (this[source][`elements`][`results list`] ? this[source][`elements`][`results list`].length : false) {
|
if (this[source][`elements`][`results list`] ? this[source][`elements`][`results list`].length : false) {
|
||||||
const design = () => {
|
/*
|
||||||
// Prepare the access keys.
|
Add the selected state.
|
||||||
let ACCESS_KEYS = {"top": ["1", "2", "3", "4", "5", "6", "7", "8", "9"], "nav": ["<", ">"]};
|
*/
|
||||||
|
const select = (element) => {
|
||||||
/*
|
if (element) {
|
||||||
Add the selected state.
|
// Remove all active classes.
|
||||||
*/
|
(element).parentElement.querySelectorAll(`li:has(a)`).forEach((ELEMENT) => {
|
||||||
const select = (element) => {
|
ELEMENT.classList.remove(`active`);
|
||||||
if (element) {
|
});
|
||||||
// Remove all active classes.
|
|
||||||
(element.parentElement).parentElement.querySelectorAll(`li`).forEach((ELEMENT) => {
|
// Add the active.
|
||||||
ELEMENT.classList.remove(`active`);
|
element.classList.add(`active`);
|
||||||
});
|
|
||||||
|
return (element);
|
||||||
// Add the active.
|
|
||||||
element.parentElement.classList.add(`active`);
|
|
||||||
|
|
||||||
return (element);
|
|
||||||
};
|
|
||||||
};
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
const design = () => {
|
||||||
|
// Prepare the access keys.
|
||||||
|
let ACCESS_KEYS = {"top": ["1", "2", "3", "4", "5", "6", "7", "8", "9"], "nav": ["<", ">"]};
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Add the access keys (shortcut).
|
Add the access keys (shortcut).
|
||||||
|
|
||||||
@param {string} name the name of the element
|
@param {string} name the name of the element
|
||||||
@param {object} ELEMENT the element to add the access key to
|
@param {object} ELEMENT the element to add the access key to
|
||||||
@param {object} state the current state of the element
|
@param {object} state the current state of the element
|
||||||
*/
|
*/
|
||||||
const shortcut = (name, element, state) => {
|
const shortcut = (name, element, state) => {
|
||||||
let RESULT_INDEX = (Object.keys(data)).indexOf(name);
|
let RESULT_INDEX = (Object.keys(data)).indexOf(name);
|
||||||
|
|
||||||
if (RESULT_INDEX >= 0) {
|
if (RESULT_INDEX >= 0) {
|
||||||
if (state.includes(`config`)) {
|
if (state.includes(`config`)) {
|
||||||
((RESULT_INDEX < ACCESS_KEYS[`top`].length) && (RESULT_INDEX >= 0))
|
((RESULT_INDEX < ACCESS_KEYS[`top`].length) && (RESULT_INDEX >= 0))
|
||||||
|
@ -272,54 +274,57 @@ class Search {
|
||||||
} else if (state.includes(`execute`)) {
|
} else if (state.includes(`execute`)) {
|
||||||
let ELEMENT = {"selected": element};
|
let ELEMENT = {"selected": element};
|
||||||
ELEMENT[`neighbors`] = (ELEMENT[`selected`].parentElement.parentElement).querySelectorAll(`a`);
|
ELEMENT[`neighbors`] = (ELEMENT[`selected`].parentElement.parentElement).querySelectorAll(`a`);
|
||||||
|
|
||||||
// Remove elements with accesskeys in nav.
|
// Remove elements with accesskeys in nav.
|
||||||
(ELEMENT[`neighbors`]).forEach((OTHER) => {
|
(ELEMENT[`neighbors`]).forEach((OTHER) => {
|
||||||
(OTHER.getAttribute(`accesskey`) ? (ACCESS_KEYS[`nav`].includes(OTHER.getAttribute(`accesskey`))) : false)
|
(OTHER.getAttribute(`accesskey`) ? (ACCESS_KEYS[`nav`].includes(OTHER.getAttribute(`accesskey`))) : false)
|
||||||
? OTHER.removeAttribute(`accesskey`)
|
? OTHER.removeAttribute(`accesskey`)
|
||||||
: false;
|
: false;
|
||||||
})
|
})
|
||||||
|
|
||||||
if ((RESULT_INDEX + 1 >= ACCESS_KEYS[`top`].length) && (RESULT_INDEX + 1 < ELEMENT[`neighbors`].length)) {
|
if ((RESULT_INDEX + 1 >= ACCESS_KEYS[`top`].length) && (RESULT_INDEX + 1 < ELEMENT[`neighbors`].length)) {
|
||||||
ELEMENT[`neighbors`][RESULT_INDEX + 1].setAttribute(`accesskey`, ACCESS_KEYS[`nav`][1])
|
ELEMENT[`neighbors`][RESULT_INDEX + 1].setAttribute(`accesskey`, ACCESS_KEYS[`nav`][1])
|
||||||
}
|
}
|
||||||
|
|
||||||
(RESULT_INDEX > ACCESS_KEYS[`top`].length)
|
(RESULT_INDEX > ACCESS_KEYS[`top`].length)
|
||||||
? (ELEMENT[`neighbors`])[RESULT_INDEX - 1].setAttribute(`accesskey`, ACCESS_KEYS[`nav`][0])
|
? (ELEMENT[`neighbors`])[RESULT_INDEX - 1].setAttribute(`accesskey`, ACCESS_KEYS[`nav`][0])
|
||||||
: false;
|
: false;
|
||||||
|
|
||||||
(RESULT_INDEX >= ACCESS_KEYS[`top`].length)
|
(RESULT_INDEX >= ACCESS_KEYS[`top`].length)
|
||||||
? ELEMENT[`selected`].setAttribute(`accesskey`, `0`)
|
? ELEMENT[`selected`].setAttribute(`accesskey`, `0`)
|
||||||
: false;
|
: false;
|
||||||
|
|
||||||
return (ELEMENT);
|
return (ELEMENT);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let ELEMENTS = [];
|
let ELEMENTS = [];
|
||||||
|
|
||||||
(data ? Object.keys(data).length : false)
|
(data ? Object.keys(data).length : false)
|
||||||
? (Object.keys(data)).forEach((RESULT) => {
|
? (Object.keys(data)).forEach((RESULT) => {
|
||||||
let ELEMENTS_RESULT = {}
|
let ELEMENTS_RESULT = {}
|
||||||
ELEMENTS_RESULT[`container`] = document.createElement(`li`);
|
ELEMENTS_RESULT[`container`] = document.createElement(`li`);
|
||||||
ELEMENTS_RESULT[`title`] = document.createElement(`a`);
|
ELEMENTS_RESULT[`title`] = document.createElement(`a`);
|
||||||
|
|
||||||
// Add the classes.
|
// Add the classes.
|
||||||
ELEMENTS_RESULT[`title`].classList.add(`waves-effect`);
|
ELEMENTS_RESULT[`title`].classList.add(`waves-effect`);
|
||||||
ELEMENTS_RESULT[`title`].textContent = String((title && data[RESULT][title]) ? data[RESULT][title] : RESULT);
|
ELEMENTS_RESULT[`title`].textContent = String((title && data[RESULT][title]) ? data[RESULT][title] : RESULT);
|
||||||
|
|
||||||
// Add the action.
|
// Add the action.
|
||||||
ELEMENTS_RESULT[`title`].addEventListener(`click`, () => {
|
ELEMENTS_RESULT[`title`].addEventListener(`click`, () => {
|
||||||
// Set the visual state.
|
// Set the visual state.
|
||||||
select(ELEMENTS_RESULT[`title`]);
|
select(ELEMENTS_RESULT[`container`]);
|
||||||
shortcut(RESULT, ELEMENTS_RESULT[`title`], `execute`);
|
shortcut(RESULT, ELEMENTS_RESULT[`title`], `execute`);
|
||||||
|
|
||||||
// Pick the data.
|
// Pick the data.
|
||||||
this.pick(source, RESULT, data[RESULT]);
|
this.pick(source, RESULT, data[RESULT]);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Add the shortcut.
|
// Add the real linked data name temporarily.
|
||||||
|
ELEMENTS_RESULT[`container`][`linked`] = RESULT;
|
||||||
|
|
||||||
|
// Add the shortcut.
|
||||||
ELEMENTS_RESULT[`title`] = shortcut(RESULT, ELEMENTS_RESULT[`title`], `config`);
|
ELEMENTS_RESULT[`title`] = shortcut(RESULT, ELEMENTS_RESULT[`title`], `config`);
|
||||||
|
|
||||||
// Add the elements to the container.
|
// Add the elements to the container.
|
||||||
|
@ -327,10 +332,10 @@ class Search {
|
||||||
ELEMENTS.push(ELEMENTS_RESULT[`container`]);
|
ELEMENTS.push(ELEMENTS_RESULT[`container`]);
|
||||||
})
|
})
|
||||||
: false;
|
: false;
|
||||||
|
|
||||||
return (ELEMENTS);
|
return (ELEMENTS);
|
||||||
}
|
}
|
||||||
|
|
||||||
let TEMPLATE = design();
|
let TEMPLATE = design();
|
||||||
(this[source][`elements`][`results list`]).forEach((ELEMENT_TARGET) => {
|
(this[source][`elements`][`results list`]).forEach((ELEMENT_TARGET) => {
|
||||||
// Clear the target element.
|
// Clear the target element.
|
||||||
|
@ -338,6 +343,11 @@ class Search {
|
||||||
(TEMPLATE.length)
|
(TEMPLATE.length)
|
||||||
? TEMPLATE.forEach((ELEMENT) => {
|
? TEMPLATE.forEach((ELEMENT) => {
|
||||||
ELEMENT_TARGET.appendChild(ELEMENT);
|
ELEMENT_TARGET.appendChild(ELEMENT);
|
||||||
|
|
||||||
|
// Preselect the item.
|
||||||
|
if (ELEMENT[`linked`] == nested.dictionary.get(this, [source, `selected`])) {
|
||||||
|
select(ELEMENT);
|
||||||
|
};
|
||||||
})
|
})
|
||||||
: this.pick(source, null);
|
: this.pick(source, null);
|
||||||
})
|
})
|
||||||
|
@ -345,7 +355,7 @@ class Search {
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Display the search results in the log.
|
Display the search results in the log.
|
||||||
*/
|
*/
|
||||||
function log (data, title) {
|
function log (data, title) {
|
||||||
if (Object.keys(data).length) {
|
if (Object.keys(data).length) {
|
||||||
|
@ -373,7 +383,7 @@ class Search {
|
||||||
@param {string} details the details of the selected item
|
@param {string} details the details of the selected item
|
||||||
*/
|
*/
|
||||||
pick(source, item, details) {
|
pick(source, item, details) {
|
||||||
// Fill in the details if it's missing when the item and source isn't.
|
// Fill in the details if it's missing when the item and source isn't.
|
||||||
if (!details && (source && item)) {
|
if (!details && (source && item)) {
|
||||||
(Object.hasOwn(this[source][`results`], item))
|
(Object.hasOwn(this[source][`results`], item))
|
||||||
? details = this[source][`results`][item]
|
? details = this[source][`results`][item]
|
||||||
|
@ -382,7 +392,7 @@ class Search {
|
||||||
|
|
||||||
const set = () => {
|
const set = () => {
|
||||||
this[source][`selected`] = item;
|
this[source][`selected`] = item;
|
||||||
|
|
||||||
// Set the background state.
|
// Set the background state.
|
||||||
nested.dictionary.get(this, [source, `scripts`, `background`, `selected`])
|
nested.dictionary.get(this, [source, `scripts`, `background`, `selected`])
|
||||||
? this[source][`scripts`][`background`][`selected`].cancel()
|
? this[source][`scripts`][`background`][`selected`].cancel()
|
||||||
|
@ -391,8 +401,8 @@ class Search {
|
||||||
this[source][`scripts`][`reader`] = wait((this[`state`][`read/write`] ? this[`state`][`read/write`] >= 0 : true)).then(
|
this[source][`scripts`][`reader`] = wait((this[`state`][`read/write`] ? this[`state`][`read/write`] >= 0 : true)).then(
|
||||||
() => {(this[source][`selected`] == item) ? gui_display() : false;}
|
() => {(this[source][`selected`] == item) ? gui_display() : false;}
|
||||||
);
|
);
|
||||||
|
|
||||||
// Reset the background.
|
// Reset the background.
|
||||||
this[source][`scripts`][`background`][`selected`] = new background(() => {this[source][`scripts`][`reader`]});
|
this[source][`scripts`][`background`][`selected`] = new background(() => {this[source][`scripts`][`reader`]});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -433,10 +443,10 @@ class Search {
|
||||||
case `radio`:
|
case `radio`:
|
||||||
ELEMENT.checked = false;
|
ELEMENT.checked = false;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
ELEMENT.value = ``;
|
ELEMENT.value = ``;
|
||||||
};
|
};
|
||||||
|
|
||||||
if ((ELEMENT.nodeName.toLowerCase()).includes(`input`) || (ELEMENT.nodeName.toLowerCase()).includes(`textarea`)) {
|
if ((ELEMENT.nodeName.toLowerCase()).includes(`input`) || (ELEMENT.nodeName.toLowerCase()).includes(`textarea`)) {
|
||||||
// Check if the element has an event listener and remove it.
|
// Check if the element has an event listener and remove it.
|
||||||
(ELEMENT.func)
|
(ELEMENT.func)
|
||||||
|
@ -464,46 +474,46 @@ class Search {
|
||||||
: ((typeof item).includes(`str`)
|
: ((typeof item).includes(`str`)
|
||||||
? item.trim()
|
? item.trim()
|
||||||
: item);
|
: item);
|
||||||
|
|
||||||
this[source][`elements`][ELEMENTS][SOURCE].forEach((ELEMENT) => {
|
this[source][`elements`][ELEMENTS][SOURCE].forEach((ELEMENT) => {
|
||||||
if ((ELEMENT.nodeName.toLowerCase()).includes(`input`) || (ELEMENT.nodeName.toLowerCase()).includes(`textarea`) || (ELEMENT.nodeName.toLowerCase()).includes(`progress`)) {
|
if ((ELEMENT.nodeName.toLowerCase()).includes(`input`) || (ELEMENT.nodeName.toLowerCase()).includes(`textarea`) || (ELEMENT.nodeName.toLowerCase()).includes(`progress`)) {
|
||||||
|
|
||||||
switch (ELEMENT.type) {
|
switch (ELEMENT.type) {
|
||||||
case `checkbox`:
|
case `checkbox`:
|
||||||
case `radio`:
|
case `radio`:
|
||||||
ELEMENT.checked = (DATA[`value`]);
|
ELEMENT.checked = (DATA[`value`]);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
ELEMENT.value = DATA[`value`];
|
ELEMENT.value = DATA[`value`];
|
||||||
};
|
};
|
||||||
|
|
||||||
if ((DATA[`source`] != `*`) && (ELEMENT.nodeName.toLowerCase()).includes(`input`) || (ELEMENT.nodeName.toLowerCase()).includes(`textarea`)) {
|
if ((DATA[`source`] != `*`) && (ELEMENT.nodeName.toLowerCase()).includes(`input`) || (ELEMENT.nodeName.toLowerCase()).includes(`textarea`)) {
|
||||||
// Remove the existing function.
|
// Remove the existing function.
|
||||||
(ELEMENT.func)
|
(ELEMENT.func)
|
||||||
? [`change`, `blur`].forEach((EVENT) => {
|
? [`change`, `blur`].forEach((EVENT) => {
|
||||||
ELEMENT.removeEventListener(EVENT, ELEMENT.func)
|
ELEMENT.removeEventListener(EVENT, ELEMENT.func)
|
||||||
})
|
})
|
||||||
: false;
|
: false;
|
||||||
|
|
||||||
// Add the new function.
|
// Add the new function.
|
||||||
ELEMENT.func = () => {};
|
ELEMENT.func = () => {};
|
||||||
switch (ELEMENT.type) {
|
switch (ELEMENT.type) {
|
||||||
case `checkbox`:
|
case `checkbox`:
|
||||||
case `radio`:
|
case `radio`:
|
||||||
ELEMENT.func = () => {
|
ELEMENT.func = () => {
|
||||||
this[`state`][`read/write`] = -1;
|
this[`state`][`read/write`] = -1;
|
||||||
this[`state`][`last result`] = global.write(DATA[`target`], ELEMENT.checked, (ELEMENT[`data store location`] ? ELEMENT[`data store location`] : -1));
|
this[`state`][`last result`] = global.write(DATA[`target`], ELEMENT.checked, (ELEMENT[`data store location`] ? ELEMENT[`data store location`] : -1));
|
||||||
|
|
||||||
this[`state`][`read/write`] = 0;
|
this[`state`][`read/write`] = 0;
|
||||||
return(this[`state`][`last result`]);
|
return(this[`state`][`last result`]);
|
||||||
};
|
};
|
||||||
|
|
||||||
ELEMENT.checked = (DATA[`value`]);
|
ELEMENT.checked = (DATA[`value`]);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
if ((typeof DATA[`value`]).includes(`obj`) && !Array.isArray(DATA[`value`])) {
|
if ((typeof DATA[`value`]).includes(`obj`) && !Array.isArray(DATA[`value`])) {
|
||||||
ELEMENT.value = JSON.stringify(DATA[`value`]);
|
ELEMENT.value = JSON.stringify(DATA[`value`]);
|
||||||
|
|
||||||
ELEMENT.func = () => {
|
ELEMENT.func = () => {
|
||||||
this[`state`][`read/write`] = -1;
|
this[`state`][`read/write`] = -1;
|
||||||
this[`state`][`last result`] = false;
|
this[`state`][`last result`] = false;
|
||||||
|
@ -520,7 +530,7 @@ class Search {
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
ELEMENT.value = DATA[`value`];
|
ELEMENT.value = DATA[`value`];
|
||||||
|
|
||||||
ELEMENT.func = () => {
|
ELEMENT.func = () => {
|
||||||
this[`state`][`read/write`] = -1;
|
this[`state`][`read/write`] = -1;
|
||||||
|
|
||||||
|
@ -530,20 +540,20 @@ class Search {
|
||||||
: parseInt(ELEMENT.value.trim())
|
: parseInt(ELEMENT.value.trim())
|
||||||
)
|
)
|
||||||
: ELEMENT.value.trim());
|
: ELEMENT.value.trim());
|
||||||
|
|
||||||
this[`state`][`last result`] = global.write(DATA[`target`], ELEMENT.val, (ELEMENT[`data store location`] ? ELEMENT[`data store location`] : -1));
|
this[`state`][`last result`] = global.write(DATA[`target`], ELEMENT.val, (ELEMENT[`data store location`] ? ELEMENT[`data store location`] : -1));
|
||||||
this[`state`][`read/write`] = 0;
|
this[`state`][`read/write`] = 0;
|
||||||
|
|
||||||
delete ELEMENT.val;
|
delete ELEMENT.val;
|
||||||
return (this[`state`][`last result`]);
|
return (this[`state`][`last result`]);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
(ELEMENT.nodeName.toLowerCase().includes(`textarea`))
|
(ELEMENT.nodeName.toLowerCase().includes(`textarea`))
|
||||||
? ELEMENT.addEventListener(`blur`, ELEMENT.func)
|
? ELEMENT.addEventListener(`blur`, ELEMENT.func)
|
||||||
: false;
|
: false;
|
||||||
|
|
||||||
ELEMENT.addEventListener(`change`, ELEMENT.func);
|
ELEMENT.addEventListener(`change`, ELEMENT.func);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -551,7 +561,7 @@ class Search {
|
||||||
};
|
};
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
: false;
|
: false;
|
||||||
|
@ -559,7 +569,7 @@ class Search {
|
||||||
}
|
}
|
||||||
|
|
||||||
enable();
|
enable();
|
||||||
fill();
|
fill();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -576,4 +586,4 @@ class Search {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export { Search };
|
export { Search };
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue