diff --git a/chrome/content/zotero/fileInterface.js b/chrome/content/zotero/fileInterface.js index 75cb863cb..2f9140790 100644 --- a/chrome/content/zotero/fileInterface.js +++ b/chrome/content/zotero/fileInterface.js @@ -209,20 +209,6 @@ var Zotero_File_Interface = new function() { } - this.startImport = async function () { - // Show the wizard if a Mendeley database is found - var mendeleyDBs = await this.findMendeleyDatabases(); - var showWizard = !!mendeleyDBs.length; - if (showWizard) { - this.showImportWizard(); - } - // Otherwise just show the filepicker - else { - await this.importFile(null, true); - } - } - - this.getMendeleyDirectory = function () { Components.classes["@mozilla.org/net/osfileconstantsservice;1"] .getService(Components.interfaces.nsIOSFileConstantsService) @@ -301,7 +287,8 @@ var Zotero_File_Interface = new function() { * * @param {Object} options * @param {nsIFile|string|null} [options.file=null] - File to import, or none to show a filepicker - * @param {Boolean} [options.createNewCollection=false] - Put items in a new collection + * @param {Boolean} [options.addToLibraryRoot=false] + * @param {Boolean} [options.createNewCollection=true] - Put items in a new collection * @param {Function} [options.onBeforeImport] - Callback to receive translation object, useful * for displaying progress in a different way. This also causes an error to be throw * instead of shown in the main window. @@ -317,11 +304,13 @@ var Zotero_File_Interface = new function() { var file = options.file ? Zotero.File.pathToFile(options.file) : null; var createNewCollection = options.createNewCollection; + var addToLibraryRoot = options.addToLibraryRoot; var onBeforeImport = options.onBeforeImport; - if(createNewCollection === undefined) { + if (createNewCollection === undefined && !addToLibraryRoot) { createNewCollection = true; - } else if(!createNewCollection) { + } + else if (!createNewCollection) { try { if (!ZoteroPane.collectionsView.editable) { ZoteroPane.collectionsView.selectLibrary(null); @@ -329,44 +318,9 @@ var Zotero_File_Interface = new function() { } catch(e) {} } - var translation = new Zotero.Translate.Import(); - if (!file) { - let translators = yield translation.getTranslators(); - const nsIFilePicker = Components.interfaces.nsIFilePicker; - var fp = Components.classes["@mozilla.org/filepicker;1"] - .createInstance(nsIFilePicker); - fp.init(window, Zotero.getString("fileInterface.import"), nsIFilePicker.modeOpen); - - fp.appendFilters(nsIFilePicker.filterAll); - - var collation = Zotero.getLocaleCollation(); - - // Add Mendeley DB, which isn't a translator - let mendeleyFilter = { - label: "Mendeley Database", // TODO: Localize - target: "*.sqlite" - }; - let filters = [...translators]; - filters.push(mendeleyFilter); - - filters.sort((a, b) => collation.compareString(1, a.label, b.label)); - for (let filter of filters) { - fp.appendFilter(filter.label, "*." + filter.target); - } - - var rv = fp.show(); - Zotero.debug(rv); - if (rv !== nsIFilePicker.returnOK && rv !== nsIFilePicker.returnReplace) { - return false; - } - - file = fp.file; - - Zotero.debug(`File is ${file.path}`); - } - var defaultNewCollectionPrefix = Zotero.getString("fileInterface.imported"); + var translation; // Check if the file is an SQLite database var sample = yield Zotero.File.getSample(file.path); if (Zotero.MIME.sniffForMIMEType(sample) == 'application/x-sqlite3' @@ -377,11 +331,15 @@ var Zotero_File_Interface = new function() { translation = yield _getMendeleyTranslation(); defaultNewCollectionPrefix = "Mendeley Import"; } + else { + translation = new Zotero.Translate.Import(); + } translation.setLocation(file); return _finishImport({ translation, createNewCollection, + addToLibraryRoot, defaultNewCollectionPrefix, onBeforeImport }); @@ -430,10 +388,15 @@ var Zotero_File_Interface = new function() { var t = performance.now(); var translation = options.translation; + var addToLibraryRoot = options.addToLibraryRoot; var createNewCollection = options.createNewCollection; var defaultNewCollectionPrefix = options.defaultNewCollectionPrefix; var onBeforeImport = options.onBeforeImport; + if (addToLibraryRoot && createNewCollection) { + throw new Error("Can't add to library root and create new collection"); + } + var showProgressWindow = !onBeforeImport; let translators = yield translation.getTranslators(); @@ -468,7 +431,12 @@ var Zotero_File_Interface = new function() { try { let zp = Zotero.getActiveZoteroPane(); libraryID = zp.getSelectedLibraryID(); - importCollection = zp.getSelectedCollection(); + if (addToLibraryRoot) { + yield zp.collectionsView.selectLibrary(libraryID); + } + else if (!createNewCollection) { + importCollection = zp.getSelectedCollection(); + } } catch (e) { Zotero.logError(e); diff --git a/chrome/content/zotero/import/importWizard.js b/chrome/content/zotero/import/importWizard.js index c7331d44f..f48ca5a29 100644 --- a/chrome/content/zotero/import/importWizard.js +++ b/chrome/content/zotero/import/importWizard.js @@ -5,9 +5,14 @@ var Zotero_Import_Wizard = { _translation: null, - init: function () { + init: async function () { this._wizard = document.getElementById('import-wizard'); + var dbs = await Zotero_File_Interface.findMendeleyDatabases(); + if (dbs.length) { + document.getElementById('radio-import-source-mendeley').hidden = false; + } + Zotero.Translators.init(); // async }, @@ -15,15 +20,11 @@ var Zotero_Import_Wizard = { onModeChosen: async function () { var wizard = this._wizard; - this._disableCancel(); - wizard.canRewind = false; - wizard.canAdvance = false; - var mode = document.getElementById('import-source').selectedItem.id; try { switch (mode) { case 'radio-import-source-file': - await this.doImport(); + await this.chooseFile(); break; case 'radio-import-source-mendeley': @@ -36,7 +37,7 @@ var Zotero_Import_Wizard = { this._populateFileList(this._dbs); document.getElementById('file-options-header').textContent = Zotero.getString('fileInterface.chooseAppDatabaseToImport', 'Mendeley') - wizard.goTo('page-file-options'); + wizard.goTo('page-file-list'); wizard.canRewind = true; this._enableCancel(); } @@ -57,11 +58,64 @@ var Zotero_Import_Wizard = { }, + goToStart: function () { + this._wizard.goTo('page-start'); + this._wizard.canAdvance = true; + return false; + }, + + + chooseFile: async function (translation) { + var translation = new Zotero.Translate.Import(); + var translators = await translation.getTranslators(); + const nsIFilePicker = Components.interfaces.nsIFilePicker; + var fp = Components.classes["@mozilla.org/filepicker;1"] + .createInstance(nsIFilePicker); + fp.init(window, Zotero.getString("fileInterface.import"), nsIFilePicker.modeOpen); + + fp.appendFilters(nsIFilePicker.filterAll); + + var collation = Zotero.getLocaleCollation(); + + // Add Mendeley DB, which isn't a translator + var mendeleyFilter = { + label: "Mendeley Database", // TODO: Localize + target: "*.sqlite" + }; + var filters = [...translators]; + filters.push(mendeleyFilter); + + filters.sort((a, b) => collation.compareString(1, a.label, b.label)); + for (let filter of filters) { + fp.appendFilter(filter.label, "*." + filter.target); + } + + var rv = fp.show(); + if (rv !== nsIFilePicker.returnOK && rv !== nsIFilePicker.returnReplace) { + return false; + } + + Zotero.debug(`File is ${fp.file.path}`); + + this._file = fp.file.path; + this._wizard.canAdvance = true; + this._wizard.goTo('page-options'); + }, + + /** * When a file is clicked on in the file list */ onFileSelected: async function () { - this._wizard.canAdvance = true; + var index = document.getElementById('file-list').selectedIndex; + if (index != -1) { + this._file = this._dbs[index].path; + this._wizard.canAdvance = true; + } + else { + this._file = null; + this._wizard.canAdvance = false; + } }, @@ -69,6 +123,7 @@ var Zotero_Import_Wizard = { * When the user clicks "Other…" to choose a file not in the list */ chooseMendeleyDB: async function () { + document.getElementById('file-list').selectedIndex = -1; const nsIFilePicker = Components.interfaces.nsIFilePicker; var fp = Components.classes["@mozilla.org/filepicker;1"] .createInstance(nsIFilePicker); @@ -84,7 +139,12 @@ var Zotero_Import_Wizard = { }, - onFileChosen: async function () { + onOptionsShown: function () { + + }, + + + onImportStart: async function () { if (!this._file) { let index = document.getElementById('file-list').selectedIndex; this._file = this._dbs[index].path; @@ -92,7 +152,9 @@ var Zotero_Import_Wizard = { this._disableCancel(); this._wizard.canRewind = false; this._wizard.canAdvance = false; - await this.doImport(); + await this.doImport({ + createNewCollection: document.getElementById('create-collection-checkbox').hasAttribute('checked') + }); }, @@ -118,11 +180,12 @@ var Zotero_Import_Wizard = { }, - doImport: async function () { + doImport: async function (options) { try { let result = await Zotero_File_Interface.importFile({ file: this._file, - onBeforeImport: this.onBeforeImport.bind(this) + onBeforeImport: this.onBeforeImport.bind(this), + addToLibraryRoot: !options.createNewCollection }); // Cancelled by user or due to error diff --git a/chrome/content/zotero/import/importWizard.xul b/chrome/content/zotero/import/importWizard.xul index f90730365..2e6115a65 100644 --- a/chrome/content/zotero/import/importWizard.xul +++ b/chrome/content/zotero/import/importWizard.xul @@ -16,18 +16,17 @@ - + - + @@ -47,6 +46,15 @@ + + + + - + diff --git a/chrome/locale/en-US/zotero/zotero.dtd b/chrome/locale/en-US/zotero/zotero.dtd index f59eec576..d4b61d54c 100644 --- a/chrome/locale/en-US/zotero/zotero.dtd +++ b/chrome/locale/en-US/zotero/zotero.dtd @@ -10,6 +10,7 @@ + @@ -210,6 +211,7 @@ + diff --git a/chrome/skin/default/zotero/importWizard.css b/chrome/skin/default/zotero/importWizard.css index 98869f3e2..78abdb3fc 100644 --- a/chrome/skin/default/zotero/importWizard.css +++ b/chrome/skin/default/zotero/importWizard.css @@ -22,8 +22,8 @@ radio { padding-top: 5px; } -/* File options */ -wizard[currentpageid="page-file-options"] .wizard-header { +/* File list */ +wizard[currentpageid="page-file-list"] .wizard-header { display: none; } @@ -37,7 +37,7 @@ listbox, #result-description { font-size: 13px; } -button { +button, checkbox { font-size: 13px; }