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

440 lines
15 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'),
DropboxLink = require('../comp/dropbox-link');
2015-10-17 23:49:24 +02:00
var OpenView = Backbone.View.extend({
template: require('templates/open.html'),
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,
offline: false,
availOffline: false,
keyFileName: null,
keyFileData: null,
fileData: null
};
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-11-07 20:02:45 +01:00
switch (f.storage) {
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);
},
fileSelected: function(e) {
var file = e.target.files[0];
if (file) {
this.processFile(file);
}
},
processFile: function(file, complete) {
var reader = new FileReader();
reader.onload = (function(e) {
2015-12-06 21:32:41 +01:00
this.params[this.reading] = e.target.result;
2015-11-06 21:14:47 +01:00
if (this.reading === 'fileData') {
2015-12-06 21:32:41 +01:00
_.extend(this.params, { name: file.name.replace(/\.\w+$/i, ''), offline: false });
2015-11-06 21:14:47 +01:00
if (file.path) {
2015-12-06 21:32:41 +01:00
_.extend(this.params, { path: file.path, storage: file.storage || 'file' });
2015-11-06 21:14:47 +01:00
}
this.displayOpenFile();
} else {
2015-12-06 21:32:41 +01:00
_.extend(this.params, { keyFileName: file.name });
2015-11-06 21:14:47 +01:00
this.displayOpenKeyFile();
}
if (complete) {
complete(true);
}
}).bind(this);
reader.onerror = (function() {
Alerts.error({ header: 'Failed to read file' });
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.$el.find('#open__settings-check-offline')[0].removeAttribute('disabled');
2015-12-06 21:32:41 +01:00
var canSwitchOffline = this.params.storage !== 'file' && !this.params.offline;
2015-11-07 20:02:45 +01:00
this.$el.find('.open__settings-offline').toggleClass('hide', !canSwitchOffline);
2015-12-06 21:32:41 +01:00
this.$el.find('.open__settings-offline-warning').toggleClass('hide', !this.params.offline);
2015-11-06 21:14:47 +01:00
this.inputEl[0].removeAttribute('readonly');
2015-12-06 21:32:41 +01:00
this.inputEl[0].setAttribute('placeholder', 'Password for ' + 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));
},
2015-11-17 21:57:32 +01:00
showClosedFile: function(file) {
2015-12-06 21:32:41 +01:00
this.params = {
id: file.get('id'),
name: file.get('name'),
storage: file.get('storage'),
path: file.get('path'),
offline: file.get('offline'),
availOffline: file.get('availOffline'),
keyFileName: null,
keyFileData: null,
fileData: file.data
};
2015-11-17 21:57:32 +01:00
this.displayOpenFile();
},
2015-11-06 21:14:47 +01:00
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')) {
this.model.removeFileInfo(id);
this.$el.find('.open__last-item[data-id="' + id + '"]').remove();
return;
}
//var lastOpenFile = LastOpenFiles.byName(name);
//switch (lastOpenFile.storage) {
// case 'dropbox':
// return this.openDropboxFile(lastOpenFile.path);
// case 'file':
// return this.showOpenLocalFile(lastOpenFile.path);
// default:
// return this.openCache(name);
//}
2015-11-06 21:14:47 +01:00
},
inputKeydown: function(e) {
var code = e.keyCode || e.which;
if (code === Keys.DOM_VK_RETURN && this.passwordInput.length) {
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');
var files = e.target.files || e.dataTransfer.files;
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) {
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);
DropboxLink.getFileList(function(err, files, dirStat) {
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-11-07 20:02:45 +01:00
var allFileNames = {};
2015-11-06 21:14:47 +01:00
files.forEach(function(file) {
2015-11-07 20:02:45 +01:00
var fileName = file.replace(/\.kdbx/i, '');
buttons.push({ result: file, title: fileName });
allFileNames[fileName] = true;
2015-11-06 21:14:47 +01:00
});
if (!buttons.length) {
Alerts.error({
header: 'Nothing found',
body: 'You have no files in your Dropbox which could be opened.' +
2015-12-06 21:32:41 +01:00
(dirStat && dirStat.inAppFolder ? ' Files are searched inside app folder in your Dropbox.' : '')
2015-11-06 21:14:47 +01:00
});
return;
}
buttons.push({ result: '', title: 'Cancel' });
Alerts.alert({
header: 'Select a file',
body: 'Select a file from your Dropbox which you would like to open',
icon: 'dropbox',
buttons: buttons,
esc: '',
click: '',
2015-12-06 21:32:41 +01:00
success: that.openDropboxFile.bind(that)
2015-11-06 21:14:47 +01:00
});
2015-12-06 21:32:41 +01:00
that.model.fileInfos.forEach(function(fi) {
if (fi.get('storage') === 'dropbox' && !fi.get('modified') && !allFileNames[fi.get('name')]) {
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
});
});
},
openDropboxFile: function(file) {
2015-12-06 21:32:41 +01:00
//var fileName = file.replace(/\.kdbx/i, '');
//this.busy = true;
//var lastOpen = LastOpenFiles.byName(fileName);
//var errorAlertCallback = lastOpen && lastOpen.storage === 'dropbox' && lastOpen.availOffline ?
// this.dropboxErrorCallback.bind(this, fileName) : null;
//DropboxLink.openFile(file, (function(err, data) {
// this.busy = false;
// if (err || !data || !data.size) {
// return;
// }
// Object.defineProperties(data, {
// storage: { value: 'dropbox' },
// path: { value: file },
// name: { value: fileName }
// });
// this.setFile(data);
//}).bind(this), errorAlertCallback);
},
showOpenFileInfo: function() {
2015-11-07 20:02:45 +01:00
},
2015-12-06 21:32:41 +01:00
showOpenLocalFile: function(path) {
//if (path && Launcher) {
// var that = this;
// Storage.file.load(path, function(data, err) {
// if (!err) {
// var name = path.match(/[^/\\]*$/)[0];
// var file = new Blob([data]);
// Object.defineProperties(file, {
// path: { value: path },
// name: { value: name }
// });
// that.setFile(file);
// }
// });
//}
2015-11-07 20:02:45 +01:00
},
openCache: function(name, storage) {
2015-12-06 21:32:41 +01:00
//Storage.cache.load(name, (function(data, err) {
// if (err) {
// this.delLast(name);
// Alerts.error({
// header: 'Error loading file',
// body: 'There was an error loading offline file ' + name + '. Please, open it from file'
// });
// } else {
// this.fileData = data;
// this.file.set({ name: name, offline: true, availOffline: true });
// if (storage) {
// this.file.set({ storage: storage });
// }
// this.displayOpenFile();
// }
//}).bind(this));
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() {
if (!this.busy) {
var offlineChecked = this.$el.find('#open__settings-check-offline').is(':checked');
this.params.availOffline = this.params.offline ||
this.params.storage !== 'file' && offlineChecked;
this.$el.toggleClass('open--opening', true);
this.inputEl.attr('disabled', 'disabled');
this.$el.find('#open__settings-check-offline').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);
this.$el.find('#open__settings-check-offline').removeAttr('disabled');
if (err) {
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;