From 51e2d36dd1b2332cd526863ed213dd3bb5857428 Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Tue, 6 Feb 2007 09:08:06 +0000 Subject: [PATCH] Closes #532, Highlight collections containing item(s) when pressing alt/option Also: - New method, Collections.getCollectionsContainingItems(itemIDs, asIDs) - Convenience property Zotero.isWin - ZoteroPane.onKeyUp() --- chrome/content/zotero/overlay.js | 66 ++++++++++++++++++- chrome/content/zotero/overlay.xul | 7 +- .../zotero/xpcom/collectionTreeView.js | 29 +++++++- chrome/content/zotero/xpcom/data_access.js | 21 ++++++ chrome/content/zotero/xpcom/zotero.js | 2 + chrome/skin/default/zotero/overlay.css | 6 ++ 6 files changed, 125 insertions(+), 6 deletions(-) diff --git a/chrome/content/zotero/overlay.js b/chrome/content/zotero/overlay.js index 20650d9dd..4e6236ac5 100644 --- a/chrome/content/zotero/overlay.js +++ b/chrome/content/zotero/overlay.js @@ -33,7 +33,9 @@ var ZoteroPane = new function() this.onUnload = onUnload; this.toggleDisplay = toggleDisplay; this.fullScreen = fullScreen; - this.handleKeyPress = handleKeyPress; + this.handleKeyDown = handleKeyDown; + this.handleKeyUp = handleKeyUp; + this.setHighlightedRowsCallback = setHighlightedRowsCallback; this.newItem = newItem; this.newCollection = newCollection; this.newSearch = newSearch; @@ -91,6 +93,9 @@ var ZoteroPane = new function() newPane.setAttribute('id','zotero-pane'); newPane.setAttribute('persist','height'); newPane.setAttribute('hidden', true); + newPane.setAttribute('onkeydown', 'ZoteroPane.handleKeyDown(event, this.id)'); + newPane.setAttribute('onkeyup', 'ZoteroPane.handleKeyUp(event, this.id)'); + newPane.height = oldPane.height; while(oldPane.hasChildNodes()) newPane.appendChild(oldPane.firstChild); @@ -214,7 +219,33 @@ var ZoteroPane = new function() /* * Trigger actions based on keyboard shortcuts */ - function handleKeyPress(event) { + function handleKeyDown(event, from) { + if (from == 'zotero-pane') { + // Highlight collections containing selected items + // + // We use Control (17) on Windows because Alt triggers the menubar; + // otherwise we use Alt/Option (18) + if ((Zotero.isWin && event.keyCode == 17 && !event.altKey) || + (!Zotero.isWin && event.keyCode == 18 && !event.ctrlKey) + && !event.shiftKey && !event.metaKey) { + + this.highlightTimer = Components.classes["@mozilla.org/timer;1"]. + createInstance(Components.interfaces.nsITimer); + // {} implements nsITimerCallback + this.highlightTimer.initWithCallback({ + notify: ZoteroPane.setHighlightedRowsCallback + }, 225, Components.interfaces.nsITimer.TYPE_ONE_SHOT); + } + else if ((Zotero.isWin && event.ctrlKey) || + (!Zotero.isWin && event.altKey)) { + if (this.highlightTimer) { + this.highlightTimer.cancel(); + this.highlightTimer = null; + } + ZoteroPane.collectionsView.setHighlightedRows(); + } + } + // Ignore keystrokes if Zotero pane is closed if (document.getElementById('zotero-pane').getAttribute('hidden') == 'true') { return; @@ -264,13 +295,42 @@ var ZoteroPane = new function() ZoteroPane.fullScreen(); break; default: - throw ('Command "' + command + '" not found in ZoteroPane.handleKeyPress()'); + throw ('Command "' + command + '" not found in ZoteroPane.handleKeyDown()'); } event.preventDefault(); } + function handleKeyUp(event, from) { + if (from == 'zotero-pane') { + if ((Zotero.isWin && event.keyCode == 17) || + (!Zotero.isWin && event.keyCode == 18)) { + if (this.highlightTimer) { + this.highlightTimer.cancel(); + this.highlightTimer = null; + } + ZoteroPane.collectionsView.setHighlightedRows(); + } + } + } + + + /* + * Highlights collections containing selected items on Ctrl (Win) or + * Option/Alt (Mac/Linux) press + */ + function setHighlightedRowsCallback() { + var itemIDs = ZoteroPane.getSelectedItems(true); + if (itemIDs) { + var collectionIDs = Zotero.Collections.getCollectionsContainingItems(itemIDs, true); + if (collectionIDs) { + ZoteroPane.collectionsView.setHighlightedRows(collectionIDs); + } + } + } + + /* * Create a new item * diff --git a/chrome/content/zotero/overlay.xul b/chrome/content/zotero/overlay.xul index 60521a259..f64e8e607 100644 --- a/chrome/content/zotero/overlay.xul +++ b/chrome/content/zotero/overlay.xul @@ -66,7 +66,9 @@