1
0
mirror of https://github.com/keeweb/keeweb.git synced 2024-06-26 07:39:04 +02:00
keeweb/app/scripts/views/open-config-view.js

82 lines
2.0 KiB
JavaScript
Raw Normal View History

2019-09-16 20:42:33 +02:00
import { View } from 'framework/views/view';
2019-09-15 14:16:32 +02:00
import { Keys } from 'const/keys';
import { Locale } from 'util/locale';
2019-09-15 23:24:53 +02:00
import template from 'templates/open-config.hbs';
2016-03-12 12:22:35 +01:00
2019-09-15 23:24:53 +02:00
class OpenConfigView extends View {
template = template;
2016-03-12 12:22:35 +01:00
2019-09-15 23:24:53 +02:00
events = {
2016-03-12 12:22:35 +01:00
'click .open__config-btn-cancel': 'cancel',
'click .open__config-btn-ok': 'apply',
'input input': 'changeInput',
'keyup input': 'keyup'
2019-09-15 23:24:53 +02:00
};
2016-03-12 12:22:35 +01:00
2019-08-18 10:17:09 +02:00
render() {
2019-09-15 23:24:53 +02:00
super.render(this.model);
2016-03-12 12:22:35 +01:00
this.$el.find(':input:first').focus();
this.checkValidity();
2019-09-15 23:24:53 +02:00
}
2016-03-12 12:22:35 +01:00
2019-08-18 10:17:09 +02:00
cancel() {
2019-09-15 23:24:53 +02:00
this.emit('cancel');
}
2016-03-12 12:22:35 +01:00
2019-08-18 10:17:09 +02:00
apply() {
2017-01-31 07:50:28 +01:00
const data = this.getData();
2016-03-12 12:22:35 +01:00
if (data) {
2019-09-15 23:24:53 +02:00
this.emit('apply', data);
2016-03-12 12:22:35 +01:00
}
2019-09-15 23:24:53 +02:00
}
2016-03-12 12:22:35 +01:00
2019-08-18 10:17:09 +02:00
changeInput() {
2016-03-12 12:22:35 +01:00
this.checkValidity();
2019-09-15 23:24:53 +02:00
}
2016-03-12 12:22:35 +01:00
2019-08-18 10:17:09 +02:00
keyup(e) {
2016-03-12 12:22:35 +01:00
if (e.which === Keys.DOM_VK_RETURN) {
this.apply();
}
2019-09-15 23:24:53 +02:00
}
2016-03-12 12:22:35 +01:00
2019-08-18 10:17:09 +02:00
checkValidity() {
2017-01-31 07:50:28 +01:00
const isValid = this.getData();
2016-03-12 12:22:35 +01:00
this.$el.find('.open__config-btn-ok').prop('disabled', !isValid);
2019-09-15 23:24:53 +02:00
}
2016-03-12 12:22:35 +01:00
2019-08-18 10:17:09 +02:00
getData() {
2017-01-31 07:50:28 +01:00
let data = { storage: this.model.id };
2020-06-01 16:53:51 +02:00
this.model.fields.every(function (field) {
2017-01-31 07:50:28 +01:00
const input = this.$el.find('#open__config-field-' + field.id)[0];
2016-03-12 12:22:35 +01:00
if (data && input.checkValidity()) {
data[field.id] = input.value;
} else {
data = null;
return false;
}
return true;
}, this);
return data;
2019-09-15 23:24:53 +02:00
}
2016-03-12 12:22:35 +01:00
2019-08-18 10:17:09 +02:00
setDisabled(disabled) {
2016-03-12 12:22:35 +01:00
disabled = !!disabled;
this.$el.find(':input:not(.open__config-btn-cancel)').prop('disabled', disabled);
this.$el.toggleClass('open__config--disabled', disabled);
if (disabled) {
this.$el.find('.open__config-error').text('');
}
2019-09-15 23:24:53 +02:00
}
2016-03-12 12:22:35 +01:00
2019-08-18 10:17:09 +02:00
setError(err) {
2019-08-16 23:05:39 +02:00
const errText =
2019-08-18 08:05:38 +02:00
err && err.notFound
? Locale.openConfigErrorNotFound
: Locale.openConfigError.replace('{}', err);
2016-03-12 18:23:28 +01:00
this.$el.find('.open__config-error').text(errText);
2016-03-12 12:22:35 +01:00
}
2019-09-15 23:24:53 +02:00
}
2016-03-12 12:22:35 +01:00
2019-09-15 14:16:32 +02:00
export { OpenConfigView };