/* ***** BEGIN LICENSE BLOCK ***** Copyright © 2009 Center for History and New Media George Mason University, Fairfax, Virginia, USA http://zotero.org This file is part of Zotero. Zotero is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. Zotero is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with Zotero. If not, see . ***** END LICENSE BLOCK ***** */ import FilePicker from 'zotero/modules/filePicker'; /* * This object contains the various functions for the interface */ var ZoteroPane = new function() { var _unserialized = false; this.collectionsView = false; this.itemsView = false; this.progressWindow = false; this._listeners = {}; this.__defineGetter__('loaded', function () { return _loaded; }); var _lastSelectedItems = []; //Privileged methods this.destroy = destroy; this.isFullScreen = isFullScreen; this.handleKeyDown = handleKeyDown; this.handleKeyUp = handleKeyUp; this.setHighlightedRowsCallback = setHighlightedRowsCallback; this.handleKeyPress = handleKeyPress; this.getSelectedCollection = getSelectedCollection; this.getSelectedSavedSearch = getSelectedSavedSearch; this.getSelectedItems = getSelectedItems; this.getSortedItems = getSortedItems; this.getSortField = getSortField; this.getSortDirection = getSortDirection; this.setItemsPaneMessage = setItemsPaneMessage; this.clearItemsPaneMessage = clearItemsPaneMessage; this.viewSelectedAttachment = viewSelectedAttachment; this.reportErrors = reportErrors; this.document = document; var self = this, _loaded = false, _madeVisible = false, titlebarcolorState, titleState, observerService, _reloadFunctions = [], _beforeReloadFunctions = []; /** * Called when the window containing Zotero pane is open */ this.init = function () { Zotero.debug("Initializing Zotero pane"); // Set key down handler document.addEventListener('keydown', ZoteroPane_Local.handleKeyDown, true); document.addEventListener('blur', ZoteroPane.handleBlur); // Init toolbar buttons for all progress queues let progressQueueButtons = document.getElementById('zotero-pq-buttons'); let progressQueues = Zotero.ProgressQueues.getAll(); for (let progressQueue of progressQueues) { let button = document.createElement('toolbarbutton'); button.id = 'zotero-tb-pq-' + progressQueue.getID(); button.hidden = progressQueue.getTotal() < 1; button.addEventListener('command', function () { Zotero.ProgressQueues.get(progressQueue.getID()).getDialog().open(); }, false); progressQueue.addListener('empty', function () { button.hidden = true; }); progressQueue.addListener('nonempty', function () { button.hidden = false; }); progressQueueButtons.appendChild(button); } _loaded = true; var zp = document.getElementById('zotero-pane'); Zotero.setFontSize(zp); ZoteroPane_Local.updateLayout(); ZoteroPane_Local.updateToolbarPosition(); this.updateWindow(); window.addEventListener("resize", () => { this.updateWindow(); let tabsDeck = document.querySelector('#tabs-deck') if (!tabsDeck || tabsDeck.getAttribute('selectedIndex') == 0) { this.updateToolbarPosition(); this.updateTagsBoxSize(); } }); window.setTimeout(this.updateToolbarPosition.bind(this), 0); Zotero.updateQuickSearchBox(document); if (Zotero.isMac) { document.getElementById('zotero-pane-stack').setAttribute('platform', 'mac'); } else if(Zotero.isWin) { document.getElementById('zotero-pane-stack').setAttribute('platform', 'win'); } // Set the sync tooltip label Components.utils.import("resource://zotero/config.js"); document.getElementById('zotero-tb-sync-label').value = Zotero.getString( 'sync.syncWith', ZOTERO_CONFIG.DOMAIN_NAME ); // register an observer for Zotero reload observerService = Components.classes["@mozilla.org/observer-service;1"] .getService(Components.interfaces.nsIObserverService); observerService.addObserver(_reloadObserver, "zotero-reloaded", false); observerService.addObserver(_reloadObserver, "zotero-before-reload", false); this.addReloadListener(_loadPane); // continue loading pane _loadPane(); }; /** * Called on window load or when pane has been reloaded after switching into or out of connector * mode */ async function _loadPane() { if (!Zotero || !Zotero.initialized) return; // Set flags for hi-res displays Zotero.hiDPI = window.devicePixelRatio > 1; Zotero.hiDPISuffix = Zotero.hiDPI ? "@2x" : ""; Zotero_Tabs.init(); ZoteroContextPane.init(); await ZoteroPane.initCollectionsTree(); await ZoteroPane.initItemsTree(); // Add a default progress window ZoteroPane.progressWindow = new Zotero.ProgressWindow({ window }); ZoteroPane.setItemsPaneMessage(Zotero.getString('pane.items.loading')); Zotero.Keys.windowInit(document); if (Zotero.restoreFromServer) { Zotero.restoreFromServer = false; setTimeout(function () { var ps = Components.classes["@mozilla.org/embedcomp/prompt-service;1"] .getService(Components.interfaces.nsIPromptService); var buttonFlags = (ps.BUTTON_POS_0) * (ps.BUTTON_TITLE_IS_STRING) + (ps.BUTTON_POS_1) * (ps.BUTTON_TITLE_CANCEL); var index = ps.confirmEx( null, "Zotero Restore", "The local Zotero database has been cleared." + " " + "Would you like to restore from the Zotero server now?", buttonFlags, "Sync Now", null, null, null, {} ); if (index == 0) { Zotero.Sync.Server.sync({ onSuccess: function () { Zotero.Sync.Runner.updateIcons(); ps.alert( null, "Restore Completed", "The local Zotero database has been successfully restored." ); }, onError: function (msg) { ps.alert( null, "Restore Failed", "An error occurred while restoring from the server:\n\n" + msg ); Zotero.Sync.Runner.error(msg); } }); } }, 1000); } // If the database was initialized or there are no sync credentials and // Zotero hasn't been run before in this profile, display the start page // -- this way the page won't be displayed when they sync their DB to // another profile or if the DB is initialized erroneously (e.g. while // switching data directory locations) else if (Zotero.Prefs.get('firstRun2')) { if (Zotero.Schema.dbInitialized || !Zotero.Sync.Server.enabled) { setTimeout(function () { ZoteroPane_Local.loadURI(ZOTERO_CONFIG.START_URL); }, 400); } Zotero.Prefs.set('firstRun2', false); try { Zotero.Prefs.clear('firstRun'); } catch (e) {} } if (Zotero.openPane) { Zotero.openPane = false; setTimeout(function () { ZoteroPane_Local.show(); }, 0); } setTimeout(function () { ZoteroPane.showRetractionBanner(); ZoteroPane.initSyncReminders(true); }); // TEMP: Clean up extra files from Mendeley imports <5.0.51 setTimeout(async function () { var needsCleanup = await Zotero.DB.valueQueryAsync( "SELECT COUNT(*) FROM settings WHERE setting='mImport' AND key='cleanup'" ) if (!needsCleanup) return; Components.utils.import("chrome://zotero/content/import/mendeley/mendeleyImport.js"); var importer = new Zotero_Import_Mendeley(); importer.deleteNonPrimaryFiles(); }, 10000) // Restore pane state try { let state = Zotero.Session.state.windows.find(x => x.type == 'pane'); if (state) { Zotero_Tabs.restoreState(state.tabs); } } catch (e) { Zotero.logError(e); } } this.initContainers = function () { this.initTagSelector(); }; this.uninitContainers = function () { if (this.tagSelector) this.tagSelector.uninit(); }; var _lastPrimaryTypes; this.updateNewItemTypes = function () { var primaryTypes = Zotero.ItemTypes.getPrimaryTypes(); var primaryTypesJoined = primaryTypes.join(','); if (_lastPrimaryTypes == primaryTypesJoined) { return; } var addMenu = document.getElementById('zotero-tb-add').firstElementChild; // Remove all nodes so we can regenerate var options = [...addMenu.querySelectorAll('.zotero-tb-add')]; while (options.length) { options.shift().remove(); } var moreMenu = document.getElementById('zotero-tb-add-more'); while (moreMenu.hasChildNodes()) { moreMenu.removeChild(moreMenu.firstChild); } var separator = addMenu.firstChild; // Populate primary types from MRU var itemTypes = []; for (let type of primaryTypes) { itemTypes.push({ id: type.id, name: type.name, localized: Zotero.ItemTypes.getLocalizedString(type.id) }); } var collation = Zotero.getLocaleCollation(); itemTypes.sort(function(a, b) { return collation.compareString(1, a.localized, b.localized); }); for (let itemType of itemTypes) { let menuitem = document.createXULElement("menuitem"); menuitem.setAttribute("label", itemType.localized); menuitem.setAttribute("tooltiptext", ""); let type = itemType.id; menuitem.addEventListener("command", function () { ZoteroPane.newItem(type, {}, null, true); }); menuitem.className = "zotero-tb-add"; addMenu.insertBefore(menuitem, separator); } // Populate submenu with each item type not in the MRU list itemTypes = []; for (let type of Zotero.ItemTypes.getSecondaryTypes()) { itemTypes.push({ id: type.id, name: type.name, localized: Zotero.ItemTypes.getLocalizedString(type.id) }); } var collation = Zotero.getLocaleCollation(); itemTypes.sort(function(a, b) { return collation.compareString(1, a.localized, b.localized); }); for (var i = 0; i