Newer
Older

Commander-lol
committed
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
//Prevent pollution of global namespace with closure
(function(){
var tinyDOM = function(selector){
return new tinyDOMFunction(selector);
}
var tinyDOMFunction = function(selector) {
var elements = document.querySelectorAll(selector);
this.length = elements.length;
for(var i = 0; i < elements.length; i++){
var e = elements.item(i);
if(typeof(e.td_prop) === 'undefined'){
e.td_prop = {
isHidden: false
};
}
this[i] = elements.item(i);
}
return this;
}
tinyDOM.fn = tinyDOMFunction.prototype = {
each: function(fn) {
var l = this.length;
while(l--){
fn(l, this[l], this);
}
return this;
},
hide: function(){
this.each(function(i, e){
if(!e.td_prop.isHidden){
e.style.td_previousDisplay = e.style.display;
e.style.display = 'none';
e.td_prop.isHidden = true;
}
});
return this;
},
show: function(){
this.each(function(i, e){
if(e.td_prop.isHidden === true){
if(typeof(e.style.td_previousDisplay) !== 'undefined'){
e.style.display = e.style.td_previousDisplay;
} else {
e.style.display = 'block';
}
e.td_prop.isHidden = false;
}
});
return this;
},
on: function(ev, fn){
this.each(function(i, e){
e.addEventListener(ev, fn);
});
return this;
},
first: function(){
if(typeof(this[0]) !== 'undefined'){
return this[0];
} else {
return null;
}
},
data: function(key, value){
var e = this[0];
if(typeof(value) !== 'undefined'){
e.setAttribute('data-'+key, value);
return this;
} else {
return e.getAttribute('data-'+key);
}
}
}
tinyDOM.exists = function(obj){
return obj !== null && typeof(obj) !== 'undefined';
}
tinyDOM.merge = function(json1, json2){
if(!this.exists(json1) || !this.exists(json2)){
return null;
} else {
for(var prop in json2) {
if(json2.hasOwnProperty(prop)){
json1[prop] = json2[prop];
}
}
return json1;
}
}
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
tinyDOM.ajax = function(options){
var req = new XMLHttpRequest();
var _this = this;
var params = {
method: 'GET',
url: '',
async: true,
user: null,
password: null,
responseType: 'text',
data: null,
headers: [],
success: null,
error: null
}
this.merge(params, options);
req.responseType = params.responseType;
req.onreadystatechange = function(){
if(req.readyState > 1){
if(req.status === 200){
if (req.readyState === 4 && _this.exists(params.success)){
params.success(req.response, req);
}
} else {
req.abort();
if(_this.exists(params.error)){
params.error({status: req.status, message: req.statusText}, req);
}
}
}
}
req.open(
params.method,
params.url,
params.async,
params.user,
params.password
);
for(var i = 0; i < params.headers.length; i++){
req.setRequestHeader(params.headers[i].header, params.headers[i].value);
}
req.send(params.data);
};

Commander-lol
committed
}
if(!window.mu){
window.mu = tinyDOM;
}
})();