1
0
mirror of https://github.com/keeweb/keeweb.git synced 2024-06-26 07:39:04 +02:00

auto-type-select view

This commit is contained in:
antelle 2019-09-15 23:02:51 +02:00
parent c9ac7f2fa3
commit de2443eb5a
5 changed files with 71 additions and 81 deletions

View File

@ -230,9 +230,7 @@ const AutoType = {
} }
this.focusMainWindow(); this.focusMainWindow();
evt.filter.ignoreWindowInfo = true; evt.filter.ignoreWindowInfo = true;
this.selectEntryView = new AutoTypeSelectView({ this.selectEntryView = new AutoTypeSelectView({ filter: evt.filter });
model: { filter: evt.filter }
}).render();
this.selectEntryView.on('result', result => { this.selectEntryView.on('result', result => {
logger.debug('Entry selected', result); logger.debug('Entry selected', result);
this.selectEntryView.off('result'); this.selectEntryView.off('result');
@ -248,6 +246,7 @@ const AutoType = {
} }
}); });
}); });
this.selectEntryView.render();
this.selectEntryView.on('show-open-files', () => { this.selectEntryView.on('show-open-files', () => {
this.selectEntryView.hide(); this.selectEntryView.hide();
Backbone.trigger('open-file'); Backbone.trigger('open-file');

View File

@ -26,6 +26,8 @@ class View extends EventEmitter {
if (options.replace) { if (options.replace) {
this.replace = options.replace; this.replace = options.replace;
} }
this.setMaxListeners(100);
} }
render(templateData) { render(templateData) {
@ -186,6 +188,14 @@ class View extends EventEmitter {
KeyHandler.onKey(key, handler, this, shortcut, modal, noPrevent); KeyHandler.onKey(key, handler, this, shortcut, modal, noPrevent);
this.once('remove', () => KeyHandler.offKey(key, handler, this)); this.once('remove', () => KeyHandler.offKey(key, handler, this));
} }
off(event, listener) {
if (listener === undefined) {
return super.removeAllListeners(event);
} else {
return super.off(event, listener);
}
}
} }
export { View }; export { View };

View File

@ -1,4 +1,5 @@
import Backbone from 'backbone'; import Backbone from 'backbone';
import { View } from 'view-engine/view';
import { Shortcuts } from 'comp/app/shortcuts'; import { Shortcuts } from 'comp/app/shortcuts';
import { KeyHandler } from 'comp/browser/key-handler'; import { KeyHandler } from 'comp/browser/key-handler';
import { Keys } from 'const/keys'; import { Keys } from 'const/keys';
@ -8,58 +9,53 @@ import { StringFormat } from 'util/formatting/string-format';
import { Locale } from 'util/locale'; import { Locale } from 'util/locale';
import { Scrollable } from 'view-engine/scrollable'; import { Scrollable } from 'view-engine/scrollable';
import { DropdownView } from 'views/dropdown-view'; import { DropdownView } from 'views/dropdown-view';
import template from 'templates/auto-type/auto-type-select.hbs';
import itemTemplate from 'templates/auto-type/auto-type-select-item.hbs';
const AutoTypeSelectView = Backbone.View.extend({ class AutoTypeSelectView extends View {
el: 'body', parent = 'body';
template: require('templates/auto-type/auto-type-select.hbs'), template = template;
itemTemplate: require('templates/auto-type/auto-type-select-item.hbs'),
events: { itemTemplate = itemTemplate;
events = {
'click .at-select__header-filter-clear': 'clearFilterText', 'click .at-select__header-filter-clear': 'clearFilterText',
'click .at-select__item': 'itemClicked', 'click .at-select__item': 'itemClicked',
'contextmenu .at-select__item': 'itemRightClicked' 'contextmenu .at-select__item': 'itemRightClicked'
}, };
result: null, result = null;
entries: null, entries = null;
initialize() { constructor(model) {
this.views = {}; super(model);
this.initScroll(); this.initScroll();
this.listenTo(Backbone, 'main-window-blur', this.mainWindowBlur); this.listenTo(Backbone, 'main-window-blur', this.mainWindowBlur);
this.listenTo(Backbone, 'main-window-will-close', this.mainWindowWillClose); this.listenTo(Backbone, 'main-window-will-close', this.mainWindowWillClose);
this.setupKeys(); this.setupKeys();
}, }
setupKeys() { setupKeys() {
KeyHandler.onKey(Keys.DOM_VK_ESCAPE, this.escPressed, this, false, 'auto-type'); this.onKey(Keys.DOM_VK_ESCAPE, this.escPressed, false, 'auto-type');
KeyHandler.onKey(Keys.DOM_VK_RETURN, this.enterPressed, this, false, 'auto-type'); this.onKey(Keys.DOM_VK_RETURN, this.enterPressed, false, 'auto-type');
KeyHandler.onKey( this.onKey(
Keys.DOM_VK_RETURN, Keys.DOM_VK_RETURN,
this.actionEnterPressed, this.actionEnterPressed,
this,
KeyHandler.SHORTCUT_ACTION, KeyHandler.SHORTCUT_ACTION,
'auto-type' 'auto-type'
); );
KeyHandler.onKey( this.onKey(Keys.DOM_VK_RETURN, this.optEnterPressed, KeyHandler.SHORTCUT_OPT, 'auto-type');
Keys.DOM_VK_RETURN, this.onKey(
this.optEnterPressed,
this,
KeyHandler.SHORTCUT_OPT,
'auto-type'
);
KeyHandler.onKey(
Keys.DOM_VK_RETURN, Keys.DOM_VK_RETURN,
this.shiftEnterPressed, this.shiftEnterPressed,
this,
KeyHandler.SHORTCUT_SHIFT, KeyHandler.SHORTCUT_SHIFT,
'auto-type' 'auto-type'
); );
KeyHandler.onKey(Keys.DOM_VK_UP, this.upPressed, this, false, 'auto-type'); this.onKey(Keys.DOM_VK_UP, this.upPressed, false, 'auto-type');
KeyHandler.onKey(Keys.DOM_VK_DOWN, this.downPressed, this, false, 'auto-type'); this.onKey(Keys.DOM_VK_DOWN, this.downPressed, false, 'auto-type');
KeyHandler.onKey(Keys.DOM_VK_BACK_SPACE, this.backSpacePressed, this, false, 'auto-type'); this.onKey(Keys.DOM_VK_BACK_SPACE, this.backSpacePressed, false, 'auto-type');
KeyHandler.onKey( this.onKey(
Keys.DOM_VK_O, Keys.DOM_VK_O,
this.openKeyPressed, this.openKeyPressed,
this, this,
@ -68,21 +64,11 @@ const AutoTypeSelectView = Backbone.View.extend({
); );
KeyHandler.on('keypress:auto-type', this.keyPressed.bind(this)); KeyHandler.on('keypress:auto-type', this.keyPressed.bind(this));
KeyHandler.setModal('auto-type'); KeyHandler.setModal('auto-type');
}, this.once('remove', () => {
KeyHandler.off('keypress:auto-type');
removeKeys() { KeyHandler.setModal(null);
KeyHandler.offKey(Keys.DOM_VK_ESCAPE, this.escPressed, this); });
KeyHandler.offKey(Keys.DOM_VK_RETURN, this.enterPressed, this); }
KeyHandler.offKey(Keys.DOM_VK_RETURN, this.actionEnterPressed, this);
KeyHandler.offKey(Keys.DOM_VK_RETURN, this.optEnterPressed, this);
KeyHandler.offKey(Keys.DOM_VK_RETURN, this.shiftEnterPressed, this);
KeyHandler.offKey(Keys.DOM_VK_UP, this.upPressed, this);
KeyHandler.offKey(Keys.DOM_VK_DOWN, this.downPressed, this);
KeyHandler.offKey(Keys.DOM_VK_BACK_SPACE, this.backSpacePressed, this);
KeyHandler.offKey(Keys.DOM_VK_O, this.openKeyPressed, this);
KeyHandler.off('keypress:auto-type');
KeyHandler.setModal(null);
},
render() { render() {
let topMessage; let topMessage;
@ -104,7 +90,7 @@ const AutoTypeSelectView = Backbone.View.extend({
presenter.present(entry); presenter.present(entry);
itemsHtml += itemTemplate(presenter); itemsHtml += itemTemplate(presenter);
}); });
this.renderTemplate({ super.render({
filterText: this.model.filter.text, filterText: this.model.filter.text,
topMessage, topMessage,
itemsHtml, itemsHtml,
@ -119,25 +105,19 @@ const AutoTypeSelectView = Backbone.View.extend({
scroller: this.$el.find('.scroller')[0], scroller: this.$el.find('.scroller')[0],
bar: this.$el.find('.scroller__bar')[0] bar: this.$el.find('.scroller__bar')[0]
}); });
return this; }
},
remove() {
this.removeKeys();
Backbone.View.prototype.remove.apply(this);
},
cancelAndClose() { cancelAndClose() {
this.result = null; this.result = null;
this.trigger('result', this.result); this.emit('result', this.result);
}, }
closeWithResult(sequence) { closeWithResult(sequence) {
this.trigger('result', { this.emit('result', {
entry: this.result, entry: this.result,
sequence sequence
}); });
}, }
escPressed() { escPressed() {
if (this.model.filter.text) { if (this.model.filter.text) {
@ -145,29 +125,29 @@ const AutoTypeSelectView = Backbone.View.extend({
} else { } else {
this.cancelAndClose(); this.cancelAndClose();
} }
}, }
enterPressed() { enterPressed() {
this.closeWithResult(); this.closeWithResult();
}, }
actionEnterPressed() { actionEnterPressed() {
this.closeWithResult('{PASSWORD}'); this.closeWithResult('{PASSWORD}');
}, }
optEnterPressed() { optEnterPressed() {
this.closeWithResult('{USERNAME}'); this.closeWithResult('{USERNAME}');
}, }
openKeyPressed() { openKeyPressed() {
this.removeKeys(); this.removeKeys();
this.trigger('show-open-files'); this.emit('show-open-files');
}, }
shiftEnterPressed(e) { shiftEnterPressed(e) {
const activeItem = this.$el.find('.at-select__item[data-id="' + this.result.id + '"]'); const activeItem = this.$el.find('.at-select__item[data-id="' + this.result.id + '"]');
this.showItemOptions(activeItem, e); this.showItemOptions(activeItem, e);
}, }
upPressed(e) { upPressed(e) {
e.preventDefault(); e.preventDefault();
@ -176,7 +156,7 @@ const AutoTypeSelectView = Backbone.View.extend({
this.result = this.entries.at(activeIndex); this.result = this.entries.at(activeIndex);
this.highlightActive(); this.highlightActive();
} }
}, }
downPressed(e) { downPressed(e) {
e.preventDefault(); e.preventDefault();
@ -185,7 +165,7 @@ const AutoTypeSelectView = Backbone.View.extend({
this.result = this.entries.at(activeIndex); this.result = this.entries.at(activeIndex);
this.highlightActive(); this.highlightActive();
} }
}, }
highlightActive() { highlightActive() {
this.$el.find('.at-select__item').removeClass('at-select__item--active'); this.$el.find('.at-select__item').removeClass('at-select__item--active');
@ -198,14 +178,14 @@ const AutoTypeSelectView = Backbone.View.extend({
} else if (itemRect.bottom > listRect.bottom) { } else if (itemRect.bottom > listRect.bottom) {
this.scroller[0].scrollTop += itemRect.bottom - listRect.bottom; this.scroller[0].scrollTop += itemRect.bottom - listRect.bottom;
} }
}, }
keyPressed(e) { keyPressed(e) {
if (e.which && e.which !== Keys.DOM_VK_RETURN) { if (e.which && e.which !== Keys.DOM_VK_RETURN) {
this.model.filter.text += String.fromCharCode(e.which); this.model.filter.text += String.fromCharCode(e.which);
this.render(); this.render();
} }
}, }
backSpacePressed() { backSpacePressed() {
if (this.model.filter.text) { if (this.model.filter.text) {
@ -215,12 +195,12 @@ const AutoTypeSelectView = Backbone.View.extend({
); );
this.render(); this.render();
} }
}, }
clearFilterText() { clearFilterText() {
this.model.filter.text = ''; this.model.filter.text = '';
this.render(); this.render();
}, }
itemClicked(e) { itemClicked(e) {
const itemEl = $(e.target).closest('.at-select__item'); const itemEl = $(e.target).closest('.at-select__item');
@ -233,21 +213,21 @@ const AutoTypeSelectView = Backbone.View.extend({
this.result = this.entries.get(id); this.result = this.entries.get(id);
this.closeWithResult(); this.closeWithResult();
} }
}, }
itemRightClicked(e) { itemRightClicked(e) {
const itemEl = $(e.target).closest('.at-select__item'); const itemEl = $(e.target).closest('.at-select__item');
this.showItemOptions(itemEl, e); this.showItemOptions(itemEl, e);
}, }
mainWindowBlur() { mainWindowBlur() {
this.cancelAndClose(); this.cancelAndClose();
}, }
mainWindowWillClose(e) { mainWindowWillClose(e) {
e.preventDefault(); e.preventDefault();
this.cancelAndClose(); this.cancelAndClose();
}, }
showItemOptions(itemEl, event) { showItemOptions(itemEl, event) {
if (event) { if (event) {
@ -327,22 +307,22 @@ const AutoTypeSelectView = Backbone.View.extend({
}); });
this.views.optionsDropdown = view; this.views.optionsDropdown = view;
}, }
hideItemOptionsDropdown() { hideItemOptionsDropdown() {
if (this.views.optionsDropdown) { if (this.views.optionsDropdown) {
this.views.optionsDropdown.remove(); this.views.optionsDropdown.remove();
delete this.views.optionsDropdown; delete this.views.optionsDropdown;
} }
}, }
itemOptionsDropdownSelect(e) { itemOptionsDropdownSelect(e) {
this.hideItemOptionsDropdown(); this.hideItemOptionsDropdown();
const sequence = e.item; const sequence = e.item;
this.closeWithResult(sequence); this.closeWithResult(sequence);
} }
}); }
_.extend(AutoTypeSelectView.prototype, Scrollable); Object.assign(AutoTypeSelectView.prototype, Scrollable);
export { AutoTypeSelectView }; export { AutoTypeSelectView };

View File

@ -1,4 +1,5 @@
<tr class="at-select__item {{#if active}}at-select__item--active{{/if}}" data-id="{{id}}"> <tr class="at-select__item {{#if active}}at-select__item--active{{/if}}" data-id="{{id}}"
id="at-select__item--{{id}}">
<td> <td>
{{~#if customIcon~}} {{~#if customIcon~}}
<img src="{{{customIcon}}}" class="at-select__item-icon at-select__item-icon--custom {{#if color}}{{color}}{{/if}}" /> <img src="{{{customIcon}}}" class="at-select__item-icon at-select__item-icon--custom {{#if color}}{{color}}{{/if}}" />

View File

@ -10,7 +10,7 @@
{{#if filterText}} {{#if filterText}}
<div class="at-select__header-filter"> <div class="at-select__header-filter">
<input type="text" readonly value="{{filterText}}" class="at-select__header-filter-input" /> <input type="text" readonly value="{{filterText}}" class="at-select__header-filter-input" />
<i class="at-select__header-filter-clear fa fa-times" /> <i class="at-select__header-filter-clear fa fa-times"></i>
</div> </div>
{{/if}} {{/if}}
</div> </div>