mirror of
https://github.com/gryf/pentadactyl-pm.git
synced 2026-02-05 19:35:46 +01:00
Add ;S ‘add search keyword’ hint mode.
This commit is contained in:
@@ -10,7 +10,7 @@ defineModule("bookmarkcache", {
|
||||
require: ["services", "storage", "util"]
|
||||
});
|
||||
|
||||
const Bookmark = Struct("url", "title", "icon", "keyword", "tags", "id");
|
||||
const Bookmark = Struct("url", "title", "icon", "post", "keyword", "tags", "id");
|
||||
const Keyword = Struct("keyword", "title", "icon", "url");
|
||||
Bookmark.defaultValue("icon", function () BookmarkCache.getFavicon(this.url));
|
||||
Bookmark.prototype.__defineGetter__("extra", function () [
|
||||
@@ -18,12 +18,15 @@ Bookmark.prototype.__defineGetter__("extra", function () [
|
||||
["tags", this.tags.join(", "), "Tag"]
|
||||
].filter(function (item) item[1]));
|
||||
|
||||
const bookmarks = services.get("bookmarks");
|
||||
const history = services.get("history");
|
||||
const tagging = services.get("tagging");
|
||||
const name = "bookmark-cache";
|
||||
const annotation = services.get("annotation");
|
||||
const bookmarks = services.get("bookmarks");
|
||||
const history = services.get("history");
|
||||
const tagging = services.get("tagging");
|
||||
const name = "bookmark-cache";
|
||||
|
||||
const BookmarkCache = Module("BookmarkCache", XPCOM(Ci.nsINavBookmarkObserver), {
|
||||
POST: "bookmarkProperties/POSTData",
|
||||
|
||||
init: function init() {
|
||||
bookmarks.addObserver(this, false);
|
||||
},
|
||||
@@ -32,17 +35,14 @@ const BookmarkCache = Module("BookmarkCache", XPCOM(Ci.nsINavBookmarkObserver),
|
||||
|
||||
get bookmarks() Class.replaceProperty(this, "bookmarks", this.load()),
|
||||
|
||||
get keywords() array.toObject([[b.keyword, b] for (b in this) if (b.keyword)]),
|
||||
|
||||
rootFolders: ["toolbarFolder", "bookmarksMenuFolder", "unfiledBookmarksFolder"]
|
||||
.map(function (s) bookmarks[s]),
|
||||
|
||||
_deleteBookmark: function deleteBookmark(id) {
|
||||
let length = this.bookmarks.length;
|
||||
let result;
|
||||
this.bookmarks = this.bookmarks.filter(function (item) {
|
||||
if (item.id == id)
|
||||
result = item;
|
||||
return item.id != id;
|
||||
});
|
||||
let result = this.bookmarks[item.id] || null;
|
||||
delete this.bookmarks[id];
|
||||
return result;
|
||||
},
|
||||
|
||||
@@ -52,7 +52,8 @@ const BookmarkCache = Module("BookmarkCache", XPCOM(Ci.nsINavBookmarkObserver),
|
||||
let uri = util.newURI(node.uri);
|
||||
let keyword = bookmarks.getKeywordForBookmark(node.itemId);
|
||||
let tags = tagging.getTagsForURI(uri, {}) || [];
|
||||
return Bookmark(node.uri, node.title, node.icon && node.icon.spec, keyword, tags, node.itemId);
|
||||
let post = BookmarkCache.getAnnotation(node.itemId, this.POST);
|
||||
return Bookmark(node.uri, node.title, node.icon && node.icon.spec, post, keyword, tags, node.itemId);
|
||||
},
|
||||
|
||||
readBookmark: function readBookmark(id) {
|
||||
@@ -83,11 +84,9 @@ const BookmarkCache = Module("BookmarkCache", XPCOM(Ci.nsINavBookmarkObserver),
|
||||
return this.rootFolders.indexOf(root) >= 0;
|
||||
},
|
||||
|
||||
get keywords() [Keyword(k.keyword, k.title, k.icon, k.url) for ([, k] in Iterator(this.bookmarks)) if (k.keyword)],
|
||||
|
||||
// Should be made thread safe.
|
||||
load: function load() {
|
||||
let bookmarks = [];
|
||||
let bookmarks = {};
|
||||
|
||||
let folders = this.rootFolders.slice();
|
||||
let query = history.getNewQuery();
|
||||
@@ -105,7 +104,7 @@ const BookmarkCache = Module("BookmarkCache", XPCOM(Ci.nsINavBookmarkObserver),
|
||||
if (node.type == node.RESULT_TYPE_FOLDER) // folder
|
||||
folders.push(node.itemId);
|
||||
else if (node.type == node.RESULT_TYPE_URI) // bookmark
|
||||
bookmarks.push(this._loadBookmark(node));
|
||||
bookmarks[node.itemId] = this._loadBookmark(node);
|
||||
}
|
||||
|
||||
// close a container after using it!
|
||||
@@ -119,7 +118,7 @@ const BookmarkCache = Module("BookmarkCache", XPCOM(Ci.nsINavBookmarkObserver),
|
||||
if (bookmarks.getItemType(itemId) == bookmarks.TYPE_BOOKMARK) {
|
||||
if (this.isBookmark(itemId)) {
|
||||
let bmark = this._loadBookmark(this.readBookmark(itemId));
|
||||
this.bookmarks.push(bmark);
|
||||
this.bookmarks[bmark.id] = bmark;
|
||||
storage.fireEvent(name, "add", bmark);
|
||||
}
|
||||
}
|
||||
@@ -131,8 +130,12 @@ const BookmarkCache = Module("BookmarkCache", XPCOM(Ci.nsINavBookmarkObserver),
|
||||
},
|
||||
onItemChanged: function onItemChanged(itemId, property, isAnnotation, value) {
|
||||
if (isAnnotation)
|
||||
return;
|
||||
let bookmark = bookmarkcache.bookmarks.filter(function (item) item.id == itemId)[0];
|
||||
if (property === this.POST)
|
||||
[property, value] = ["post", BookmarkCache.getAnnotation(itemId, this.POST)];
|
||||
else
|
||||
return;
|
||||
|
||||
let bookmark = this.bookmarks[itemId];
|
||||
if (bookmark) {
|
||||
if (property == "tags")
|
||||
value = tagging.getTagsForURI(util.newURI(bookmark.url), {});
|
||||
@@ -143,6 +146,9 @@ const BookmarkCache = Module("BookmarkCache", XPCOM(Ci.nsINavBookmarkObserver),
|
||||
}
|
||||
}
|
||||
}, {
|
||||
getAnnotation: function getAnnotation(item, anno)
|
||||
annotation.itemHasAnnotation(item, anno) ?
|
||||
annotation.getItemAnnotation(item, anno) : null,
|
||||
getFavicon: function getFavicon(uri) {
|
||||
try {
|
||||
return service.get("favicon").getFaviconImageForPage(util.newURI(uri)).spec;
|
||||
|
||||
@@ -18,6 +18,7 @@ const Services = Module("Services", {
|
||||
this.classes = {};
|
||||
this.services = {};
|
||||
|
||||
this.add("annotation", "@mozilla.org/browser/annotation-service;1", Ci.nsIAnnotationService);
|
||||
this.add("appStartup", "@mozilla.org/toolkit/app-startup;1", Ci.nsIAppStartup);
|
||||
this.add("autoCompleteSearch", "@mozilla.org/autocomplete/search;1?name=history", Ci.nsIAutoCompleteSearch);
|
||||
this.add("bookmarks", "@mozilla.org/browser/nav-bookmarks-service;1", Ci.nsINavBookmarksService);
|
||||
|
||||
@@ -246,6 +246,10 @@ const Util = Module("Util", {
|
||||
util.dump((msg || "Stack") + "\n" + stack + "\n");
|
||||
},
|
||||
|
||||
editableInputs: set(["date", "datetime", "datetime-local", "email", "file",
|
||||
"month", "number", "password", "range", "search",
|
||||
"tel", "text", "time", "url", "week"]),
|
||||
|
||||
/**
|
||||
* Converts HTML special characters in <b>str</b> to the equivalent HTML
|
||||
* entities.
|
||||
@@ -521,9 +525,7 @@ const Util = Module("Util", {
|
||||
* @returns {nsIURI}
|
||||
*/
|
||||
// FIXME: createURI needed too?
|
||||
newURI: function (uri) {
|
||||
return services.get("io").newURI(uri, null, null);
|
||||
},
|
||||
newURI: function (uri, charset, base) services.get("io").newURI(uri, charset, base),
|
||||
|
||||
/**
|
||||
* Pretty print a JavaScript object. Use HTML markup to color certain items
|
||||
@@ -641,6 +643,50 @@ const Util = Module("Util", {
|
||||
return color ? string : [s for each (s in string)].join("");
|
||||
},
|
||||
|
||||
/**
|
||||
* Parses the fields of a form and returns a URL/POST-data pair
|
||||
* that is the equivalent of submitting the form.
|
||||
*
|
||||
* @param {nsINode} field One of the fields of the given form.
|
||||
*/
|
||||
// Nuances gleaned from browser.jar/content/browser/browser.js
|
||||
parseForm: function parseForm(field) {
|
||||
function encode(name, value, param) {
|
||||
if (param)
|
||||
value = "%s";
|
||||
if (post)
|
||||
return name + "=" + value;
|
||||
return encodeURIComponent(name) + "=" + (param ? value : encodeURIComponent(value));
|
||||
}
|
||||
|
||||
let form = field.form;
|
||||
let doc = form.ownerDocument;
|
||||
let charset = doc.charset;
|
||||
let uri = util.newURI(doc.baseURI.replace(/\?.*/, ""), charset);
|
||||
let url = util.newURI(form.action, charset, uri).spec;
|
||||
|
||||
let post = form.method.toUpperCase() == "POST";
|
||||
|
||||
let elems = [];
|
||||
if (field instanceof Ci.nsIDOMHTMLInputElement && field.type == "submit")
|
||||
elems.push(encode(field.name, field.value));
|
||||
|
||||
for (let [,elem] in iter(form.elements)) {
|
||||
if (set.has(util.editableInputs, elem.type)
|
||||
|| /^(?:hidden|textarea)$/.test(elem.type)
|
||||
|| elem.checked && /^(?:checkbox|radio)$/.test(elem.type))
|
||||
elems.push(encode(elem.name, elem.value, elem === field));
|
||||
else if (elem instanceof Ci.nsIDOMHTMLSelectElement) {
|
||||
for (let [,opt] in Iterator(elem.options))
|
||||
if (opt.selected)
|
||||
elems.push(encode(elem.name, opt.value));
|
||||
}
|
||||
}
|
||||
if (post)
|
||||
return [url, elems.map(encodeURIComponent).join('&'), elems];
|
||||
return [url + "?" + elems.join('&'), null];
|
||||
},
|
||||
|
||||
/**
|
||||
* A generator that returns the values between <b>start</b> and <b>end</b>,
|
||||
* in <b>step</b> increments.
|
||||
|
||||
Reference in New Issue
Block a user