Closes #909, Have New Item drop-down display the most recently used item types

This commit is contained in:
Dan Stillman 2011-06-01 04:22:45 +00:00
parent 8f3a8479f8
commit 1e73421522
3 changed files with 95 additions and 46 deletions

View file

@ -97,12 +97,15 @@ Zotero.CachedTypes = function() {
}
function getTypes(where) {
return Zotero.DB.query('SELECT ' + this._idCol + ' AS id, '
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 : ''));
+ (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() {

View file

@ -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,14 +233,41 @@ 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
// Sort by localized name
var t = Zotero.ItemTypes.getSecondaryTypes();
var itemTypes = [];
for (var i=0; i<t.length; i++) {
itemTypes.push({
id: t[i].id,
name: t[i].name,
localized: Zotero.ItemTypes.getLocalizedString(t[i].id)
});
}
var collation = Zotero.getLocaleCollation();
itemTypes.sort(function(a, b) {
return collation.compareString(1, a.localized, b.localized);
});
for (var i = 0; i<itemTypes.length; i++) {
var menuitem = document.createElement("menuitem");
menuitem.setAttribute("label", itemTypes[i].localized);
menuitem.setAttribute("oncommand","ZoteroPane_Local.newItem("+itemTypes[i]['id']+")");
menuitem.setAttribute("tooltiptext", "");
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;
@ -272,36 +299,8 @@ var ZoteroPane = new function()
menuitem.className = "zotero-tb-add";
addMenu.insertBefore(menuitem, separator);
}
//
// Create submenu for secondary item types
//
// Sort by localized name
var t = Zotero.ItemTypes.getSecondaryTypes();
var itemTypes = [];
for (var i=0; i<t.length; i++) {
itemTypes.push({
id: t[i].id,
name: t[i].name,
localized: Zotero.ItemTypes.getLocalizedString(t[i].id)
});
}
var collation = Zotero.getLocaleCollation();
itemTypes.sort(function(a, b) {
return collation.compareString(1, a.localized, b.localized);
});
for (var i = 0; i<itemTypes.length; i++) {
var menuitem = document.createElement("menuitem");
menuitem.setAttribute("label", itemTypes[i].localized);
menuitem.setAttribute("oncommand","ZoteroPane_Local.newItem("+itemTypes[i]['id']+")");
menuitem.setAttribute("tooltiptext", "");
menuitem.className = "zotero-tb-add";
moreMenu.appendChild(menuitem);
}
}
/*
@ -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);
}

View file

@ -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>