Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(selected): Adding context about original event triggering an autocomplete:selected event #267

Merged
merged 1 commit into from
Nov 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions dist/autocomplete.angular.js
Original file line number Diff line number Diff line change
Expand Up @@ -358,11 +358,9 @@

// ### public

trigger: function(type) {
var args = [].slice.call(arguments, 1);

trigger: function(type, suggestion, dataset, context) {
var event = _.Event(namespace + type);
this.$el.trigger(event, args);
this.$el.trigger(event, [suggestion, dataset, context]);
return event;
}
});
Expand Down Expand Up @@ -532,9 +530,10 @@

_onSuggestionClicked: function onSuggestionClicked(type, $el) {
var datum;
var context = { selectionMethod: 'click' }

if (datum = this.dropdown.getDatumForSuggestion($el)) {
this._select(datum);
this._select(datum, context);
}
},

Expand Down Expand Up @@ -631,12 +630,13 @@

cursorDatum = this.dropdown.getDatumForCursor();
topSuggestionDatum = this.dropdown.getDatumForTopSuggestion();
var context = { selectionMethod: 'blur' }

if (!this.debug) {
if (this.autoselectOnBlur && cursorDatum) {
this._select(cursorDatum);
this._select(cursorDatum, context);
} else if (this.autoselectOnBlur && topSuggestionDatum) {
this._select(topSuggestionDatum);
this._select(topSuggestionDatum, context);
} else {
this.isActivated = false;
this.dropdown.empty();
Expand All @@ -651,12 +651,13 @@

cursorDatum = this.dropdown.getDatumForCursor();
topSuggestionDatum = this.dropdown.getDatumForTopSuggestion();
var context = { selectionMethod: 'enterKey' }

if (cursorDatum) {
this._select(cursorDatum);
this._select(cursorDatum, context);
$e.preventDefault();
} else if (this.autoselect && topSuggestionDatum) {
this._select(topSuggestionDatum);
this._select(topSuggestionDatum, context);
$e.preventDefault();
}
},
Expand All @@ -669,9 +670,10 @@
}

var datum;
var context = { selectionMethod: 'tabKey' }

if (datum = this.dropdown.getDatumForCursor()) {
this._select(datum);
this._select(datum, context);
$e.preventDefault();
} else {
this._autocomplete(true);
Expand Down Expand Up @@ -797,7 +799,7 @@
}
},

