-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
169 lines (152 loc) · 3.75 KB
/
utils.js
File metadata and controls
169 lines (152 loc) · 3.75 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
function registerActionListener(target, component, callback)
{
target.addEventListener("keyup", function(ev)
{
onKeyUp(ev, component, callback);
}, false);
target.addEventListener("click", function(ev)
{
onClick(ev, component, callback);
}, false);
};
function onKeyUp(ev, component, callback)
{
const root = ev.target.getRootNode();
const key = ev.key;
const activeElem = root.activeElement;
let actions = null;
switch (key)
{
case " ":
case "Enter":
actions = activeElem.dataset.keyAction;
break;
case "Delete":
case "Backspace":
actions = activeElem.dataset.keyDelete;
break;
case "ArrowUp":
actions = activeElem.dataset.keyUp;
break;
case "ArrowDown":
actions = activeElem.dataset.keyDown;
break;
case "ArrowRight":
actions = activeElem.dataset.keyRight;
break;
case "ArrowLeft":
actions = activeElem.dataset.keyLeft;
break;
case "Escape":
actions = activeElem.dataset.keyQuite;
break;
}
if (!actions)
return;
// TODO: Fix duplication
ev.preventDefault;
actions.split(",").forEach(function(action)
{
callback.call(component, action, activeElem);
});
}
function onClick(ev, component, callback)
{
let element = ev.target;
let actions = null;
while (true)
{
if (element == this)
break;
if (element.hasAttribute("data-action"))
{
actions = element.getAttribute("data-action");
break;
}
element = element.parentElement;
}
if (!actions)
return;
ev.preventDefault;
actions.split(",").forEach(function(action)
{
callback.call(component, action, element);
});
}
function getMsg(id)
{
// Mock data
const data =
{
"btn-txt": "Label text",
"btn-desc": "Longer, more informative description of the toggle button",
"cookieDialog_domain": "Domain",
"cookieDialog_name": "Name",
"cookieDialog_value": "Value",
"cookieDialog_path": "Path",
"cookieDialog_expDate": "Exp. date",
"cookieDialog_expTime": "Exp. time",
"cookieDialog_hostOnyl": "Host only",
"cookieDialog_httpOnyl": "HTTP only",
"cookieDialog_secure": "Secure",
"cookieDialog_session": "Session",
"cookieDialog_storeId Store": "ID",
"cookieDialog_add": "Add",
"cookieDialog_update": "Update",
"cookieDialog_delete": "Delete",
"cookieDialog_cancel": "Cancel",
"cookieDialog_deleteAll_msg": "You are about to delete all cookies, are you sure ?"
};
return data[id] || id;
}
function initI18n(rootElement)
{
rootElement.querySelectorAll("[data-i18n]").forEach(function(node)
{
node.textContent = getMsg(node.dataset.i18n);
});
}
function deepCopy(object)
{
return JSON.parse(JSON.stringify(object));
}
/**
* Currently Constructable Stylesheets are only supported in Chrome 73+, for the
* browsers that do not support them importing the style into shadowDom should
* be done by appending a child element.
*/
class ConstructableCSS
{
/**
* Initialize CSSStyleSheet element
* @param {String} css Style Sheet string
*/
constructor(css)
{
this.sheet = null;
try
{
this.sheet = new CSSStyleSheet();
this.sheet.replaceSync(css);
}
catch(e)
{
const style = document.createElement("style");
style.textContent = css;
this.sheet = style;
}
}
/**
* Import styles into Shadow DOM
* @param shadowRoot Shadow DOM root
* @param sheet StyleSheet to be used for loading into shadowDom
*/
load(shadowRoot)
{
if (this.sheet instanceof CSSStyleSheet)
shadowRoot.adoptedStyleSheets = [this.sheet];
else
shadowRoot.appendChild(this.sheet.cloneNode(true));
}
}
export {registerActionListener, deepCopy, getMsg, initI18n, ConstructableCSS};