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

450 lines
14 KiB
JavaScript
Raw Normal View History

2015-10-17 23:49:24 +02:00
'use strict';
var Backbone = require('backbone'),
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'),
2015-12-17 19:25:25 +01:00
Logger = require('../util/logger'),
Locale = require('../util/locale');
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',
'click .open__icon-dropbox': 'openFromDropbox',
'click .open__icon-demo': 'createDemo',
'click .open__pass-input[readonly]': 'openFile',
'input .open__pass-input': 'inputInput',
'keydown .open__pass-input': 'inputKeydown',
'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'
},
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 () {
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);
}
2015-11-15 21:08:26 +01:00
this.renderTemplate({ lastOpenFiles: this.getLastOpenFiles() });
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
},
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) {
var icon;
2015-12-07 22:20:18 +01:00
switch (f.get('storage')) {
2015-11-07 20:02:45 +01:00
case 'dropbox':
2015-12-06 21:32:41 +01:00
icon = 'dropbox';
break;
case 'file':
icon = 'hdd-o';
2015-11-07 20:02:45 +01:00
break;
default:
2015-12-06 21:32:41 +01:00
icon = 'file-text';
2015-11-07 20:02:45 +01:00
break;
}
2015-12-06 21:32:41 +01:00
return {
id: f.get('id'),
name: f.get('name'),
icon: icon
};
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) {
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-02-23 13:16:50 +01:00
if (!file.path) {
this.showLocalFileAlert();
}
2015-11-06 21:14:47 +01:00
this.processFile(file);
}
},
processFile: function(file, complete) {
var reader = new FileReader();
reader.onload = (function(e) {
if (this.reading === 'fileData') {
2015-12-07 20:07:56 +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;
2015-11-06 21:14:47 +01:00
this.displayOpenFile();
} else {
2015-12-07 20:07:56 +01:00
this.params.keyFileData = e.target.result;
this.params.keyFileName = file.name;
2015-11-06 21:14:47 +01:00
this.displayOpenKeyFile();
}
if (complete) {
complete(true);
}
}).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);
reader.readAsArrayBuffer(file);
},
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);
2015-11-06 21:14:47 +01:00
this.inputEl.focus();
},
displayOpenKeyFile: function() {
2015-12-06 21:32:41 +01:00
this.$el.find('.open__settings-key-file-name').text(this.params.keyFileName);
2015-11-06 21:14:47 +01:00
this.$el.addClass('open--key-file');
this.inputEl.focus();
},
setFile: function(file, keyFile) {
this.reading = 'fileData';
this.processFile(file, (function(success) {
if (success && keyFile) {
this.reading = 'keyFileData';
this.processFile(keyFile);
}
}).bind(this));
},
openFile: function() {
2015-12-06 21:32:41 +01:00
if (!this.busy) {
2015-11-07 20:02:45 +01:00
this.openAny('fileData');
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');
this.$el.find('.open__settings-key-file-name').text('key file');
} 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')) {
var that = this;
Alerts.yesno({
header: Locale.openRemoveLastQuestion,
body: Locale.openRemoveLastQuestionBody,
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) {
this.$el.find('.open__pass-warning').removeClass('invisible');
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
}
},
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) {
this.$el.find('.open__file-warning').toggleClass('invisible', on);
},
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();
if (this.dragTimeout) {
clearTimeout(this.dragTimeout);
}
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-02-23 13:16:50 +01:00
if (!dataFile.path) {
this.showLocalFileAlert();
}
2015-12-06 21:32:41 +01:00
this.setFile(dataFile, keyFile);
}
},
displayDropboxLoading: function(isLoading) {
this.$el.find('.open__icon-dropbox .open__icon-i').toggleClass('flip3d', !!isLoading);
},
2015-11-06 21:14:47 +01:00
openFromDropbox: function() {
2015-12-06 21:32:41 +01:00
if (this.busy) {
2015-11-06 21:14:47 +01:00
return;
}
var that = this;
DropboxLink.authenticate(function(err) {
if (err) {
return;
}
2015-12-06 21:32:41 +01:00
that.busy = true;
that.displayDropboxLoading(true);
2015-12-07 20:07:56 +01:00
DropboxLink.getFileList(function(err, files, dirStat, filesStat) {
2015-12-06 21:32:41 +01:00
that.busy = false;
that.displayDropboxLoading(false);
2015-11-06 21:14:47 +01:00
if (err) {
return;
}
var buttons = [];
2015-12-07 20:07:56 +01:00
var allDropboxFiles = {};
filesStat.forEach(function(file) {
if (!file.isFolder && !file.isRemoved) {
var fileName = file.name.replace(/\.kdbx/i, '');
2015-12-07 22:00:44 +01:00
buttons.push({ result: file.path, title: fileName });
allDropboxFiles[file.path] = file;
2015-12-07 20:07:56 +01:00
}
2015-11-06 21:14:47 +01:00
});
if (!buttons.length) {
Alerts.error({
2015-12-17 19:25:25 +01:00
header: Locale.openNothingFound,
body: Locale.openNothingFoundBody + (dirStat && dirStat.inAppFolder ? ' ' + Locale.openNothingFoundBodyAppFolder : '')
2015-11-06 21:14:47 +01:00
});
return;
}
2015-12-17 19:25:25 +01:00
buttons.push({ result: '', title: Locale.alertCancel });
2015-11-06 21:14:47 +01:00
Alerts.alert({
2015-12-17 19:25:25 +01:00
header: Locale.openSelectFile,
body: Locale.openSelectFileBody,
2015-11-06 21:14:47 +01:00
icon: 'dropbox',
buttons: buttons,
esc: '',
click: '',
2015-12-07 20:07:56 +01:00
success: function(file) {
that.openDropboxFile(allDropboxFiles[file]);
}
2015-11-06 21:14:47 +01:00
});
2015-12-06 21:32:41 +01:00
that.model.fileInfos.forEach(function(fi) {
2015-12-07 22:00:44 +01:00
if (fi.get('storage') === 'dropbox' && !fi.get('modified') && !allDropboxFiles[fi.get('path')]) {
2015-12-06 23:13:29 +01:00
that.model.removeFileInfo(fi.id);
2015-11-07 20:02:45 +01:00
}
});
2015-11-06 21:14:47 +01:00
});
});
},
2015-12-07 20:07:56 +01:00
openDropboxFile: function(fileStat) {
if (this.busy) {
return;
}
this.params.id = null;
this.params.storage = 'dropbox';
this.params.path = fileStat.path;
this.params.name = fileStat.name.replace(/\.kdbx/i, '');
this.params.rev = fileStat.versionTag;
this.params.fileData = null;
this.displayOpenFile();
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;
this.displayOpenFile();
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) {
if (!this.model.createDemoFile()) {
this.trigger('close');
}
}
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() {
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);
2015-12-06 21:32:41 +01:00
this.inputEl.focus();
this.inputEl[0].selectionStart = 0;
this.inputEl[0].selectionEnd = this.inputEl.val().length;
} else {
this.trigger('close');
2015-11-07 20:02:45 +01:00
}
2015-10-17 23:49:24 +02:00
}
});
module.exports = OpenView;