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

Make Bookmark objects live writeable.

This commit is contained in:
Kris Maglione
2010-12-10 01:38:39 -05:00
parent 38c2d07ef1
commit 36205c6c0e
7 changed files with 57 additions and 26 deletions

View File

@@ -57,11 +57,10 @@ const Bookmarks = Module("bookmarks", {
try { try {
let uri = util.createURI(url); let uri = util.createURI(url);
if (!force && this.isBookmarked(uri.spec)) if (!force && this.isBookmarked(uri.spec))
for (let bmark in bookmarkcache) for (var bmark in bookmarkcache)
if (bmark.url == uri.spec) { if (bmark.url == uri.spec) {
var id = bmark.id;
if (title) if (title)
services.bookmarks.setItemTitle(id, title); bmark.title = title;
break; break;
} }
@@ -69,17 +68,18 @@ const Bookmarks = Module("bookmarks", {
PlacesUtils.tagging.untagURI(uri, null); PlacesUtils.tagging.untagURI(uri, null);
PlacesUtils.tagging.tagURI(uri, tags); PlacesUtils.tagging.tagURI(uri, tags);
} }
if (id == undefined) if (bmark == undefined)
id = services.bookmarks.insertBookmark( bmark = bookmarkcache.bookmarks[
services.bookmarks.insertBookmark(
services.bookmarks[unfiled ? "unfiledBookmarksFolder" : "bookmarksMenuFolder"], services.bookmarks[unfiled ? "unfiledBookmarksFolder" : "bookmarksMenuFolder"],
uri, -1, title || url); uri, -1, title || url)];
if (!id) if (!bmark)
return false; return false;
if (post !== undefined) if (post !== undefined)
PlacesUtils.setPostDataForBookmark(id, post); bmark.post = post;
if (keyword) if (keyword)
services.bookmarks.setKeywordForBookmark(id, keyword); bmark.keyword = keyword;
} }
catch (e) { catch (e) {
dactyl.log(e, 0); dactyl.log(e, 0);

View File

@@ -527,9 +527,8 @@ const CompletionContext = Class("CompletionContext", {
// A simple binary search to find the longest substring // A simple binary search to find the longest substring
// of the given string which also matches the current // of the given string which also matches the current
// item's text. // item's text.
var m, len = substring.length; let len = substring.length;
var n = substring.length; let i = 0, m, n = len;
var i = 0;
while (n) { while (n) {
m = Math.floor(n / 2); m = Math.floor(n / 2);
let keep = compare(fixCase(item.text), substring.substring(0, i + m)); let keep = compare(fixCase(item.text), substring.substring(0, i + m));

View File

@@ -887,7 +887,7 @@ function Struct() {
let args = Array.slice(arguments); let args = Array.slice(arguments);
const Struct = Class("Struct", StructBase, { const Struct = Class("Struct", StructBase, {
length: args.length, length: args.length,
members: args members: array.toObject(args.map(function (v, k) [v, k]))
}); });
args.forEach(function (name, i) { args.forEach(function (name, i) {
Struct.prototype.__defineGetter__(name, function () this[i]); Struct.prototype.__defineGetter__(name, function () this[i]);
@@ -909,7 +909,7 @@ let StructBase = Class("StructBase", Array, {
// Iterator over our named members // Iterator over our named members
__iterator__: function () { __iterator__: function () {
let self = this; let self = this;
return ([k, self[k]] for (k in values(self.members))) return ([k, self[k]] for (k in keys(self.members)))
} }
}, { }, {
fromArray: function (ary) { fromArray: function (ary) {
@@ -928,7 +928,7 @@ let StructBase = Class("StructBase", Array, {
* the default value. * the default value.
*/ */
defaultValue: function (key, val) { defaultValue: function (key, val) {
let i = this.prototype.members.indexOf(key); let i = this.prototype.members[key];
this.prototype.__defineGetter__(i, function () (this[i] = val.call(this))); this.prototype.__defineGetter__(i, function () (this[i] = val.call(this)));
this.prototype.__defineSetter__(i, function (value) this.prototype.__defineSetter__(i, function (value)
Class.replaceProperty(this, i, value)); Class.replaceProperty(this, i, value));

View File

@@ -13,10 +13,25 @@ defineModule("bookmarkcache", {
const Bookmark = Struct("url", "title", "icon", "post", "keyword", "tags", "id"); const Bookmark = Struct("url", "title", "icon", "post", "keyword", "tags", "id");
const Keyword = Struct("keyword", "title", "icon", "url"); const Keyword = Struct("keyword", "title", "icon", "url");
Bookmark.defaultValue("icon", function () BookmarkCache.getFavicon(this.url)); Bookmark.defaultValue("icon", function () BookmarkCache.getFavicon(this.url));
Bookmark.setter = function (key, func) this.prototype.__defineSetter__(key, func);
Bookmark.prototype.__defineGetter__("extra", function () [ Bookmark.prototype.__defineGetter__("extra", function () [
["keyword", this.keyword, "Keyword"], ["keyword", this.keyword, "Keyword"],
["tags", this.tags.join(", "), "Tag"] ["tags", this.tags.join(", "), "Tag"]
].filter(function (item) item[1])); ].filter(function (item) item[1]));
Bookmark.setter("url", function (val) {
let tags = this.tags;
this.tags = null;
services.bookmarks.changeBookmarkURI(this.id, val);
this.tags = tags;
});
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("keyword", function (val) { services.bookmarks.setKeywordForBookmark(this.id, val); });
Bookmark.setter("tags", function (val) {
services.tagging.untagURI(this.uri, null);
if (val)
services.tagging.tagURI(this.uri, val);
});
const name = "bookmark-cache"; const name = "bookmark-cache";
@@ -52,6 +67,13 @@ const BookmarkCache = Module("BookmarkCache", XPCOM(Ci.nsINavBookmarkObserver),
return Bookmark(node.uri, node.title, node.icon && node.icon.spec, post, keyword, tags, node.itemId); return Bookmark(node.uri, node.title, node.icon && node.icon.spec, post, keyword, tags, node.itemId);
}, },
annotate: function (id, key, val) {
if (val)
services.annotation.setItemAnnotation(id, key, val, 0, services.annotation.EXPIRE_NEVER);
else if (services.annotation.itemHasAnnotation(id, key))
services.annotation.removeItemAnnotation(id, key);
},
get: function (url) { get: function (url) {
let ids = services.bookmarks.getBookmarkIdsForURI(util.newURI(url), {}); let ids = services.bookmarks.getBookmarkIdsForURI(util.newURI(url), {});
for (let id in values(ids)) for (let id in values(ids))
@@ -142,7 +164,7 @@ const BookmarkCache = Module("BookmarkCache", XPCOM(Ci.nsINavBookmarkObserver),
if (property == "tags") if (property == "tags")
value = services.tagging.getTagsForURI(util.newURI(bookmark.url), {}); value = services.tagging.getTagsForURI(util.newURI(bookmark.url), {});
if (property in bookmark) { if (property in bookmark) {
bookmark[property] = value; bookmark[bookmark.members[property]] = value;
storage.fireEvent(name, "change", { __proto__: bookmark, changed: property }); storage.fireEvent(name, "change", { __proto__: bookmark, changed: property });
} }
} }

View File

@@ -15,7 +15,7 @@ const Highlight = Struct("class", "selector", "sites",
"default", "value", "agent", "default", "value", "agent",
"base", "baseClass", "style"); "base", "baseClass", "style");
Highlight.liveProperty = function (name, prop) { Highlight.liveProperty = function (name, prop) {
let i = this.prototype.members.indexOf(name); let i = this.prototype.members[name];
this.prototype.__defineGetter__(name, function () this[i]); this.prototype.__defineGetter__(name, function () this[i]);
this.prototype.__defineSetter__(name, function (val) { this.prototype.__defineSetter__(name, function (val) {
this[i] = val; this[i] = val;

View File

@@ -18,7 +18,7 @@ const namespace = "@namespace html " + XHTML.uri.quote() + ";\n" +
const Sheet = Struct("name", "id", "sites", "css", "system", "agent"); const Sheet = Struct("name", "id", "sites", "css", "system", "agent");
Sheet.liveProperty = function (name) { Sheet.liveProperty = function (name) {
let i = this.prototype.members.indexOf(name); let i = this.prototype.members[name];
this.prototype.__defineGetter__(name, function () this[i]); this.prototype.__defineGetter__(name, function () this[i]);
this.prototype.__defineSetter__(name, function (val) { this.prototype.__defineSetter__(name, function (val) {
this[i] = val; this[i] = val;

View File

@@ -11,14 +11,17 @@ defineModule("template", {
}); });
default xml namespace = XHTML; default xml namespace = XHTML;
XML.ignoreWhiteSpace = true; function fixXML() {
XML.prettyPrinting = false; XML.ignoreWhiteSpace = false;
XML.prettyPrinting = false;
}
const Template = Module("Template", { const Template = Module("Template", {
add: function add(a, b) a + b, add: function add(a, b) a + b,
join: function join(c) function (a, b) a + c + b, join: function join(c) function (a, b) a + c + b,
map: function map(iter, func, sep, interruptable) { map: function map(iter, func, sep, interruptable) {
XML.ignoreWhitespace = false; XML.prettyPrinting = false;
if (iter.length) // FIXME: Kludge? if (iter.length) // FIXME: Kludge?
iter = array.iterValues(iter); iter = array.iterValues(iter);
let ret = <></>; let ret = <></>;
@@ -78,6 +81,7 @@ const Template = Module("Template", {
var desc = this.processor[1].call(this, item, item.description); var desc = this.processor[1].call(this, item, item.description);
} }
XML.ignoreWhitespace = false; XML.prettyPrinting = false;
// <e4x> // <e4x>
return <div highlight={highlightGroup || "CompItem"} style="white-space: nowrap"> return <div highlight={highlightGroup || "CompItem"} style="white-space: nowrap">
<!-- The non-breaking spaces prevent empty elements <!-- The non-breaking spaces prevent empty elements
@@ -108,6 +112,7 @@ const Template = Module("Template", {
// if "processStrings" is true, any passed strings will be surrounded by " and // if "processStrings" is true, any passed strings will be surrounded by " and
// any line breaks are displayed as \n // any line breaks are displayed as \n
highlight: function highlight(arg, processStrings, clip) { highlight: function highlight(arg, processStrings, clip) {
XML.ignoreWhitespace = false; XML.prettyPrinting = false;
// some objects like window.JSON or getBrowsers()._browsers need the try/catch // some objects like window.JSON or getBrowsers()._browsers need the try/catch
try { try {
let str = clip ? util.clip(String(arg), clip) : String(arg); let str = clip ? util.clip(String(arg), clip) : String(arg);
@@ -173,6 +178,7 @@ const Template = Module("Template", {
}, },
highlightSubstrings: function highlightSubstrings(str, iter, highlight) { highlightSubstrings: function highlightSubstrings(str, iter, highlight) {
XML.ignoreWhitespace = false; XML.prettyPrinting = false;
if (typeof str == "xml") if (typeof str == "xml")
return str; return str;
if (str == "") if (str == "")
@@ -205,6 +211,7 @@ const Template = Module("Template", {
</>, </>,
jumps: function jumps(index, elems) { jumps: function jumps(index, elems) {
XML.ignoreWhitespace = false; XML.prettyPrinting = false;
// <e4x> // <e4x>
return <table> return <table>
<tr style="text-align: left;" highlight="Title"> <tr style="text-align: left;" highlight="Title">
@@ -224,6 +231,7 @@ const Template = Module("Template", {
}, },
options: function options(title, opts) { options: function options(title, opts) {
XML.ignoreWhitespace = false; XML.prettyPrinting = false;
// <e4x> // <e4x>
return <table> return <table>
<tr highlight="Title" align="left"> <tr highlight="Title" align="left">
@@ -255,6 +263,7 @@ const Template = Module("Template", {
} }
} }
XML.ignoreWhitespace = false; XML.prettyPrinting = false;
return <a xmlns:dactyl={NS} dactyl:command="buffer.viewSource" return <a xmlns:dactyl={NS} dactyl:command="buffer.viewSource"
href={url} line={frame.lineNumber} href={url} line={frame.lineNumber}
highlight="URL">{ highlight="URL">{
@@ -263,8 +272,8 @@ const Template = Module("Template", {
}, },
table: function table(title, data, indent) { table: function table(title, data, indent) {
let table = XML.ignoreWhitespace = false; XML.prettyPrinting = false;
// <e4x> let table = // <e4x>
<table> <table>
<tr highlight="Title" align="left"> <tr highlight="Title" align="left">
<th colspan="2">{title}</th> <th colspan="2">{title}</th>
@@ -285,6 +294,7 @@ const Template = Module("Template", {
tabular: function tabular(headings, style, iter) { tabular: function tabular(headings, style, iter) {
// TODO: This might be mind-bogglingly slow. We'll see. // TODO: This might be mind-bogglingly slow. We'll see.
XML.ignoreWhitespace = false; XML.prettyPrinting = false;
// <e4x> // <e4x>
return <table> return <table>
<tr highlight="Title" align="left"> <tr highlight="Title" align="left">
@@ -307,6 +317,7 @@ const Template = Module("Template", {
}, },
usage: function usage(iter) { usage: function usage(iter) {
XML.ignoreWhitespace = false; XML.prettyPrinting = false;
// <e4x> // <e4x>
return <table> return <table>
{ {
@@ -314,10 +325,9 @@ const Template = Module("Template", {
<tr> <tr>
<td style="padding-right: 20px" highlight="Usage">{ <td style="padding-right: 20px" highlight="Usage">{
let (name = item.name || item.names[0], frame = item.definedAt) let (name = item.name || item.names[0], frame = item.definedAt)
!frame ? name : <> !frame ? name :
<span highlight="Title">{name}</span>&#xa0; <span highlight="Title">{name}</span> + <> </> +
<span highlight="LineInfo">Defined at&#xa0;{template.sourceLink(frame)}</span> <span highlight="LineInfo">Defined at {template.sourceLink(frame)}</span>
</>
}</td> }</td>
<td>{item.description}</td> <td>{item.description}</td>
</tr>) </tr>)