]>
git.saurik.com Git - cydia.git/blob - Cydia.app/menes/menes.js
1 /* XXX: this message is ultra-lame */
2 var _assert = function (expr
, value
) {
4 var message
= "_assert(" + value
+ ")";
11 if (typeof Array
.prototype.push
!= "function")
12 Array
.prototype.push = function (value
) {
13 this[this.length
] = value
;
17 var $ = function (arg
, doc
) {
18 if (this.magic_
!= $.prototype.magic_
)
24 var type
= $.type(arg
);
26 if (type
== "function")
28 else if (type
== "string") {
29 if (typeof doc
== 'undefined')
31 if (arg
.charAt(0) == '#') {
32 /* XXX: this is somewhat incorrect-a-porter */
33 var element
= doc
.getElementById(arg
.substring(1));
34 return $(element
== null ? [] : [element
]);
35 } else if (arg
.charAt(0) == '.')
36 return $(doc
.getElementsByClassName(arg
.substring(1)));
38 return $([doc
]).descendants(arg
);
39 } else if (typeof arg
.length
!= 'undefined') {
40 _assert(typeof doc
== 'undefined', "non-query with document to $");
43 } else _assert(false, "unknown argument to $: " + typeof arg
);
46 $.xml = function (value
) {
48 .replace(/&/, "&")
51 .replace(/"/, ""
;")
52 .replace(/'/, "&apos
;")
56 $.type = function (value) {
57 var type = typeof value;
59 if ((type == "function" || type == "object
") && value.toString != null) {
60 var string = value.toString();
61 if (string.substring(0, 8) == "[object
")
62 return string.substring(8, string.length - 1);
71 $.ready = function (_function) {
75 document.addEventListener("DOMContentLoaded
", function () {
76 for (var i = 0; i != ready_.length; ++i)
81 ready_.push(_function);
85 /* XXX: verify arg3 overflow */
86 $.each = function (values, _function, arg0, arg1, arg2) {
87 for (var i = 0, e = values.length; i != e; ++i)
88 _function(values[i], arg0, arg1, arg2);
91 /* XXX: verify arg3 overflow */
92 $.map = function (values, _function, arg0, arg1, arg2) {
94 for (var i = 0, e = values.length; i != e; ++i)
95 mapped.push(_function(values[i], arg0, arg1, arg2));
99 $.array = function (values) {
100 if (values.constructor == Array)
102 _assert(typeof values.length != 'undefined', "$.array on underlying non
-array
");
104 for (var i = 0; i != values.length; ++i)
105 array.push(values[i]);
109 $.document = function (node) {
111 var parent = node.parentNode;
118 $.reclass = function (_class) {
119 return new RegExp('(\\s|^)' + _class + '(\\s|$)');
125 add: function (nodes) {
126 Array.prototype.push.apply(this, $.array(nodes));
129 at: function (name, value) {
130 if (typeof value == 'undefined')
131 return $.map(this, function (node) {
132 return node.getAttribute(name);
134 else if (value == null)
135 $.each(this, function (node) {
136 node.removeAttribute();
139 $.each(this, function (node) {
140 node.setAttribute(name, value);
144 set: function (nodes) {
149 /* XXX: verify arg3 overflow */
150 each: function (_function, arg0, arg1, arg2) {
151 $.each(this, function (node) {
152 _function($([node]), arg0, arg1, arg2);
156 css: function (name, value) {
157 $.each(this, function (node) {
158 node.style[name] = value;
162 addClass: function (_class) {
163 $.each(this, function (node) {
164 if (!$([node]).hasClass(_class)[0])
165 node.className += " " + _class;
170 $.each(this, function (node) {
176 $.each(this, function (node) {
181 removeClass: function (_class) {
182 $.each(this, function (node) {
183 node.className = node.className.replace($.reclass(_class), ' ');
187 hasClass: function (_class) {
188 return $.map(this, function (node) {
189 return node.className.match($.reclass(_class));
193 append: function (children) {
194 if ($.type(children) == "string
")
195 $.each(this, function (node) {
196 var doc = $.document(node);
198 // XXX: implement wrapper system
199 var div = doc.createElement("div
");
200 div.innerHTML = children;
202 while (div.childNodes.length != 0) {
203 var child = div.childNodes[0];
204 node.appendChild(child);
208 $.each(this, function (node) {
209 $.each(children, function (child) {
210 node.appendChild(child);
215 xpath: function (expression) {
218 $.each(this, function (node) {
219 var doc = $.document(node);
220 var results = doc.evaluate(expression, node, null, XPathResult.ANY_TYPE, null);
222 while (result = results.iterateNext())
229 clone: function (deep) {
230 return $($.map(this, function (node) {
231 return node.cloneNode(deep);
235 descendants: function (expression) {
236 var descendants = $([]);
238 $.each(this, function (node) {
239 var nodes = node.getElementsByTagName(expression);
240 descendants.add(nodes);
246 remove: function () {
247 $.each(this, function (node) {
248 node.parentNode.removeChild(node);
253 $.scroll = function (x, y) {
254 window.scrollTo(x, y);
257 // XXX: document.all?
258 $.all = function (doc) {
259 if (typeof doc == 'undefined')
261 return $(doc.getElementsByTagName("*"));
264 $.inject = function (a, b) {
265 if ($.type(a) == "string
") {
266 $.prototype[a] = function (value) {
267 if (typeof value == 'undefined')
268 return $.map(this, function (node) {
272 $.each(this, function (node, value) {
276 } else for (var name in a)
277 $.inject(name, a[name]);
282 get: function (node) {
283 return node.style.defaultValue;
285 set: function (node, value) {
286 node.style.defaultValue = value;
291 get: function (node) {
294 set: function (node, value) {
300 get: function (node) {
301 return node.innerHTML;
303 set: function (node, value) {
304 node.innerHTML = value;
309 get: function (node) {
312 set: function (node, value) {
318 get: function (node) {
321 set: function (node, value) {
327 get: function (node) {
328 return node.parentNode;
333 get: function (node) {
336 set: function (node, value) {
342 get: function (node) {
343 return node.localName;
348 get: function (node) {
351 set: function (node, value) {
352 // XXX: do I really need this?
353 if (true || node.localName != "select
")
356 var options = node.options;
357 for (var i = 0, e = options.length; i != e; ++i)
358 if (options[i].value == value) {
359 if (node.selectedIndex != i)
360 node.selectedIndex = i;
368 get: function (node) {
369 return node.offsetWidth;
374 // Query String Parsing {{{
375 $.query = function () {
378 var search = location.search;
379 if (search != null) {
380 _assert(search[0] == "?", "query string without
?");
382 var values = search.substring(1).split("&");
383 for (var index in values) {
384 var value = values[index]
385 var equal = value.indexOf("=");
392 name = value.substring(0, equal);
393 value = value.substring(equal + 1);
394 value = decodeURIComponent(value);
397 name = decodeURIComponent(name);
398 if (typeof args[name] == "undefined")
401 args[name].push(value);
408 // Event Registration {{{
409 // XXX: unable to remove registration
410 $.prototype.event = function (event, _function) {
411 $.each(this, function (node) {
412 // XXX: smooth over this pointer ugliness
413 if (node.addEventListener)
414 node.addEventListener(event, _function, false);
415 else if (node.attachEvent)
416 node.attachEvent("on
" + event, _function);
418 // XXX: multiple registration SNAFU
419 node["on
" + event] = _function;
424 "click
", "load
", "submit
"
425 ], function (event) {
426 $.prototype[event] = function (_function) {
427 if (typeof _function == 'undefined')
428 _assert(false, "undefined function to
$.[event
]");
430 this.event(event, _function);
434 // Timed Animation {{{
435 $.interpolate = function (duration, event) {
436 var start = new Date();
438 var next = function () {
439 setTimeout(update, 0);
442 var update = function () {
443 var time = new Date() - start;
445 if (time >= duration)
448 event(time / duration);
457 // XXX: abstract and implement other cases
458 $.xhr = function (url, method, headers, data, events) {
459 var xhr = new XMLHttpRequest();
460 xhr.open(method, url, true);
462 for (var name in headers)
463 xhr.setRequestHeader(name.replace(/_/, "-"), headers[name]);
468 xhr.onreadystatechange = function () {
469 if (xhr.readyState == 4) {
470 var status = xhr.status;
471 var text = xhr.responseText;
472 if (events.response != null)
473 events.response(status, text);
475 if (events.success != null)
476 events.success(text);
478 if (events.failure != null)
479 events.failure(status);
487 $.call = function (url, post, onsuccess) {
490 if (onsuccess != null)
491 events.complete = function (text) {
492 onsuccess(eval(text));
496 $.xhr(url, "POST
", null, null, events);
499 Content_Type: "application
/json
"
500 }, $.json(post), events);
503 // WWW Form URL Encoder {{{
504 $.form = function (parameters) {
507 var ampersand = false;
508 for (var name in parameters) {
514 var value = parameters[name];
516 data += escape(name);
518 data += escape(value);
524 // JSON Serializer {{{
525 $.json = function (value) {
529 var type = $.type(value);
531 if (type == "number
")
533 else if (type == "string
")
535 .replace(/\\/, "\\\\")
536 .replace(/\t/, "\\t
")
537 .replace(/\r/, "\\r
")
538 .replace(/\n/, "\\n
")
539 .replace(/"/, "\\\"")
541 else if (value
.constructor == Array
) {
545 for (var i
= 0; i
!= value
.length
; ++i
) {
551 json
+= $.json(value
[i
]);
556 value
.constructor == Object
&&
557 value
.toString() == "[object Object]"
562 for (var name
in value
) {
568 json
+= name
+ ":" + $.json(value
[name
]);