-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate.js
More file actions
204 lines (188 loc) · 4.74 KB
/
Copy pathcreate.js
File metadata and controls
204 lines (188 loc) · 4.74 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
define([], function () {
'use strict';
var namespaces = {
svg: 'http://www.w3.org/2000/svg',
xlink: 'http://www.w3.org/1999/xlink',
ev: 'http://www.w3.org/2001/xml-events',
xml: 'http://www.w3.org/XML/1998/namespace'
},
parseName = /^(?:(\w+)\:)?([^\s\.#]*)/,
parseSelector = /[\.#][^\s\.#]+/g;
function allKeys (object) {
var keys = [];
for (var key in object) { keys.push(key); }
return keys;
}
function assignStyle (node, styles, options) {
var keys = allKeys(styles);
for (var i = 0; i < keys.length; ++i) {
var key = keys[i];
if (key === '$') {
create.setStyle(node, styles.$, options);
} else {
node.style[key] = styles[key];
}
}
return node;
}
function setStyle (node, styles, options) {
var keys = allKeys(styles);
for (var i = 0; i < keys.length; ++i) {
var key = keys[i];
if (key === '$') {
create.assignStyle(node, styles.$, options);
} else {
node.style.setProperty(key, styles[key]);
}
}
return node;
}
function setData (node, dataset, options) {
var keys = allKeys(dataset);
for (var i = 0; i < keys.length; ++i) {
var key = keys[i];
node.dataset[key] = dataset[key];
}
return node;
}
function addListener (node, name, value) {
var type = name;
if (type.substring(0, 2) == 'on') {
type = type.substring(2);
}
node.addEventListener(type, value, false);
}
function setProperties (node, props, options) {
var keys = allKeys(props);
for (var i = 0; i < keys.length; ++i) {
var key = keys[i];
switch (key) {
case '$':
create.setAttrs(node, props.$, options);
break;
case 'style':
if (typeof props.style == 'string') {
node.style.cssText = props.style;
} else {
create.setStyle(node, props.style, options);
}
break;
case 'dataset':
create.setData(node, props.dataset, options);
break;
default:
if (typeof props[key] == 'function') {
addListener(node, key, props[key]);
} else {
node[key] = props[key];
}
break;
}
}
return node;
}
function setAttributes (node, attributes, options) {
var keys = allKeys(attributes);
for (var i = 0; i < keys.length; ++i) {
var key = keys[i];
switch (key) {
case '$':
if (options && typeof options.setComponentProperties == 'function' && node.tagName.indexOf('-') > 0) {
options.setComponentProperties(node, attributes.$, options);
} else {
create.setProps(node, attributes.$, options);
}
break;
case 'style':
if (typeof attributes.style == 'string') {
node.style.cssText = attributes.style;
} else {
create.setStyle(node, attributes.style);
}
break;
case 'class':
case 'className':
node.className = attributes[key];
break;
default:
var name = parseName.exec(key);
if (name && name[1]) {
node.setAttributeNS(create.namespaces[name[1]], name[2], attributes[key]);
} else {
if (typeof attributes[key] == 'function') {
addListener(node, key, attributes[key]);
} else {
node.setAttribute(key, attributes[key]);
}
}
break;
}
}
return node;
}
function createText (text, parent, options) {
var doc = options && options.document || document;
if (parent) {
if (parent.nodeType === 9) {
doc = parent;
parent = null;
} else {
doc = parent.ownerDocument || doc;
}
}
var node = doc.createTextNode(text);
if (parent && parent.nodeType === 1) {
parent.appendChild(node);
}
return node;
}
function create (tag, attributes, parent, ns, options) {
var doc = options && options.document || document;
if (parent) {
if (parent.nodeType === 9) {
doc = parent;
parent = null;
} else {
doc = parent.ownerDocument || doc;
}
}
// create an element
var name = parseName.exec(tag), node;
ns = name[1] || ns;
if (ns) {
node = doc.createElementNS(create.namespaces[ns], name[2] || 'div');
} else {
node = doc.createElement(name[2] || 'div');
}
if (name[0].length < tag.length) {
// add selector's classes and ids
tag.substring(name[0].length).replace(parseSelector, function (match) {
switch (match.charAt(0)) {
case '.':
node.classList.add(match.substring(1));
break;
case '#':
node.id = match.substring(1);
break;
}
return '';
});
}
if (attributes) {
create.setAttrs(node, attributes, options);
}
if (parent && parent.nodeType === 1) {
parent.appendChild(node);
}
return node;
}
create.text = createText;
create.allKeys = allKeys;
create.setAttrs = setAttributes;
create.setProps = setProperties;
create.setData = setData;
create.setStyle = setStyle;
create.assignStyle = assignStyle;
create.namespaces = namespaces;
return create;
});