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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
|
/* XXX: this message is ultra-lame */
var _assert = function (expr, value) {
if (!expr) {
var message = "_assert(" + value + ")";
console.log(message);
throw message;
}
}
// Compatibility {{{
if (typeof Array.prototype.push != "function")
Array.prototype.push = function (value) {
this[this.length] = value;
};
// }}}
var $ = function (arg, doc) {
if (this.magic_ != $.prototype.magic_)
return new $(arg);
if (arg == null)
arg = [];
var type = $.type(arg);
if (type == "function")
$.ready(arg);
else if (type == "string") {
if (typeof doc == 'undefined')
doc = document;
if (arg.charAt(0) == '#') {
/* XXX: this is somewhat incorrect-a-porter */
var element = doc.getElementById(arg.substring(1));
return $(element == null ? [] : [element]);
} else if (arg.charAt(0) == '.')
return $(doc.getElementsByClassName(arg.substring(1)));
else
return $([doc]).descendants(arg);
} else if (typeof arg.length != 'undefined') {
_assert(typeof doc == 'undefined', "non-query with document to $");
this.set(arg);
return this;
} else _assert(false, "unknown argument to $: " + typeof arg);
};
$.xml = function (value) {
return value
.replace(/&/, "&")
.replace(/</, "<")
.replace(/>/, ">")
.replace(/"/, """)
.replace(/'/, "'")
;
}
$.type = function (value) {
var type = typeof value;
if ((type == "function" || type == "object") && value.toString != null) {
var string = value.toString();
if (string.substring(0, 8) == "[object ")
return string.substring(8, string.length - 1);
}
return type;
};
(function () {
var ready_ = null;
$.ready = function (_function) {
if (ready_ == null) {
ready_ = [];
document.addEventListener("DOMContentLoaded", function () {
for (var i = 0; i != ready_.length; ++i)
ready_[i]();
}, false);
}
ready_.push(_function);
};
})();
/* XXX: verify arg3 overflow */
$.each = function (values, _function, arg0, arg1, arg2) {
for (var i = 0, e = values.length; i != e; ++i)
_function(values[i], arg0, arg1, arg2);
};
/* XXX: verify arg3 overflow */
$.map = function (values, _function, arg0, arg1, arg2) {
var mapped = [];
for (var i = 0, e = values.length; i != e; ++i)
mapped.push(_function(values[i], arg0, arg1, arg2));
return mapped;
};
$.array = function (values) {
if (values.constructor == Array)
return values;
_assert(typeof values.length != 'undefined', "$.array on underlying non-array");
var array = [];
for (var i = 0; i != values.length; ++i)
array.push(values[i]);
return array;
};
$.document = function (node) {
for (;;) {
var parent = node.parentNode;
if (parent == null)
return node;
node = parent;
}
};
$.reclass = function (_class) {
return new RegExp('(\\s|^)' + _class + '(\\s|$)');
};
$.prototype = {
magic_: 2041085062,
add: function (nodes) {
Array.prototype.push.apply(this, $.array(nodes));
},
at: function (name, value) {
if (typeof value == 'undefined')
return $.map(this, function (node) {
return node.getAttribute(name);
});
else if (value == null)
$.each(this, function (node) {
node.removeAttribute();
});
else
$.each(this, function (node) {
node.setAttribute(name, value);
});
},
set: function (nodes) {
this.length = 0;
this.add(nodes);
},
/* XXX: verify arg3 overflow */
each: function (_function, arg0, arg1, arg2) {
$.each(this, function (node) {
_function($([node]), arg0, arg1, arg2);
});
},
css: function (name, value) {
$.each(this, function (node) {
node.style[name] = value;
});
},
addClass: function (_class) {
$.each(this, function (node) {
if (!$([node]).hasClass(_class)[0])
node.className += " " + _class;
});
},
blur: function () {
$.each(this, function (node) {
node.blur();
});
},
focus: function () {
$.each(this, function (node) {
node.focus();
});
},
removeClass: function (_class) {
$.each(this, function (node) {
node.className = node.className.replace($.reclass(_class), ' ');
});
},
hasClass: function (_class) {
return $.map(this, function (node) {
return node.className.match($.reclass(_class));
});
},
append: function (children) {
if ($.type(children) == "string")
$.each(this, function (node) {
var doc = $.document(node);
// XXX: implement wrapper system
var div = doc.createElement("div");
div.innerHTML = children;
while (div.childNodes.length != 0) {
var child = div.childNodes[0];
node.appendChild(child);
}
});
else
$.each(this, function (node) {
$.each(children, function (child) {
node.appendChild(child);
});
});
},
xpath: function (expression) {
var value = $([]);
$.each(this, function (node) {
var doc = $.document(node);
var results = doc.evaluate(expression, node, null, XPathResult.ANY_TYPE, null);
var result;
while (result = results.iterateNext())
value.add([result]);
});
return value;
},
clone: function (deep) {
return $($.map(this, function (node) {
return node.cloneNode(deep);
}));
},
descendants: function (expression) {
var descendants = $([]);
$.each(this, function (node) {
var nodes = node.getElementsByTagName(expression);
descendants.add(nodes);
});
return descendants;
},
remove: function () {
$.each(this, function (node) {
node.parentNode.removeChild(node);
});
}
};
$.scroll = function (x, y) {
window.scrollTo(x, y);
};
// XXX: document.all?
$.all = function (doc) {
if (typeof doc == 'undefined')
doc = document;
return $(doc.getElementsByTagName("*"));
};
$.inject = function (a, b) {
if ($.type(a) == "string") {
$.prototype[a] = function (value) {
if (typeof value == 'undefined')
return $.map(this, function (node) {
return b.get(node);
});
else
$.each(this, function (node, value) {
b.set(node, value);
}, value);
};
} else for (var name in a)
$.inject(name, a[name]);
};
$.inject({
_default: {
get: function (node) {
return node.style.defaultValue;
},
set: function (node, value) {
node.style.defaultValue = value;
}
},
html: {
get: function (node) {
return node.innerHTML;
},
set: function (node, value) {
node.innerHTML = value;
}
},
href: {
get: function (node) {
return node.href;
},
set: function (node, value) {
node.href = value;
}
},
name: {
get: function (node) {
return node.name;
},
set: function (node, value) {
node.name = value;
}
},
parent: {
get: function (node) {
return node.parentNode;
}
},
src: {
get: function (node) {
return node.src;
},
set: function (node, value) {
node.src = value;
}
},
type: {
get: function (node) {
return node.localName;
}
},
value: {
get: function (node) {
return node.value;
},
set: function (node, value) {
// XXX: do I really need this?
if (true || node.localName != "select")
node.value = value;
else {
var options = node.options;
for (var i = 0, e = options.length; i != e; ++i)
if (options[i].value == value) {
if (node.selectedIndex != i)
node.selectedIndex = i;
break;
}
}
}
},
width: {
get: function (node) {
return node.offsetWidth;
}
}
});
// Query String Parsing {{{
$.query = function () {
var args = {};
var search = location.search;
if (search != null) {
_assert(search[0] == "?", "query string without ?");
var values = search.substring(1).split("&");
for (var index in values) {
var value = values[index]
var equal = value.indexOf("=");
var name;
if (equal == -1) {
name = value;
value = null;
} else {
name = value.substring(0, equal);
value = value.substring(equal + 1);
value = decodeURIComponent(value);
}
name = decodeURIComponent(name);
if (typeof args[name] == "undefined")
args[name] = [];
if (value != null)
args[name].push(value);
}
}
return args;
};
// }}}
// Event Registration {{{
// XXX: unable to remove registration
$.prototype.event = function (event, _function) {
$.each(this, function (node) {
// XXX: smooth over this pointer ugliness
if (node.addEventListener)
node.addEventListener(event, _function, false);
else if (node.attachEvent)
node.attachEvent("on" + event, _function);
else
// XXX: multiple registration SNAFU
node["on" + event] = _function;
});
};
$.each([
"click", "load", "submit"
], function (event) {
$.prototype[event] = function (_function) {
if (typeof _function == 'undefined')
_assert(false, "undefined function to $.[event]");
else
this.event(event, _function);
};
});
// }}}
// Timed Animation {{{
$.interpolate = function (duration, event) {
var start = new Date();
var next = function () {
setTimeout(update, 0);
};
var update = function () {
var time = new Date() - start;
if (time >= duration)
event(1);
else {
event(time / duration);
next();
}
};
next();
};
// }}}
// AJAX Requests {{{
// XXX: abstract and implement other cases
$.xhr = function (url, method, headers, data, events) {
var xhr = new XMLHttpRequest();
xhr.open(method, url, true);
for (var name in headers)
xhr.setRequestHeader(name.replace(/_/, "-"), headers[name]);
if (events == null)
events = {};
xhr.onreadystatechange = function () {
if (xhr.readyState == 4) {
var status = xhr.status;
var text = xhr.responseText;
if (events.response != null)
events.response(status, text);
if (status == 200) {
if (events.success != null)
events.success(text);
} else {
if (events.failure != null)
events.failure(status);
}
}
};
xhr.send(data);
};
$.call = function (url, post, onsuccess) {
var events = {};
if (onsuccess != null)
events.complete = function (text) {
onsuccess(eval(text));
};
if (post == null)
$.xhr(url, "POST", null, null, events);
else
$.xhr(url, "POST", {
Content_Type: "application/json"
}, $.json(post), events);
};
// }}}
// WWW Form URL Encoder {{{
$.form = function (parameters) {
var data = "";
var ampersand = false;
for (var name in parameters) {
if (!ampersand)
ampersand = true;
else
data += "&";
var value = parameters[name];
data += escape(name);
data += "=";
data += escape(value);
}
return data;
};
// }}}
// JSON Serializer {{{
$.json = function (value) {
if (value == null)
return "null";
var type = $.type(value);
if (type == "number")
return value;
else if (type == "string")
return "\"" + value
.replace(/\\/, "\\\\")
.replace(/\t/, "\\t")
.replace(/\r/, "\\r")
.replace(/\n/, "\\n")
.replace(/"/, "\\\"")
+ "\"";
else if (value.constructor == Array) {
var json = "[";
var comma = false;
for (var i = 0; i != value.length; ++i) {
if (!comma)
comma = true;
else
json += ",";
json += $.json(value[i]);
}
return json + "]";
} else if (
value.constructor == Object &&
value.toString() == "[object Object]"
) {
var json = "{";
var comma = false;
for (var name in value) {
if (!comma)
comma = true;
else
json += ",";
json += name + ":" + $.json(value[name]);
}
return json + "}";
} else {
return value;
}
};
// }}}
|