diff --git a/chrome/content/zotero/locateMenu.js b/chrome/content/zotero/locateMenu.js index 13df37cd27..0996055abc 100644 --- a/chrome/content/zotero/locateMenu.js +++ b/chrome/content/zotero/locateMenu.js @@ -575,10 +575,14 @@ var Zotero_LocateMenu = new function() { this.icon = "chrome://zotero/skin/locate-library-lookup.png"; this.canHandleItem = function (item) { return Zotero.Promise.resolve(item.isRegularItem()); }; this.handleItems = Zotero.Promise.method(function (items, event) { + // If no resolver configured, just switch to the default + if (!Zotero.Prefs.get('openURL.resolver')) { + Zotero.Prefs.clear('openURL.resolver') + } var urls = []; for (let item of items) { if(!item.isRegularItem()) continue; - var url = Zotero.OpenURL.resolve(item); + var url = Zotero.Utilities.Internal.OpenURL.resolve(item); if(url) urls.push(url); } ZoteroPane_Local.loadURI(urls, event); diff --git a/chrome/content/zotero/preferences/preferences_advanced.js b/chrome/content/zotero/preferences/preferences_advanced.js index d4d44e4298..b3f32d97ca 100644 --- a/chrome/content/zotero/preferences/preferences_advanced.js +++ b/chrome/content/zotero/preferences/preferences_advanced.js @@ -27,8 +27,9 @@ Components.utils.import("resource://gre/modules/Services.jsm"); import FilePicker from 'zotero/filePicker'; Zotero_Preferences.Advanced = { - _openURLResolvers: null, + DEFAULT_OPENURL_RESOLVER: 'https://www.worldcat.org/registry/gateway', + _openURLResolvers: null, init: function () { Zotero_Preferences.Keys.init(); @@ -50,6 +51,14 @@ Zotero_Preferences.Advanced = { input.value = Zotero.Prefs.get(preferenceName); } + // Set OpenURL resolver drop-down to last-known name + if (Zotero.Prefs.get('openURL.resolver')) { + let name = Zotero.Prefs.get('openURL.name'); + if (name) { + document.getElementById('openurl-primary-popup').firstChild.setAttribute('label', name); + } + } + this.onDataDirLoad(); this.refreshLocale(); }, @@ -421,55 +430,163 @@ Zotero_Preferences.Advanced = { }, - populateOpenURLResolvers: function () { - var openURLMenu = document.getElementById('openURLMenu'); - - this._openURLResolvers = Zotero.OpenURL.discoverResolvers(); - var i = 0; - for (let r of this._openURLResolvers) { - openURLMenu.insertItemAt(i, r.name); - if (r.url == Zotero.Prefs.get('openURL.resolver') && r.version == Zotero.Prefs.get('openURL.version')) { - openURLMenu.selectedIndex = i; + handleOpenURLPopupShowing: async function (event) { + if (event.target.id != 'openurl-primary-popup') { + return; + } + if (!this._openURLResolvers) { + let menupopup = document.getElementById('openurl-primary-popup'); + menupopup.firstChild.setAttribute('label', Zotero.getString('general.loading')); + try { + this._openURLResolvers = await Zotero.Utilities.Internal.OpenURL.getResolvers(); + } + catch (e) { + Zotero.logError(e); + menupopup.firstChild.setAttribute('label', "Error loading resolvers"); + return; } - i++; } - - var button = document.getElementById('openURLSearchButton'); - switch (this._openURLResolvers.length) { - case 0: - var num = 'zero'; - break; - case 1: - var num = 'singular'; - break; - default: - var num = 'plural'; - } - - button.setAttribute('label', Zotero.getString('zotero.preferences.openurl.resolversFound.' + num, this._openURLResolvers.length)); + this.updateOpenURLResolversMenu(); }, - onOpenURLSelected: function () { + updateOpenURLResolversMenu: function () { + if (!this._openURLResolvers) { + Zotero.debug("Resolvers not loaded -- not updating menu"); + return; + } + + var currentResolver = Zotero.Prefs.get('openURL.resolver'); + + var openURLMenu = document.getElementById('openurl-menu'); + var menupopup = openURLMenu.firstChild; + menupopup.innerHTML = ''; + + var defaultMenuItem = document.createElement('menuitem'); + defaultMenuItem.setAttribute('label', Zotero.getString('general.default')); + defaultMenuItem.setAttribute('value', this.DEFAULT_OPENURL_RESOLVER); + defaultMenuItem.setAttribute('type', 'checkbox'); + menupopup.appendChild(defaultMenuItem); + + var customMenuItem = document.createElement('menuitem'); + customMenuItem.setAttribute('label', Zotero.getString('general.custom')); + customMenuItem.setAttribute('value', 'custom'); + customMenuItem.setAttribute('type', 'checkbox'); + menupopup.appendChild(customMenuItem); + + menupopup.appendChild(document.createElement('menuseparator')); + + var selectedName; + var lastContinent; + var lastCountry; + var currentContinentPopup; + var currentMenuPopup; + for (let r of this._openURLResolvers) { + // Create submenus for continents + if (r.continent != lastContinent) { + let menu = document.createElement('menu'); + menu.setAttribute('label', r.continent); + openURLMenu.firstChild.appendChild(menu); + + currentContinentPopup = currentMenuPopup = document.createElement('menupopup'); + menu.appendChild(currentContinentPopup); + lastContinent = r.continent; + } + if (r.country != lastCountry) { + // If there's a country, create a submenu for it + if (r.country) { + let menu = document.createElement('menu'); + menu.setAttribute('label', r.country); + currentContinentPopup.appendChild(menu); + + let menupopup = document.createElement('menupopup'); + menu.appendChild(menupopup); + currentMenuPopup = menupopup; + } + // Otherwise use the continent popup + else { + currentMenuPopup = currentContinentPopup; + } + lastCountry = r.country; + } + let menuitem = document.createElement('menuitem'); + menuitem.setAttribute('label', r.name); + menuitem.setAttribute('value', r.url); + menuitem.setAttribute('type', 'checkbox'); + currentMenuPopup.appendChild(menuitem); + var checked = r.url == Zotero.Prefs.get('openURL.resolver'); + menuitem.setAttribute('checked', checked); + if (checked) { + selectedName = r.name; + } + } + + // Default + if (currentResolver == this.DEFAULT_OPENURL_RESOLVER) { + openURLMenu.setAttribute('label', Zotero.getString('general.default')); + defaultMenuItem.setAttribute('checked', true); + Zotero.Prefs.clear('openURL.name'); + } + else if (selectedName) { + openURLMenu.setAttribute('label', selectedName); + // If we found a match, update stored name + Zotero.Prefs.set('openURL.name', selectedName); + } + // Custom + else { + openURLMenu.setAttribute('label', Zotero.getString('general.custom')); + customMenuItem.setAttribute('checked', true); + Zotero.Prefs.clear('openURL.name'); + } + }, + + + handleOpenURLSelected: function (event) { + event.stopPropagation(); + event.preventDefault(); + + if (event.target.localName != 'menuitem') { + Zotero.debug("Ignoring click on " + event.target.localName); + return; + } + + var openURLMenu = document.getElementById('openurl-menu'); + var openURLServerField = document.getElementById('openURLServerField'); var openURLVersionMenu = document.getElementById('openURLVersionMenu'); - var openURLMenu = document.getElementById('openURLMenu'); - if(openURLMenu.value == "custom") - { + // Default + if (event.target.value == this.DEFAULT_OPENURL_RESOLVER) { + Zotero.Prefs.clear('openURL.name'); + Zotero.Prefs.clear('openURL.resolver'); + Zotero.Prefs.clear('openURL.version'); + openURLServerField.value = this.DEFAULT_OPENURL_RESOLVER; + } + // If "Custom" selected, clear URL field + else if (event.target.value == "custom") { + Zotero.Prefs.clear('openURL.name'); + Zotero.Prefs.set('openURL.resolver', ''); + Zotero.Prefs.clear('openURL.version'); + openURLServerField.value = ''; openURLServerField.focus(); } - else - { - openURLServerField.value = this._openURLResolvers[openURLMenu.selectedIndex]['url']; - openURLVersionMenu.value = this._openURLResolvers[openURLMenu.selectedIndex]['version']; - Zotero.Prefs.set("openURL.resolver", this._openURLResolvers[openURLMenu.selectedIndex]['url']); - Zotero.Prefs.set("openURL.version", this._openURLResolvers[openURLMenu.selectedIndex]['version']); + else { + Zotero.Prefs.set('openURL.name', openURLServerField.value = event.target.label); + Zotero.Prefs.set('openURL.resolver', openURLServerField.value = event.target.value); + Zotero.Prefs.set('openURL.version', openURLVersionMenu.value = "1.0"); } + + openURLMenu.firstChild.hidePopup(); + + setTimeout(() => { + this.updateOpenURLResolversMenu(); + }); }, onOpenURLCustomized: function () { - document.getElementById('openURLMenu').value = "custom"; + setTimeout(() => { + this.updateOpenURLResolversMenu(); + }); }, diff --git a/chrome/content/zotero/preferences/preferences_advanced.xul b/chrome/content/zotero/preferences/preferences_advanced.xul index a68cda89a7..9a9a12c868 100644 --- a/chrome/content/zotero/preferences/preferences_advanced.xul +++ b/chrome/content/zotero/preferences/preferences_advanced.xul @@ -95,25 +95,23 @@ - - - - - - - - - - -