X-Git-Url: https://git.saurik.com/cydia.git/blobdiff_plain/853d14d30b0f905c441a66b1ab70a63346a8bd7f..ad8c38ed9da0221550a5f9ee955945bb3c3b83ec:/Cydia.app/menes/menes.js?ds=inline diff --git a/Cydia.app/menes/menes.js b/Cydia.app/menes/menes.js index f0070bcd..dc210bb4 100644 --- a/Cydia.app/menes/menes.js +++ b/Cydia.app/menes/menes.js @@ -1,7 +1,8 @@ -var _assert = function (expr) { +/* XXX: this message is ultra-lame */ +var _assert = function (expr, value) { if (!expr) { - var message = "_assert(" + expr + ")"; - alert(message); + var message = "_assert(" + value + ")"; + console.log(message); throw message; } } @@ -17,39 +18,51 @@ var $ = function (arg, doc) { if (this.magic_ != $.prototype.magic_) return new $(arg); + if (arg == null) + arg = []; + var type = $.type(arg); if (type == "function") $.ready(arg); else if (type == "string") { - if (doc == undefined) + if (typeof doc == 'undefined') doc = document; if (arg.charAt(0) == '#') { - var node = doc.getElementById(arg.substring(1)); - return $(node == null ? [] : [node]); - } else if (arg.charAt(0) == '.') { - var nodes = doc.getElementsByClassName(arg.substring(1)); - return $(nodes == null ? [] : nodes); - } else + /* XXX: this is somewhat incorrect-a-porter */ + var element = doc.getElementById(arg.substring(1)); + return $(element == null ? [] : [element]); + } else if (arg.charAt(0) == '.') + return $(doc.getElementsByClassName(arg.substring(1))); + else return $([doc]).descendants(arg); - } else { - _assert(doc == undefined); - this.set($.array(arg)); + } else if (typeof arg.length != 'undefined') { + _assert(typeof doc == 'undefined', "non-query with document to $"); + this.set(arg); return this; - } + } else _assert(false, "unknown argument to $: " + typeof arg); }; +$.xml = function (value) { + return value + .replace(/&/, "&") + .replace(//, ">") + .replace(/"/, """) + .replace(/'/, "'") + ; +} + $.type = function (value) { var type = typeof value; - if ( - type == "function" && - value.toString != null && - value.toString().substring(0, 8) == "[object " - ) - return "object"; - else - return type; + if ((type == "function" || type == "object") && value.toString != null) { + var string = value.toString(); + if (string.substring(0, 8) == "[object ") + return string.substring(8, string.length - 1); + } + + return type; }; (function () { @@ -86,6 +99,7 @@ $.map = function (values, _function, arg0, arg1, arg2) { $.array = function (values) { if (values.constructor == Array) return values; + _assert(typeof values.length != 'undefined', "$.array on underlying non-array"); var array = []; for (var i = 0; i != values.length; ++i) array.push(values[i]); @@ -101,11 +115,30 @@ $.document = function (node) { } }; +$.reclass = function (_class) { + return new RegExp('(\\s|^)' + _class + '(\\s|$)'); +}; + $.prototype = { magic_: 2041085062, add: function (nodes) { - Array.prototype.push.apply(this, nodes); + Array.prototype.push.apply(this, $.array(nodes)); + }, + + at: function (name, value) { + if (typeof value == 'undefined') + return $.map(this, function (node) { + return node.getAttribute(name); + }); + else if (value == null) + $.each(this, function (node) { + node.removeAttribute(); + }); + else + $.each(this, function (node) { + node.setAttribute(name, value); + }); }, set: function (nodes) { @@ -113,32 +146,98 @@ $.prototype = { this.add(nodes); }, + /* XXX: verify arg3 overflow */ + each: function (_function, arg0, arg1, arg2) { + $.each(this, function (node) { + _function($([node]), arg0, arg1, arg2); + }); + }, + css: function (name, value) { $.each(this, function (node) { node.style[name] = value; }); }, - append: function (html) { + addClass: function (_class) { $.each(this, function (node) { - var doc = $.document(node); + if (!$([node]).hasClass(_class)[0]) + node.className += " " + _class; + }); + }, + + blur: function () { + $.each(this, function (node) { + node.blur(); + }); + }, - // XXX: implement wrapper system - var div = doc.createElement("div"); - div.innerHTML = html; + focus: function () { + $.each(this, function (node) { + node.focus(); + }); + }, - while (div.childNodes.length != 0) { - var child = div.childNodes[0]; - node.appendChild(child); - } + removeClass: function (_class) { + $.each(this, function (node) { + node.className = node.className.replace($.reclass(_class), ' '); + }); + }, + + hasClass: function (_class) { + return $.map(this, function (node) { + return node.className.match($.reclass(_class)); }); }, + append: function (children) { + if ($.type(children) == "string") + $.each(this, function (node) { + var doc = $.document(node); + + // XXX: implement wrapper system + var div = doc.createElement("div"); + div.innerHTML = children; + + while (div.childNodes.length != 0) { + var child = div.childNodes[0]; + node.appendChild(child); + } + }); + else + $.each(this, function (node) { + $.each(children, function (child) { + node.appendChild(child); + }); + }); + }, + + xpath: function (expression) { + var value = $([]); + + $.each(this, function (node) { + var doc = $.document(node); + var results = doc.evaluate(expression, node, null, XPathResult.ANY_TYPE, null); + var result; + while (result = results.iterateNext()) + value.add([result]); + }); + + return value; + }, + + clone: function (deep) { + return $($.map(this, function (node) { + return node.cloneNode(deep); + })); + }, + descendants: function (expression) { var descendants = $([]); $.each(this, function (node) { - descendants.add(node.getElementsByTagName(expression)); + var nodes = node.getElementsByTagName(expression); + descendants.add(nodes); }); return descendants; @@ -148,12 +247,6 @@ $.prototype = { $.each(this, function (node) { node.parentNode.removeChild(node); }); - }, - - parent: function () { - return $($.map(this, function (node) { - return node.parentNode; - })); } }; @@ -163,7 +256,7 @@ $.scroll = function (x, y) { // XXX: document.all? $.all = function (doc) { - if (doc == undefined) + if (typeof doc == 'undefined') doc = document; return $(doc.getElementsByTagName("*")); }; @@ -171,7 +264,7 @@ $.all = function (doc) { $.inject = function (a, b) { if ($.type(a) == "string") { $.prototype[a] = function (value) { - if (value == undefined) + if (typeof value == 'undefined') return $.map(this, function (node) { return b.get(node); }); @@ -185,12 +278,21 @@ $.inject = function (a, b) { }; $.inject({ - display: { + _default: { get: function (node) { - return node.style.display; + return node.style.defaultValue; }, set: function (node, value) { - node.style.display = value; + node.style.defaultValue = value; + } + }, + + height: { + get: function (node) { + return node.height; + }, + set: function (node, value) { + node.height = value; } }, @@ -212,6 +314,21 @@ $.inject({ } }, + name: { + get: function (node) { + return node.name; + }, + set: function (node, value) { + node.name = value; + } + }, + + parent: { + get: function (node) { + return node.parentNode; + } + }, + src: { get: function (node) { return node.src; @@ -221,16 +338,73 @@ $.inject({ } }, + type: { + get: function (node) { + return node.localName; + } + }, + value: { get: function (node) { return node.value; }, set: function (node, value) { - node.value = value; + // XXX: do I really need this? + if (true || node.localName != "select") + node.value = value; + else { + var options = node.options; + for (var i = 0, e = options.length; i != e; ++i) + if (options[i].value == value) { + if (node.selectedIndex != i) + node.selectedIndex = i; + break; + } + } + } + }, + + width: { + get: function (node) { + return node.offsetWidth; } } }); +// Query String Parsing {{{ +$.query = function () { + var args = {}; + + var search = location.search; + if (search != null) { + _assert(search[0] == "?", "query string without ?"); + + var values = search.substring(1).split("&"); + for (var index in values) { + var value = values[index] + var equal = value.indexOf("="); + var name; + + if (equal == -1) { + name = value; + value = null; + } else { + name = value.substring(0, equal); + value = value.substring(equal + 1); + value = decodeURIComponent(value); + } + + name = decodeURIComponent(name); + if (typeof args[name] == "undefined") + args[name] = []; + if (value != null) + args[name].push(value); + } + } + + return args; +}; +// }}} // Event Registration {{{ // XXX: unable to remove registration $.prototype.event = function (event, _function) { @@ -250,8 +424,8 @@ $.each([ "click", "load", "submit" ], function (event) { $.prototype[event] = function (_function) { - if (_function == undefined) - _assert(false); + if (typeof _function == 'undefined') + _assert(false, "undefined function to $.[event]"); else this.event(event, _function); }; @@ -292,9 +466,19 @@ $.xhr = function (url, method, headers, data, events) { events = {}; xhr.onreadystatechange = function () { - if (xhr.readyState == 4) - if (events.complete != null) - events.complete(xhr.responseText); + if (xhr.readyState == 4) { + var status = xhr.status; + var text = xhr.responseText; + if (events.response != null) + events.response(status, text); + if (status == 200) { + if (events.success != null) + events.success(text); + } else { + if (events.failure != null) + events.failure(status); + } + } }; xhr.send(data);