Use Sets instead of Arrays in ZoteroPane.buildItemContextMenu()

This commit is contained in:
Dan Stillman 2021-12-26 23:27:44 -05:00
parent f3a71a2c09
commit 5c024e2859

View file

@ -2719,7 +2719,7 @@ var ZoteroPane = new function()
menu.removeChild(menu.firstChild); menu.removeChild(menu.firstChild);
} }
var disable = [], show = [], multiple = ''; var disable = new Set(), show = new Set(), multiple = '';
if (!this.itemsView) { if (!this.itemsView) {
return; return;
@ -2729,15 +2729,18 @@ var ZoteroPane = new function()
var isTrash = collectionTreeRow.isTrash(); var isTrash = collectionTreeRow.isTrash();
if (isTrash) { if (isTrash) {
show.push(m.deleteFromLibrary); show.add(m.deleteFromLibrary);
show.push(m.restoreToLibrary); show.add(m.restoreToLibrary);
} }
else if (!collectionTreeRow.isFeed()) { else if (!collectionTreeRow.isFeed()) {
show.push(m.moveToTrash); show.add(m.moveToTrash);
} }
if(!collectionTreeRow.isFeed()) { if(!collectionTreeRow.isFeed()) {
show.push(m.sep4, m.exportItems, m.createBib, m.loadReport); show.add(m.sep4);
show.add(m.exportItems);
show.add(m.createBib);
show.add(m.loadReport);
} }
var items = this.getSelectedItems(); var items = this.getSelectedItems();
@ -2784,23 +2787,23 @@ var ZoteroPane = new function()
} }
if (canMerge) { if (canMerge) {
show.push(m.mergeItems); show.add(m.mergeItems);
} }
if (canIndex) { if (canIndex) {
show.push(m.reindexItem); show.add(m.reindexItem);
} }
if (canRecognize) { if (canRecognize) {
show.push(m.recognizePDF); show.add(m.recognizePDF);
} }
if (canUnrecognize) { if (canUnrecognize) {
show.push(m.unrecognize); show.add(m.unrecognize);
} }
if (canMarkRead) { if (canMarkRead) {
show.push(m.toggleRead); show.add(m.toggleRead);
if (markUnread) { if (markUnread) {
menu.childNodes[m.toggleRead].setAttribute('label', Zotero.getString('pane.item.markAsUnread')); menu.childNodes[m.toggleRead].setAttribute('label', Zotero.getString('pane.item.markAsUnread'));
} else { } else {
@ -2813,11 +2816,13 @@ var ZoteroPane = new function()
&& !collectionTreeRow.isDuplicates() && !collectionTreeRow.isDuplicates()
&& !collectionTreeRow.isFeed()) { && !collectionTreeRow.isFeed()) {
if (items.some(item => attachmentsWithExtractableAnnotations(item).length)) { if (items.some(item => attachmentsWithExtractableAnnotations(item).length)) {
show.push(m.createNoteFromAnnotations, m.sep3); show.add(m.createNoteFromAnnotations);
show.add(m.sep3);
} }
if (items.some(item => item.isRegularItem())) { if (items.some(item => item.isRegularItem())) {
show.push(m.findPDF, m.sep3); show.add(m.findPDF);
show.add(m.sep3);
} }
} }
@ -2830,16 +2835,16 @@ var ZoteroPane = new function()
} }
} }
if (canCreateParent) { if (canCreateParent) {
show.push(m.createParent); show.add(m.createParent);
} }
if (canRename) { if (canRename) {
show.push(m.renameAttachments); show.add(m.renameAttachments);
} }
// Add in attachment separator // Add in attachment separator
if (canCreateParent || canRecognize || canUnrecognize || canRename || canIndex) { if (canCreateParent || canRecognize || canUnrecognize || canRename || canIndex) {
show.push(m.sep5); show.add(m.sep5);
} }
// Block certain actions on files if no access and at least one item is a file // Block certain actions on files if no access and at least one item is a file
@ -2847,11 +2852,9 @@ var ZoteroPane = new function()
if (!collectionTreeRow.filesEditable) { if (!collectionTreeRow.filesEditable) {
for (let item of items) { for (let item of items) {
if (item.isFileAttachment()) { if (item.isFileAttachment()) {
disable.push( disable.add(m.moveToTrash);
m.moveToTrash, disable.add(m.createParent);
m.createParent, disable.add(m.renameAttachments);
m.renameAttachments
);
break; break;
} }
} }
@ -2869,11 +2872,14 @@ var ZoteroPane = new function()
if (!isTrash) { if (!isTrash) {
// Show in Library // Show in Library
if (!collectionTreeRow.isLibrary(true)) { if (!collectionTreeRow.isLibrary(true)) {
show.push(m.showInLibrary, m.sep1); show.add(m.showInLibrary);
show.add(m.sep1);
} }
if (item.isRegularItem() && !item.isFeedItem) { if (item.isRegularItem() && !item.isFeedItem) {
show.push(m.addNote, m.addAttachments, m.sep2); show.add(m.addNote);
show.add(m.addAttachments);
show.add(m.sep2);
// Create Note from Annotations // Create Note from Annotations
let popup = document.getElementById('create-note-from-annotations-popup'); let popup = document.getElementById('create-note-from-annotations-popup');
@ -2887,7 +2893,7 @@ var ZoteroPane = new function()
// there's only attachment with annotations, so it's clear which one // there's only attachment with annotations, so it's clear which one
// the annotations are coming from // the annotations are coming from
if (eligibleAttachments.length > 1) { if (eligibleAttachments.length > 1) {
show.push(m.createNoteFromAnnotationsMenu); show.add(m.createNoteFromAnnotationsMenu);
for (let attachment of attachmentsWithAnnotations) { for (let attachment of attachmentsWithAnnotations) {
let menuitem = document.createElement('menuitem'); let menuitem = document.createElement('menuitem');
menuitem.setAttribute('label', attachment.getDisplayTitle()); menuitem.setAttribute('label', attachment.getDisplayTitle());
@ -2899,51 +2905,53 @@ var ZoteroPane = new function()
} }
// Single attachment with annotations // Single attachment with annotations
else { else {
show.push(m.createNoteFromAnnotations); show.add(m.createNoteFromAnnotations);
} }
} }
} }
if (Zotero.Attachments.canFindPDFForItem(item)) { if (Zotero.Attachments.canFindPDFForItem(item)) {
show.push(m.findPDF, m.sep3); show.add(m.findPDF);
show.add(m.sep3);
} }
if (Zotero.RecognizePDF.canUnrecognize(item)) { if (Zotero.RecognizePDF.canUnrecognize(item)) {
show.push(m.sep5, m.unrecognize); show.add(m.sep5);
show.add(m.unrecognize);
} }
if (item.isAttachment()) { if (item.isAttachment()) {
var showSep5 = false; var showSep5 = false;
if (Zotero.RecognizePDF.canRecognize(item)) { if (Zotero.RecognizePDF.canRecognize(item)) {
show.push(m.recognizePDF); show.add(m.recognizePDF);
showSep5 = true; showSep5 = true;
} }
// Allow parent item creation for standalone attachments // Allow parent item creation for standalone attachments
if (item.isTopLevelItem()) { if (item.isTopLevelItem()) {
show.push(m.createParent); show.add(m.createParent);
showSep5 = true; showSep5 = true;
} }
// Attachment rename option // Attachment rename option
if (!item.isTopLevelItem() && item.attachmentLinkMode != Zotero.Attachments.LINK_MODE_LINKED_URL) { if (!item.isTopLevelItem() && item.attachmentLinkMode != Zotero.Attachments.LINK_MODE_LINKED_URL) {
show.push(m.renameAttachments); show.add(m.renameAttachments);
showSep5 = true; showSep5 = true;
} }
// If not linked URL, show reindex line // If not linked URL, show reindex line
if (yield Zotero.Fulltext.canReindex(item)) { if (yield Zotero.Fulltext.canReindex(item)) {
show.push(m.reindexItem); show.add(m.reindexItem);
showSep5 = true; showSep5 = true;
} }
if (showSep5) { if (showSep5) {
show.push(m.sep5); show.add(m.sep5);
} }
} }
else if (item.isFeedItem) { else if (item.isFeedItem) {
show.push(m.toggleRead); show.add(m.toggleRead);
if (item.isRead) { if (item.isRead) {
menu.childNodes[m.toggleRead].setAttribute('label', Zotero.getString('pane.item.markAsUnread')); menu.childNodes[m.toggleRead].setAttribute('label', Zotero.getString('pane.item.markAsUnread'));
} else { } else {
@ -2951,7 +2959,7 @@ var ZoteroPane = new function()
} }
} }
else if (!collectionTreeRow.isPublications()) { else if (!collectionTreeRow.isPublications()) {
show.push(m.duplicateItem); show.add(m.duplicateItem);
} }
} }
@ -2963,7 +2971,7 @@ var ZoteroPane = new function()
if (item.isFileAttachment() && !collectionTreeRow.filesEditable) { if (item.isFileAttachment() && !collectionTreeRow.filesEditable) {
[m.moveToTrash, m.createParent, m.renameAttachments] [m.moveToTrash, m.createParent, m.renameAttachments]
.forEach(function (x) { .forEach(function (x) {
disable.push(x); disable.add(x);
}); });
} }
} }
@ -2973,17 +2981,27 @@ var ZoteroPane = new function()
{ {
// Show in Library // Show in Library
if (!collectionTreeRow.isLibrary()) { if (!collectionTreeRow.isLibrary()) {
show.push(m.showInLibrary, m.sep1); show.add(m.showInLibrary);
show.add(m.sep1);
} }
disable.push(m.showInLibrary, m.duplicateItem, m.removeItems, [
m.moveToTrash, m.deleteFromLibrary, m.exportItems, m.createBib, m.loadReport); m.showInLibrary,
m.duplicateItem,
m.removeItems,
m.moveToTrash,
m.deleteFromLibrary,
m.exportItems,
m.createBib,
m.loadReport
].forEach(x => disable.add(x));
} }
if (!disable.includes(m.exportItems) if (!disable.has(m.exportItems)
&& items.every(item => item.isNote() || item.isAttachment()) && items.every(item => item.isNote() || item.isAttachment())
&& !items.some(item => item.note)) { && !items.some(item => item.note)) {
disable.push(m.exportItems); disable.add(m.exportItems);
} }
if ((!collectionTreeRow.editable || collectionTreeRow.isPublications()) && !collectionTreeRow.isFeed()) { if ((!collectionTreeRow.editable || collectionTreeRow.isPublications()) && !collectionTreeRow.isFeed()) {
@ -3012,18 +3030,18 @@ var ZoteroPane = new function()
continue; continue;
} }
} }
disable.push(m[i]); disable.add(m[i]);
} }
} }
// Remove from collection // Remove from collection
if (collectionTreeRow.isCollection() && items.every(item => item.isTopLevelItem())) { if (collectionTreeRow.isCollection() && items.every(item => item.isTopLevelItem())) {
menu.childNodes[m.removeItems].setAttribute('label', Zotero.getString('pane.items.menu.remove' + multiple)); menu.childNodes[m.removeItems].setAttribute('label', Zotero.getString('pane.items.menu.remove' + multiple));
show.push(m.removeItems); show.add(m.removeItems);
} }
else if (collectionTreeRow.isPublications()) { else if (collectionTreeRow.isPublications()) {
menu.childNodes[m.removeItems].setAttribute('label', Zotero.getString('pane.items.menu.removeFromPublications' + multiple)); menu.childNodes[m.removeItems].setAttribute('label', Zotero.getString('pane.items.menu.removeFromPublications' + multiple));
show.push(m.removeItems); show.add(m.removeItems);
} }
// Set labels, plural if necessary // Set labels, plural if necessary
@ -3047,14 +3065,12 @@ var ZoteroPane = new function()
menu.childNodes[pos].setAttribute('disabled', false); menu.childNodes[pos].setAttribute('disabled', false);
} }
for (var i in disable) for (let x of disable) {
{ menu.childNodes[x].setAttribute('disabled', true);
menu.childNodes[disable[i]].setAttribute('disabled', true);
} }
for (var i in show) for (let x of show) {
{ menu.childNodes[x].setAttribute('hidden', false);
menu.childNodes[show[i]].setAttribute('hidden', false);
} }
// add locate menu options // add locate menu options