]>
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 var nodes
= doc
.getElementsByClassName(arg
.substring(1));
32 return $(nodes
== null ? [] : nodes
);
34 return $([doc
]).descendants(arg
);
36 _assert(doc
== undefined);
37 this.set($.array(arg
));
42 $.type = function (value
) {
43 var type
= typeof value
;
47 value
.toString
!= null &&
48 value
.toString().substring(0, 8) == "[object "
58 $.ready = function (_function
) {
62 document
.addEventListener("DOMContentLoaded", function () {
63 for (var i
= 0; i
!= ready_
.length
; ++i
)
68 ready_
.push(_function
);
72 /* XXX: verify arg3 overflow */
73 $.each = function (values
, _function
, arg0
, arg1
, arg2
) {
74 for (var i
= 0, e
= values
.length
; i
!= e
; ++i
)
75 _function(values
[i
], arg0
, arg1
, arg2
);
78 /* XXX: verify arg3 overflow */
79 $.map = function (values
, _function
, arg0
, arg1
, arg2
) {
81 for (var i
= 0, e
= values
.length
; i
!= e
; ++i
)
82 mapped
.push(_function(values
[i
], arg0
, arg1
, arg2
));
86 $.array = function (values
) {
87 if (values
.constructor == Array
)
90 for (var i
= 0; i
!= values
.length
; ++i
)
91 array
.push(values
[i
]);
95 $.document = function (node
) {
97 var parent
= node
.parentNode
;
107 add: function (nodes
) {
108 Array
.prototype.push
.apply(this, nodes
);
111 set: function (nodes
) {
116 css: function (name
, value
) {
117 $.each(this, function (node
) {
118 node
.style
[name
] = value
;
122 append: function (html
) {
123 $.each(this, function (node
) {
124 var doc
= $.document(node
);
126 // XXX: implement wrapper system
127 var div
= doc
.createElement("div");
128 div
.innerHTML
= html
;
130 while (div
.childNodes
.length
!= 0) {
131 var child
= div
.childNodes
[0];
132 node
.appendChild(child
);
137 descendants: function (expression
) {
138 var descendants
= $([]);
140 $.each(this, function (node
) {
141 descendants
.add(node
.getElementsByTagName(expression
));
147 remove: function () {
148 $.each(this, function (node
) {
149 node
.parentNode
.removeChild(node
);
153 parent: function () {
154 return $($.map(this, function (node
) {
155 return node
.parentNode
;
160 $.scroll = function (x
, y
) {
161 window
.scrollTo(x
, y
);
164 // XXX: document.all?
165 $.all = function (doc
) {
166 if (doc
== undefined)
168 return $(doc
.getElementsByTagName("*"));
171 $.inject = function (a
, b
) {
172 if ($.type(a
) == "string") {
173 $.prototype[a
] = function (value
) {
174 if (value
== undefined)
175 return $.map(this, function (node
) {
179 $.each(this, function (node
, value
) {
183 } else for (var name
in a
)
184 $.inject(name
, a
[name
]);
189 get: function (node
) {
190 return node
.style
.display
;
192 set: function (node
, value
) {
193 node
.style
.display
= value
;
198 get: function (node
) {
199 return node
.innerHTML
;
201 set: function (node
, value
) {
202 node
.innerHTML
= value
;
207 get: function (node
) {
210 set: function (node
, value
) {
216 get: function (node
) {
219 set: function (node
, value
) {
225 get: function (node
) {
228 set: function (node
, value
) {
234 // Event Registration {{{
235 // XXX: unable to remove registration
236 $.prototype.event = function (event
, _function
) {
237 $.each(this, function (node
) {
238 // XXX: smooth over this pointer ugliness
239 if (node
.addEventListener
)
240 node
.addEventListener(event
, _function
, false);
241 else if (node
.attachEvent
)
242 node
.attachEvent("on" + event
, _function
);
244 // XXX: multiple registration SNAFU
245 node
["on" + event
] = _function
;
250 "click", "load", "submit"
251 ], function (event
) {
252 $.prototype[event
] = function (_function
) {
253 if (_function
== undefined)
256 this.event(event
, _function
);
260 // Timed Animation {{{
261 $.interpolate = function (duration
, event
) {
262 var start
= new Date();
264 var next = function () {
265 setTimeout(update
, 0);
268 var update = function () {
269 var time
= new Date() - start
;
271 if (time
>= duration
)
274 event(time
/ duration
);
283 // XXX: abstract and implement other cases
284 $.xhr = function (url
, method
, headers
, data
, events
) {
285 var xhr
= new XMLHttpRequest();
286 xhr
.open(method
, url
, true);
288 for (var name
in headers
)
289 xhr
.setRequestHeader(name
.replace(/_
/, "-"), headers
[name
]);
294 xhr
.onreadystatechange = function () {
295 if (xhr
.readyState
== 4)
296 if (events
.complete
!= null)
297 events
.complete(xhr
.responseText
);
303 $.call = function (url
, post
, onsuccess
) {
306 if (onsuccess
!= null)
307 events
.complete = function (text
) {
308 onsuccess(eval(text
));
312 $.xhr(url
, "POST", null, null, events
);
315 Content_Type: "application/json"
316 }, $.json(post
), events
);
319 // WWW Form URL Encoder {{{
320 $.form = function (parameters
) {
323 var ampersand
= false;
324 for (var name
in parameters
) {
330 var value
= parameters
[name
];
332 data
+= escape(name
);
334 data
+= escape(value
);
340 // JSON Serializer {{{
341 $.json = function (value
) {
345 var type
= $.type(value
);
347 if (type
== "number")
349 else if (type
== "string")
351 .replace(/\\/, "\\\\")
352 .replace(/\t/, "\\t")
353 .replace(/\r/, "\\r")
354 .replace(/\n/, "\\n")
355 .replace(/"/, "\\\"")
357 else if (value.constructor == Array) {
361 for (var i = 0; i != value.length; ++i) {
367 json += $.json(value[i]);
372 value.constructor == Object &&
373 value.toString() == "[object Object
]"
378 for (var name in value) {
384 json += name + ":" + $.json(value[name]);