]>
git.saurik.com Git - cydia.git/blob - Cydia.app/menes/menes.js
1 var _assert = function (expr
) {
3 var message
= "_assert(" + expr
+ ")";
10 if (typeof Array
.prototype.push
!= "function")
11 Array
.prototype.push = function (value
) {
12 this[this.length
] = value
;
16 var $ = function (arg
, doc
) {
17 if (this.magic_
!= $.prototype.magic_
)
20 var type
= $.type(arg
);
22 if (type
== "function")
24 else if (type
== "string") {
27 if (arg
.charAt(0) == '#') {
28 var node
= doc
.getElementById(arg
.substring(1));
29 return $(node
== null ? [] : [node
]);
30 } else if (arg
.charAt(0) == '.')
31 return new $(doc
.getElementsByClassName(arg
.substring(1)));
33 return $([doc
]).descendants(arg
);
35 _assert(doc
== undefined);
36 this.set($.array(arg
));
41 $.type = function (value
) {
42 var type
= typeof value
;
46 value
.toString
!= null &&
47 value
.toString().substring(0, 8) == "[object "
57 $.ready = function (_function
) {
61 document
.addEventListener("DOMContentLoaded", function () {
62 for (var i
= 0; i
!= ready_
.length
; ++i
)
67 ready_
.push(_function
);
71 /* XXX: verify arg3 overflow */
72 $.each = function (values
, _function
, arg0
, arg1
, arg2
) {
73 for (var i
= 0, e
= values
.length
; i
!= e
; ++i
)
74 _function(values
[i
], arg0
, arg1
, arg2
);
77 /* XXX: verify arg3 overflow */
78 $.map = function (values
, _function
, arg0
, arg1
, arg2
) {
80 for (var i
= 0, e
= values
.length
; i
!= e
; ++i
)
81 mapped
.push(_function(values
[i
], arg0
, arg1
, arg2
));
85 $.array = function (values
) {
86 if (values
.constructor == Array
)
89 for (var i
= 0; i
!= values
.length
; ++i
)
90 array
.push(values
[i
]);
94 $.document = function (node
) {
96 var parent
= node
.parentNode
;
106 add: function (nodes
) {
107 Array
.prototype.push
.apply(this, nodes
);
110 set: function (nodes
) {
115 css: function (name
, value
) {
116 $.each(this, function (node
) {
117 node
.style
[name
] = value
;
121 append: function (html
) {
122 $.each(this, function (node
) {
123 var doc
= $.document(node
);
125 // XXX: implement wrapper system
126 var div
= doc
.createElement("div");
127 div
.innerHTML
= html
;
129 while (div
.childNodes
.length
!= 0) {
130 var child
= div
.childNodes
[0];
131 node
.appendChild(child
);
136 descendants: function (expression
) {
137 var descendants
= $([]);
139 $.each(this, function (node
) {
140 descendants
.add(node
.getElementsByTagName(expression
));
146 remove: function () {
147 $.each(this, function (node
) {
148 node
.parentNode
.removeChild(node
);
152 parent: function () {
153 return $($.map(this, function (node
) {
154 return node
.parentNode
;
159 $.scroll = function (x
, y
) {
160 window
.scrollTo(x
, y
);
163 // XXX: document.all?
164 $.all = function (doc
) {
165 if (doc
== undefined)
167 return $(doc
.getElementsByTagName("*"));
170 $.inject = function (a
, b
) {
171 if ($.type(a
) == "string") {
172 $.prototype[a
] = function (value
) {
173 if (value
== undefined)
174 return $.map(this, function (node
) {
178 $.each(this, function (node
, value
) {
182 } else for (var name
in a
)
183 $.inject(name
, a
[name
]);
188 get: function (node
) {
189 return node
.innerHTML
;
191 set: function (node
, value
) {
192 node
.innerHTML
= value
;
197 get: function (node
) {
200 set: function (node
, value
) {
206 get: function (node
) {
209 set: function (node
, value
) {
215 get: function (node
) {
218 set: function (node
, value
) {
224 // Event Registration {{{
225 // XXX: unable to remove registration
226 $.prototype.event = function (event
, _function
) {
227 $.each(this, function (node
) {
228 // XXX: smooth over this pointer ugliness
229 if (node
.addEventListener
)
230 node
.addEventListener(event
, _function
, false);
231 else if (node
.attachEvent
)
232 node
.attachEvent("on" + event
, _function
);
234 // XXX: multiple registration SNAFU
235 node
["on" + event
] = _function
;
240 "click", "load", "submit"
241 ], function (event
) {
242 $.prototype[event
] = function (_function
) {
243 if (_function
== undefined)
246 this.event(event
, _function
);
250 // Timed Animation {{{
251 $.interpolate = function (duration
, event
) {
252 var start
= new Date();
254 var next = function () {
255 setTimeout(update
, 0);
258 var update = function () {
259 var time
= new Date() - start
;
261 if (time
>= duration
)
264 event(time
/ duration
);
273 // XXX: abstract and implement other cases
274 $.xhr = function (url
, method
, headers
, data
, events
) {
275 var xhr
= new XMLHttpRequest();
276 xhr
.open(method
, url
, true);
278 for (var name
in headers
)
279 xhr
.setRequestHeader(name
.replace(/_
/, "-"), headers
[name
]);
284 xhr
.onreadystatechange = function () {
285 if (xhr
.readyState
== 4)
286 if (events
.complete
!= null)
287 events
.complete(xhr
.responseText
);
293 $.call = function (url
, post
, onsuccess
) {
296 if (onsuccess
!= null)
297 events
.complete = function (text
) {
298 onsuccess(eval(text
));
302 $.xhr(url
, "POST", null, null, events
);
305 Content_Type: "application/json"
306 }, $.json(post
), events
);
309 // WWW Form URL Encoder {{{
310 $.form = function (parameters
) {
313 var ampersand
= false;
314 for (var name
in parameters
) {
320 var value
= parameters
[name
];
322 data
+= escape(name
);
324 data
+= escape(value
);
330 // JSON Serializer {{{
331 $.json = function (value
) {
335 var type
= $.type(value
);
337 if (type
== "number")
339 else if (type
== "string")
341 .replace(/\\/, "\\\\")
342 .replace(/\t/, "\\t")
343 .replace(/\r/, "\\r")
344 .replace(/\n/, "\\n")
345 .replace(/"/, "\\\"")
347 else if (value.constructor == Array) {
351 for (var i = 0; i != value.length; ++i) {
357 json += $.json(value[i]);
362 value.constructor == Object &&
363 value.toString() == "[object Object
]"
368 for (var name in value) {
374 json += name + ":" + $.json(value[name]);