-
Notifications
You must be signed in to change notification settings - Fork 0
/
jdolar.js
175 lines (168 loc) · 4.89 KB
/
jdolar.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
Array.prototype.each = function(fun){
if (typeof fun != "function") throw new TypeError();
for (var i = 0; i < this.length; i++) {
if (i in this) fun.call(arguments[1], this[i], i)
}
return this;
};
Array.prototype.map = function(fun){
var res = Array();
this.each(function(el){res.push(fun(el));});
return res;
}
//code taken form http://www.anieto2k.com/2009/01/14/selectores-css-y-frameworks-actuales/
var selector = {
cache: {},
cleanCache: function(){
this.cache = {};
},
uniq: function(arr){
var result = new Array();
for(var i=0;i<arr.length;i++){
for(var j=i+1;j<arr.length;j++){
if(arr[i] == arr[j]){
j = ++i;
}
}
result.push(arr[i]);
}
return result;
},
trim: function(string){
return string.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
},
get: function(selector, context, cache) {
var cache = cache || false;
// Navegadore modernos
if (document.querySelectorAll) return document.querySelectorAll(selector);
// Navegadores MUY antiguos
if (!document.getElementsByTagName) { return new Array();}
if (cache && this.cache[selector]) return this.cache[selector];
var tokens = selector.split(' ');
var context = new Array(context || document);
for (var i = 0; i < tokens.length; i++) {
var found = new Array, h = 0;
token = this.trim(tokens[i]);
// ID
if (token.indexOf('#') > -1) {
var el = this.getID(token);
context = el?new Array(el):new Array;
continue;
}
// Class
if (token.indexOf('.') > -1) {
while(h<context.length)
found = found.concat(this.getClass(token, context[h++]));
context = this.uniq(found);
continue;
}
// Tag
while(h<context.length)
found = found.concat(this.toArray(this.getTag(token, context[h++])));
context = this.uniq(found);
}
if (cache) this.cache[selector] = context;
return context;
},
getID: function(token){
var bits = token.split('#');
return document.getElementById(bits[1]);
},
getClass: function(token, context) {
var context = context || document;
var bits = token.split('.');
var tagName = bits[0]?bits[0]:"*",className = bits[1], result = new Array;
if (context.getElementsByClassName) {
var found = this.toArray(context.getElementsByClassName(className));
if (tagName == "*") return found;
var regExp = new RegExp(tagName, "i");
for (var x in found)
if (regExp.test(found[x].tagName)) result.push(found[x]);
} else {
var t = context.getElementsByTagName(tagName);
var oRegExp = new RegExp("(^|\\s)" + className.replace(/\-/g, "\\-") + "(\\s|$)");
for (var y in t)
if (oRegExp.test(t[y].className)) result.push(t[y]);
}
return result;
},
getTag: function(token, context){
return (context || document).getElementsByTagName(token);
},
toArray: function(els){
return Array.prototype.slice.call(els);
}
}
var jdolar=(function(){
var methods = {
find: function(selExpr,context,cache){
return selector.get(selExpr,context,cache).map(this.extend);
},
extend: function(el, opt){
var opt = opt || methods;
if (typeof el.get != 'undefined') return el;
for (var name in opt) el[name] = opt[name];
return el;
},
css: function() {
if(arguments.length == 0){
return this.style;
}else if (arguments.length == 1){
var a = arguments[0];
if (typeof a == 'string')
return this.style[a];
else {
for (var p in a)
this.style[p] = a[p];
}
}else {
for(var i = 0, l = arguments.length;i<l;)
this.style[arguments[i++]] = arguments[i++]
}
return this;
},
parent: function(){
if (!this.parentNode) return this;
if (arguments.length == 0) {
return this.extend(this.parentNode);
} else {
if (this.parentNode.tagName.toUpperCase() == (arguments[0]).toUpperCase())
return this.extend(this.parentNode);
else
return this.extend(this.parentNode).parent(arguments[0]);
}
},
addClass: function(cl){
if (!this.getAttribute) return false;
if(this.hasClass(cl)) return this;
var classes = this.getAttribute('class');
if(classes==null) this.setAttribute('class',cl);
else this.setAttribute('class',classes+" "+cl);
return this;
},
removeClass: function(cl){
if (!this.getAttribute) return false;
var classes = this.getAttribute('class');
if(classes==null) return this;
var arrClasses=classes.split(" ");
for(var i=0;i<arrClasses.length;i++){
if(arrClasses[i]==cl){
arrClasses.splice(i,1);
break;
}
}
this.setAttribute('class',arrClasses.join(" "));
return this;
},
hasClass: function(cl){
if (!this.getAttribute) return false;
var classes=this.getAttribute('class');
if(classes==null) return false;
var arrClasses=classes.split(" ");
for(var i=0;i<arrClasses.length;i++)
if(arrClasses[i]==cl) return true;
return false;
}
}
return methods;
})();