-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
169 lines (117 loc) · 3.16 KB
/
utils.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
!function () {
var Utils;
Utils = function() {
var __init__, onload, onclick, click, noOp, AJAX, selectElement, hide, show;
hide = function(target) {
target.setAttribute('hidden', true);
return target;
};
show = function(target) {
target.removeAttribute('hidden', false);
return target;
};
selectElement = function(selector){
// selector is a dom element id
var collection;
collection = [].slice.call(document.querySelectorAll(selector));
if(!collection.length){
throw new Error("Cannot find dom element: " + selector);
return false;
} else {
collection.html = function(htmlString){
var _self;
_self = this;
_self.forEach(function(node, index){
node.innerHTML = htmlString;
});
return _self;
};
collection.css = function(css){
var _self, applyStyles;
_self = this;
applyStyles = function (node, styles){
for (attr in styles){
node.style[attr] = styles[attr];
}
};
_self.forEach(function(node, index){
applyStyles(node, css);
});
return _self;
};
}
return collection;
};
AJAX = function (httpMethod, url, callback) {
var xhr;
xhr = new XMLHttpRequest();
xhr.open(httpMethod, url, true);
xhr.send();
xhr.onreadystatechange = function(){
if(xhr.readyState === 4){
callback(null, xhr);
}
};
xhr.onerror = function() {
callback(xhr);
};
}
noOp = function() {
return false;
};
__listeners__ = {
clicklisteners : {},
onloadlisteners : {}
};
onload = function(callback){
document.onreadystatechange = function (){
if (document.readyState === "complete") {
}
};
};
/**
* Takes an {} full of methods to be executed onclick
**/
onclick = function(methods) {
// sets global click listener
document.addEventListener('click', function (E){
E.target.id ? click(E.target) : noOp();
});
for (target in methods) {
__listeners__.clicklisteners[target] = methods[target];
}
return __listeners__.clicklisteners;
};
click = function(target){
// selector = DOM Elem {}
var _self, id, methods;
id = target.id;
methods = __listeners__.clicklisteners;
methods.hasOwnProperty(id) ? methods[target.id](target) : noOp();
}; // end
// on page load
document.onreadystatechange = function () {
if (document.readyState === "complete") {
__init__();
}
};
__init__ = function() {
AJAX('GET', '../home.html', function (err, xhr) {
if(err){
console.log(err);
return false;
} else {
selectElement('body')
.html(xhr.response);
}
});
};
return {
selectElement : selectElement,
AJAX : AJAX,
onload : onload,
onclick : onclick
};
};
U = window.Utils = Utils();
}();