makes the bookmarks toolbar display bookmark titles generated by the microsummary service or extensions and stored in the bookmarks/generatedTitle annotation

bug=333052
r=annie.sullivan@gmail.com
sr=ben@mozilla.org


git-svn-id: svn://10.0.0.236/trunk@194042 18797224-902f-48f8-a5cc-f745e15eee43
This commit is contained in:
myk%mozilla.org
2006-04-10 20:56:37 +00:00
parent 0e40b92d36
commit 95a6500a6b

View File

@@ -61,6 +61,9 @@
<destructor><![CDATA[
this._bms.removeObserver(this._observer);
this.genericAnnoObserver.removeObserver("bookmarks/generatedTitle",
this._generatedTitleAnnoObserver);
this._anno.removeObserver(this.genericAnnoObserver);
]]></destructor>
<method name="_init">
@@ -71,13 +74,20 @@
this._bms =
Cc["@mozilla.org/browser/nav-bookmarks-service;1"].
getService(Ci.nsINavBookmarksService);
this._anno =
Cc["@mozilla.org/browser/annotation-service;1"].
getService(Ci.nsIAnnotationService);
this._bms.addObserver(this._observer, false);
var t = this;
window.addEventListener("resize",
function f(e) { t.updateChevron(e); },
false);
this._anno.addObserver(this.genericAnnoObserver);
this.genericAnnoObserver.addObserver("bookmarks/generatedTitle",
this._generatedTitleAnnoObserver);
if (this.hasAttribute("place")) {
// Do the initial build.
this.place = this.place;
@@ -94,6 +104,37 @@
<field name="_result">null</field>
<!-- A cache of URIs currently in this view. The annotation observer
- uses this to determine which annotation changes matter because they
- are happening to URIs currently in this view. This gets generated
- by _rebuild().
-->
<field name="_currentURIs">null</field>
<!-- A cache of titles generated by the microsummary service/extensions.
- Generated titles override page/user-set titles as bookmark labels.
- The getter builds the cache the first time it's needed; afterwards,
- the _generatedTitleAnnoObserver maintains it by reflecting changes
- to "bookmarks/generatedTitle" annotations into it.
-->
<field name="__generatedTitles">null</field>
<property name="_generatedTitles">
<getter><![CDATA[
if (!this.__generatedTitles) {
this.__generatedTitles = {};
var pages = this._anno.getPagesWithAnnotation("bookmarks/generatedTitle", {});
// XXX It'd be faster to grab the annotations in a single query
// instead of querying separately for each one, but the annotation
// service provides no mechanism for doing so.
for ( var i = 0; i < pages.length; i++)
this.__generatedTitles[pages[i].spec] =
this._anno.getAnnotationString(pages[i], "bookmarks/generatedTitle");
}
return this.__generatedTitles;
]]></getter>
</property>
<!-- nsIPlacesView -->
<method name="getResult">
<body><![CDATA[
@@ -107,6 +148,7 @@
if (this._DNDObserver._overFolder.node)
this._DNDObserver._clearOverFolder();
this._openedMenuButton = null;
this._currentURIs = {};
while (this.hasChildNodes())
this.removeChild(this.firstChild);
@@ -115,10 +157,19 @@
var cc = this._result.root.childCount;
for (var i = 0; i < cc; ++i) {
var child = this._result.root.getChild(i);
var title = child.title;
var button = null;
if (PlacesController.nodeIsURI(child)) {
button = document.createElementNS(XULNS, "toolbarbutton");
button.setAttribute("url", child.uri);
// Add the URI to the list of URIs currently in the view.
this._currentURIs[child.uri] = true;
// If the URI has a generated title, use that instead.
if (this._generatedTitles[child.uri])
title = this._generatedTitles[child.uri];
} else if (PlacesController.nodeIsSeparator(child)) {
button = document.createElementNS(XULNS, "toolbarseparator");
} else if (PlacesController.nodeIsContainer(child)) {
@@ -129,10 +180,7 @@
button.setAttribute("livemark", "true");
var folder = asFolder(child).folderId;
var folderURI = this._bms.getFolderURI(folder);
var anno =
Cc["@mozilla.org/browser/annotation-service;1"].
getService(Ci.nsIAnnotationService);
var siteURI = anno.getAnnotationString(folderURI, "livemark/siteURI");
var siteURI = this._anno.getAnnotationString(folderURI, "livemark/siteURI");
button.setAttribute("siteURI", siteURI);
}
var popup = document.createElementNS(XULNS, "menupopup");
@@ -147,7 +195,7 @@
popup._result = this._result;
popup._resultNode = child;
}
button.setAttribute("label", child.title);
button.setAttribute("label", title);
if (child.icon)
button.setAttribute("image", child.icon.spec);
button.className = "menuitem-iconic bookmark-item";
@@ -652,6 +700,109 @@
}
})]]></field>
<!-- nsIAnnotationObserver -->
<field name="_generatedTitleAnnoObserver"><![CDATA[({
// Observes changes to "bookmarks/generatedTitle" annotations,
// which override page and user-set titles as bookmark labels.
// The microsummary service sets this annotation for summary bookmarks,
// and extensions might also set it.
// Inside this observer object's functions, "this" points to this
// observer object, while "_self" points to the toolbar XBL object.
_self: this,
onAnnotationSet: function TBV_GTAO_onAnnotationSet(uri, annoName) {
NS_ASSERT(annoName == "bookmarks/generatedTitle",
"annotation " + annoName + ", is not 'bookmarks/generatedTitle'");
var newTitle = this._self._anno.getAnnotationString(uri, annoName);
this._self._generatedTitles[uri.spec] = newTitle;
this._doRebuild();
},
onAnnotationRemoved: function TBV_GTAO_onAnnotationRemoved(uri, annoName) {
NS_ASSERT(annoName == "bookmarks/generatedTitle",
"annotation " + annoName + ", is not 'bookmarks/generatedTitle'");
delete this._self._generatedTitles[uri.spec];
this._doRebuild();
},
_doRebuild: function TBV_GTAO_doRebuild() {
function hitch(obj, meth) {
return function() { meth.apply(obj, arguments); }
}
setTimeout(hitch(this._self, this._self._rebuild), 1);
},
})]]></field>
<!-- nsIAnnotationObserver -->
<field name="genericAnnoObserver"><![CDATA[({
// A generic nsIAnnotationObserver that provides methods for registering
// annotation-specific observers for this view.
// Inside this observer object's functions, "this" points to this
// observer object, while "_self" points to the toolbar XBL object.
_self: this,
// Observers, indexed by annotation name.
_observers: {},
addObserver: function TBV_GAO_addObserver(annoName, observer) {
if (!this._observers[annoName])
this._observers[annoName] = [];
// Register the observer, but only if it isn't already registered,
// so that we don't call the same observer twice for any given change.
if (this._observers[annoName].indexOf(observer) == -1)
this._observers[annoName].push(observer);
},
removeObserver: function TBV_GAO_removeObserver(annoName, observer) {
NS_ASSERT(this._observers[annoName] &&
this._observers[annoName].indexOf(observer) != -1,
"can't remove annotation observer " + observer +
" for annotation " + annoName + ": not registered");
this._observers[annoName] =
this._observers[annoName].filter(function(i) { observer != i });
if (this._observers[annoName].length == 0)
delete this._observers[annoName];
},
// Determines whether or not a given annotation change applies to
// this view. A change applies if the annotation being changed is one
// we're observing, and the change is happening to a URI currently
// in this view.
_applies: function TBV_GAO_applies(uri, annoName) {
if (!this._observers[annoName])
return false;
if (!this._self._currentURIs[uri.spec])
return false;
return true;
},
onAnnotationSet: function TBV_GAO_onAnnotationSet(uri, annoName) {
if (!this._applies(uri, annoName))
return;
for ( var i = 0; i < this._observers[annoName].length; i++)
this._observers[annoName][i].onAnnotationSet(uri, annoName);
},
onAnnotationRemoved: function TBV_GAO_onAnnotationRemoved(uri, annoName) {
if (!this._applies(uri, annoName))
return;
for ( var i = 0; i < this._observers[annoName].length; i++)
this._observers[annoName][i].onAnnotationRemoved(uri, annoName);
}
})]]></field>
<method name="checkForMenuEvent">
<parameter name="event"/>