98 lines
3.6 KiB
JavaScript
98 lines
3.6 KiB
JavaScript
"use strict";
|
|
|
|
/**
|
|
* @typedef {import("./AnP.ecma.js").AnP} AnP
|
|
*/
|
|
|
|
import {Common} from "../Utils/Common.ecma.js";
|
|
import {Check} from "../Utils/Checks.ecma.js";
|
|
|
|
export const Attributes = (function(){
|
|
|
|
/**
|
|
* @callback attributes_special_callback
|
|
* @param {?any} data
|
|
* @returns {[string, string]}
|
|
*/
|
|
|
|
const Attributes = function(anp){
|
|
|
|
const self = this,
|
|
standard = [
|
|
"div", "span", "p", "a", "img", "button", "input", "form", "label",
|
|
"ul", "ol", "li", "table", "thead", "tbody", "tr", "th", "td",
|
|
"header", "footer", "section", "article", "nav", "main", "i", "del",
|
|
"h1", "h2", "h3", "h4", "h5", "h6", "br", "hr", "iframe", "canvas",
|
|
"svg", "video", "audio", "source", "data", "details", "summary",
|
|
"dialog", "meter", "progress", "time", "abbr", "address", "b", "bdi",
|
|
"bdo", "cite", "code", "dfn", "em", "i", "kbd", "mark", "q", "rp",
|
|
"rt", "ruby", "s", "samp", "small", "strong", "sub", "sup", "u",
|
|
"var", "wbr", "pre", "blockquote", "dl", "dt", "dd", "figure",
|
|
"fieldset", "legend", "textarea", "select", "option", "optgroup",
|
|
"datalist", "keygen", "output"
|
|
];
|
|
|
|
const constructor = () => {};
|
|
|
|
/** @type {Object.<string, attributes_special_callback>} */
|
|
const special = {
|
|
permissions : data => ["data-permissions", Common.data_encode(data)],
|
|
toggle : data => ["data-toggle", data ? "true" : "false"]
|
|
};
|
|
|
|
this.set_standards = (inputs, callback = null) => {
|
|
if(Check.is_array(inputs)){
|
|
if(inputs.every(item => Check.is_string(item) && /^[a-z0-9\-]+$/.test(item))){
|
|
inputs.forEach(item => {
|
|
standard.includes(item) || standard.push(item);
|
|
});
|
|
Common.execute(callback);
|
|
return;
|
|
};
|
|
Common.execute_array(inputs, (item, next) => {
|
|
anp.files.load_json(item, results => {
|
|
self.set_standards(results, next);
|
|
});
|
|
}, callback);
|
|
}else if(Check.is_string(inputs))
|
|
self.set_standards([inputs], callback);
|
|
else
|
|
Common.execute_callback(callback);
|
|
};
|
|
|
|
this.set = (item, attributes, excluded = [], only = []) => {
|
|
Object.entries(attributes).forEach(([key, raw_value]) => {
|
|
if((
|
|
(excluded.length && excluded.includes(key)) ||
|
|
(only.length && !only.includes(key))
|
|
))
|
|
return;
|
|
|
|
let name, value;
|
|
|
|
if(special[key])
|
|
[name, value] = special[key](raw_value);
|
|
else{
|
|
[name, value] = [Common.to_kebab(key).toLowerCase(), raw_value];
|
|
if(name.slice(0, 2) == "on")
|
|
name = name.replace(/[^a-z]+/g, "");
|
|
else if(!["data-", "aria-"].includes(name.slice(0, 5)) && !standard.includes(name))
|
|
name = "data-" + name;
|
|
};
|
|
|
|
if(name.slice(0, 2) == "on" && Check.is_function(value))
|
|
item.addEventListener(name, event => {
|
|
value(item, event);
|
|
});
|
|
else if(value !== null)
|
|
item.setAttribute(name, value);
|
|
|
|
});
|
|
};
|
|
|
|
constructor();
|
|
|
|
};
|
|
|
|
return Attributes;
|
|
})(); |