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

205 lines
5.7 KiB
JavaScript
Raw Normal View History

2019-09-16 20:42:33 +02:00
import { View } from 'framework/views/view';
2019-09-16 22:57:56 +02:00
import { Events } from 'framework/events';
2019-09-15 14:16:32 +02:00
import { GeneratorPresets } from 'comp/app/generator-presets';
import { CopyPaste } from 'comp/browser/copy-paste';
import { AppSettingsModel } from 'models/app-settings-model';
import { PasswordGenerator } from 'util/generators/password-generator';
2019-09-27 07:37:26 +02:00
import { PasswordPresenter } from 'util/formatting/password-presenter';
2019-09-15 14:16:32 +02:00
import { Locale } from 'util/locale';
import { Tip } from 'util/ui/tip';
2019-09-15 18:33:45 +02:00
import template from 'templates/generator.hbs';
2015-10-18 23:30:29 +02:00
2019-09-15 18:33:45 +02:00
class GeneratorView extends View {
parent = 'body';
2015-10-18 23:30:29 +02:00
2019-09-15 18:33:45 +02:00
template = template;
2015-10-18 23:30:29 +02:00
2019-09-15 18:33:45 +02:00
events = {
2015-10-19 23:24:32 +02:00
'click': 'click',
2015-10-20 19:17:15 +02:00
'mousedown .gen__length-range': 'generate',
2016-09-14 20:04:37 +02:00
'input .gen__length-range': 'lengthChange',
2015-10-19 23:24:32 +02:00
'change .gen__length-range': 'lengthChange',
2015-10-20 21:58:07 +02:00
'change .gen__check input[type=checkbox]': 'checkChange',
'change .gen__check-hide': 'hideChange',
2016-02-15 22:06:11 +01:00
'click .gen__btn-ok': 'btnOkClick',
2016-08-13 21:13:16 +02:00
'change .gen__sel-tpl': 'presetChange',
2016-02-23 07:43:43 +01:00
'click .gen__btn-refresh': 'newPass'
2019-09-15 18:33:45 +02:00
};
2015-10-18 23:30:29 +02:00
2019-09-15 18:33:45 +02:00
valuesMap = [
2019-08-18 08:05:38 +02:00
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
16,
17,
18,
19,
20,
22,
24,
26,
28,
30,
32,
48,
64
2019-09-15 18:33:45 +02:00
];
2019-09-15 18:33:45 +02:00
presets = null;
preset = null;
2016-02-15 22:06:11 +01:00
2019-09-15 18:33:45 +02:00
constructor(model) {
super(model);
2016-02-15 22:06:11 +01:00
this.createPresets();
2017-01-31 07:50:28 +01:00
const preset = this.preset;
2020-06-01 16:53:51 +02:00
this.gen = { ...this.presets.find((pr) => pr.name === preset) };
2019-09-17 19:50:42 +02:00
this.hide = AppSettingsModel.generatorHidePassword;
2015-10-19 23:24:32 +02:00
$('body').one('click', this.remove.bind(this));
2019-09-16 22:57:56 +02:00
this.listenTo(Events, 'lock-workspace', this.remove.bind(this));
2019-09-15 18:33:45 +02:00
}
2015-10-18 23:30:29 +02:00
2019-08-18 10:17:09 +02:00
render() {
2017-01-31 07:50:28 +01:00
const canCopy = document.queryCommandSupported('copy');
2019-08-18 08:05:38 +02:00
const btnTitle = this.model.copy
? canCopy
? Locale.alertCopy
: Locale.alertClose
: Locale.alertOk;
2019-09-15 18:33:45 +02:00
super.render({
2019-08-18 10:17:09 +02:00
btnTitle,
2019-08-15 04:59:53 +02:00
showToggleButton: this.model.copy,
opt: this.gen,
hide: this.hide,
presets: this.presets,
preset: this.preset,
showTemplateEditor: !this.model.noTemplateEditor
});
2015-10-20 21:58:07 +02:00
this.resultEl = this.$el.find('.gen__result');
2015-10-18 23:30:29 +02:00
this.$el.css(this.model.pos);
2015-10-19 23:24:32 +02:00
this.generate();
2019-09-15 18:33:45 +02:00
}
2015-10-18 23:30:29 +02:00
2019-08-18 10:17:09 +02:00
createPresets() {
2016-08-14 18:18:51 +02:00
this.presets = GeneratorPresets.enabled;
2019-08-18 08:05:38 +02:00
if (
this.model.password &&
(!this.model.password.isProtected || this.model.password.byteLength)
) {
2017-01-31 07:50:28 +01:00
const derivedPreset = { name: 'Derived', title: Locale.genPresetDerived };
2019-09-17 22:17:40 +02:00
Object.assign(derivedPreset, PasswordGenerator.deriveOpts(this.model.password));
2016-08-14 18:18:51 +02:00
this.presets.splice(0, 0, derivedPreset);
2016-02-15 22:06:11 +01:00
this.preset = 'Derived';
} else {
2020-06-01 16:53:51 +02:00
const defaultPreset = this.presets.filter((p) => p.default)[0] || this.presets[0];
2016-08-14 18:18:51 +02:00
this.preset = defaultPreset.name;
2016-02-15 22:06:11 +01:00
}
2020-06-01 16:53:51 +02:00
this.presets.forEach((pr) => {
2016-08-14 18:18:51 +02:00
pr.pseudoLength = this.lengthToPseudoValue(pr.length);
2019-09-15 18:33:45 +02:00
});
}
2016-02-15 22:06:11 +01:00
2019-08-18 10:17:09 +02:00
lengthToPseudoValue(length) {
2016-08-14 18:18:51 +02:00
for (let ix = 0; ix < this.valuesMap.length; ix++) {
if (this.valuesMap[ix] >= length) {
return ix;
}
}
return this.valuesMap.length - 1;
2019-09-15 18:33:45 +02:00
}
2016-08-14 18:18:51 +02:00
2019-08-18 10:17:09 +02:00
showPassword() {
if (this.hide && !this.model.copy) {
2019-09-27 07:37:26 +02:00
this.resultEl.text(PasswordPresenter.present(this.password.length));
2019-08-06 01:22:29 +02:00
} else {
this.resultEl.text(this.password);
}
2019-09-15 18:33:45 +02:00
}
2019-08-06 01:22:29 +02:00
2019-08-18 10:17:09 +02:00
click(e) {
2015-10-18 23:30:29 +02:00
e.stopPropagation();
2019-09-15 18:33:45 +02:00
}
2015-10-19 23:24:32 +02:00
2019-08-18 10:17:09 +02:00
lengthChange(e) {
2017-01-31 07:50:28 +01:00
const val = this.valuesMap[e.target.value];
2015-10-19 23:24:32 +02:00
if (val !== this.gen.length) {
this.gen.length = val;
2020-05-09 17:00:39 +02:00
this.$el.find('.gen__length-range-val').text(val);
2016-02-15 22:06:11 +01:00
this.optionChanged('length');
2015-10-19 23:24:32 +02:00
this.generate();
}
2019-09-15 18:33:45 +02:00
}
2015-10-19 23:24:32 +02:00
2019-08-18 10:17:09 +02:00
checkChange(e) {
2017-01-31 07:50:28 +01:00
const id = $(e.target).data('id');
2015-10-19 23:24:32 +02:00
if (id) {
this.gen[id] = e.target.checked;
}
2016-02-15 22:06:11 +01:00
this.optionChanged(id);
2015-10-19 23:24:32 +02:00
this.generate();
2019-09-15 18:33:45 +02:00
}
2015-10-19 23:24:32 +02:00
2019-08-18 10:17:09 +02:00
optionChanged(option) {
2019-08-16 23:05:39 +02:00
if (
this.preset === 'Custom' ||
(this.preset === 'Pronounceable' && ['length', 'lower', 'upper'].indexOf(option) >= 0)
) {
2016-02-15 22:06:11 +01:00
return;
}
this.preset = this.gen.name = 'Custom';
this.$el.find('.gen__sel-tpl').val('');
2019-09-15 18:33:45 +02:00
}
2016-02-15 22:06:11 +01:00
2019-08-18 10:17:09 +02:00
generate() {
2015-10-20 21:58:07 +02:00
this.password = PasswordGenerator.generate(this.gen);
2019-08-06 01:22:29 +02:00
this.showPassword();
2017-01-31 07:50:28 +01:00
const isLong = this.password.length > 32;
this.resultEl.toggleClass('gen__result--long-pass', isLong);
2019-09-15 18:33:45 +02:00
}
2015-10-20 21:58:07 +02:00
2019-08-18 10:17:09 +02:00
hideChange(e) {
2019-08-06 01:22:29 +02:00
this.hide = e.target.checked;
2019-09-17 19:50:42 +02:00
AppSettingsModel.generatorHidePassword = this.hide;
const label = this.$el.find('.gen__check-hide-label');
2019-08-16 23:05:39 +02:00
Tip.updateTip(label[0], { title: this.hide ? Locale.genShowPass : Locale.genHidePass });
2019-08-06 01:22:29 +02:00
this.showPassword();
2019-09-15 18:33:45 +02:00
}
2019-08-06 01:22:29 +02:00
2019-08-18 10:17:09 +02:00
btnOkClick() {
if (!CopyPaste.simpleCopy) {
CopyPaste.createHiddenInput(this.password);
}
2016-01-22 18:51:36 +01:00
CopyPaste.copy(this.password);
2019-09-15 18:33:45 +02:00
this.emit('result', this.password);
2015-10-20 21:58:07 +02:00
this.remove();
2019-09-15 18:33:45 +02:00
}
2016-02-15 22:06:11 +01:00
2019-08-18 10:17:09 +02:00
presetChange(e) {
2017-01-31 07:50:28 +01:00
const name = e.target.value;
2016-08-13 21:13:16 +02:00
if (name === '...') {
2019-09-16 22:57:56 +02:00
Events.emit('edit-generator-presets');
2016-08-13 21:13:16 +02:00
this.remove();
return;
}
2016-02-15 22:06:11 +01:00
this.preset = name;
2020-06-01 16:53:51 +02:00
const preset = this.presets.find((t) => t.name === name);
2019-09-17 23:44:17 +02:00
this.gen = { ...preset };
2016-02-15 22:06:11 +01:00
this.render();
2019-09-15 18:33:45 +02:00
}
2016-02-23 07:43:43 +01:00
2019-08-18 10:17:09 +02:00
newPass() {
2016-02-23 21:31:21 +01:00
this.generate();
2015-10-18 23:30:29 +02:00
}
2019-09-15 18:33:45 +02:00
}
2015-10-18 23:30:29 +02:00
2019-09-15 14:16:32 +02:00
export { GeneratorView };