Folder upload only shows a modal for global folder options

pull/1/head
ClemDee 5 years ago
parent 06cbabd373
commit 165feef7d4

@ -234,11 +234,6 @@ define([
owned: true, owned: true,
store: true store: true
}; };
var fileUploadModal = function (file, cb) {
var parsedName = /^(\.?.+?)(\.[^.]+)?$/.exec(file.name) || [];
var name = parsedName[1] || file.name;
var ext = parsedName[2] || "";
var createHelper = function (href, text) { var createHelper = function (href, text) {
var q = h('a.fa.fa-question-circle', { var q = h('a.fa.fa-question-circle', {
style: 'text-decoration: none !important;', style: 'text-decoration: none !important;',
@ -249,22 +244,30 @@ define([
}); });
return q; return q;
}; };
var createManualStore = function (isFolderUpload) {
var privateData = common.getMetadataMgr().getPrivateData(); var privateData = common.getMetadataMgr().getPrivateData();
var autoStore = Util.find(privateData, ['settings', 'general', 'autostore']) || 0; var autoStore = Util.find(privateData, ['settings', 'general', 'autostore']) || 0;
var initialState = modalState.owned || modalState.store; var initialState = modalState.owned || modalState.store;
var initialDisabled = modalState.owned ? { disabled: true } : {}; var initialDisabled = modalState.owned ? { disabled: true } : {};
var manualStore = autoStore === 1 ? undefined : var manualStore = autoStore === 1 ? undefined :
UI.createCheckbox('cp-upload-store', Messages.autostore_forceSave, initialState, { UI.createCheckbox('cp-upload-store', isFolderUpload ? (Messages.uploadFolder_modal_forceSave || "Store files in your cryptDrive") : Messages.autostore_forceSave, initialState, {
input: initialDisabled input: initialDisabled
}); });
return manualStore;
}
var fileUploadModal = function (defaultFileName, cb) {
var parsedName = /^(\.?.+?)(\.[^.]+)?$/.exec(defaultFileName) || [];
var name = parsedName[1] || defaultFileName;
var ext = parsedName[2] || "";
var manualStore = createManualStore();
// Ask for name, password and owner // Ask for name, password and owner
var content = h('div', [ var content = h('div', [
h('h4', Messages.upload_modal_title), h('h4', Messages.upload_modal_title),
UIElements.setHTML(h('label', {for: 'cp-upload-name'}), UIElements.setHTML(h('label', {for: 'cp-upload-name'}),
Messages._getKey('upload_modal_filename', [ext])), Messages._getKey('upload_modal_filename', [ext])),
h('input#cp-upload-name', {type: 'text', placeholder: file.name, value: file.name}), h('input#cp-upload-name', {type: 'text', placeholder: defaultFileName, value: defaultFileName}),
h('label', {for: 'cp-upload-password'}, Messages.creation_passwordValue), h('label', {for: 'cp-upload-password'}, Messages.creation_passwordValue),
UI.passwordInput({id: 'cp-upload-password'}), UI.passwordInput({id: 'cp-upload-password'}),
h('span', { h('span', {
@ -298,7 +301,7 @@ define([
modalState.store = forceSave; modalState.store = forceSave;
// Add extension to the name if needed // Add extension to the name if needed
if (!newName || !newName.trim()) { newName = file.name; } if (!newName || !newName.trim()) { newName = defaultFileName; }
var newExtIdx = newName.lastIndexOf('.'); var newExtIdx = newName.lastIndexOf('.');
var newExt = newExtIdx !== -1 ? newName.slice(newExtIdx) : ""; var newExt = newExtIdx !== -1 ? newName.slice(newExtIdx) : "";
if (newExt !== ext) { newName += ext; } if (newExt !== ext) { newName += ext; }
@ -312,12 +315,64 @@ define([
}); });
}; };
var showFolderUploadModal = File.showFolderUploadModal = function (foldername, cb) {
var manualStore = createManualStore();
// Ask for name, password and owner
var content = h('div', [
h('h4', Messages.uploadFolder_modal_title || "Folder upload options"),
UIElements.setHTML(h('label', {for: 'cp-upload-name'}), Messages.fm_folderName),
h('input#cp-upload-foldername', {type: 'text', placeholder: foldername, value: foldername}),
h('label', {for: 'cp-upload-password'}, Messages.uploadFolder_modal_filesPassword || "Files password"),
UI.passwordInput({id: 'cp-upload-password'}),
h('span', {
style: 'display:flex;align-items:center;justify-content:space-between'
}, [
UI.createCheckbox('cp-upload-owned', Messages.uploadFolder_modal_owner || "Owned files", modalState.owned),
createHelper('/faq.html#keywords-owned', Messages.creation_owned1)
]),
manualStore
]);
$(content).find('#cp-upload-owned').on('change', function () {
var val = $(content).find('#cp-upload-owned').is(':checked');
if (val) {
$(content).find('#cp-upload-store').prop('checked', true).prop('disabled', true);
} else {
$(content).find('#cp-upload-store').prop('disabled', false);
}
});
UI.confirm(content, function (yes) {
if (!yes) { return void cb(); }
// Get the values
var newName = $(content).find('#cp-upload-foldername').val();
var password = $(content).find('#cp-upload-password').val() || undefined;
var owned = $(content).find('#cp-upload-owned').is(':checked');
var forceSave = owned || $(content).find('#cp-upload-store').is(':checked');
modalState.owned = owned;
modalState.store = forceSave;
if (!newName || !newName.trim()) { newName = foldername; }
cb({
folderName: newName,
password: password,
owned: owned,
forceSave: forceSave
});
});
}
var handleFileState = { var handleFileState = {
queue: [], queue: [],
inProgress: false inProgress: false
}; };
var handleFile = File.handleFile = function (file, e) { /* if defaultOptions is passed, the function does not show the upload options modal, and directly save the file with the specified options */
if (handleFileState.inProgress) { return void handleFileState.queue.push([file, e]); } var handleFile = File.handleFile = function (file, e, defaultOptions) {
if (handleFileState.inProgress) { return void handleFileState.queue.push([file, e, defaultOptions]); }
handleFileState.inProgress = true; handleFileState.inProgress = true;
var thumb; var thumb;
@ -345,7 +400,7 @@ define([
handleFileState.inProgress = false; handleFileState.inProgress = false;
if (handleFileState.queue.length) { if (handleFileState.queue.length) {
var next = handleFileState.queue.shift(); var next = handleFileState.queue.shift();
handleFile(next[0], next[1]); handleFile(next[0], next[1], next[2]);
} }
}; };
var getName = function () { var getName = function () {
@ -354,7 +409,17 @@ define([
if (config.noStore) { return void finish(); } if (config.noStore) { return void finish(); }
// Otherwise, ask for password, name and ownership // Otherwise, ask for password, name and ownership
fileUploadModal(file, function (obj) { // if default options were passed, upload file immediately
if (defaultOptions && typeof defaultOptions === "object") {
name = defaultOptions.name || file.name;
password = defaultOptions.password || undefined;
owned = defaultOptions.owned || true;
forceSave = defaultOptions.forceSave || true;
finish();
}
// if no default options were passed, ask the user
else {
fileUploadModal(file.name, function (obj) {
if (!obj) { return void finish(true); } if (!obj) { return void finish(true); }
name = obj.name; name = obj.name;
password = obj.password; password = obj.password;
@ -362,6 +427,7 @@ define([
forceSave = obj.forceSave; forceSave = obj.forceSave;
finish(); finish();
}); });
}
}; };
blobToArrayBuffer(file, function (e, buffer) { blobToArrayBuffer(file, function (e, buffer) {

@ -2146,6 +2146,13 @@ define([
// if folder name already exist in drive, rename it // if folder name already exist in drive, rename it
var uploadedFolderName = files[0].path[0]; var uploadedFolderName = files[0].path[0];
var availableName = manager.user.userObject.getAvailableName(manager.find(currentFolder), uploadedFolderName); var availableName = manager.user.userObject.getAvailableName(manager.find(currentFolder), uploadedFolderName);
// ask for folder name and files options, then upload all the files!
APP.FM.showFolderUploadModal(availableName, function (folderUploadOptions) {
if (!folderUploadOptions) { return; }
// verfify folder name is possible, and update files path
availableName = manager.user.userObject.getAvailableName(manager.find(currentFolder), folderUploadOptions.folderName);
if (uploadedFolderName !== availableName) { if (uploadedFolderName !== availableName) {
files.forEach(function (file) { files.forEach(function (file) {
file.path[0] = availableName; file.path[0] = availableName;
@ -2163,17 +2170,16 @@ define([
}; };
} }
files.forEach(function (file) { files.forEach(function (file) {
// add steps for subfolders containing file // add steps to create subfolders containing file
for (var depth = 0 ; depth < file.path.length - 1 ; depth++) { for (var depth = 0 ; depth < file.path.length - 1 ; depth++) {
var subfolderStr = file.path.slice(0, depth + 1).join("/"); var subfolderStr = file.path.slice(0, depth + 1).join("/");
if (uploadSteps[depth].folders.indexOf(subfolderStr) === -1) { if (uploadSteps[depth].folders.indexOf(subfolderStr) === -1) {
uploadSteps[depth].folders.push(subfolderStr); uploadSteps[depth].folders.push(subfolderStr);
} }
} }
// add step for file (one step later than the step for its direct parent folder) // add step to upload file (one step later than the step of its direct parent folder)
uploadSteps[file.path.length - 1].files.push(file); uploadSteps[file.path.length - 1].files.push(file);
}); });
console.log(uploadSteps);
// add folders, then add files when theirs folders have been created // add folders, then add files when theirs folders have been created
// wait for the folders to be created to go to the next step (don't wait for the files) // wait for the folders to be created to go to the next step (don't wait for the files)
@ -2193,13 +2199,15 @@ define([
target: $content[0], target: $content[0],
path: currentFolder.concat(file.path.slice(0, -1)), path: currentFolder.concat(file.path.slice(0, -1)),
}; };
APP.FM.handleFile(file.file, ev); APP.FM.handleFile(file.file, ev, folderUploadOptions);
}); });
}).nThen(function () { }).nThen(function () {
stepByStep(uploadSteps, i + 1); stepByStep(uploadSteps, i + 1);
}); });
}; };
stepByStep(uploadSteps, 0); stepByStep(uploadSteps, 0);
});
}; };
var showUploadFolderModal = function () { var showUploadFolderModal = function () {
console.log("%cIMPORT FOLDER", "color: red"); console.log("%cIMPORT FOLDER", "color: red");

Loading…
Cancel
Save