-
Notifications
You must be signed in to change notification settings - Fork 7
/
myquery.html
262 lines (194 loc) · 5.23 KB
/
myquery.html
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
<html>
<head>
<title>myQuery experiment</title>
</head>
<body>
<style>
#container{
width:300px;
height:300px;
border:1px solid black;
}
#sidebar{
color:blue;
background:red;
}
.item{
color:blue;
}
</style>
<div id="container">
AOL
</div>
<div class="item">a</div>
<div class="item">b</div>
<div class="item">c</div>
<div id="sidebar" data-role="side">
Testing testing
</div>
<script type="text/javascript">
// myQuery
// Light-weight DOM library for learning purposes
// Copyright Addy Osmani, 2011.
// Released under an MIT license
var myQuery = function( selector ){
// As per jQuery, handle myQuery('')
// myQuery(null) or myQuery(undefined)
// by returning the instance if no
// valid selectors are provided
if(!selector){
return this;
}
// Handle selections (ID, Class, all others)
if(selector.slice(0,1) === '#'){
//ID selector: only grab the portion after # if using getElementById (faster than qSA)
this.selection = document.getElementById(selector.slice(1, selector.length))
}else{
if(selector.slice(0,1) === '.'){
//Class selector
this.selection = document.getElementsByClassName(selector.slice(1, selector.length));
}else{
//All others
this.selection = document.querySelectorAll(selector);
}
}
// Set the length of myQuery to the selection length
this.length = this.selection.length || 0;
//document ready
this.ready = function(callback){
//this.bind('load', callback);
if ( document.addEventListener ) {
//Supported by Opera/Webkit and some other modern browsers
//document.addEventListener( "DOMContentLoaded", callback, false );
window.addEventListener( "load", callback, false );
// If IE event model is used
} else if ( document.attachEvent ) {
// safe also for iframes
//document.attachEvent( "onreadystatechange", callback );
window.attachEvent( "onload", callback );
}
}
// Set attribute values
// e.g.: el.attr(prop, val);
this.attr = function(prop, val){
if(prop && prop!== undefined && this.length <1){
if(val === undefined){
//attribute getter
return this.access('attr', this.selection, prop);
}else{
//attribute setter
this.access('attr', this.selection, prop, val);
}
}
return this;
}
// Set CSS property values
// e.g.: el.css(prop, val);
this.css = function( prop, val ){
if(prop && prop!==undefined){
//user intent: property getter
if(val === undefined){
return this.access('css', this.selection, prop);
}else{
//check if setter arguments valid
//if(prop && prop!==undefined){
if(this.length && this.length >0){
//handle multiple elements
for(i=0; i<this.length; i++){
this.access('css',this.selection[i].style, prop, val);
}
}else{
//handle single elements without the need to iterate
this.access('css', this.selection.style, prop, val);
}
return this;
}
}
}
// A generic property setter and getter
this.access = function( mode, context, prop, val){
if(context && prop && val!==undefined && prop!==undefined){
context[prop] = val;
}else{
//console.log('getter', context, prop, this.selection, selector);
//return context[prop];
///
if(mode==='css'){
var y = "";
if (context.currentStyle){
y = context.currentStyle[prop];
}
else{
if (window.getComputedStyle){
y = document.defaultView.getComputedStyle(context,null).getPropertyValue(prop);
}
}
return y;
}else if(mode==='attr'){
return context[prop];
}
///
}
}
// Bind an eventType to a handler
// el.bind(eventName, handler)
this.bind = function(eventName, handler, context) {
//avoid duplication in .ready() by supporting document
//usage here.(todo)
var element = this.selection;
if (element.addEventListener) {
element.addEventListener(eventName, handler, false);
}
else if (element.attachEvent) {
element.attachEvent('on' + eventName, handler);
}
else {
element['on' + eventName] = handler;
}
/*
if (context.addEventListener) {
context.addEventListener(eventName, handler, false);
}
else if (context.attachEvent) {
context.attachEvent('on' + eventName, handler);
}
else {
context['on' + eventName] = handler;
}*/
return this;
};
this.log = function(){
console.log(this.selection);
};
if(this instanceof myQuery){
return this.myQuery;
}else{
return new myQuery(selector);
}
}
/////Usage///
//Ready..
myQuery('document').ready(function(){
console.log('something loaded on document ready');
//Setter tests
myQuery('#container')
.css('color','red')
.attr('alt', 'myContainer')
.attr('title','testing')
.bind('click', function(){
console.log('magic');
})
.css('background-color','green')
.bind('mouseover', function(){
console.log('hover');
})
.css('width','400px');
myQuery('.item').css('color','green');
//Getter tests
var q = (myQuery('#sidebar'));
console.log('value of the sidebar color is:' + q.css('color'));
console.log('value of the sidebar id is:' + q.attr('id'));
});
</script>
</body>
</html>