-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjmicro.js
More file actions
101 lines (87 loc) · 3.44 KB
/
jmicro.js
File metadata and controls
101 lines (87 loc) · 3.44 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
/**
* Copyright (c) 2012 Robert Kunze [rk.klatcher at gmail dot com]
* Licensed under the MIT license
*/
jMicro = function () {
var value = "value", innerhtml = "innerHTML", readystatechange = "readystatechange", xmlhttprequest = XMLHttpRequest, readystate = 'readyState', responsetext = "responseText", TRUE = true, FALSE = false,
a = function (element) {
return a.extend(typeof element == "string" && document.getElementById(element) || element, a)
};
a.elize = function (a, b) {
var c = a;
return function () {
return c.apply(b, arguments)
}
};
a.extend = function (b, c) {
b._m = 1;
for (var d in c) typeof c[d] == "function" && (b[d] = a.elize(c[d], b));
return b
};
a.listen = function (eventName, callback) {
original = this["on" + eventName];
this["on" + eventName] = function (event) {
return (typeof original != "function" || original(event) !== FALSE) && (callback.apply(this, [event || window.event]) === FALSE ? FALSE : TRUE)
};
return this
};
a.css = function (style) {
this.style.cssText += ";" + style;
return this
};
a.down = function (selector) {
return a(this.querySelector(selector))
};
a.up = function () {
return a(this.parentNode)
};
a.remove = function () {
a(this).up().removeChild(this)
};
a.encode = function (data) {
var component = encodeURIComponent,
content, all;
if (typeof data == "string") return data;
for (all in data)
typeof data[all] != "function" && (data[all][value] && (content += "&" + data[all].name + "=" + component(data[all][value])) || (content += "&" + all + "=" + component(data[all][value] || data[all])));
return content
};
a.set = function (data) {
var element = this;
return element._m && (element[value] !== undefined && (element[value] = data) || (element[innerhtml] = data) || (element = data)) && element
};
a.get = function (url, callback) {
var element = this,
xmlrequest;
if (!url && !callback) return element[value] || element[innerhtml];
(xmlrequest = a(new xmlhttprequest).listen(readystatechange, function (a) {
xmlrequest[readystate] == 4 && (element.set(xmlrequest[responsetext]) || TRUE) && callback && callback(xmlrequest)
})).open("GET", url, TRUE);
xmlrequest.send(null)
};
a.post = function (url, data, callback) {
var element = this,
xmlrequest;
(xmlrequest = a(new xmlhttprequest).listen(readystatechange, function (a) {
xmlrequest[readystate] == 4 && (element.set(xmlrequest[responsetext]) || TRUE) && callback && callback(xmlrequest)
})).open("POST", url, TRUE);
xmlrequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlrequest.send(a.encode(data))
};
a.create = function(tagName, attributes) {
var element = document.createElement(tagName),all;
for(all in attributes)
element[all] = attributes[all];
element = a.extend(element, this);
return element;
};
a.add = function(content) {
var element = this;
if (typeof(content) != "string")
element.appendChild(content);
else
element[innerhtml] = element[innerhtml] + content;
return element;
};
return a
}();