Add import option for creating a new collection

Use the new wizard for all imports (even if no Mendeley DB), and add a
page with a "Place imported collections and items into new collection"
option. If deselected, collections are added to the library root.
This commit is contained in:
Dan Stillman 2018-06-05 22:39:23 -04:00
parent 4008848c64
commit d98e89cf26
6 changed files with 117 additions and 76 deletions

View File

@ -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);

View File

@ -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

View File

@ -16,18 +16,17 @@
<wizardpage pageid="page-start"
label="&zotero.import.whereToImportFrom;"
next="page-progress"
next="page-options"
onpageadvanced="Zotero_Import_Wizard.onModeChosen(); return false;">
<radiogroup id="import-source">
<radio id="radio-import-source-file" label="&zotero.import.source.file;"/>
<radio id="radio-import-source-mendeley" label="Mendeley"/>
<radio id="radio-import-source-mendeley" label="Mendeley" hidden="true"/>
</radiogroup>
</wizardpage>
<wizardpage pageid="page-file-options"
next="page-progress"
onpagerewound="var w = document.getElementById('import-wizard'); w.goTo('page-start'); w.canAdvance = true; return false;"
onpageadvanced="Zotero_Import_Wizard.onFileChosen()">
<wizardpage pageid="page-file-list"
next="page-options"
onpagerewound="return Zotero_Import_Wizard.goToStart()">
<description id="file-options-header"/>
<listbox id="file-list" onselect="Zotero_Import_Wizard.onFileSelected()">
<listhead>
@ -47,6 +46,15 @@
</hbox>
</wizardpage>
<wizardpage pageid="page-options"
label="&zotero.general.options;"
next="page-progress"
onpageshow="Zotero_Import_Wizard.onOptionsShown()"
onpagerewound="return Zotero_Import_Wizard.goToStart()"
onpageadvanced="Zotero_Import_Wizard.onImportStart()">
<checkbox id="create-collection-checkbox" label="&zotero.import.createCollection;" checked="true" />
</wizardpage>
<wizardpage pageid="page-progress"
label="&zotero.import.importing;"
onpageshow="document.getElementById('import-wizard').canRewind = false;"

View File

@ -49,7 +49,7 @@
<commandset id="mainCommandSet">
<command id="cmd_zotero_reportErrors" oncommand="ZoteroPane_Local.reportErrors();"/>
<command id="cmd_zotero_import" oncommand="Zotero_File_Interface.startImport();"/>
<command id="cmd_zotero_import" oncommand="Zotero_File_Interface.showImportWizard();"/>
<command id="cmd_zotero_importFromClipboard" oncommand="Zotero_File_Interface.importFromClipboard();"/>
<command id="cmd_zotero_exportLibrary" oncommand="Zotero_File_Interface.exportFile();"/>
<command id="cmd_zotero_advancedSearch" oncommand="ZoteroPane_Local.openAdvancedSearchWindow();"/>

View File

@ -10,6 +10,7 @@
<!ENTITY zotero.general.cancel "Cancel">
<!ENTITY zotero.general.refresh "Refresh">
<!ENTITY zotero.general.saveAs "Save As…">
<!ENTITY zotero.general.options "Options">
<!ENTITY zotero.general.advancedOptions.label "Advanced Options">
<!ENTITY zotero.general.tools "Tools">
<!ENTITY zotero.general.more "More">
@ -210,6 +211,7 @@
<!ENTITY zotero.import.database "Database">
<!ENTITY zotero.import.lastModified "Last Modified">
<!ENTITY zotero.import.size "Size">
<!ENTITY zotero.import.createCollection "Place imported collections and items into new collection">
<!ENTITY zotero.exportOptions.title "Export…">
<!ENTITY zotero.exportOptions.format.label "Format:">

View File

@ -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;
}