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

169 lines
5.5 KiB
JavaScript
Raw Normal View History

2016-08-14 18:18:51 +02:00
const Backbone = require('backbone');
const PasswordGenerator = require('../util/password-generator');
const CopyPaste = require('../comp/copy-paste');
2019-08-06 01:22:29 +02:00
const AppSettingsModel = require('../models/app-settings-model');
2016-08-14 18:18:51 +02:00
const GeneratorPresets = require('../comp/generator-presets');
const Locale = require('../util/locale');
2019-08-06 04:19:20 +02:00
const Tip = require('../util/tip');
2015-10-18 23:30:29 +02:00
2017-01-31 07:50:28 +01:00
const GeneratorView = Backbone.View.extend({
2015-10-18 23:30:29 +02:00
el: 'body',
2015-12-16 22:50:45 +01:00
template: require('templates/generator.hbs'),
2015-10-18 23:30:29 +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
'mousemove .gen__length-range': 'lengthMouseMove',
'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'
2015-10-18 23:30:29 +02:00
},
2016-07-17 13:30:38 +02:00
valuesMap: [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],
2016-02-15 22:06:11 +01:00
presets: null,
preset: null,
2019-08-16 23:05:39 +02:00
initialize: function() {
2016-02-15 22:06:11 +01:00
this.createPresets();
2017-01-31 07:50:28 +01:00
const preset = this.preset;
2016-07-17 13:30:38 +02:00
this.gen = _.clone(_.find(this.presets, pr => pr.name === preset));
2019-08-15 04:59:53 +02:00
this.hide = AppSettingsModel.instance.get('generatorHidePassword');
2015-10-19 23:24:32 +02:00
$('body').one('click', this.remove.bind(this));
2016-02-16 21:01:04 +01:00
this.listenTo(Backbone, 'lock-workspace', this.remove.bind(this));
2015-10-18 23:30:29 +02:00
},
render: function() {
2017-01-31 07:50:28 +01:00
const canCopy = document.queryCommandSupported('copy');
2019-08-16 23:05:39 +02:00
const btnTitle = this.model.copy ? (canCopy ? Locale.alertCopy : Locale.alertClose) : Locale.alertOk;
this.renderTemplate({
btnTitle: 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
});
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();
2015-10-20 21:58:07 +02:00
return this;
2015-10-18 23:30:29 +02:00
},
2016-02-15 22:06:11 +01:00
createPresets: function() {
2016-08-14 18:18:51 +02:00
this.presets = GeneratorPresets.enabled;
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 };
2016-02-15 22:06:11 +01:00
_.extend(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 {
2017-01-31 07:50:28 +01: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
}
this.presets.forEach(function(pr) {
2016-08-14 18:18:51 +02:00
pr.pseudoLength = this.lengthToPseudoValue(pr.length);
2016-02-15 22:06:11 +01:00
}, this);
},
2016-08-14 18:18:51 +02:00
lengthToPseudoValue: function(length) {
for (let ix = 0; ix < this.valuesMap.length; ix++) {
if (this.valuesMap[ix] >= length) {
return ix;
}
}
return this.valuesMap.length - 1;
},
2019-08-06 01:22:29 +02:00
showPassword: function() {
if (this.hide && !this.model.copy) {
2019-08-15 04:59:53 +02:00
this.resultEl.text(PasswordGenerator.present(this.password.length));
2019-08-06 01:22:29 +02:00
} else {
this.resultEl.text(this.password);
}
},
2015-10-19 23:24:32 +02:00
click: function(e) {
2015-10-18 23:30:29 +02:00
e.stopPropagation();
2015-10-19 23:24:32 +02:00
},
lengthChange: function(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;
this.$el.find('.gen__length-range-val').html(val);
2016-02-15 22:06:11 +01:00
this.optionChanged('length');
2015-10-19 23:24:32 +02:00
this.generate();
}
},
checkChange: function(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();
},
2016-02-15 22:06:11 +01:00
optionChanged: function(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('');
},
2015-10-19 23:24:32 +02:00
generate: function() {
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);
2015-10-20 21:58:07 +02:00
},
2019-08-06 01:22:29 +02:00
hideChange: function(e) {
this.hide = e.target.checked;
// AppSettingsModel.instance.unset('generatorHidePassword', { silent: true });
2019-08-15 04:59:53 +02:00
AppSettingsModel.instance.set('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();
},
2015-10-20 21:58:07 +02:00
btnOkClick: function() {
if (!CopyPaste.simpleCopy) {
CopyPaste.createHiddenInput(this.password);
}
2016-01-22 18:51:36 +01:00
CopyPaste.copy(this.password);
2015-10-20 21:58:07 +02:00
this.trigger('result', this.password);
this.remove();
2016-02-15 22:06:11 +01:00
},
2016-08-13 21:13:16 +02:00
presetChange: function(e) {
2017-01-31 07:50:28 +01:00
const name = e.target.value;
2016-08-13 21:13:16 +02:00
if (name === '...') {
Backbone.trigger('edit-generator-presets');
this.remove();
return;
}
2016-02-15 22:06:11 +01:00
this.preset = name;
2017-01-31 07:50:28 +01:00
const preset = _.find(this.presets, t => t.name === name);
2016-02-15 22:06:11 +01:00
this.gen = _.clone(preset);
this.render();
2016-02-23 07:43:43 +01:00
},
newPass: function() {
2016-02-23 21:31:21 +01:00
this.generate();
2015-10-18 23:30:29 +02:00
}
});
module.exports = GeneratorView;