_select: function select(datum) {
_select: function select(datum, context) {
if (typeof datum.value !== 'undefined') {
this.input.setQuery(datum.value);
}
Expand All @@ -809,7 +811,7 @@

this._setLanguageDirection();

var event = this.eventBus.trigger('selected', datum.raw, datum.datasetName);
var event = this.eventBus.trigger('selected', datum.raw, datum.datasetName, context);
if (event.isDefaultPrevented() === false) {
this.dropdown.close();

Expand Down
4 changes: 2 additions & 2 deletions dist/autocomplete.angular.min.js

Large diffs are not rendered by default.

26 changes: 14 additions & 12 deletions dist/autocomplete.jquery.js
Original file line number Diff line number Diff line change
Expand Up @@ -540,9 +540,10 @@

_onSuggestionClicked: function onSuggestionClicked(type, $el) {
var datum;
var context = { selectionMethod: 'click' }

if (datum = this.dropdown.getDatumForSuggestion($el)) {
this._select(datum);
this._select(datum, context);
}
},

Expand Down Expand Up @@ -639,12 +640,13 @@

cursorDatum = this.dropdown.getDatumForCursor();
topSuggestionDatum = this.dropdown.getDatumForTopSuggestion();
var context = { selectionMethod: 'blur' }

if (!this.debug) {
if (this.autoselectOnBlur && cursorDatum) {
this._select(cursorDatum);
this._select(cursorDatum, context);
} else if (this.autoselectOnBlur && topSuggestionDatum) {
this._select(topSuggestionDatum);
this._select(topSuggestionDatum, context);
} else {
this.isActivated = false;
this.dropdown.empty();
Expand All @@ -659,12 +661,13 @@

cursorDatum = this.dropdown.getDatumForCursor();
topSuggestionDatum = this.dropdown.getDatumForTopSuggestion();
var context = { selectionMethod: 'enterKey' }

if (cursorDatum) {
this._select(cursorDatum);
this._select(cursorDatum, context);
$e.preventDefault();
} else if (this.autoselect && topSuggestionDatum) {
this._select(topSuggestionDatum);
this._select(topSuggestionDatum, context);
$e.preventDefault();
}
},
Expand All @@ -677,9 +680,10 @@
}

var datum;
var context = { selectionMethod: 'tabKey' }

if (datum = this.dropdown.getDatumForCursor()) {
this._select(datum);
this._select(datum, context);
$e.preventDefault();
} else {
this._autocomplete(true);
Expand Down Expand Up @@ -805,7 +809,7 @@
}
},

_select: function select(datum) {
_select: function select(datum, context) {
if (typeof datum.value !== 'undefined') {
this.input.setQuery(datum.value);
}
Expand All @@ -817,7 +821,7 @@

this._setLanguageDirection();

var event = this.eventBus.trigger('selected', datum.raw, datum.datasetName);
var event = this.eventBus.trigger('selected', datum.raw, datum.datasetName, context);
if (event.isDefaultPrevented() === false) {
this.dropdown.close();

Expand Down Expand Up @@ -1063,11 +1067,9 @@

// ### public

trigger: function(type) {
var args = [].slice.call(arguments, 1);

trigger: function(type, suggestion, dataset, context) {
var event = _.Event(namespace + type);
this.$el.trigger(event, args);
this.$el.trigger(event, [suggestion, dataset, context]);
return event;
}
});
Expand Down
4 changes: 2 additions & 2 deletions dist/autocomplete.jquery.min.js

Large diffs are not rendered by default.

26 changes: 14 additions & 12 deletions dist/autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -1794,9 +1794,10 @@ return /******/ (function(modules) { // webpackBootstrap

_onSuggestionClicked: function onSuggestionClicked(type, $el) {
var datum;
var context = { selectionMethod: 'click' }

if (datum = this.dropdown.getDatumForSuggestion($el)) {
this._select(datum);
this._select(datum, context);
}
},

Expand Down Expand Up @@ -1893,12 +1894,13 @@ return /******/ (function(modules) { // webpackBootstrap

cursorDatum = this.dropdown.getDatumForCursor();
topSuggestionDatum = this.dropdown.getDatumForTopSuggestion();
var context = { selectionMethod: 'blur' }

if (!this.debug) {
if (this.autoselectOnBlur && cursorDatum) {
this._select(cursorDatum);
this._select(cursorDatum, context);
} else if (this.autoselectOnBlur && topSuggestionDatum) {
this._select(topSuggestionDatum);
this._select(topSuggestionDatum, context);
} else {
this.isActivated = false;
this.dropdown.empty();
Expand All @@ -1913,12 +1915,13 @@ return /******/ (function(modules) { // webpackBootstrap

cursorDatum = this.dropdown.getDatumForCursor();
topSuggestionDatum = this.dropdown.getDatumForTopSuggestion();
var context = { selectionMethod: 'enterKey' }

if (cursorDatum) {
this._select(cursorDatum);
this._select(cursorDatum, context);
$e.preventDefault();
} else if (this.autoselect && topSuggestionDatum) {
this._select(topSuggestionDatum);
this._select(topSuggestionDatum, context);
$e.preventDefault();
}
},
Expand All @@ -1931,9 +1934,10 @@ return /******/ (function(modules) { // webpackBootstrap
}

var datum;
var context = { selectionMethod: 'tabKey' }

if (datum = this.dropdown.getDatumForCursor()) {
this._select(datum);
this._select(datum, context);
$e.preventDefault();
} else {
this._autocomplete(true);
Expand Down Expand Up @@ -2059,7 +2063,7 @@ return /******/ (function(modules) { // webpackBootstrap
}
},

_select: function select(datum) {
_select: function select(datum, context) {
if (typeof datum.value !== 'undefined') {
this.input.setQuery(datum.value);
}
Expand All @@ -2071,7 +2075,7 @@ return /******/ (function(modules) { // webpackBootstrap

this._setLanguageDirection();

var event = this.eventBus.trigger('selected', datum.raw, datum.datasetName);
var event = this.eventBus.trigger('selected', datum.raw, datum.datasetName, context);
if (event.isDefaultPrevented() === false) {
this.dropdown.close();

Expand Down Expand Up @@ -2317,11 +2321,9 @@ return /******/ (function(modules) { // webpackBootstrap

// ### public

trigger: function(type) {
var args = [].slice.call(arguments, 1);

trigger: function(type, suggestion, dataset, context) {
var event = _.Event(namespace + type);
this.$el.trigger(event, args);
this.$el.trigger(event, [suggestion, dataset, context]);
return event;
}
});
Expand Down
4 changes: 2 additions & 2 deletions dist/autocomplete.min.js

Large diffs are not rendered by default.

6 changes: 2 additions & 4 deletions src/autocomplete/event_bus.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,9 @@ _.mixin(EventBus.prototype, {

// ### public

trigger: function(type) {
var args = [].slice.call(arguments, 1);

trigger: function(type, suggestion, dataset, context) {
var event = _.Event(namespace + type);
this.$el.trigger(event, args);
this.$el.trigger(event, [suggestion, dataset, context]);
return event;
}
});
Expand Down
20 changes: 12 additions & 8 deletions src/autocomplete/typeahead.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,10 @@ _.mixin(Typeahead.prototype, {

_onSuggestionClicked: function onSuggestionClicked(type, $el) {
var datum;
var context = {selectionMethod: 'click'};

if (datum = this.dropdown.getDatumForSuggestion($el)) {
this._select(datum);
this._select(datum, context);
}
},

Expand Down Expand Up @@ -255,12 +256,13 @@ _.mixin(Typeahead.prototype, {

cursorDatum = this.dropdown.getDatumForCursor();
topSuggestionDatum = this.dropdown.getDatumForTopSuggestion();
var context = {selectionMethod: 'blur'};

if (!this.debug) {
if (this.autoselectOnBlur && cursorDatum) {
this._select(cursorDatum);
this._select(cursorDatum, context);
} else if (this.autoselectOnBlur && topSuggestionDatum) {
this._select(topSuggestionDatum);
this._select(topSuggestionDatum, context);
} else {
this.isActivated = false;
this.dropdown.empty();
Expand All @@ -275,12 +277,13 @@ _.mixin(Typeahead.prototype, {

cursorDatum = this.dropdown.getDatumForCursor();
topSuggestionDatum = this.dropdown.getDatumForTopSuggestion();
var context = {selectionMethod: 'enterKey'};

if (cursorDatum) {
this._select(cursorDatum);
this._select(cursorDatum, context);
$e.preventDefault();
} else if (this.autoselect && topSuggestionDatum) {
this._select(topSuggestionDatum);
this._select(topSuggestionDatum, context);
$e.preventDefault();
}
},
Expand All @@ -293,9 +296,10 @@ _.mixin(Typeahead.prototype, {
}

var datum;
var context = {selectionMethod: 'tabKey'};

if (datum = this.dropdown.getDatumForCursor()) {
this._select(datum);
this._select(datum, context);
$e.preventDefault();
} else {
this._autocomplete(true);
Expand Down Expand Up @@ -421,7 +425,7 @@ _.mixin(Typeahead.prototype, {
}
},

_select: function select(datum) {
_select: function select(datum, context) {
if (typeof datum.value !== 'undefined') {
this.input.setQuery(datum.value);
}
Expand All @@ -433,7 +437,7 @@ _.mixin(Typeahead.prototype, {

this._setLanguageDirection();

var event = this.eventBus.trigger('selected', datum.raw, datum.datasetName);
var event = this.eventBus.trigger('selected', datum.raw, datum.datasetName, context);
if (event.isDefaultPrevented() === false) {
this.dropdown.close();

Expand Down
6 changes: 3 additions & 3 deletions test/playground.html
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ <h4>Simple auto-complete with debounce</h4>
}
}
}
]).on('autocomplete:selected', function(even, suggestion, dataset) {
console.log('selected', suggestion, dataset);
}).on('autocomplete:cursorchanged', function(even, suggestion, dataset) {
]).on('autocomplete:selected', function(event, suggestion, dataset, context) {
console.log('selected', suggestion, dataset, context);
}).on('autocomplete:cursorchanged', function(event, suggestion, dataset) {
console.log('cursorchanged', suggestion, dataset);
});

Expand Down
Loading