-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
107 lines (90 loc) · 2.46 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
let template = document.createElement("template")
function defined(obj){
return (typeof(obj) != "undefined")
}
function uid(){
return Date.now()+"_"+Math.floor(Math.random() * 10000)
}
function suid(){
let date = (Date.now()).toString();
const sub = date.substring(date.length-6,date.length-1);
return sub+"_"+Math.floor(Math.random() * 10000)
}
function send(event_name,data){
var event = new CustomEvent(event_name, {detail:data});
window.dispatchEvent(event);
}
function temp(html_text){
const fragment = document.createRange().createContextualFragment(html_text);
template.appendChild(fragment);//this also returns fragment, not the newly created node
return template.childNodes[template.childNodes.length-1];
}
function html(parent,html_text){
parent.insertAdjacentHTML("beforeend",html_text);
return parent.childNodes[parent.childNodes.length-1];
}
function htmls(parent,html_text){
parent.insertAdjacentHTML("beforeend",html_text);
return parent.childNodes;
}
function html_tag(parent,tagName,html_text){
console.warn("html_tag() deprecated, replace with html()")
parent.insertAdjacentHTML("beforeend",html_text);
let elements = parent.getElementsByTagName(tagName);
let res_svg = elements[elements.length-1];
return res_svg;
}
function css(sheet,text){
sheet.insertRule(text);
}
function br(parent){
parent.appendChild(document.createElement("br"))
}
function hr(parent){
parent.appendChild(document.createElement("hr"))
}
function image(parent,url){
return html_tag(parent,"image",/*html*/`
<image x="0" y="0" xlink:href=${url}></image>
`)
}
//mini jQuery like events wrapper
class Events{
constructor(){
const events_list = ["click","change","input"]
events_list.forEach((evtName)=>{
this[evtName] = (element,func)=> {
element.addEventListener(evtName,func)
}
})
}
}
function save_json(object,fileName){
const json_str = JSON.stringify(object,null,'\t');
var blob = new Blob([json_str], {type: 'application/json'});
saveAs(blob, fileName);
}
function rand_col() {
const letters = '0123456789ABCDEF';
let color = '#';
for (let i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
export{
html,
br,hr,
defined,
Events,
save_json,
rand_col,
image,
uid,
suid,
send,
temp,
css,
html_tag,
htmls
}