1
0
mirror of https://github.com/gryf/pentadactyl-pm.git synced 2026-04-18 02:23:33 +02:00

Minorish refactoring.

This commit is contained in:
Kris Maglione
2011-09-08 10:22:43 -04:00
parent e9a0b1eea3
commit c6035ce674
7 changed files with 227 additions and 147 deletions

View File

@@ -133,6 +133,7 @@ function defineModule(name, params, module) {
module.NAME = name; module.NAME = name;
module.EXPORTED_SYMBOLS = params.exports || []; module.EXPORTED_SYMBOLS = params.exports || [];
if (!~module.EXPORTED_SYMBOLS.indexOf("File"))
delete module.File; delete module.File;
defineModule.loadLog.push("[Begin " + name + "]"); defineModule.loadLog.push("[Begin " + name + "]");

View File

@@ -10,6 +10,8 @@ defineModule("bookmarkcache", {
require: ["services", "storage", "util"] require: ["services", "storage", "util"]
}, this); }, this);
function newURI(url, charset, base) services.io.newURI(url, charset, base);
var Bookmark = Struct("url", "title", "icon", "post", "keyword", "tags", "charset", "id"); var Bookmark = Struct("url", "title", "icon", "post", "keyword", "tags", "charset", "id");
var Keyword = Struct("keyword", "title", "icon", "url"); var Keyword = Struct("keyword", "title", "icon", "url");
Bookmark.defaultValue("icon", function () BookmarkCache.getFavicon(this.url)); Bookmark.defaultValue("icon", function () BookmarkCache.getFavicon(this.url));
@@ -19,7 +21,7 @@ update(Bookmark.prototype, {
["tags", this.tags.join(", "), "Tag"] ["tags", this.tags.join(", "), "Tag"]
].filter(function (item) item[1]), ].filter(function (item) item[1]),
get uri() util.newURI(this.url), get uri() newURI(this.url),
set uri(uri) { set uri(uri) {
let tags = this.tags; let tags = this.tags;
this.tags = null; this.tags = null;
@@ -36,7 +38,7 @@ update(Bookmark.prototype, {
}) })
Bookmark.prototype.members.uri = Bookmark.prototype.members.url; Bookmark.prototype.members.uri = Bookmark.prototype.members.url;
Bookmark.setter = function (key, func) this.prototype.__defineSetter__(key, func); Bookmark.setter = function (key, func) this.prototype.__defineSetter__(key, func);
Bookmark.setter("url", function (val) { this.uri = isString(val) ? util.newURI(val) : val; }); Bookmark.setter("url", function (val) { this.uri = isString(val) ? newURI(val) : val; });
Bookmark.setter("title", function (val) { services.bookmarks.setItemTitle(this.id, val); }); Bookmark.setter("title", function (val) { services.bookmarks.setItemTitle(this.id, val); });
Bookmark.setter("post", function (val) { bookmarkcache.annotate(this.id, bookmarkcache.POST, val); }); Bookmark.setter("post", function (val) { bookmarkcache.annotate(this.id, bookmarkcache.POST, val); });
Bookmark.setter("charset", function (val) { bookmarkcache.annotate(this.id, bookmarkcache.CHARSET, val); }); Bookmark.setter("charset", function (val) { bookmarkcache.annotate(this.id, bookmarkcache.CHARSET, val); });
@@ -79,7 +81,7 @@ var BookmarkCache = Module("BookmarkCache", XPCOM(Ci.nsINavBookmarkObserver), {
_loadBookmark: function loadBookmark(node) { _loadBookmark: function loadBookmark(node) {
if (node.uri == null) // How does this happen? if (node.uri == null) // How does this happen?
return false; return false;
let uri = util.newURI(node.uri); let uri = newURI(node.uri);
let keyword = services.bookmarks.getKeywordForBookmark(node.itemId); let keyword = services.bookmarks.getKeywordForBookmark(node.itemId);
let tags = services.tagging.getTagsForURI(uri, {}) || []; let tags = services.tagging.getTagsForURI(uri, {}) || [];
let post = BookmarkCache.getAnnotation(node.itemId, this.POST); let post = BookmarkCache.getAnnotation(node.itemId, this.POST);
@@ -96,7 +98,7 @@ var BookmarkCache = Module("BookmarkCache", XPCOM(Ci.nsINavBookmarkObserver), {
}, },
get: function (url) { get: function (url) {
let ids = services.bookmarks.getBookmarkIdsForURI(util.newURI(url), {}); let ids = services.bookmarks.getBookmarkIdsForURI(newURI(url), {});
for (let id in values(ids)) for (let id in values(ids))
if (id in this.bookmarks) if (id in this.bookmarks)
return this.bookmarks[id]; return this.bookmarks[id];
@@ -129,7 +131,7 @@ var BookmarkCache = Module("BookmarkCache", XPCOM(Ci.nsINavBookmarkObserver), {
*/ */
isBookmarked: function isBookmarked(uri) { isBookmarked: function isBookmarked(uri) {
if (isString(uri)) if (isString(uri))
uri = util.newURI(uri); uri = newURI(uri);
try { try {
return services.bookmarks return services.bookmarks
@@ -221,9 +223,10 @@ var BookmarkCache = Module("BookmarkCache", XPCOM(Ci.nsINavBookmarkObserver), {
getAnnotation: function getAnnotation(item, anno) getAnnotation: function getAnnotation(item, anno)
services.annotation.itemHasAnnotation(item, anno) ? services.annotation.itemHasAnnotation(item, anno) ?
services.annotation.getItemAnnotation(item, anno) : null, services.annotation.getItemAnnotation(item, anno) : null,
getFavicon: function getFavicon(uri) { getFavicon: function getFavicon(uri) {
try { try {
return services.favicon.getFaviconImageForPage(util.newURI(uri)).spec; return services.favicon.getFaviconImageForPage(newURI(uri)).spec;
} }
catch (e) { catch (e) {
return ""; return "";

View File

@@ -339,7 +339,7 @@ var IO = Module("io", {
*/ */
isJarURL: function isJarURL(url) { isJarURL: function isJarURL(url) {
try { try {
let uri = util.newURI(util.fixURI(url)); let uri = util.newURI(url);
if (uri instanceof Ci.nsIJARURI) if (uri instanceof Ci.nsIJARURI)
return uri; return uri;

View File

@@ -12,6 +12,8 @@ defineModule("main", {
require: ["config", "help", "highlight", "io", "overlay", "services", "util"] require: ["config", "help", "highlight", "io", "overlay", "services", "util"]
}, this); }, this);
const BASE = "resource://dactyl-content/";
/** /**
* @class ModuleBase * @class ModuleBase
* The base class for all modules. * The base class for all modules.
@@ -26,10 +28,7 @@ var ModuleBase = Class("ModuleBase", {
toString: function () "[module " + this.constructor.className + "]" toString: function () "[module " + this.constructor.className + "]"
}); });
config.loadStyles(); var Modules = function Modules(window) {
overlay.overlayWindow(config.overlayChrome, function _overlay(window) ({
ready: function onInit(document) {
/** /**
* @constructor Module * @constructor Module
* *
@@ -85,8 +84,6 @@ overlay.overlayWindow(config.overlayChrome, function _overlay(window) ({
Module.list = []; Module.list = [];
Module.constructors = {}; Module.constructors = {};
const BASE = "resource://dactyl-content/";
const create = window.Object.create || (function () { const create = window.Object.create || (function () {
window.__dactyl_eval_string = "(function (proto) ({ __proto__: proto }))"; window.__dactyl_eval_string = "(function (proto) ({ __proto__: proto }))";
JSMLoader.loadSubScript(BASE + "eval.js", window); JSMLoader.loadSubScript(BASE + "eval.js", window);
@@ -97,6 +94,9 @@ overlay.overlayWindow(config.overlayChrome, function _overlay(window) ({
return res; return res;
})(); })();
const BASES = [BASE, "resource://dactyl-local-content/"];
const jsmodules = { NAME: "jsmodules" }; const jsmodules = { NAME: "jsmodules" };
const modules = update(create(jsmodules), { const modules = update(create(jsmodules), {
yes_i_know_i_should_not_report_errors_in_these_branches_thanks: [], yes_i_know_i_should_not_report_errors_in_these_branches_thanks: [],
@@ -110,7 +110,7 @@ overlay.overlayWindow(config.overlayChrome, function _overlay(window) ({
Module: Module, Module: Module,
load: function load(script) { load: function load(script) {
for (let [i, base] in Iterator(prefix)) { for (let [i, base] in Iterator(BASES)) {
try { try {
JSMLoader.loadSubScript(base + script + ".js", modules, "UTF-8"); JSMLoader.loadSubScript(base + script + ".js", modules, "UTF-8");
return; return;
@@ -137,6 +137,7 @@ overlay.overlayWindow(config.overlayChrome, function _overlay(window) ({
let sandbox = Components.utils.Sandbox(window, { sandboxPrototype: proto || modules, wantXrays: false }); let sandbox = Components.utils.Sandbox(window, { sandboxPrototype: proto || modules, wantXrays: false });
// Hack: // Hack:
sandbox.Object = jsmodules.Object; sandbox.Object = jsmodules.Object;
sandbox.File = jsmodules.File;
sandbox.Math = jsmodules.Math; sandbox.Math = jsmodules.Math;
sandbox.__proto__ = proto || modules; sandbox.__proto__ = proto || modules;
return sandbox; return sandbox;
@@ -148,20 +149,28 @@ overlay.overlayWindow(config.overlayChrome, function _overlay(window) ({
get moduleList() this.ownPropertyValues.filter(function (mod) mod instanceof this.ModuleBase || mod.isLocalModule, this) get moduleList() this.ownPropertyValues.filter(function (mod) mod instanceof this.ModuleBase || mod.isLocalModule, this)
}); });
modules.plugins = create(modules); modules.plugins = create(modules);
modules.modules = modules; modules.modules = modules;
window.dactyl = { modules: modules }; return modules;
}
let prefix = [BASE, "resource://dactyl-local-content/"]; config.loadStyles();
overlay.overlayWindow(config.overlayChrome, function _overlay(window) ({
ready: function onInit(document) {
const modules = Modules(window);
window.dactyl = { modules: modules };
defineModule.time("load", null, function _load() { defineModule.time("load", null, function _load() {
config.modules.global config.modules.global
.forEach(function (name) defineModule.time("load", name, require, null, jsmodules, name)); .forEach(function (name) defineModule.time("load", name, require, null, modules.jsmodules, name));
config.modules.window config.modules.window
.forEach(function (name) defineModule.time("load", name, modules.load, modules, name)); .forEach(function (name) defineModule.time("load", name, modules.load, modules, name));
}, this); }, this);
}, },
load: function onLoad(document) { load: function onLoad(document) {
// This is getting to be horrible. --Kris // This is getting to be horrible. --Kris
@@ -184,19 +193,23 @@ overlay.overlayWindow(config.overlayChrome, function _overlay(window) ({
try { try {
if (module.className in loaded) if (module.className in loaded)
return; return;
if (module.className in seen) if (module.className in seen)
throw Error("Module dependency loop."); throw Error("Module dependency loop.");
Set.add(seen, module.className); Set.add(seen, module.className);
for (let dep in values(module.requires)) for (let dep in values(module.requires))
load(Module.constructors[dep], module.className); load(Module.constructors[dep], module.className);
defineModule.loadLog.push("Load" + (isString(prereq) ? " " + prereq + " dependency: " : ": ") + module.className); defineModule.loadLog.push("Load" + (isString(prereq) ? " " + prereq + " dependency: " : ": ") + module.className);
if (frame && frame.filename) if (frame && frame.filename)
defineModule.loadLog.push(" from: " + util.fixURI(frame.filename) + ":" + frame.lineNumber); defineModule.loadLog.push(" from: " + util.fixURI(frame.filename) + ":" + frame.lineNumber);
let obj = defineModule.time(module.className, "init", module); let obj = defineModule.time(module.className, "init", module);
Class.replaceProperty(modules, module.className, obj); Class.replaceProperty(modules, module.className, obj);
loaded[module.className] = true; loaded[module.className] = true;
if (loaded.dactyl && obj.signals) if (loaded.dactyl && obj.signals)
@@ -238,6 +251,7 @@ overlay.overlayWindow(config.overlayChrome, function _overlay(window) ({
mod.frobbed = true; mod.frobbed = true;
}); });
} }
defineModule.modules.forEach(function defModule(mod) { defineModule.modules.forEach(function defModule(mod) {
let names = Set(Object.keys(mod.INIT)); let names = Set(Object.keys(mod.INIT));
if ("init" in mod.INIT) if ("init" in mod.INIT)
@@ -269,20 +283,19 @@ overlay.overlayWindow(config.overlayChrome, function _overlay(window) ({
return modules[className] = modules[className]; return modules[className] = modules[className];
}); });
}); });
},
modules.events.listen(window, "unload", function onUnload() { unload: function unload(window) {
window.removeEventListener("unload", onUnload.wrapped, false);
overlay.windows = overlay.windows.filter(function (w) w != window); overlay.windows = overlay.windows.filter(function (w) w != window);
for each (let mod in modules.moduleList.reverse()) { for each (let mod in this.modules.moduleList.reverse()) {
mod.stale = true; mod.stale = true;
if ("destroy" in mod) if ("destroy" in mod)
util.trapErrors("destroy", mod); util.trapErrors("destroy", mod);
} }
}, false);
}, },
visible: function visible(window) { visible: function visible(window) {
// Module.list.forEach(load); // Module.list.forEach(load);
this.frob("load"); this.frob("load");

View File

@@ -52,6 +52,63 @@ var Overlay = Module("Overlay", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakReferen
id: Class.Memoize(function () config.addon.id), id: Class.Memoize(function () config.addon.id),
/**
* Adds an event listener for this session and removes it on
* dactyl shutdown.
*
* @param {Element} target The element on which to listen.
* @param {string} event The event to listen for.
* @param {function} callback The function to call when the event is received.
* @param {boolean} capture When true, listen during the capture
* phase, otherwise during the bubbling phase.
* @param {boolean} allowUntrusted When true, allow capturing of
* untrusted events.
*/
listen: function (target, event, callback, capture, allowUntrusted) {
let doc = target.ownerDocument || target.document || target;
let listeners = this.getData(doc, "listeners");
if (!isObject(event))
var [self, events] = [null, array.toObject([[event, callback]])];
else
[self, events] = [event, event[callback || "events"]];
for (let [event, callback] in Iterator(events)) {
let args = [Cu.getWeakReference(target),
event,
util.wrapCallback(callback, self),
capture,
allowUntrusted];
target.addEventListener.apply(target, args.slice(1));
listeners.push(args);
}
},
/**
* Remove an event listener.
*
* @param {Element} target The element on which to listen.
* @param {string} event The event to listen for.
* @param {function} callback The function to call when the event is received.
* @param {boolean} capture When true, listen during the capture
* phase, otherwise during the bubbling phase.
*/
unlisten: function (target, event, callback, capture) {
let doc = target.ownerDocument || target.document || target;
let listeners = this.getData(doc, "listeners");
if (event === true)
target = null;
this.setData(doc, "listeners", listeners.filter(function (args) {
if (target == null || args[0].get() == target && args[1] == event && args[2].wrapped == callback && args[3] == capture) {
args[0].get().removeEventListener.apply(args[0].get(), args.slice(1));
return false;
}
return !args[0].get();
}));
},
cleanup: function cleanup(reason) { cleanup: function cleanup(reason) {
for (let doc in util.iterDocuments()) { for (let doc in util.iterDocuments()) {
for (let elem in values(this.getData(doc, "overlayElements"))) for (let elem in values(this.getData(doc, "overlayElements")))
@@ -65,6 +122,8 @@ var Overlay = Module("Overlay", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakReferen
for (let callback in values(this.getData(doc, "cleanup"))) for (let callback in values(this.getData(doc, "cleanup")))
util.trapErrors(callback, doc, reason); util.trapErrors(callback, doc, reason);
this.unlisten(doc, true);
delete doc[this.id]; delete doc[this.id];
delete doc.defaultView[this.id]; delete doc.defaultView[this.id];
} }
@@ -265,7 +324,7 @@ var Overlay = Module("Overlay", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakReferen
if (!callable(val) || Function.prototype.toString(val).indexOf(sentinel) < 0) if (!callable(val) || Function.prototype.toString(val).indexOf(sentinel) < 0)
Class.replaceProperty(this, k, val); Class.replaceProperty(this, k, val);
else { else {
let package_ = util.newURI(util.fixURI(Components.stack.caller.filename)).host; let package_ = util.newURI(Components.stack.caller.filename).host;
util.reportError(Error(_("error.monkeyPatchOverlay", package_))); util.reportError(Error(_("error.monkeyPatchOverlay", package_)));
util.dactyl.echoerr(_("error.monkeyPatchOverlay", package_)); util.dactyl.echoerr(_("error.monkeyPatchOverlay", package_));
} }

View File

@@ -294,7 +294,7 @@ var Storage = Module("Storage", {
* current directory. @default true * current directory. @default true
* @param {string} charset The charset of the file. @default File.defaultEncoding * @param {string} charset The charset of the file. @default File.defaultEncoding
*/ */
this.File = Class("File", { var File = Class("File", {
init: function (path, checkPWD, charset) { init: function (path, checkPWD, charset) {
let file = services.File(); let file = services.File();

View File

@@ -629,7 +629,7 @@ var Util = Module("Util", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakReference]),
getFile: function getFile(uri) { getFile: function getFile(uri) {
try { try {
if (isString(uri)) if (isString(uri))
uri = util.newURI(util.fixURI(uri)); uri = util.newURI(uri);
if (uri instanceof Ci.nsIFileURL) if (uri instanceof Ci.nsIFileURL)
return File(uri.file); return File(uri.file);
@@ -837,6 +837,10 @@ var Util = Module("Util", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakReference]),
* @returns {nsIURI} * @returns {nsIURI}
*/ */
newURI: function newURI(uri, charset, base) { newURI: function newURI(uri, charset, base) {
let idx = uri.lastIndexOf(" -> ");
if (~idx)
uri = uri.slice(idx + 4);
let res = this.withProperErrors("newURI", services.io, uri, charset, base); let res = this.withProperErrors("newURI", services.io, uri, charset, base);
res instanceof Ci.nsIURL; res instanceof Ci.nsIURL;
return res; return res;