From 5779b60418226f6f20e5f9299a6b32142ba3ea28 Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Fri, 25 Oct 2019 15:21:56 -0400 Subject: [PATCH] Update new item type MRU menu for global schema And fix a Zotero 5 regression in which the MRU menu wasn't updated when changing the item type after using the new-item keyboard shortcut --- chrome/content/zotero/bindings/itembox.xml | 9 ++++-- .../content/zotero/xpcom/data/cachedTypes.js | 19 ++++++----- chrome/content/zotero/zoteroPane.js | 32 +++++++++---------- 3 files changed, 32 insertions(+), 28 deletions(-) diff --git a/chrome/content/zotero/bindings/itembox.xml b/chrome/content/zotero/bindings/itembox.xml index 4c2efd1102..4bbbe6093f 100644 --- a/chrome/content/zotero/bindings/itembox.xml +++ b/chrome/content/zotero/bindings/itembox.xml @@ -1106,6 +1106,11 @@ f.bind(this)()); - } + functionsToRun.forEach(f => f.bind(this)()); return true; } diff --git a/chrome/content/zotero/xpcom/data/cachedTypes.js b/chrome/content/zotero/xpcom/data/cachedTypes.js index 197ce75f62..68009a681d 100644 --- a/chrome/content/zotero/xpcom/data/cachedTypes.js +++ b/chrome/content/zotero/xpcom/data/cachedTypes.js @@ -351,6 +351,7 @@ Zotero.ItemTypes = new function() { this._table = 'itemTypesCombined'; this._hasCustom = true; + var _primaryTypeNames = ['book', 'bookSection', 'journalArticle', 'newspaperArticle', 'document']; var _primaryTypes; var _secondaryTypes; var _hiddenTypes; @@ -364,12 +365,14 @@ Zotero.ItemTypes = new function() { this.init = Zotero.Promise.coroutine(function* () { yield this.constructor.prototype.init.apply(this); - // TODO: get rid of ' AND itemTypeID!=5' and just remove display=2 - // from magazineArticle in system.sql - _primaryTypes = yield this._getTypesFromDB('WHERE (display=2 AND itemTypeID!=5) LIMIT ' + _numPrimary); + _primaryTypes = yield this._getTypesFromDB( + `WHERE typeName IN ('${_primaryTypeNames.join("', '")}')` + ); // Secondary types - _secondaryTypes = yield this._getTypesFromDB('WHERE display IN (1,2)'); + _secondaryTypes = yield this._getTypesFromDB( + `WHERE display != 0 AND display NOT IN ('${_primaryTypeNames.join("', '")}')` + ); // Hidden types _hiddenTypes = yield this._getTypesFromDB('WHERE display=0') @@ -397,17 +400,17 @@ Zotero.ItemTypes = new function() { mru = new Set( mru.split(',') .slice(0, _numPrimary) - .map(id => parseInt(id)) + .map(name => this.getName(name)) // Ignore 'webpage' item type - .filter(id => !isNaN(id) && id != 13) + .filter(name => name && name != 'webpage') ); // Add types from defaults until we reach our limit for (let i = 0; i < _primaryTypes.length && mru.size < _numPrimary; i++) { - mru.add(_primaryTypes[i].id); + mru.add(_primaryTypes[i].name); } - return Array.from(mru).map(id => ({ id, name: this.getName(id) })); + return Array.from(mru).map(name => ({ id: this.getID(name), name })); } return _primaryTypes; diff --git a/chrome/content/zotero/zoteroPane.js b/chrome/content/zotero/zoteroPane.js index dfad305ca8..0879ca1631 100644 --- a/chrome/content/zotero/zoteroPane.js +++ b/chrome/content/zotero/zoteroPane.js @@ -702,22 +702,20 @@ var ZoteroPane = new function() document.getElementById('zotero-tb-search').select(); break; case 'newItem': - Zotero.Promise.coroutine(function* () { - // Default to most recent item type from here or the - // New Type menu + (async function () { + // Default to most recent item type from here or the New Type menu, + // or fall back to 'book' var mru = Zotero.Prefs.get('newItemTypeMRU'); - // Or fall back to 'book' - var typeID = mru ? mru.split(',')[0] : 2; - yield ZoteroPane_Local.newItem(typeID); + var type = mru ? mru.split(',')[0] : 'book'; + await ZoteroPane.newItem(Zotero.ItemTypes.getID(type)); let itemBox = document.getElementById('zotero-editpane-item-box'); var menu = itemBox.itemTypeMenu; - var self = this; + // If the new item's type is changed immediately, update the MRU var handleTypeChange = function () { - self.addItemTypeToNewItemTypeMRU(this.itemTypeMenu.value); + this.addItemTypeToNewItemTypeMRU(Zotero.ItemTypes.getName(menu.value)); itemBox.removeHandler('itemtypechange', handleTypeChange); - }; - // Only update the MRU when the menu is opened for the - // keyboard shortcut, not on subsequent opens + }.bind(this); + // Don't update the MRU on subsequent opens of the item type menu var removeTypeChangeHandler = function () { itemBox.removeHandler('itemtypechange', handleTypeChange); itemBox.itemTypeMenu.firstChild.removeEventListener('popuphiding', removeTypeChangeHandler); @@ -729,7 +727,7 @@ var ZoteroPane = new function() menu.focus(); document.getElementById('zotero-editpane-item-box').itemTypeMenu.menupopup.openPopup(menu, "before_start", 0, 0); - })(); + }.bind(this)()); break; case 'newNote': // If a regular item is selected, use that as the parent. @@ -836,7 +834,7 @@ var ZoteroPane = new function() if (manual) { // Update most-recently-used list for New Item menu - this.addItemTypeToNewItemTypeMRU(typeID); + this.addItemTypeToNewItemTypeMRU(Zotero.ItemTypes.getName(typeID)); // Focus the title field document.getElementById('zotero-editpane-item-box').focusFirstField(); @@ -846,18 +844,18 @@ var ZoteroPane = new function() }); - this.addItemTypeToNewItemTypeMRU = function (itemTypeID) { + this.addItemTypeToNewItemTypeMRU = function (itemType) { var mru = Zotero.Prefs.get('newItemTypeMRU'); if (mru) { var mru = mru.split(','); - var pos = mru.indexOf(itemTypeID + ''); + var pos = mru.indexOf(itemType); if (pos != -1) { mru.splice(pos, 1); } - mru.unshift(itemTypeID); + mru.unshift(itemType); } else { - var mru = [itemTypeID + '']; + var mru = [itemType]; } Zotero.Prefs.set('newItemTypeMRU', mru.slice(0, 5).join(',')); }