]> git.saurik.com Git - cydia.git/blob - Cydia.app/menes/menes.js
Fixed title bar of confirmation and finally fixed stupid memory allocation bug.
[cydia.git] / Cydia.app / menes / menes.js
1 /* XXX: this message is ultra-lame */
2 var _assert = function (expr, value) {
3 if (!expr) {
4 var message = "_assert(" + value + ")";
5 console.log(message);
6 throw message;
7 }
8 }
9
10 // Compatibility {{{
11 if (typeof Array.prototype.push != "function")
12 Array.prototype.push = function (value) {
13 this[this.length] = value;
14 };
15 // }}}
16
17 var $ = function (arg, doc) {
18 if (this.magic_ != $.prototype.magic_)
19 return new $(arg);
20
21 if (arg == null)
22 arg = [];
23
24 var type = $.type(arg);
25
26 if (type == "function")
27 $.ready(arg);
28 else if (type == "string") {
29 if (typeof doc == 'undefined')
30 doc = document;
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)));
37 else
38 return $([doc]).descendants(arg);
39 } else if (typeof arg.length != 'undefined') {
40 _assert(typeof doc == 'undefined', "non-query with document to $");
41 this.set(arg);
42 return this;
43 } else _assert(false, "unknown argument to $: " + typeof arg);
44 };
45
46 $.xml = function (value) {
47 return value
48 .replace(/&/, "&")
49 .replace(/</, "&lt;")
50 .replace(/>/, "&gt;")
51 .replace(/"/, "&quot;")
52 .replace(/'/, "&apos;")
53 ;
54 }
55
56 $.type = function (value) {
57 var type = typeof value;
58
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);
63 }
64
65 return type;
66 };
67
68 (function () {
69 var ready_ = null;
70
71 $.ready = function (_function) {
72 if (ready_ == null) {
73 ready_ = [];
74
75 document.addEventListener("DOMContentLoaded", function () {
76 for (var i = 0; i != ready_.length; ++i)
77 ready_[i]();
78 }, false);
79 }
80
81 ready_.push(_function);
82 };
83 })();
84
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);
89 };
90
91 /* XXX: verify arg3 overflow */
92 $.map = function (values, _function, arg0, arg1, arg2) {
93 var mapped = [];
94 for (var i = 0, e = values.length; i != e; ++i)
95 mapped.push(_function(values[i], arg0, arg1, arg2));
96 return mapped;
97 };
98
99 $.array = function (values) {
100 if (values.constructor == Array)
101 return values;
102 _assert(typeof values.length != 'undefined', "$.array on underlying non-array");
103 var array = [];
104 for (var i = 0; i != values.length; ++i)
105 array.push(values[i]);
106 return array;
107 };
108
109 $.document = function (node) {
110 for (;;) {
111 var parent = node.parentNode;
112 if (parent == null)
113 return node;
114 node = parent;
115 }
116 };
117
118 $.reclass = function (_class) {
119 return new RegExp('(\\s|^)' + _class + '(\\s|$)');
120 };
121
122 $.prototype = {
123 magic_: 2041085062,
124
125 add: function (nodes) {
126 Array.prototype.push.apply(this, $.array(nodes));
127 },
128
129 at: function (name, value) {
130 if (typeof value == 'undefined')
131 return $.map(this, function (node) {
132 return node.getAttribute(name);
133 });
134 else if (value == null)
135 $.each(this, function (node) {
136 node.removeAttribute();
137 });
138 else
139 $.each(this, function (node) {
140 node.setAttribute(name, value);
141 });
142 },
143
144 set: function (nodes) {
145 this.length = 0;
146 this.add(nodes);
147 },
148
149 /* XXX: verify arg3 overflow */
150 each: function (_function, arg0, arg1, arg2) {
151 $.each(this, function (node) {
152 _function($([node]), arg0, arg1, arg2);
153 });
154 },
155
156 css: function (name, value) {
157 $.each(this, function (node) {
158 node.style[name] = value;
159 });
160 },
161
162 addClass: function (_class) {
163 $.each(this, function (node) {
164 if (!$([node]).hasClass(_class)[0])
165 node.className += " " + _class;
166 });
167 },
168
169 blur: function () {
170 $.each(this, function (node) {
171 node.blur();
172 });
173 },
174
175 focus: function () {
176 $.each(this, function (node) {
177 node.focus();
178 });
179 },
180
181 removeClass: function (_class) {
182 $.each(this, function (node) {
183 node.className = node.className.replace($.reclass(_class), ' ');
184 });
185 },
186
187 hasClass: function (_class) {
188 return $.map(this, function (node) {
189 return node.className.match($.reclass(_class));
190 });
191 },
192
193 append: function (html) {
194 $.each(this, function (node) {
195 var doc = $.document(node);
196
197 // XXX: implement wrapper system
198 var div = doc.createElement("div");
199 div.innerHTML = html;
200
201 while (div.childNodes.length != 0) {
202 var child = div.childNodes[0];
203 node.appendChild(child);
204 }
205 });
206 },
207
208 descendants: function (expression) {
209 var descendants = $([]);
210
211 $.each(this, function (node) {
212 var nodes = node.getElementsByTagName(expression);
213 descendants.add(nodes);
214 });
215
216 return descendants;
217 },
218
219 remove: function () {
220 $.each(this, function (node) {
221 node.parentNode.removeChild(node);
222 });
223 }
224 };
225
226 $.scroll = function (x, y) {
227 window.scrollTo(x, y);
228 };
229
230 // XXX: document.all?
231 $.all = function (doc) {
232 if (typeof doc == 'undefined')
233 doc = document;
234 return $(doc.getElementsByTagName("*"));
235 };
236
237 $.inject = function (a, b) {
238 if ($.type(a) == "string") {
239 $.prototype[a] = function (value) {
240 if (typeof value == 'undefined')
241 return $.map(this, function (node) {
242 return b.get(node);
243 });
244 else
245 $.each(this, function (node, value) {
246 b.set(node, value);
247 }, value);
248 };
249 } else for (var name in a)
250 $.inject(name, a[name]);
251 };
252
253 $.inject({
254 _default: {
255 get: function (node) {
256 return node.style.defaultValue;
257 },
258 set: function (node, value) {
259 node.style.defaultValue = value;
260 }
261 },
262
263 display: {
264 get: function (node) {
265 return node.style.display;
266 },
267 set: function (node, value) {
268 node.style.display = value;
269 }
270 },
271
272 html: {
273 get: function (node) {
274 return node.innerHTML;
275 },
276 set: function (node, value) {
277 node.innerHTML = value;
278 }
279 },
280
281 href: {
282 get: function (node) {
283 return node.href;
284 },
285 set: function (node, value) {
286 node.href = value;
287 }
288 },
289
290 name: {
291 get: function (node) {
292 return node.name;
293 },
294 set: function (node, value) {
295 node.name = value;
296 }
297 },
298
299 parent: {
300 get: function (node) {
301 return node.parentNode;
302 }
303 },
304
305 src: {
306 get: function (node) {
307 return node.src;
308 },
309 set: function (node, value) {
310 node.src = value;
311 }
312 },
313
314 type: {
315 get: function (node) {
316 return node.localName;
317 }
318 },
319
320 value: {
321 get: function (node) {
322 return node.value;
323 },
324 set: function (node, value) {
325 // XXX: do I really need this?
326 if (true || node.localName != "select")
327 node.value = value;
328 else {
329 var options = node.options;
330 for (var i = 0, e = options.length; i != e; ++i)
331 if (options[i].value == value) {
332 if (node.selectedIndex != i)
333 node.selectedIndex = i;
334 break;
335 }
336 }
337 }
338 },
339
340 width: {
341 get: function (node) {
342 return node.offsetWidth;
343 }
344 }
345 });
346
347 // Query String Parsing {{{
348 $.query = function () {
349 var args = {};
350
351 var search = location.search;
352 if (search != null) {
353 _assert(search[0] == "?", "query string without ?");
354
355 var values = search.substring(1).split("&");
356 for (var index in values) {
357 var value = values[index]
358 var equal = value.indexOf("=");
359 var name;
360
361 if (equal == -1) {
362 name = value;
363 value = null;
364 } else {
365 name = value.substring(0, equal);
366 value = value.substring(equal + 1);
367 value = decodeURIComponent(value);
368 }
369
370 name = decodeURIComponent(name);
371 if (typeof args[name] == "undefined")
372 args[name] = [];
373 if (value != null)
374 args[name].push(value);
375 }
376 }
377
378 return args;
379 };
380 // }}}
381 // Event Registration {{{
382 // XXX: unable to remove registration
383 $.prototype.event = function (event, _function) {
384 $.each(this, function (node) {
385 // XXX: smooth over this pointer ugliness
386 if (node.addEventListener)
387 node.addEventListener(event, _function, false);
388 else if (node.attachEvent)
389 node.attachEvent("on" + event, _function);
390 else
391 // XXX: multiple registration SNAFU
392 node["on" + event] = _function;
393 });
394 };
395
396 $.each([
397 "click", "load", "submit"
398 ], function (event) {
399 $.prototype[event] = function (_function) {
400 if (typeof _function == 'undefined')
401 _assert(false, "undefined function to $.[event]");
402 else
403 this.event(event, _function);
404 };
405 });
406 // }}}
407 // Timed Animation {{{
408 $.interpolate = function (duration, event) {
409 var start = new Date();
410
411 var next = function () {
412 setTimeout(update, 0);
413 };
414
415 var update = function () {
416 var time = new Date() - start;
417
418 if (time >= duration)
419 event(1);
420 else {
421 event(time / duration);
422 next();
423 }
424 };
425
426 next();
427 };
428 // }}}
429 // AJAX Requests {{{
430 // XXX: abstract and implement other cases
431 $.xhr = function (url, method, headers, data, events) {
432 var xhr = new XMLHttpRequest();
433 xhr.open(method, url, true);
434
435 for (var name in headers)
436 xhr.setRequestHeader(name.replace(/_/, "-"), headers[name]);
437
438 if (events == null)
439 events = {};
440
441 xhr.onreadystatechange = function () {
442 if (xhr.readyState == 4) {
443 var status = xhr.status;
444 var text = xhr.responseText;
445 if (events.response != null)
446 events.response(status, text);
447 if (events.success != null)
448 if (status == 200)
449 events.success(text);
450 }
451 };
452
453 xhr.send(data);
454 };
455
456 $.call = function (url, post, onsuccess) {
457 var events = {};
458
459 if (onsuccess != null)
460 events.complete = function (text) {
461 onsuccess(eval(text));
462 };
463
464 if (post == null)
465 $.xhr(url, "POST", null, null, events);
466 else
467 $.xhr(url, "POST", {
468 Content_Type: "application/json"
469 }, $.json(post), events);
470 };
471 // }}}
472 // WWW Form URL Encoder {{{
473 $.form = function (parameters) {
474 var data = "";
475
476 var ampersand = false;
477 for (var name in parameters) {
478 if (!ampersand)
479 ampersand = true;
480 else
481 data += "&";
482
483 var value = parameters[name];
484
485 data += escape(name);
486 data += "=";
487 data += escape(value);
488 }
489
490 return data;
491 };
492 // }}}
493 // JSON Serializer {{{
494 $.json = function (value) {
495 if (value == null)
496 return "null";
497
498 var type = $.type(value);
499
500 if (type == "number")
501 return value;
502 else if (type == "string")
503 return "\"" + value
504 .replace(/\\/, "\\\\")
505 .replace(/\t/, "\\t")
506 .replace(/\r/, "\\r")
507 .replace(/\n/, "\\n")
508 .replace(/"/, "\\\"")
509 + "\"";
510 else if (value.constructor == Array) {
511 var json = "[";
512 var comma = false;
513
514 for (var i = 0; i != value.length; ++i) {
515 if (!comma)
516 comma = true;
517 else
518 json += ",";
519
520 json += $.json(value[i]);
521 }
522
523 return json + "]";
524 } else if (
525 value.constructor == Object &&
526 value.toString() == "[object Object]"
527 ) {
528 var json = "{";
529 var comma = false;
530
531 for (var name in value) {
532 if (!comma)
533 comma = true;
534 else
535 json += ",";
536
537 json += name + ":" + $.json(value[name]);
538 }
539 return json + "}";
540 } else {
541 return value;
542 }
543 };
544 // }}}