Prevent Add Item by Identifier button from flashing on textbox click

Nesting the panel in the toolbarbutton seems to cause the button to
appear as if it were clicked on any click/right-click in the textbox,
and I couldn't find a way to prevent that with event.stopPropagation().

Also cleans up this code in general
This commit is contained in:
Dan Stillman 2017-12-10 23:15:09 -05:00
parent 383f8d2219
commit b2c9a42103
2 changed files with 80 additions and 64 deletions

View file

@ -92,6 +92,45 @@ var Zotero_Lookup = new function () {
return false;
});
this.showPanel = function (button) {
var panel = document.getElementById('zotero-lookup-panel');
panel.openPopup(button, "after_start", 16, -2, false, false);
}
/**
* Focuses the field
*/
this.onShowing = function (event) {
// Ignore context menu
if (event.originalTarget.id != 'zotero-lookup-panel') return;
document.getElementById("zotero-lookup-panel").style.padding = "10px";
this.getActivePanel().getElementsByTagName('textbox')[0].focus();
}
/**
* Cancels the popup and resets fields
*/
this.onHidden = function (event) {
// Ignore context menu
if (event.originalTarget.id != 'zotero-lookup-panel') return;
document.getElementById("zotero-lookup-textbox").value = "";
document.getElementById("zotero-lookup-multiline-textbox").value = "";
Zotero_Lookup.toggleProgress(false);
}
this.getActivePanel = function() {
var mlPanel = document.getElementById("zotero-lookup-multiline");
if (mlPanel.collapsed) return document.getElementById("zotero-lookup-singleLine");
return mlPanel;
}
/**
* Handles a key press
*/
@ -114,50 +153,27 @@ var Zotero_Lookup = new function () {
return true;
}
/**
* Focuses the field
*/
this.onShowing = function (event) {
// Ignore context menu
if (event.originalTarget.id != 'zotero-lookup-panel') return;
document.getElementById("zotero-lookup-panel").style.padding = "10px";
// Workaround for field being truncated in middle
// https://github.com/zotero/zotero/issues/343
this.toggleMultiline(true);
var identifierElement = Zotero_Lookup.toggleMultiline(false);
Zotero_Lookup.toggleProgress(false);
identifierElement.focus();
}
/**
* Cancels the popup and resets fields
*/
this.onHidden = function (event) {
// Ignore context menu to prevent blanking on paste
if (event.originalTarget.id != 'zotero-lookup-panel') return;
var txtBox = Zotero_Lookup.toggleMultiline(false);
var mlTextbox = document.getElementById("zotero-lookup-multiline-textbox");
txtBox.value = "";
mlTextbox.value = "";
}
this.onInput = function (event, textbox) {
this.adjustTextbox(textbox);
};
/**
* Converts the textbox to multiline if newlines are detected
*/
this.adjustTextbox = function(txtBox) {
if(txtBox.value.trim().match(/[\r\n]/)) {
this.adjustTextbox = function (textbox) {
if (textbox.value.trim().match(/[\r\n]/)) {
Zotero_Lookup.toggleMultiline(true);
} else {
//since we ignore trailing and leading newlines, we should also trim them for display
//can't use trim, because then we cannot add leading/trailing spaces to the single line textbox
txtBox.value = txtBox.value.replace(/^([ \t]*[\r\n]+[ \t]*)+|([ \t]*[\r\n]+[ \t]*)+$/g,"");
}
// Since we ignore trailing and leading newlines, we should also trim them for display
// can't use trim, because then we cannot add leading/trailing spaces to the single line textbox
else {
textbox.value = textbox.value.replace(/^([ \t]*[\r\n]+[ \t]*)+|([ \t]*[\r\n]+[ \t]*)+$/g,"");
}
}
/**
* Performs the switch to multiline textbox and returns that textbox
*/
@ -200,11 +216,4 @@ var Zotero_Lookup = new function () {
document.getElementById("zotero-lookup-multiline-textbox").disabled = !!on;
document.getElementById("zotero-lookup-multiline-progress").setAttribute("collapsed", !on);
}
this.getActivePanel = function() {
var mlPanel = document.getElementById("zotero-lookup-multiline");
if(mlPanel.collapsed) return document.getElementById("zotero-lookup-singleLine");
return mlPanel;
}
}

View file

@ -160,27 +160,34 @@
</menu>
</menupopup>
</toolbarbutton>
<toolbarbutton id="zotero-tb-lookup" class="zotero-tb-button" tooltiptext="&zotero.toolbar.lookup.label;" type="panel">
<panel id="zotero-lookup-panel" type="arrow" onpopupshown="Zotero_Lookup.onShowing(event)"
onpopuphidden="Zotero_Lookup.onHidden(event)">
<vbox>
<description>&zotero.lookup.description;</description>
<vbox id="zotero-lookup-singleLine">
<stack>
<progressmeter id="zotero-lookup-progress" mode="undetermined" collapsed="true"/>
<textbox id="zotero-lookup-textbox" onkeypress="return Zotero_Lookup.onKeyPress(event, this)" oninput="Zotero_Lookup.adjustTextbox(this)" flex="1" newlines="pasteintact"/>
</stack>
</vbox>
<vbox id="zotero-lookup-multiline" collapsed="true">
<textbox id="zotero-lookup-multiline-textbox" onkeypress="return Zotero_Lookup.onKeyPress(event, this)" multiline="true" rows="5" wrap="off" flex="1"/>
<hbox align="start" id="zotero-lookup-buttons" class="zotero-button-clear-image">
<button label="&zotero.lookup.button.search;" align="start" oncommand="Zotero_Lookup.accept(document.getElementById('zotero-lookup-multiline-textbox'))"/>
<progressmeter id="zotero-lookup-multiline-progress" mode="undetermined" collapsed="true" flex="1"/>
</hbox>
</vbox>
<toolbarbutton id="zotero-tb-lookup" class="zotero-tb-button" tooltiptext="&zotero.toolbar.lookup.label;" type="panel"
onmousedown="Zotero_Lookup.showPanel(this)"/>
<panel id="zotero-lookup-panel" type="arrow" onpopupshown="Zotero_Lookup.onShowing(event)"
onpopuphidden="Zotero_Lookup.onHidden(event)">
<vbox>
<description>&zotero.lookup.description;</description>
<vbox id="zotero-lookup-singleLine">
<stack>
<progressmeter id="zotero-lookup-progress" mode="undetermined" collapsed="true"/>
<textbox id="zotero-lookup-textbox"
onkeypress="return Zotero_Lookup.onKeyPress(event, this)"
oninput="Zotero_Lookup.onInput(event, this)"
flex="1"
newlines="pasteintact"/>
</stack>
</vbox>
</panel>
</toolbarbutton>
<vbox id="zotero-lookup-multiline" collapsed="true">
<textbox id="zotero-lookup-multiline-textbox" onkeypress="return Zotero_Lookup.onKeyPress(event, this)" multiline="true" rows="5" wrap="off" flex="1"/>
<hbox align="start" id="zotero-lookup-buttons" class="zotero-button-clear-image">
<button label="&zotero.lookup.button.search;" align="start" oncommand="Zotero_Lookup.accept(document.getElementById('zotero-lookup-multiline-textbox'))"/>
<progressmeter id="zotero-lookup-multiline-progress" mode="undetermined" collapsed="true" flex="1"/>
</hbox>
</vbox>
</vbox>
</panel>
<!--<toolbarbutton id="zotero-tb-note-add" class="zotero-tb-button" tooltiptext="&zotero.toolbar.note.standalone;" oncommand="ZoteroPane_Local.newNote(event.shiftKey);"/>-->
<toolbarbutton id="zotero-tb-note-add" class="zotero-tb-button" tooltiptext="&zotero.toolbar.newNote;" type="menu">
<menupopup onpopupshowing="ZoteroPane_Local.updateNoteButtonMenu()">