keeweb/app/scripts/views/open-view.js

681 lines
22 KiB
JavaScript
Raw Normal View History

2015-10-17 23:49:24 +02:00
'use strict';
var Backbone = require('backbone'),
2016-03-04 19:45:37 +01:00
kdbxweb = require('kdbxweb'),
2016-03-12 12:22:35 +01:00
OpenConfigView = require('./open-config-view'),
2015-11-05 21:58:33 +01:00
Keys = require('../const/keys'),
Alerts = require('../comp/alerts'),
SecureInput = require('../comp/secure-input'),
2015-12-12 09:53:50 +01:00
DropboxLink = require('../comp/dropbox-link'),
2016-05-13 14:09:21 +02:00
FeatureDetector = require('../util/feature-detector'),
2015-12-17 19:25:25 +01:00
Logger = require('../util/logger'),
2016-03-12 12:22:35 +01:00
Locale = require('../util/locale'),
2016-03-12 17:49:52 +01:00
UrlUtil = require('../util/url-util'),
2016-03-12 12:22:35 +01:00
Storage = require('../storage');
2015-12-12 09:53:50 +01:00
var logger = new Logger('open-view');
2015-10-17 23:49:24 +02:00
var OpenView = Backbone.View.extend({
2015-12-16 22:50:45 +01:00
template: require('templates/open.hbs'),
2015-10-17 23:49:24 +02:00
events: {
2015-11-05 21:58:33 +01:00
'change .open__file-ctrl': 'fileSelected',
'click .open__icon-open': 'openFile',
'click .open__icon-new': 'createNew',
2016-03-09 21:52:19 +01:00
'click .open__icon-import-xml': 'importFromXml',
2015-11-05 21:58:33 +01:00
'click .open__icon-demo': 'createDemo',
2016-03-09 21:52:19 +01:00
'click .open__icon-more': 'toggleMore',
2016-03-13 17:08:25 +01:00
'click .open__icon-storage': 'openStorage',
'click .open__icon-settings': 'openSettings',
2015-11-05 21:58:33 +01:00
'click .open__pass-input[readonly]': 'openFile',
'input .open__pass-input': 'inputInput',
'keydown .open__pass-input': 'inputKeydown',
2016-02-18 19:57:18 +01:00
'keyup .open__pass-input': 'inputKeyup',
2015-11-05 21:58:33 +01:00
'keypress .open__pass-input': 'inputKeypress',
2015-11-06 21:14:47 +01:00
'click .open__pass-enter-btn': 'openDb',
2015-11-05 21:58:33 +01:00
'click .open__settings-key-file': 'openKeyFile',
2015-11-07 20:02:45 +01:00
'click .open__last-item': 'openLast',
2015-10-17 23:49:24 +02:00
'dragover': 'dragover',
'dragleave': 'dragleave',
'drop': 'drop'
},
2016-03-12 12:22:35 +01:00
views: null,
2015-12-06 21:32:41 +01:00
params: null,
2015-11-05 21:58:33 +01:00
passwordInput: null,
2015-12-06 21:32:41 +01:00
busy: false,
2015-11-05 21:58:33 +01:00
2015-10-17 23:49:24 +02:00
initialize: function () {
2016-03-12 12:22:35 +01:00
this.views = {};
2015-12-06 21:32:41 +01:00
this.params = {
id: null,
name: '',
storage: null,
path: null,
keyFileName: null,
keyFileData: null,
2015-12-07 20:07:56 +01:00
fileData: null,
rev: null
2015-12-06 21:32:41 +01:00
};
2015-11-05 21:58:33 +01:00
this.passwordInput = new SecureInput();
2015-11-17 21:57:32 +01:00
},
2015-10-17 23:49:24 +02:00
render: function () {
if (this.dragTimeout) {
clearTimeout(this.dragTimeout);
}
2016-03-13 17:08:25 +01:00
var storageProviders = [];
Object.keys(Storage).forEach(function(name) {
var prv = Storage[name];
if (!prv.system && prv.enabled) {
storageProviders.push(prv);
}
});
storageProviders.sort(function(x, y) { return (x.uipos || Infinity) - (y.uipos || Infinity); });
2016-03-13 10:54:16 +01:00
this.renderTemplate({
lastOpenFiles: this.getLastOpenFiles(),
2016-03-13 17:08:25 +01:00
canOpenKeyFromDropbox: DropboxLink.canChooseFile() && Storage.dropbox.enabled,
demoOpened: this.model.settings.get('demoOpened'),
storageProviders: storageProviders
2016-03-13 10:54:16 +01:00
});
2015-11-05 21:58:33 +01:00
this.inputEl = this.$el.find('.open__pass-input');
this.passwordInput.setElement(this.inputEl);
2015-10-17 23:49:24 +02:00
return this;
2015-11-06 21:14:47 +01:00
},
2016-05-13 14:09:21 +02:00
focusInput: function() {
if (!FeatureDetector.isMobile()) {
this.inputEl.focus();
}
},
2015-11-07 20:02:45 +01:00
getLastOpenFiles: function() {
2015-12-06 21:32:41 +01:00
return this.model.fileInfos.map(function(f) {
2016-03-12 12:22:35 +01:00
var icon = 'file-text';
2016-03-27 08:14:15 +02:00
var storage = Storage[f.get('storage')];
if (storage && storage.icon) {
icon = storage.icon;
}
if (storage && storage.iconSvg) {
icon = null;
2015-11-07 20:02:45 +01:00
}
2015-12-06 21:32:41 +01:00
return {
id: f.get('id'),
name: f.get('name'),
2016-03-27 08:14:15 +02:00
icon: icon,
iconSvg: storage ? storage.iconSvg : undefined
2015-12-06 21:32:41 +01:00
};
2015-11-07 20:02:45 +01:00
});
},
2015-11-06 21:14:47 +01:00
remove: function() {
this.passwordInput.reset();
Backbone.View.prototype.remove.apply(this, arguments);
},
2016-02-23 07:08:57 +01:00
showLocalFileAlert: function() {
if (this.model.settings.get('skipOpenLocalWarn')) {
return;
}
var that = this;
Alerts.alert({
header: Locale.openLocalFile,
body: Locale.openLocalFileBody,
icon: 'file-text',
buttons: [
{result: 'skip', title: Locale.openLocalFileDontShow, error: true},
{result: 'ok', title: Locale.alertOk}
],
click: '',
esc: '',
enter: '',
success: function(res) {
2016-05-13 14:09:21 +02:00
that.focusInput();
2016-02-23 07:08:57 +01:00
if (res === 'skip') {
that.model.settings.set('skipOpenLocalWarn', true);
}
}
});
},
2015-11-06 21:14:47 +01:00
fileSelected: function(e) {
var file = e.target.files[0];
if (file) {
2016-03-04 20:21:34 +01:00
this.processFile(file, (function(success) {
if (success && !file.path && this.reading === 'fileData') {
this.showLocalFileAlert();
}
}).bind(this));
2015-11-06 21:14:47 +01:00
}
},
processFile: function(file, complete) {
var reader = new FileReader();
reader.onload = (function(e) {
2016-03-04 20:21:34 +01:00
var success = false;
2016-03-01 04:29:20 +01:00
switch (this.reading) {
case 'fileData':
2016-03-04 19:45:37 +01:00
if (!this.checkOpenFileFormat(e.target.result)) {
2016-03-04 20:21:34 +01:00
break;
2016-03-04 19:45:37 +01:00
}
2016-03-01 04:29:20 +01:00
this.params.id = null;
this.params.fileData = e.target.result;
this.params.name = file.name.replace(/\.\w+$/i, '');
this.params.path = file.path || null;
this.params.storage = file.path ? 'file' : null;
this.params.rev = null;
2016-04-07 18:43:19 +02:00
if (!this.params.keyFileData) {
this.params.keyFileName = null;
}
2016-03-01 04:29:20 +01:00
this.displayOpenFile();
2016-04-07 18:43:19 +02:00
this.displayOpenKeyFile();
2016-03-04 20:21:34 +01:00
success = true;
2016-03-01 04:29:20 +01:00
break;
case 'fileXml':
this.params.id = null;
this.params.fileXml = e.target.result;
this.params.name = file.name.replace(/\.\w+$/i, '');
this.params.path = null;
this.params.storage = null;
2016-03-01 04:29:20 +01:00
this.params.rev = null;
this.importDbWithXml();
2016-03-04 20:21:34 +01:00
success = true;
2016-03-01 04:29:20 +01:00
break;
2016-03-04 20:21:34 +01:00
case 'keyFileData':
2016-03-01 04:29:20 +01:00
this.params.keyFileData = e.target.result;
this.params.keyFileName = file.name;
this.displayOpenKeyFile();
2016-03-04 20:21:34 +01:00
success = true;
break;
2015-11-06 21:14:47 +01:00
}
if (complete) {
2016-03-04 20:21:34 +01:00
complete(success);
2015-11-06 21:14:47 +01:00
}
}).bind(this);
reader.onerror = (function() {
2015-12-17 19:25:25 +01:00
Alerts.error({ header: Locale.openFailedRead });
2015-11-06 21:14:47 +01:00
if (complete) {
complete(false);
}
}).bind(this);
2016-03-01 22:17:19 +01:00
if (this.reading === 'fileXml') {
reader.readAsText(file);
} else {
reader.readAsArrayBuffer(file);
}
2015-11-06 21:14:47 +01:00
},
2016-03-04 19:45:37 +01:00
checkOpenFileFormat: function(fileData) {
var fileSig = new Uint32Array(fileData, 0, 2);
if (fileSig[0] !== kdbxweb.Consts.Signatures.FileMagic) {
Alerts.error({ header: Locale.openWrongFile, body: Locale.openWrongFileBody });
return false;
}
if (fileSig[1] === kdbxweb.Consts.Signatures.Sig2Kdb) {
Alerts.error({ header: Locale.openWrongFile, body: Locale.openKdbFileBody });
return false;
}
if (fileSig[1] !== kdbxweb.Consts.Signatures.Sig2Kdbx) {
Alerts.error({ header: Locale.openWrongFile, body: Locale.openWrongFileBody });
return false;
}
return true;
},
2015-11-06 21:14:47 +01:00
displayOpenFile: function() {
this.$el.addClass('open--file');
2015-11-11 19:37:31 +01:00
this.$el.find('.open__settings-key-file').removeClass('hide');
2015-11-06 21:14:47 +01:00
this.inputEl[0].removeAttribute('readonly');
2015-12-17 19:25:25 +01:00
this.inputEl[0].setAttribute('placeholder', Locale.openPassFor + ' ' + this.params.name);
2016-05-13 14:09:21 +02:00
this.focusInput();
2015-11-06 21:14:47 +01:00
},
displayOpenKeyFile: function() {
2016-04-06 23:13:44 +02:00
this.$el.toggleClass('open--key-file', !!this.params.keyFileName);
this.$el.find('.open__settings-key-file-name').text(this.params.keyFileName || Locale.openKeyFile);
2016-05-13 14:09:21 +02:00
this.focusInput();
2015-11-06 21:14:47 +01:00
},
2016-03-04 20:21:34 +01:00
setFile: function(file, keyFile, fileReadyCallback) {
2015-11-06 21:14:47 +01:00
this.reading = 'fileData';
this.processFile(file, (function(success) {
if (success && keyFile) {
this.reading = 'keyFileData';
this.processFile(keyFile);
}
2016-03-04 20:21:34 +01:00
if (success && typeof fileReadyCallback === 'function') {
fileReadyCallback();
}
2015-11-06 21:14:47 +01:00
}).bind(this));
},
openFile: function() {
2015-12-06 21:32:41 +01:00
if (!this.busy) {
2016-03-12 12:22:35 +01:00
this.closeConfig();
2015-11-07 20:02:45 +01:00
this.openAny('fileData');
2015-11-06 21:14:47 +01:00
}
},
2016-03-01 04:29:20 +01:00
importFromXml: function() {
if (!this.busy) {
2016-03-12 12:22:35 +01:00
this.closeConfig();
2016-03-01 04:29:20 +01:00
this.openAny('fileXml', 'xml');
}
},
2015-11-06 21:14:47 +01:00
openKeyFile: function(e) {
if ($(e.target).hasClass('open__settings-key-file-dropbox')) {
this.openKeyFileFromDropbox();
2015-12-06 21:32:41 +01:00
} else if (!this.busy && this.params.name) {
if (this.params.keyFileData) {
this.params.keyFileData = null;
this.params.keyFileName = '';
2015-11-06 21:14:47 +01:00
this.$el.removeClass('open--key-file');
2016-04-06 23:13:44 +02:00
this.$el.find('.open__settings-key-file-name').text(Locale.openKeyFile);
2015-11-06 21:14:47 +01:00
} else {
this.openAny('keyFileData');
}
}
},
openKeyFileFromDropbox: function() {
2015-12-06 21:32:41 +01:00
if (!this.busy) {
2015-11-06 21:14:47 +01:00
DropboxLink.chooseFile((function(err, res) {
2015-11-08 21:23:12 +01:00
if (err) {
return;
}
2015-12-06 21:32:41 +01:00
this.params.keyFileData = res.data;
this.params.keyFileName = res.name;
2015-11-06 21:14:47 +01:00
this.displayOpenKeyFile();
}).bind(this));
}
},
openAny: function(reading, ext) {
this.reading = reading;
2015-12-06 21:32:41 +01:00
this.params[reading] = null;
2015-11-06 21:14:47 +01:00
this.$el.find('.open__file-ctrl').attr('accept', ext || '').val(null).click();
},
2015-12-06 21:32:41 +01:00
openLast: function(e) {
if (this.busy) {
return;
2015-11-06 21:14:47 +01:00
}
2015-12-06 21:32:41 +01:00
var id = $(e.target).closest('.open__last-item').data('id').toString();
if ($(e.target).is('.open__last-item-icon-del')) {
2016-02-23 07:08:57 +01:00
var fileInfo = this.model.fileInfos.get(id);
if (!fileInfo.get('storage') || fileInfo.get('modified')) {
2016-02-23 07:08:57 +01:00
var that = this;
Alerts.yesno({
header: Locale.openRemoveLastQuestion,
body: fileInfo.get('modified') ? Locale.openRemoveLastQuestionModBody : Locale.openRemoveLastQuestionBody,
2016-02-23 07:08:57 +01:00
buttons: [
{result: 'yes', title: Locale.alertYes},
{result: '', title: Locale.alertNo}
],
success: function() {
that.removeFile(id);
}
});
return;
}
this.removeFile(id);
2015-12-06 21:32:41 +01:00
return;
}
2015-12-07 20:07:56 +01:00
this.showOpenFileInfo(this.model.fileInfos.get(id));
2015-11-06 21:14:47 +01:00
},
2016-02-23 07:08:57 +01:00
removeFile: function(id) {
this.model.removeFileInfo(id);
this.$el.find('.open__last-item[data-id="' + id + '"]').remove();
this.initialize();
this.render();
},
2015-11-06 21:14:47 +01:00
inputKeydown: function(e) {
var code = e.keyCode || e.which;
2015-12-08 19:02:50 +01:00
if (code === Keys.DOM_VK_RETURN) {
2015-11-06 21:14:47 +01:00
this.openDb();
} else if (code === Keys.DOM_VK_CAPS_LOCK) {
2016-02-18 19:57:18 +01:00
this.toggleCapsLockWarning(false);
2015-11-06 22:32:51 +01:00
} else if (code === Keys.DOM_VK_A) {
e.stopImmediatePropagation();
2015-11-06 21:14:47 +01:00
}
},
2016-02-18 19:57:18 +01:00
inputKeyup: function(e) {
var code = e.keyCode || e.which;
if (code === Keys.DOM_VK_CAPS_LOCK) {
this.toggleCapsLockWarning(false);
}
},
2015-11-06 21:14:47 +01:00
inputKeypress: function(e) {
var charCode = e.keyCode || e.which;
var ch = String.fromCharCode(charCode),
lower = ch.toLowerCase(),
upper = ch.toUpperCase();
if (lower !== upper && !e.shiftKey) {
this.toggleCapsLockWarning(ch !== lower);
}
},
toggleCapsLockWarning: function(on) {
2016-02-18 19:57:18 +01:00
this.$el.find('.open__pass-warning').toggleClass('invisible', !on);
2015-11-06 21:14:47 +01:00
},
2015-12-06 21:32:41 +01:00
dragover: function(e) {
e.preventDefault();
if (this.dragTimeout) {
clearTimeout(this.dragTimeout);
}
if (!this.$el.hasClass('open--drag')) {
this.$el.addClass('open--drag');
}
},
dragleave: function() {
if (this.dragTimeout) {
clearTimeout(this.dragTimeout);
}
this.dragTimeout = setTimeout((function() {
this.$el.removeClass('open--drag');
}).bind(this), 100);
},
drop: function(e) {
e.preventDefault();
2016-03-12 12:22:35 +01:00
if (this.busy) {
return;
}
2015-12-06 21:32:41 +01:00
if (this.dragTimeout) {
clearTimeout(this.dragTimeout);
}
2016-03-12 12:22:35 +01:00
this.closeConfig();
2015-12-06 21:32:41 +01:00
this.$el.removeClass('open--drag');
2016-02-17 21:57:12 +01:00
var files = e.target.files || e.originalEvent.dataTransfer.files;
2015-12-06 21:32:41 +01:00
var dataFile = _.find(files, function(file) { return file.name.split('.').pop().toLowerCase() === 'kdbx'; });
var keyFile = _.find(files, function(file) { return file.name.split('.').pop().toLowerCase() === 'key'; });
if (dataFile) {
2016-03-04 20:21:34 +01:00
this.setFile(dataFile, keyFile,
dataFile.path ? null : this.showLocalFileAlert.bind(this));
2015-12-06 21:32:41 +01:00
}
},
2015-12-07 20:07:56 +01:00
showOpenFileInfo: function(fileInfo) {
if (this.busy || !fileInfo) {
return;
}
this.params.id = fileInfo.id;
this.params.storage = fileInfo.get('storage');
this.params.path = fileInfo.get('path');
this.params.name = fileInfo.get('name');
this.params.fileData = null;
this.params.rev = null;
2016-04-06 23:13:44 +02:00
this.params.keyFileName = fileInfo.get('keyFileName');
2015-12-07 20:07:56 +01:00
this.displayOpenFile();
2016-04-06 23:13:44 +02:00
this.displayOpenKeyFile();
2015-11-07 20:02:45 +01:00
},
2015-12-06 21:32:41 +01:00
showOpenLocalFile: function(path) {
2015-12-07 20:07:56 +01:00
if (this.busy) {
return;
}
this.params.id = null;
this.params.storage = 'file';
this.params.path = path;
this.params.name = path.match(/[^/\\]*$/)[0];
this.params.rev = null;
this.params.fileData = null;
this.displayOpenFile();
2015-11-07 20:02:45 +01:00
},
2015-12-06 21:32:41 +01:00
createDemo: function() {
if (!this.busy) {
2016-03-12 12:22:35 +01:00
this.closeConfig();
2015-12-06 21:32:41 +01:00
if (!this.model.createDemoFile()) {
this.trigger('close');
}
2016-03-13 10:54:16 +01:00
if (!this.model.settings.get('demoOpened')) {
this.model.settings.set('demoOpened', true);
}
2015-12-06 21:32:41 +01:00
}
2015-11-07 20:02:45 +01:00
},
2015-12-06 21:32:41 +01:00
createNew: function() {
if (!this.busy) {
this.model.createNewFile();
2015-11-07 20:02:45 +01:00
}
},
2015-12-06 21:32:41 +01:00
openDb: function() {
2016-06-05 16:49:00 +02:00
if (this.params.id && this.model.files.get(this.params.id)) {
this.trigger('close');
return;
}
2015-12-08 22:00:31 +01:00
if (this.busy || !this.params.name) {
return;
2015-11-07 20:02:45 +01:00
}
2015-12-08 22:00:31 +01:00
this.$el.toggleClass('open--opening', true);
this.inputEl.attr('disabled', 'disabled');
this.busy = true;
this.params.password = this.passwordInput.value;
this.afterPaint(this.model.openFile.bind(this.model, this.params, this.openDbComplete.bind(this)));
2015-11-07 20:02:45 +01:00
},
2015-12-06 21:32:41 +01:00
openDbComplete: function(err) {
this.busy = false;
this.$el.toggleClass('open--opening', false);
this.inputEl.removeAttr('disabled').toggleClass('input--error', !!err);
if (err) {
2015-12-12 09:53:50 +01:00
logger.error('Error opening file', err);
2016-05-13 14:09:21 +02:00
this.focusInput();
2015-12-06 21:32:41 +01:00
this.inputEl[0].selectionStart = 0;
this.inputEl[0].selectionEnd = this.inputEl.val().length;
2016-03-15 20:08:08 +01:00
if (err.code !== 'InvalidKey') {
Alerts.error({
header: Locale.openError,
body: Locale.openErrorDescription + '<pre class="modal__pre">' + _.escape(err.toString()) +'</pre>'
});
}
2015-12-06 21:32:41 +01:00
} else {
this.trigger('close');
2015-11-07 20:02:45 +01:00
}
2016-03-01 04:29:20 +01:00
},
importDbWithXml: function() {
2016-03-01 04:29:20 +01:00
if (this.busy || !this.params.name) {
return;
}
this.$el.toggleClass('open--opening', true);
this.inputEl.attr('disabled', 'disabled');
this.busy = true;
this.afterPaint(this.model.importFileWithXml.bind(this.model, this.params, this.openDbComplete.bind(this)));
2016-03-09 21:52:19 +01:00
},
toggleMore: function() {
2016-03-12 12:22:35 +01:00
if (this.busy) {
return;
}
this.closeConfig();
2016-03-09 21:52:19 +01:00
this.$el.find('.open__icons--lower').toggleClass('hide');
2016-03-12 12:22:35 +01:00
},
openSettings: function() {
Backbone.trigger('toggle-settings');
},
2016-03-13 17:08:25 +01:00
openStorage: function(e) {
2016-03-12 12:22:35 +01:00
if (this.busy) {
return;
}
2016-03-13 17:08:25 +01:00
var storage = Storage[$(e.target).closest('.open__icon').data('storage')];
if (!storage) {
return;
}
if (storage.needShowOpenConfig && storage.needShowOpenConfig()) {
this.showConfig(storage);
} else if (storage.list) {
this.listStorage(storage);
} else {
Alerts.notImplemented();
}
},
listStorage: function(storage) {
if (this.busy) {
return;
}
this.closeConfig();
var icon = this.$el.find('.open__icon-storage[data-storage=' + storage.name + ']');
var that = this;
that.busy = true;
icon.toggleClass('flip3d', true);
2016-03-14 06:16:33 +01:00
storage.list(function(err, files, dir) {
2016-03-13 17:08:25 +01:00
icon.toggleClass('flip3d', false);
that.busy = false;
if (err || !files) {
return;
}
var buttons = [];
var allStorageFiles = {};
files.forEach(function (file) {
var fileName = UrlUtil.getDataFileName(file.name);
buttons.push({result: file.path, title: fileName});
allStorageFiles[file.path] = file;
});
if (!buttons.length) {
2016-04-10 08:44:17 +02:00
var body = Locale.openNothingFoundBody;
if (dir) {
body += ' ' + Locale.openNothingFoundBodyFolder.replace('{}', dir);
}
2016-03-14 06:16:33 +01:00
Alerts.error({
header: Locale.openNothingFound,
2016-04-10 08:44:17 +02:00
body: body
2016-03-14 06:16:33 +01:00
});
2016-03-13 17:08:25 +01:00
return;
}
buttons.push({result: '', title: Locale.alertCancel});
Alerts.alert({
header: Locale.openSelectFile,
body: Locale.openSelectFileBody,
icon: storage.icon || 'files-o',
buttons: buttons,
esc: '',
click: '',
success: function (file) {
that.openStorageFile(storage, allStorageFiles[file]);
}
});
});
},
openStorageFile: function(storage, file) {
if (this.busy) {
return;
}
this.params.id = null;
this.params.storage = storage.name;
this.params.path = file.path;
this.params.name = UrlUtil.getDataFileName(file.name);
this.params.rev = file.rev;
this.params.fileData = null;
this.displayOpenFile();
2016-03-12 12:22:35 +01:00
},
showConfig: function(storage) {
if (this.busy) {
return;
}
if (this.views.openConfig) {
this.views.openConfig.remove();
}
2016-03-13 17:45:55 +01:00
var config = _.extend({
2016-03-12 12:22:35 +01:00
id: storage.name,
name: Locale[storage.name] || storage.name,
2016-03-13 17:45:55 +01:00
icon: storage.icon
}, storage.getOpenConfig());
2016-03-12 12:22:35 +01:00
this.views.openConfig = new OpenConfigView({ el: this.$el.find('.open__config-wrap'), model: config }).render();
this.views.openConfig.on('cancel', this.closeConfig.bind(this));
this.views.openConfig.on('apply', this.applyConfig.bind(this));
2016-03-13 17:08:25 +01:00
this.$el.find('.open__pass-area').addClass('hide');
this.$el.find('.open__icons--lower').addClass('hide');
2016-03-12 12:22:35 +01:00
},
closeConfig: function() {
if (this.busy) {
this.storageWaitId = null;
this.busy = false;
}
if (this.views.openConfig) {
this.views.openConfig.remove();
delete this.views.openConfig;
}
this.$el.find('.open__pass-area').removeClass('hide');
this.$el.find('.open__config').addClass('hide');
2016-05-13 14:09:21 +02:00
this.focusInput();
2016-03-12 12:22:35 +01:00
},
applyConfig: function(config) {
if (this.busy || !config) {
return;
}
this.busy = true;
this.views.openConfig.setDisabled(true);
var storage = Storage[config.storage];
this.storageWaitId = Math.random();
2016-03-12 17:49:52 +01:00
var path = config.path;
2016-03-19 13:43:50 +01:00
var opts = _.omit(config, ['path', 'storage']);
2016-03-12 17:49:52 +01:00
var req = {
waitId: this.storageWaitId,
storage: config.storage,
path: path,
opts: opts
};
2016-03-13 17:45:55 +01:00
if (storage.applyConfig) {
storage.applyConfig(opts, this.storageApplyConfigComplete.bind(this, req));
} else {
storage.stat(path, opts, this.storageStatComplete.bind(this, req));
}
},
storageApplyConfigComplete: function(req, err) {
if (this.storageWaitId !== req.waitId) {
return;
}
this.storageWaitId = null;
this.busy = false;
if (err) {
this.views.openConfig.setDisabled(false);
this.views.openConfig.setError(err);
} else {
this.closeConfig();
}
2016-03-12 12:22:35 +01:00
},
2016-03-12 17:49:52 +01:00
storageStatComplete: function(req, err, stat) {
if (this.storageWaitId !== req.waitId) {
2016-03-12 12:22:35 +01:00
return;
}
this.storageWaitId = null;
this.busy = false;
if (err) {
2016-03-12 17:49:52 +01:00
this.views.openConfig.setDisabled(false);
2016-03-12 12:22:35 +01:00
this.views.openConfig.setError(err);
2016-03-12 17:49:52 +01:00
} else {
this.closeConfig();
this.params.id = null;
this.params.storage = req.storage;
this.params.path = req.path;
this.params.opts = req.opts;
this.params.name = UrlUtil.getDataFileName(req.path);
this.params.rev = stat.rev;
this.params.fileData = null;
this.displayOpenFile();
2016-03-12 12:22:35 +01:00
}
2015-10-17 23:49:24 +02:00
}
});
module.exports = OpenView;