commit 1e73421522cbdfec845a67e3df744f7858378853
parent 8f3a8479f881a2e3f6aa6447a63758936b5282e0
Author: Dan Stillman <dstillman@zotero.org>
Date: Wed, 1 Jun 2011 04:22:45 +0000
Closes #909, Have New Item drop-down display the most recently used item types
Diffstat:
3 files changed, 82 insertions(+), 33 deletions(-)
diff --git a/chrome/content/zotero/xpcom/data/cachedTypes.js b/chrome/content/zotero/xpcom/data/cachedTypes.js
@@ -97,12 +97,15 @@ Zotero.CachedTypes = function() {
}
- function getTypes(where) {
- return Zotero.DB.query('SELECT ' + this._idCol + ' AS id, '
- + this._nameCol + ' AS name'
- + (this._hasCustom ? ', custom' : '')
- + ' FROM ' + this._table
- + (where ? ' ' + where : ''));
+ function getTypes(where, params) {
+ return Zotero.DB.query(
+ 'SELECT ' + this._idCol + ' AS id, '
+ + this._nameCol + ' AS name'
+ + (this._hasCustom ? ', custom' : '')
+ + ' FROM ' + this._table
+ + (where ? ' ' + where : ''),
+ params ? params : false
+ );
}
@@ -231,11 +234,43 @@ Zotero.ItemTypes = new function() {
var _customLabels = {};
function getPrimaryTypes() {
- return this.getTypes('WHERE display=2');
+ var limit = 5;
+
+ // TODO: get rid of ' AND itemTypeID!=5' and just remove display=2
+ // from magazineArticle in system.sql
+ var sql = 'WHERE (display=2 AND itemTypeID!=5) ';
+
+ var mru = Zotero.Prefs.get('newItemTypeMRU');
+ if (mru) {
+ var params = [];
+ mru = mru.split(',').slice(0, limit);
+ for (var i=0, len=mru.length; i<len; i++) {
+ var id = parseInt(mru[i]);
+ if (!isNaN(id)) {
+ params.push(id);
+ }
+ }
+ if (params.length) {
+ sql += 'OR id IN '
+ + '(' + params.map(function () '?').join() + ') '
+ + 'ORDER BY id NOT IN '
+ + '(' + params.map(function () '?').join() + ') ';
+ params = params.concat(params);
+ }
+ else {
+ params = false;
+ }
+ }
+ else {
+ params = false;
+ }
+ sql += 'LIMIT ' + limit;
+
+ return this.getTypes(sql, params);
}
function getSecondaryTypes() {
- return this.getTypes('WHERE display=1');
+ return this.getTypes('WHERE display IN (1,2)');
}
function getHiddenTypes() {
diff --git a/chrome/content/zotero/zoteroPane.js b/chrome/content/zotero/zoteroPane.js
@@ -147,7 +147,7 @@ var ZoteroPane = new function()
itemsTree.controllers.appendController(new Zotero.ItemTreeCommandController(itemsTree));
itemsTree.addEventListener("click", ZoteroPane_Local.onTreeClick, true);
- this.buildItemTypeMenus();
+ this.buildItemTypeSubMenu();
var menu = document.getElementById("contentAreaContextMenu");
menu.addEventListener("popupshowing", ZoteroPane_Local.contextPopupShowing, false);
@@ -233,24 +233,14 @@ var ZoteroPane = new function()
}
- this.buildItemTypeMenus = function () {
- //
- // Create the New Item (+) menu with each item type
- //
- var addMenu = document.getElementById('zotero-tb-add').firstChild;
+ /*
+ * Create the New Item (+) submenu with each item type
+ */
+ this.buildItemTypeSubMenu = function () {
var moreMenu = document.getElementById('zotero-tb-add-more');
- // Remove all nodes, in case we're reloading
- var options = addMenu.getElementsByAttribute("class", "zotero-tb-add");
- while (options.length) {
- var p = options[0].parentNode;
- p.removeChild(options[0]);
- }
-
- var separator = addMenu.firstChild;
-
// Sort by localized name
- var t = Zotero.ItemTypes.getPrimaryTypes();
+ var t = Zotero.ItemTypes.getSecondaryTypes();
var itemTypes = [];
for (var i=0; i<t.length; i++) {
itemTypes.push({
@@ -269,17 +259,25 @@ var ZoteroPane = new function()
menuitem.setAttribute("label", itemTypes[i].localized);
menuitem.setAttribute("oncommand","ZoteroPane_Local.newItem("+itemTypes[i]['id']+")");
menuitem.setAttribute("tooltiptext", "");
- menuitem.className = "zotero-tb-add";
- addMenu.insertBefore(menuitem, separator);
+ moreMenu.appendChild(menuitem);
}
+ }
+
+
+ this.updateNewItemTypes = function () {
+ var addMenu = document.getElementById('zotero-tb-add').firstChild;
+ // Remove all nodes so we can regenerate
+ var options = addMenu.getElementsByAttribute("class", "zotero-tb-add");
+ while (options.length) {
+ var p = options[0].parentNode;
+ p.removeChild(options[0]);
+ }
- //
- // Create submenu for secondary item types
- //
+ var separator = addMenu.firstChild;
// Sort by localized name
- var t = Zotero.ItemTypes.getSecondaryTypes();
+ var t = Zotero.ItemTypes.getPrimaryTypes();
var itemTypes = [];
for (var i=0; i<t.length; i++) {
itemTypes.push({
@@ -299,11 +297,12 @@ var ZoteroPane = new function()
menuitem.setAttribute("oncommand","ZoteroPane_Local.newItem("+itemTypes[i]['id']+")");
menuitem.setAttribute("tooltiptext", "");
menuitem.className = "zotero-tb-add";
- moreMenu.appendChild(menuitem);
+ addMenu.insertBefore(menuitem, separator);
}
}
+
/*
* Called when the window closes
*/
@@ -677,6 +676,21 @@ var ZoteroPane = new function()
this.selectItem(itemID);
+ // Update most-recently-used list for New Item menu
+ var mru = Zotero.Prefs.get('newItemTypeMRU');
+ if (mru) {
+ var mru = mru.split(',');
+ var pos = mru.indexOf(typeID + '');
+ if (pos != -1) {
+ mru.splice(pos, 1);
+ }
+ mru.unshift(typeID);
+ }
+ else {
+ var mru = [typeID + ''];
+ }
+ Zotero.Prefs.set('newItemTypeMRU', mru.slice(0, 5).join(','));
+
return Zotero.Items.get(itemID);
}
diff --git a/chrome/content/zotero/zoteroPane.xul b/chrome/content/zotero/zoteroPane.xul
@@ -114,13 +114,13 @@
<hbox id="zotero-items-toolbar">
<toolbarbutton id="zotero-tb-add" class="zotero-tb-button" tooltiptext="&zotero.toolbar.newItem.label;" type="menu">
<!-- New Item drop-down built in overlay.js::onLoad() -->
- <menupopup>
+ <menupopup onpopupshowing="ZoteroPane_Local.updateNewItemTypes()">
<menuseparator/>
<menuitem label="&zotero.toolbar.attachment.linked;" oncommand="ZoteroPane_Local.addAttachmentFromDialog(true);" tooltiptext=""/>
<menuitem label="&zotero.toolbar.attachment.add;" oncommand="ZoteroPane_Local.addAttachmentFromDialog();" tooltiptext=""/>
<menuseparator/>
<menu label="&zotero.toolbar.moreItemTypes.label;" tooltiptext="">
- <menupopup id="zotero-tb-add-more"/>
+ <menupopup id="zotero-tb-add-more" onpopupshowing="event.stopPropagation()"/>
</menu>
</menupopup>
</toolbarbutton>