-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathonly.js
181 lines (158 loc) · 4.99 KB
/
only.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
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
module.exports = (function(){
//Does the browser have the iterator feature?
var iterators = Symbol && Symbol.iterator;
function isIterable(obj){
return (iterators && obj[Symbol.iterator]) || obj instanceof Array
}
function parseError(msg){
throw new TypeError("only.js parse error: " + msg);
}
function warn(msg){
console.warn("onlyjs WARNING: " + msg);
}
//takes an HTML tag name, value, and attribute list and returns HTMLElement
function parseNameandValue(name, value, attrList, css) {
if (typeof name !== "string"){
parseError("expected string for HTML tag name, but given " + name + (typeof name));
}
var el = document.createElement(name);
if (isUnknownElement(el)){
warn('"' + name + '" is not a valid HTML tag');
}
if (value.constructor === Object){
el.appendChild(parseHtmlJson(value));
} else if (value instanceof HTMLElement) {
el.appendChild(value);
} else if(typeof value === "string") {
el.innerHTML = value;
} else if (isIterable(value)) {
var htmlList = parseHtmlList(value);
for (var i in htmlList){
var htmlObj = htmlList[i];
el.appendChild(htmlObj);
}
}
for (var i in attrList){
var attr = attrList[i];
el.setAttribute(attr.name, attr.val);
}
if (css){
setElementCss(el, css);
}
return el;
}
//takes HTML Json representation and returns HTMLElement
function parseHtmlJson(obj) {
if (obj instanceof Array){//TODO why is this here
parseError("Expected HTMLElement or object, but recieved " + obj);
}
var htmlObj;
if (obj instanceof Object) {
var attrList = [];
var css = null;
var elements = Object.keys(obj);
name = elements[0];
for (var i = 1; i < elements.length; ++i) {
var el = elements[i];
if (el === "css"){
css = obj[el];
} else {
var attr = {name: el, val: obj[el]}
attrList.push(attr);
}
}
var value = obj[name];
htmlObj = parseNameandValue(name, value, attrList, css);
} else if (typeof obj === "string"){
htmlObj = document.createTextNode(obj);
} else {
parseError(String(obj) + " is not a valid HTML object: must be either a JSON HTML representation or an HTMLElement");
}
return htmlObj;
}
function isUnknownElement(el){
return el instanceof HTMLUnknownElement;
}
//returns a list of HTML elements inside base that have dataIdName=dataId attribute
function getByDataId(base, dataId){
var element = base.querySelector('[' + dataIdName + '="' + dataId + '"]');
return element;
}
//parses a list of HTMLElement and HTML json representations and
//returns list of HTMLElement as result
function parseHtmlList(list) {
if (!(isIterable(list))){
parseError("expected Array, but was given: " + String(list));
}
return list.map(function(element){
if(element instanceof Node){
return element;
} else {
return parseHtmlJson(element);
}
});
}
//takes HTMLElement and JSON representation CSS and sets the CSS on the HTMLElement
function setElementCss(el, css){
for (var property in css){
if (!(property in el.style)){
warn('"' + property + '" is not a valid css property');
}
el.style[property] = css[property];
}
}
function setHtml(html){
var html = parseHtmlJson({body: html});
document.body = html;
}
//CSS
//takes name of CSS class or id and a JSON representation of the CSS
//and returns CSS as a string
function genCss(name, css){
cssText = [];
cssText.push(camelToDash(name));
cssText.push('{');
for (var el in css){
cssText.push(el+":");
cssText.push(css[el]+";");
}
cssText.push('}');
return cssText.join('');
}
function makeCss(name, css){
var sheet = document.createElement('style');
sheet.innerHTML = genCss(name, css);
document.body.appendChild(sheet);
}
//takes camelcase name and returns lower case with dashes name
function camelToDash(name){
newNameList = [];
for (var i = 0; i < name.length; ++i){
var letter = name[i];
if (letter === letter.toLowerCase()){
newNameList.push(letter);
} else {
newNameList.push('-');
newNameList.push(letter.toLowerCase());
}
}
return newNameList.join("");
}
//Utility functions
function merge(){
var ret = {};
for (var i = 0; i < arguments.length; ++i){
var arg = arguments[i];
for (var el in arg){
ret[el] = arg[el];
}
}
return ret;
}
return {
html: parseHtmlJson,
setHtml: setHtml,
makeCss: makeCss,
merge: merge
}
})();