]>
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) {
292 return node.innerHTML;
294 set: function (node, value) {
295 node.innerHTML = value;
300 get: function (node) {
303 set: function (node, value) {
309 get: function (node) {
312 set: function (node, value) {
318 get: function (node) {
319 return node.parentNode;
324 get: function (node) {
327 set: function (node, value) {
333 get: function (node) {
334 return node.localName;
339 get: function (node) {
342 set: function (node, value) {
343 // XXX: do I really need this?
344 if (true || node.localName != "select
")
347 var options = node.options;
348 for (var i = 0, e = options.length; i != e; ++i)
349 if (options[i].value == value) {
350 if (node.selectedIndex != i)
351 node.selectedIndex = i;
359 get: function (node) {
360 return node.offsetWidth;
365 // Query String Parsing {{{
366 $.query = function () {
369 var search = location.search;
370 if (search != null) {
371 _assert(search[0] == "?", "query string without
?");
373 var values = search.substring(1).split("&");
374 for (var index in values) {
375 var value = values[index]
376 var equal = value.indexOf("=");
383 name = value.substring(0, equal);
384 value = value.substring(equal + 1);
385 value = decodeURIComponent(value);
388 name = decodeURIComponent(name);
389 if (typeof args[name] == "undefined")
392 args[name].push(value);
399 // Event Registration {{{
400 // XXX: unable to remove registration
401 $.prototype.event = function (event, _function) {
402 $.each(this, function (node) {
403 // XXX: smooth over this pointer ugliness
404 if (node.addEventListener)
405 node.addEventListener(event, _function, false);
406 else if (node.attachEvent)
407 node.attachEvent("on
" + event, _function);
409 // XXX: multiple registration SNAFU
410 node["on
" + event] = _function;
415 "click
", "load
", "submit
"
416 ], function (event) {
417 $.prototype[event] = function (_function) {
418 if (typeof _function == 'undefined')
419 _assert(false, "undefined function to
$.[event
]");
421 this.event(event, _function);
425 // Timed Animation {{{
426 $.interpolate = function (duration, event) {
427 var start = new Date();
429 var next = function () {
430 setTimeout(update, 0);
433 var update = function () {
434 var time = new Date() - start;
436 if (time >= duration)
439 event(time / duration);
448 // XXX: abstract and implement other cases
449 $.xhr = function (url, method, headers, data, events) {
450 var xhr = new XMLHttpRequest();
451 xhr.open(method, url, true);
453 for (var name in headers)
454 xhr.setRequestHeader(name.replace(/_/, "-"), headers[name]);
459 xhr.onreadystatechange = function () {
460 if (xhr.readyState == 4) {
461 var status = xhr.status;
462 var text = xhr.responseText;
463 if (events.response != null)
464 events.response(status, text);
466 if (events.success != null)
467 events.success(text);
469 if (events.failure != null)
470 events.failure(status);
478 $.call = function (url, post, onsuccess) {
481 if (onsuccess != null)
482 events.complete = function (text) {
483 onsuccess(eval(text));
487 $.xhr(url, "POST
", null, null, events);
490 Content_Type: "application
/json
"
491 }, $.json(post), events);
494 // WWW Form URL Encoder {{{
495 $.form = function (parameters) {
498 var ampersand = false;
499 for (var name in parameters) {
505 var value = parameters[name];
507 data += escape(name);
509 data += escape(value);
515 // JSON Serializer {{{
516 $.json = function (value) {
520 var type = $.type(value);
522 if (type == "number
")
524 else if (type == "string
")
526 .replace(/\\/, "\\\\")
527 .replace(/\t/, "\\t
")
528 .replace(/\r/, "\\r
")
529 .replace(/\n/, "\\n
")
530 .replace(/"/, "\\\"")
532 else if (value
.constructor == Array
) {
536 for (var i
= 0; i
!= value
.length
; ++i
) {
542 json
+= $.json(value
[i
]);
547 value
.constructor == Object
&&
548 value
.toString() == "[object Object]"
553 for (var name
in value
) {
559 json
+= name
+ ":" + $.json(value
[name
]);