]> git.saurik.com Git - cydia.git/blob - Cydia.app/menes/menes.js
Quick detour to get Cydia Safe Mode working.
[cydia.git] / Cydia.app / menes / menes.js
1 var _assert = function (expr) {
2 if (!expr) {
3 var message = "_assert(" + expr + ")";
4 alert(message);
5 throw message;
6 }
7 }
8 // Compatibility {{{
9 if (typeof Array.prototype.push != "function")
10 Array.prototype.push = function (value) {
11 this[this.length] = value;
12 };
13 // }}}
14
15 var $ = function (arg, doc) {
16 if (this.magic_ != $.prototype.magic_)
17 return new $(arg);
18
19 var type = $.type(arg);
20
21 if (type == "function")
22 $.ready(arg);
23 else if (type == "string") {
24 if (doc == undefined)
25 doc = document;
26 if (arg.charAt(0) == '#') {
27 var node = doc.getElementById(arg.substring(1));
28 return $(node == null ? [] : [node]);
29 } else if (arg.charAt(0) == '.') {
30 var nodes = doc.getElementsByClassName(arg.substring(1));
31 return $(nodes == null ? [] : nodes);
32 } else
33 return $([doc]).descendants(arg);
34 } else {
35 _assert(doc == undefined);
36 this.set($.array(arg));
37 return this;
38 }
39 };
40
41 $.xml = function (value) {
42 return value
43 .replace(/&/, "&")
44 .replace(/</, "&lt;")
45 .replace(/>/, "&gt;")
46 .replace(/"/, "&quot;")
47 .replace(/'/, "&apos;")
48 ;
49 }
50
51 $.type = function (value) {
52 var type = typeof value;
53
54 if (
55 type == "function" &&
56 value.toString != null &&
57 value.toString().substring(0, 8) == "[object "
58 )
59 return "object";
60 else
61 return type;
62 };
63
64 (function () {
65 var ready_ = null;
66
67 $.ready = function (_function) {
68 if (ready_ == null) {
69 ready_ = [];
70
71 document.addEventListener("DOMContentLoaded", function () {
72 for (var i = 0; i != ready_.length; ++i)
73 ready_[i]();
74 }, false);
75 }
76
77 ready_.push(_function);
78 };
79 })();
80
81 /* XXX: verify arg3 overflow */
82 $.each = function (values, _function, arg0, arg1, arg2) {
83 for (var i = 0, e = values.length; i != e; ++i)
84 _function(values[i], arg0, arg1, arg2);
85 };
86
87 /* XXX: verify arg3 overflow */
88 $.map = function (values, _function, arg0, arg1, arg2) {
89 var mapped = [];
90 for (var i = 0, e = values.length; i != e; ++i)
91 mapped.push(_function(values[i], arg0, arg1, arg2));
92 return mapped;
93 };
94
95 $.array = function (values) {
96 if (values.constructor == Array)
97 return values;
98 var array = [];
99 for (var i = 0; i != values.length; ++i)
100 array.push(values[i]);
101 return array;
102 };
103
104 $.document = function (node) {
105 for (;;) {
106 var parent = node.parentNode;
107 if (parent == null)
108 return node;
109 node = parent;
110 }
111 };
112
113 $.prototype = {
114 magic_: 2041085062,
115
116 add: function (nodes) {
117 Array.prototype.push.apply(this, nodes);
118 },
119
120 set: function (nodes) {
121 this.length = 0;
122 this.add(nodes);
123 },
124
125 css: function (name, value) {
126 $.each(this, function (node) {
127 node.style[name] = value;
128 });
129 },
130
131 append: function (html) {
132 $.each(this, $.type(html) == "string" ? function (node) {
133 var doc = $.document(node);
134
135 // XXX: implement wrapper system
136 var div = doc.createElement("div");
137 div.innerHTML = html;
138
139 while (div.childNodes.length != 0) {
140 var child = div.childNodes[0];
141 node.appendChild(child);
142 }
143 } : function (node) {
144 $.each(html, function (value) {
145 node.appendChild(value);
146 });
147 });
148 },
149
150 clone: function (deep) {
151 return $($.map(this, function (node) {
152 return node.cloneNode(deep);
153 }));
154 },
155
156 descendants: function (expression) {
157 var descendants = $([]);
158
159 $.each(this, function (node) {
160 descendants.add(node.getElementsByTagName(expression));
161 });
162
163 return descendants;
164 },
165
166 remove: function () {
167 $.each(this, function (node) {
168 node.parentNode.removeChild(node);
169 });
170 },
171
172 parent: function () {
173 return $($.map(this, function (node) {
174 return node.parentNode;
175 }));
176 },
177
178 xpath: function (expression) {
179 var value = $([]);
180
181 $.each(this, function (node) {
182 var doc = $.document(node);
183 var result = doc.evaluate(expression, node, null, XPathResult.ANY_TYPE, null);
184
185 if (result.resultType == XPathResult.UNORDERED_NODE_ITERATOR_TYPE)
186 for (;;) {
187 var next = result.iterateNext();
188 if (next == null)
189 break;
190 value.add([next]);
191 }
192 });
193
194 return value;
195 }
196 };
197
198 $.scroll = function (x, y) {
199 window.scrollTo(x, y);
200 };
201
202 // XXX: document.all?
203 $.all = function (doc) {
204 if (doc == undefined)
205 doc = document;
206 return $(doc.getElementsByTagName("*"));
207 };
208
209 $.inject = function (a, b) {
210 if ($.type(a) == "string") {
211 $.prototype[a] = function (value) {
212 if (value == undefined)
213 return $.map(this, function (node) {
214 return b.get(node);
215 });
216 else
217 $.each(this, function (node, value) {
218 b.set(node, value);
219 }, value);
220 };
221 } else for (var name in a)
222 $.inject(name, a[name]);
223 };
224
225 $.inject({
226 display: {
227 get: function (node) {
228 return node.style.display;
229 },
230 set: function (node, value) {
231 node.style.display = value;
232 }
233 },
234
235 html: {
236 get: function (node) {
237 return node.innerHTML;
238 },
239 set: function (node, value) {
240 node.innerHTML = value;
241 }
242 },
243
244 href: {
245 get: function (node) {
246 return node.href;
247 },
248 set: function (node, value) {
249 node.href = value;
250 }
251 },
252
253 id: {
254 get: function (node) {
255 return node.id;
256 },
257 set: function (node, value) {
258 node.id = value;
259 }
260 },
261
262 src: {
263 get: function (node) {
264 return node.src;
265 },
266 set: function (node, value) {
267 node.src = value;
268 }
269 },
270
271 value: {
272 get: function (node) {
273 return node.value;
274 },
275 set: function (node, value) {
276 node.value = value;
277 }
278 }
279 });
280
281 // Event Registration {{{
282 // XXX: unable to remove registration
283 $.prototype.event = function (event, _function) {
284 $.each(this, function (node) {
285 // XXX: smooth over this pointer ugliness
286 if (node.addEventListener)
287 node.addEventListener(event, _function, false);
288 else if (node.attachEvent)
289 node.attachEvent("on" + event, _function);
290 else
291 // XXX: multiple registration SNAFU
292 node["on" + event] = _function;
293 });
294 };
295
296 $.each([
297 "click", "load", "submit"
298 ], function (event) {
299 $.prototype[event] = function (_function) {
300 if (_function == undefined)
301 _assert(false);
302 else
303 this.event(event, _function);
304 };
305 });
306 // }}}
307 // Timed Animation {{{
308 $.interpolate = function (duration, event) {
309 var start = new Date();
310
311 var next = function () {
312 setTimeout(update, 0);
313 };
314
315 var update = function () {
316 var time = new Date() - start;
317
318 if (time >= duration)
319 event(1);
320 else {
321 event(time / duration);
322 next();
323 }
324 };
325
326 next();
327 };
328 // }}}
329 // AJAX Requests {{{
330 // XXX: abstract and implement other cases
331 $.xhr = function (url, method, headers, data, events) {
332 var xhr = new XMLHttpRequest();
333 xhr.open(method, url, true);
334
335 for (var name in headers)
336 xhr.setRequestHeader(name.replace(/_/, "-"), headers[name]);
337
338 if (events == null)
339 events = {};
340
341 xhr.onreadystatechange = function () {
342 if (xhr.readyState == 4)
343 if (events.complete != null)
344 events.complete(xhr.responseText);
345 };
346
347 xhr.send(data);
348 };
349
350 $.call = function (url, post, onsuccess) {
351 var events = {};
352
353 if (onsuccess != null)
354 events.complete = function (text) {
355 onsuccess(eval(text));
356 };
357
358 if (post == null)
359 $.xhr(url, "POST", null, null, events);
360 else
361 $.xhr(url, "POST", {
362 Content_Type: "application/json"
363 }, $.json(post), events);
364 };
365 // }}}
366 // WWW Form URL Encoder {{{
367 $.form = function (parameters) {
368 var data = "";
369
370 var ampersand = false;
371 for (var name in parameters) {
372 if (!ampersand)
373 ampersand = true;
374 else
375 data += "&";
376
377 var value = parameters[name];
378
379 data += escape(name);
380 data += "=";
381 data += escape(value);
382 }
383
384 return data;
385 };
386 // }}}
387 // JSON Serializer {{{
388 $.json = function (value) {
389 if (value == null)
390 return "null";
391
392 var type = $.type(value);
393
394 if (type == "number")
395 return value;
396 else if (type == "string")
397 return "\"" + value
398 .replace(/\\/, "\\\\")
399 .replace(/\t/, "\\t")
400 .replace(/\r/, "\\r")
401 .replace(/\n/, "\\n")
402 .replace(/"/, "\\\"")
403 + "\"";
404 else if (value.constructor == Array) {
405 var json = "[";
406 var comma = false;
407
408 for (var i = 0; i != value.length; ++i) {
409 if (!comma)
410 comma = true;
411 else
412 json += ",";
413
414 json += $.json(value[i]);
415 }
416
417 return json + "]";
418 } else if (
419 value.constructor == Object &&
420 value.toString() == "[object Object]"
421 ) {
422 var json = "{";
423 var comma = false;
424
425 for (var name in value) {
426 if (!comma)
427 comma = true;
428 else
429 json += ",";
430
431 json += name + ":" + $.json(value[name]);
432 }
433 return json + "}";
434 } else {
435 return value;
436 }
437 };
438 // }}}