]>
git.saurik.com Git - cydia.git/blob - Cydia.app/menes/menes.js
1 var _assert = function (expr
) {
3 var message
= "_assert(" + expr
+ ")";
9 if (typeof Array
.prototype.push
!= "function")
10 Array
.prototype.push = function (value
) {
11 this[this.length
] = value
;
15 var $ = function (arg
, doc
) {
16 if (this.magic_
!= $.prototype.magic_
)
19 var type
= $.type(arg
);
21 if (type
== "function")
23 else if (type
== "string") {
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
);
33 return $([doc
]).descendants(arg
);
35 _assert(doc
== undefined);
36 this.set($.array(arg
));
41 $.xml = function (value
) {
43 .replace(/&/, "&")
46 .replace(/"/, ""
;")
47 .replace(/'/, "&apos
;")
51 $.type = function (value) {
52 var type = typeof value;
56 value.toString != null &&
57 value.toString().substring(0, 8) == "[object
"
67 $.ready = function (_function) {
71 document.addEventListener("DOMContentLoaded
", function () {
72 for (var i = 0; i != ready_.length; ++i)
77 ready_.push(_function);
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);
87 /* XXX: verify arg3 overflow */
88 $.map = function (values, _function, arg0, arg1, arg2) {
90 for (var i = 0, e = values.length; i != e; ++i)
91 mapped.push(_function(values[i], arg0, arg1, arg2));
95 $.array = function (values) {
96 if (values.constructor == Array)
99 for (var i = 0; i != values.length; ++i)
100 array.push(values[i]);
104 $.document = function (node) {
106 var parent = node.parentNode;
116 add: function (nodes) {
117 Array.prototype.push.apply(this, nodes);
120 set: function (nodes) {
125 css: function (name, value) {
126 $.each(this, function (node) {
127 node.style[name] = value;
131 append: function (html) {
132 $.each(this, $.type(html) == "string
" ? function (node) {
133 var doc = $.document(node);
135 // XXX: implement wrapper system
136 var div = doc.createElement("div
");
137 div.innerHTML = html;
139 while (div.childNodes.length != 0) {
140 var child = div.childNodes[0];
141 node.appendChild(child);
143 } : function (node) {
144 $.each(html, function (value) {
145 node.appendChild(value);
150 clone: function (deep) {
151 return $($.map(this, function (node) {
152 return node.cloneNode(deep);
156 descendants: function (expression) {
157 var descendants = $([]);
159 $.each(this, function (node) {
160 descendants.add(node.getElementsByTagName(expression));
166 remove: function () {
167 $.each(this, function (node) {
168 node.parentNode.removeChild(node);
172 parent: function () {
173 return $($.map(this, function (node) {
174 return node.parentNode;
178 xpath: function (expression) {
181 $.each(this, function (node) {
182 var doc = $.document(node);
183 var result = doc.evaluate(expression, node, null, XPathResult.ANY_TYPE, null);
185 if (result.resultType == XPathResult.UNORDERED_NODE_ITERATOR_TYPE)
187 var next = result.iterateNext();
198 $.scroll = function (x, y) {
199 window.scrollTo(x, y);
202 // XXX: document.all?
203 $.all = function (doc) {
204 if (doc == undefined)
206 return $(doc.getElementsByTagName("*"));
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) {
217 $.each(this, function (node, value) {
221 } else for (var name in a)
222 $.inject(name, a[name]);
227 get: function (node) {
228 return node.style.display;
230 set: function (node, value) {
231 node.style.display = value;
236 get: function (node) {
237 return node.innerHTML;
239 set: function (node, value) {
240 node.innerHTML = value;
245 get: function (node) {
248 set: function (node, value) {
254 get: function (node) {
257 set: function (node, value) {
263 get: function (node) {
266 set: function (node, value) {
272 get: function (node) {
275 set: function (node, value) {
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);
291 // XXX: multiple registration SNAFU
292 node["on
" + event] = _function;
297 "click
", "load
", "submit
"
298 ], function (event) {
299 $.prototype[event] = function (_function) {
300 if (_function == undefined)
303 this.event(event, _function);
307 // Timed Animation {{{
308 $.interpolate = function (duration, event) {
309 var start = new Date();
311 var next = function () {
312 setTimeout(update, 0);
315 var update = function () {
316 var time = new Date() - start;
318 if (time >= duration)
321 event(time / duration);
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);
335 for (var name in headers)
336 xhr.setRequestHeader(name.replace(/_/, "-"), headers[name]);
341 xhr.onreadystatechange = function () {
342 if (xhr.readyState == 4)
343 if (events.complete != null)
344 events.complete(xhr.responseText);
350 $.call = function (url, post, onsuccess) {
353 if (onsuccess != null)
354 events.complete = function (text) {
355 onsuccess(eval(text));
359 $.xhr(url, "POST
", null, null, events);
362 Content_Type: "application
/json
"
363 }, $.json(post), events);
366 // WWW Form URL Encoder {{{
367 $.form = function (parameters) {
370 var ampersand = false;
371 for (var name in parameters) {
377 var value = parameters[name];
379 data += escape(name);
381 data += escape(value);
387 // JSON Serializer {{{
388 $.json = function (value) {
392 var type = $.type(value);
394 if (type == "number
")
396 else if (type == "string
")
398 .replace(/\\/, "\\\\")
399 .replace(/\t/, "\\t
")
400 .replace(/\r/, "\\r
")
401 .replace(/\n/, "\\n
")
402 .replace(/"/, "\\\"")
404 else if (value
.constructor == Array
) {
408 for (var i
= 0; i
!= value
.length
; ++i
) {
414 json
+= $.json(value
[i
]);
419 value
.constructor == Object
&&
420 value
.toString() == "[object Object]"
425 for (var name
in value
) {
431 json
+= name
+ ":" + $.json(value
[name
]);