2017-10-31 12:10:49 +03:00
|
|
|
/* global key */
|
|
|
|
import Component from '@ember/component';
|
|
|
|
import Ember from 'ember';
|
2018-03-13 14:17:29 +03:00
|
|
|
import {A, isArray} from '@ember/array';
|
2017-10-31 12:10:49 +03:00
|
|
|
import {
|
|
|
|
advanceSelectableOption,
|
|
|
|
defaultMatcher,
|
|
|
|
filterOptions
|
|
|
|
} from 'ember-power-select/utils/group-utils';
|
|
|
|
import {computed} from '@ember/object';
|
|
|
|
import {get} from '@ember/object';
|
|
|
|
import {htmlSafe} from '@ember/string';
|
|
|
|
import {isBlank} from '@ember/utils';
|
|
|
|
import {task} from 'ember-concurrency';
|
|
|
|
|
|
|
|
const {Handlebars} = Ember;
|
|
|
|
|
|
|
|
const BACKSPACE = 8;
|
|
|
|
const TAB = 9;
|
|
|
|
|
|
|
|
export default Component.extend({
|
|
|
|
|
|
|
|
// public attrs
|
2018-03-13 14:17:29 +03:00
|
|
|
allowCreation: true,
|
2017-10-31 12:10:49 +03:00
|
|
|
closeOnSelect: false,
|
|
|
|
labelField: 'name',
|
|
|
|
matcher: defaultMatcher,
|
|
|
|
searchField: 'name',
|
|
|
|
tagName: '',
|
|
|
|
triggerComponent: 'gh-token-input/trigger',
|
|
|
|
|
|
|
|
optionsWithoutSelected: computed('options.[]', 'selected.[]', function () {
|
2019-03-06 16:53:54 +03:00
|
|
|
return this.optionsWithoutSelectedTask.perform();
|
2017-10-31 12:10:49 +03:00
|
|
|
}),
|
|
|
|
|
|
|
|
actions: {
|
|
|
|
handleKeydown(select, event) {
|
|
|
|
// On backspace with empty text, remove the last token but deviate
|
|
|
|
// from default behaviour by not updating search to match last token
|
|
|
|
if (event.keyCode === BACKSPACE && isBlank(event.target.value)) {
|
|
|
|
let lastSelection = select.selected[select.selected.length - 1];
|
|
|
|
|
|
|
|
if (lastSelection) {
|
2019-03-06 16:53:54 +03:00
|
|
|
this.onchange(select.selected.slice(0, -1), select);
|
2017-10-31 12:10:49 +03:00
|
|
|
select.actions.search('');
|
|
|
|
select.actions.open(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
// prevent default
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tab should work the same as Enter if there's a highlighted option
|
|
|
|
if (event.keyCode === TAB && !isBlank(event.target.value) && select.highlighted) {
|
|
|
|
if (!select.selected || select.selected.indexOf(select.highlighted) === -1) {
|
|
|
|
select.actions.choose(select.highlighted, event);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// fallback to default
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
|
|
|
onfocus() {
|
|
|
|
key.setScope('gh-token-input');
|
|
|
|
|
2019-03-06 16:53:54 +03:00
|
|
|
if (this.onfocus) {
|
|
|
|
this.onfocus(...arguments);
|
2017-10-31 12:10:49 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
onblur() {
|
|
|
|
key.setScope('default');
|
|
|
|
|
2019-03-06 16:53:54 +03:00
|
|
|
if (this.onblur) {
|
|
|
|
this.onblur(...arguments);
|
2017-10-31 12:10:49 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
optionsWithoutSelectedTask: task(function* () {
|
2019-03-06 16:53:54 +03:00
|
|
|
let options = yield this.options;
|
|
|
|
let selected = yield this.selected;
|
2018-01-05 18:38:23 +03:00
|
|
|
return options.filter(o => !selected.includes(o));
|
2017-10-31 12:10:49 +03:00
|
|
|
}),
|
|
|
|
|
|
|
|
shouldShowCreateOption(term, options) {
|
2019-03-06 16:53:54 +03:00
|
|
|
if (!this.allowCreation) {
|
2018-03-13 14:17:29 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-03-06 16:53:54 +03:00
|
|
|
if (this.showCreateWhen) {
|
|
|
|
return this.showCreateWhen(term, options);
|
2017-10-31 12:10:49 +03:00
|
|
|
} else {
|
|
|
|
return this.hideCreateOptionOnSameTerm(term, options);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
hideCreateOptionOnSameTerm(term, options) {
|
2019-03-06 16:53:54 +03:00
|
|
|
let searchField = this.searchField;
|
2017-10-31 12:10:49 +03:00
|
|
|
let existingOption = options.findBy(searchField, term);
|
|
|
|
return !existingOption;
|
|
|
|
},
|
|
|
|
|
|
|
|
addCreateOption(term, options) {
|
|
|
|
if (this.shouldShowCreateOption(term, options)) {
|
|
|
|
options.unshift(this.buildSuggestionForTerm(term));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
searchAndSuggest(term, select) {
|
2019-03-06 16:53:54 +03:00
|
|
|
return this.searchAndSuggestTask.perform(term, select);
|
2017-10-31 12:10:49 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
searchAndSuggestTask: task(function* (term, select) {
|
2019-03-06 16:53:54 +03:00
|
|
|
let newOptions = (yield this.optionsWithoutSelected).toArray();
|
2017-10-31 12:10:49 +03:00
|
|
|
|
|
|
|
if (term.length === 0) {
|
|
|
|
return newOptions;
|
|
|
|
}
|
|
|
|
|
2019-03-06 16:53:54 +03:00
|
|
|
let searchAction = this.search;
|
2017-10-31 12:10:49 +03:00
|
|
|
if (searchAction) {
|
|
|
|
let results = yield searchAction(term, select);
|
|
|
|
|
|
|
|
if (results.toArray) {
|
|
|
|
results = results.toArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.addCreateOption(term, results);
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
|
|
|
newOptions = this.filter(A(newOptions), term);
|
|
|
|
this.addCreateOption(term, newOptions);
|
|
|
|
|
|
|
|
return newOptions;
|
|
|
|
}),
|
|
|
|
|
2018-01-02 16:54:02 +03:00
|
|
|
selectOrCreate(selection, select, keyboardEvent) {
|
|
|
|
// allow tokens to be created with spaces
|
|
|
|
if (keyboardEvent && keyboardEvent.code === 'Space') {
|
|
|
|
select.actions.search(`${select.searchText} `);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-13 14:17:29 +03:00
|
|
|
// guard against return being pressed when nothing is selected
|
|
|
|
if (!isArray(selection)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-05 18:38:23 +03:00
|
|
|
let suggestion = selection.find(option => option.__isSuggestion__);
|
2017-10-31 12:10:49 +03:00
|
|
|
|
|
|
|
if (suggestion) {
|
2019-03-06 16:53:54 +03:00
|
|
|
this.oncreate(suggestion.__value__, select);
|
2017-10-31 12:10:49 +03:00
|
|
|
} else {
|
2019-03-06 16:53:54 +03:00
|
|
|
this.onchange(selection, select);
|
2017-10-31 12:10:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// clear select search
|
|
|
|
select.actions.search('');
|
|
|
|
},
|
|
|
|
|
|
|
|
filter(options, searchText) {
|
|
|
|
let matcher;
|
2019-03-06 16:53:54 +03:00
|
|
|
if (this.searchField) {
|
|
|
|
matcher = (option, text) => this.matcher(get(option, this.searchField), text);
|
2017-10-31 12:10:49 +03:00
|
|
|
} else {
|
|
|
|
matcher = (option, text) => this.matcher(option, text);
|
|
|
|
}
|
|
|
|
return filterOptions(options || [], searchText, matcher);
|
|
|
|
},
|
|
|
|
|
|
|
|
buildSuggestionForTerm(term) {
|
|
|
|
return {
|
|
|
|
__isSuggestion__: true,
|
|
|
|
__value__: term,
|
|
|
|
text: this.buildSuggestionLabel(term)
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
buildSuggestionLabel(term) {
|
2019-03-06 16:53:54 +03:00
|
|
|
let buildSuggestion = this.buildSuggestion;
|
2017-10-31 12:10:49 +03:00
|
|
|
if (buildSuggestion) {
|
|
|
|
return buildSuggestion(term);
|
|
|
|
}
|
|
|
|
return htmlSafe(`Add <strong>"${Handlebars.Utils.escapeExpression(term)}"...</strong>`);
|
|
|
|
},
|
|
|
|
|
|
|
|
// always select the first item in the list that isn't the "Add x" option
|
|
|
|
defaultHighlighted(select) {
|
|
|
|
let {results} = select;
|
|
|
|
let option = advanceSelectableOption(results, undefined, 1);
|
|
|
|
|
|
|
|
if (results.length > 1 && option.__isSuggestion__) {
|
|
|
|
option = advanceSelectableOption(results, option, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return option;
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|