FIX: Bugs with autocomplete

It wasn't tearing itself down properly. It was swallowing events.
This commit is contained in:
Robin Ward 2015-04-06 14:04:22 -04:00
parent 2389bee24c
commit da9e2792eb
4 changed files with 41 additions and 14 deletions

View File

@ -57,6 +57,10 @@ export default TextField.extend({
}); });
}.on('didInsertElement'), }.on('didInsertElement'),
_removeAutocomplete: function() {
this.$().autocomplete('destroy');
}.on('willDestroyElement'),
// THIS IS A HUGE HACK TO SUPPORT CLEARING THE INPUT // THIS IS A HUGE HACK TO SUPPORT CLEARING THE INPUT
_clearInput: function() { _clearInput: function() {
if (arguments.length > 1) { if (arguments.length > 1) {

View File

@ -33,11 +33,23 @@ var keys = {
}; };
let inputTimeout;
export default function(options) { export default function(options) {
var autocompletePlugin = this; var autocompletePlugin = this;
if (this.length === 0) return; if (this.length === 0) return;
if (options === 'destroy') {
Ember.run.cancel(inputTimeout);
$(this).off('keypress.autocomplete')
.off('keydown.autocomplete')
.off('paste.autocomplete');
return;
}
if (options && options.cancel && this.data("closeAutocomplete")) { if (options && options.cancel && this.data("closeAutocomplete")) {
this.data("closeAutocomplete")(); this.data("closeAutocomplete")();
return this; return this;
@ -252,13 +264,13 @@ export default function(options) {
closeAutocomplete(); closeAutocomplete();
}); });
$(this).on('paste', function() { $(this).on('paste.autocomplete', function() {
_.delay(function(){ _.delay(function(){
me.trigger("keydown"); me.trigger("keydown");
}, 50); }, 50);
}); });
$(this).keypress(function(e) { $(this).on('keypress.autocomplete', function(e) {
var caretPosition, term; var caretPosition, term;
// keep hunting backwards till you hit a the @ key // keep hunting backwards till you hit a the @ key
@ -277,7 +289,7 @@ export default function(options) {
} }
}); });
$(this).keydown(function(e) { $(this).on('keydown.autocomplete', function(e) {
var c, caretPosition, i, initial, next, prev, prevIsGood, stopFound, term, total, userToComplete; var c, caretPosition, i, initial, next, prev, prevIsGood, stopFound, term, total, userToComplete;
if(e.ctrlKey || e.altKey || e.metaKey){ if(e.ctrlKey || e.altKey || e.metaKey){
@ -286,7 +298,9 @@ export default function(options) {
if(options.allowAny){ if(options.allowAny){
// saves us wiring up a change event as well, keypress is while its pressed // saves us wiring up a change event as well, keypress is while its pressed
_.delay(function(){
Ember.run.cancel(inputTimeout);
inputTimeout = Ember.run.later(function(){
if(inputSelectedItems.length === 0) { if(inputSelectedItems.length === 0) {
inputSelectedItems.push(""); inputSelectedItems.push("");
} }
@ -332,7 +346,7 @@ export default function(options) {
// ESC // ESC
if (e.which === keys.esc) { if (e.which === keys.esc) {
if (completeStart !== null) { if (div !== null) {
closeAutocomplete(); closeAutocomplete();
return false; return false;
} }

View File

@ -2,6 +2,22 @@ export default Ember.View.extend({
elementId: 'discourse-modal', elementId: 'discourse-modal',
templateName: 'modal/modal', templateName: 'modal/modal',
classNameBindings: [':modal', ':hidden', 'controller.modalClass'], classNameBindings: [':modal', ':hidden', 'controller.modalClass'],
attributeBindings: ['data-keyboard'],
// We handle ESC ourselves
'data-keyboard': 'false',
_bindOnInsert: function() {
$('html').on('keydown.discourse-modal', e => {
if (e.which === 27) {
Em.run.next(() => $('.modal-header a.close').click());
}
});
}.on('didInsertElement'),
_bindOnDestroy: function() {
$('html').off('keydown.discourse-modal');
}.on('willDestroyElement'),
click(e) { click(e) {
const $target = $(e.target); const $target = $(e.target);
@ -12,12 +28,5 @@ export default Ember.View.extend({
// the backdrop and makes it unclickable. // the backdrop and makes it unclickable.
$('.modal-header a.close').click(); $('.modal-header a.close').click();
} }
},
keyDown(e) {
// Delegate click to modal close when pressing ESC
if (e.which === 27) {
Em.run.next(() => $('.modal-header a.close').click());
}
} }
}); });

View File

@ -22,7 +22,7 @@ test("modal", () => {
ok(find('#discourse-modal:visible').length === 1, 'modal should reappear'); ok(find('#discourse-modal:visible').length === 1, 'modal should reappear');
}); });
keyEvent('#main-outlet', 'keyup', 27); keyEvent('#main-outlet', 'keydown', 27);
andThen(() => { andThen(() => {
ok(find('#discourse-modal:visible').length === 0, 'ESC should close the modal'); ok(find('#discourse-modal:visible').length === 0, 'ESC should close the modal');
}); });