# HG changeset patch
# User Skurikhin Aleksandr <
a.sku...@gmail.com>
# Date 1326181742 -14400
# Node ID f0159c1aca6c5d97c2c6edb9a762b479028aeba3
# Parent 14cf65d4aac2181c5e6722e682a91cfb47accf90
:bmark -F Folder support while adding bookmarks
diff -r 14cf65d4aac2 -r f0159c1aca6c common/content/bookmarks.js
--- a/common/content/bookmarks.js Sun Jan 08 15:25:13 2012 -0500
+++ b/common/content/bookmarks.js Tue Jan 10 11:49:02 2012 +0400
@@ -6,6 +6,25 @@
// given in the LICENSE.txt file included with this file.
/* use strict */
+function getFolderIdByPath(path) {
+
+ /**
+ * Gets id of a folder by given full path in format: path/to/
folder <- root
+ * If folder doesn't exist, creates it.
+ *
+ * @param {string} path Path to a folder in format: path/to/
folder
+ * @returns {int} Id of a founded or created folder
+ */
+
+ for (var b in bookmarkcache)
+ if (b.folder.path === path)
+ return
b.folder.id;
+
+ var folderTitle = path.split('/')[0];
+ var parents = path.split('/').slice(1);
+ return
services.bookmarks.createFolder(getFolderIdByPath(parents.join('/')),
folderTitle, -1);
+}
+
// also includes methods for dealing with keywords and search engines
var Bookmarks = Module("bookmarks", {
init: function () {
@@ -64,10 +83,10 @@
* @returns {boolean} True if the bookmark was updated, false if
a
* new bookmark was added.
*/
- add: function add(unfiled, title, url, keyword, tags, force) {
+ add: function add(unfiled, title, url, keyword, tags, force,
folder) {
// FIXME
if (isObject(unfiled))
- var { id, unfiled, title, url, keyword, tags, post,
charset, force } = unfiled;
+ var { id, unfiled, title, url, keyword, tags, post,
charset, force, folder} = unfiled;
let uri = util.createURI(url);
if (id != null)
@@ -85,7 +104,6 @@
PlacesUtils.tagging.untagURI(uri, null);
PlacesUtils.tagging.tagURI(uri, tags);
}
-
let updated = !!bmark;
if (bmark == undefined)
bmark = bookmarkcache.bookmarks[
@@ -107,7 +125,8 @@
bmark.post = post;
if (keyword)
bmark.keyword = keyword;
-
+ if (folder)
+ bmark.folder = folder;
return updated;
},
@@ -404,6 +423,18 @@
}, {
commands: function () {
// TODO: Clean this up.
+
+ const folder = {
+ names: ["-folder", "-F"],
+ description: "A folder where this bookmark will be
added",
+ completer: function folder(context, args) {
+ context.generate = function () array(b.folder.path
for (b in bookmarkcache)).uniq().array;
+ context.keys.text = util.identity;
+ context.keys.description = function () ""
+ },
+ type: CommandOption.STRING
+ };
+
const tags = {
names: ["-tags", "-T"],
description: "A comma-separated list of tags",
@@ -457,8 +488,8 @@
function (args) {
dactyl.assert(!args.bang || args["-id"] == null,
_("bookmark.bangOrID"));
-
let opts = {
+ folder: getFolderIdByPath(args["-folder"]) ||
null,
force: args.bang,
unfiled: false,
id: args["-id"],
@@ -469,7 +500,6 @@
title: args["-title"] || (args.length === 0 ?
buffer.title : null),
url: args.length === 0 ? buffer.uri.spec :
args[0]
};
-
let updated = bookmarks.add(opts);
let action = updated ? "updated" : "added";
@@ -491,7 +521,7 @@
}
completion.bookmark(context, args["-tags"],
{ keyword: args["-keyword"], title: args["-title"] });
},
- options: [keyword, title, tags, post,
+ options: [keyword, title, tags, post, folder,
{
names: ["-charset", "-c"],
description: "The character encoding of the
bookmark",
diff -r 14cf65d4aac2 -r f0159c1aca6c common/modules/bookmarkcache.jsm
--- a/common/modules/bookmarkcache.jsm Sun Jan 08 15:25:13 2012 -0500
+++ b/common/modules/bookmarkcache.jsm Tue Jan 10 11:49:02 2012 +0400
@@ -21,8 +21,29 @@
}
}
-var Bookmark = Struct("url", "title", "icon", "post", "keyword",
"tags", "charset", "id");
+function getParentsForItem(itemId) {
+
+ /**
+ * Looks for parent folders for a given item's id
+ *
+ * @param {int} itemId Item's id which parents we are looking for
+ * @returns {array} List of parent's titles
+ */
+
+ var parents = [];
+ parents.toString = function () this.join("/");
+
+ let id = itemId, parent, title;
+ while ((id = services.bookmarks.getFolderIdForItem(id)) &&
+ (title = services.bookmarks.getItemTitle(id)))
+ parents.push(title);
+
+ return parents;
+}
+
+var Bookmark = Struct("url", "title", "icon", "post", "keyword",
"tags", "charset", "id", "folder");
var Keyword = Struct("keyword", "title", "icon", "url");
+var Folder = Struct("id", "path");
Bookmark.defaultValue("icon", function ()
BookmarkCache.getFavicon(this.url));
update(Bookmark.prototype, {
get extra() [
@@ -46,15 +67,10 @@
},
get folder() {
- let res = [];
- res.toString = function () this.join("/");
+ let folderPath = getParentsForItem(
this.id).toString();
+ let folderId =
services.bookmarks.getFolderIdForItem(
this.id);
- let id =
this.id, parent, title;
- while ((id = services.bookmarks.getFolderIdForItem(id)) &&
- (title = services.bookmarks.getItemTitle(id)))
- res.push(title);
-
- return res.reverse();
+ return Folder(folderId, folderPath);
}
})
Bookmark.prototype.members.uri = Bookmark.prototype.members.url;
@@ -69,6 +85,7 @@
if (val)
services.tagging.tagURI(this.uri, val);
});
+Bookmark.setter("folder", function (val)
{ services.bookmarks.moveItem(
this.id, val, -1) });
var name = "bookmark-cache";
@@ -111,7 +128,8 @@
let post = BookmarkCache.getAnnotation(node.itemId,
this.POST);
let charset = BookmarkCache.getAnnotation(node.itemId,
this.CHARSET);
- return Bookmark(node.uri, node.title, node.icon &&
node.icon.spec, post, keyword, tags, charset, node.itemId);
+ let folder = node.folder;
+ return Bookmark(node.uri, node.title, node.icon &&
node.icon.spec, post, keyword, tags, charset, node.itemId, folder);
},
annotate: function (id, key, val, timespan) {