2017-10-31 12:10:49 +03:00
|
|
|
import $ from 'jquery';
|
|
|
|
import PowerSelectMultiple from 'ember-power-select/components/power-select-multiple';
|
2020-01-15 16:53:51 +03:00
|
|
|
import {action} from '@ember/object';
|
2017-10-31 12:10:49 +03:00
|
|
|
import {bind} from '@ember/runloop';
|
2020-05-18 15:14:08 +03:00
|
|
|
import {tagName} from '@ember-decorators/component';
|
2017-10-31 12:10:49 +03:00
|
|
|
|
2020-01-15 16:53:51 +03:00
|
|
|
// TODO: convert from jQuery to native DOM
|
|
|
|
const END_ACTIONS = 'click.ghToken mouseup.ghToken touchend.ghToken';
|
2017-10-31 12:10:49 +03:00
|
|
|
|
|
|
|
// triggering focus on the search input within ESA's onfocus event breaks the
|
|
|
|
// drag-n-drop functionality in ember-drag-drop so we watch for events that
|
|
|
|
// could be the start of a drag and disable the default focus behaviour until
|
|
|
|
// we get another event signalling the end of a drag
|
|
|
|
|
2020-05-18 00:35:53 +03:00
|
|
|
@tagName('div')
|
|
|
|
class GhTokenInputSelectMultiple extends PowerSelectMultiple {
|
2020-01-15 16:53:51 +03:00
|
|
|
_canFocus = true;
|
2017-10-31 12:10:49 +03:00
|
|
|
|
|
|
|
willDestroyElement() {
|
2020-01-15 16:53:51 +03:00
|
|
|
super.willDestroyElement(...arguments);
|
2017-10-31 12:10:49 +03:00
|
|
|
|
|
|
|
if (this._allowFocusListener) {
|
2020-01-15 16:53:51 +03:00
|
|
|
$(window).off(END_ACTIONS, this._allowFocusListener);
|
2017-10-31 12:10:49 +03:00
|
|
|
}
|
2020-01-15 16:53:51 +03:00
|
|
|
}
|
2017-10-31 12:10:49 +03:00
|
|
|
|
2020-01-15 16:53:51 +03:00
|
|
|
// actions
|
2017-10-31 12:10:49 +03:00
|
|
|
|
2020-01-15 16:53:51 +03:00
|
|
|
@action
|
|
|
|
optionMouseDown(event) {
|
|
|
|
if (event.which === 1 && !event.ctrlKey) {
|
2017-10-31 12:10:49 +03:00
|
|
|
this._denyFocus(event);
|
2020-01-15 16:53:51 +03:00
|
|
|
}
|
|
|
|
}
|
2017-10-31 12:10:49 +03:00
|
|
|
|
2020-01-15 16:53:51 +03:00
|
|
|
@action
|
|
|
|
optionTouchStart(event) {
|
|
|
|
this._denyFocus(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
handleFocus() {
|
|
|
|
if (this._canFocus) {
|
|
|
|
super.handleFocus(...arguments);
|
2017-10-31 12:10:49 +03:00
|
|
|
}
|
2020-01-15 16:53:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// internal
|
2017-10-31 12:10:49 +03:00
|
|
|
|
|
|
|
_denyFocus() {
|
|
|
|
if (this._canFocus) {
|
|
|
|
this._canFocus = false;
|
|
|
|
|
|
|
|
this._allowFocusListener = bind(this, this._allowFocus);
|
|
|
|
|
2020-01-15 16:53:51 +03:00
|
|
|
$(window).on(END_ACTIONS, this._allowFocusListener);
|
2017-10-31 12:10:49 +03:00
|
|
|
}
|
2020-01-15 16:53:51 +03:00
|
|
|
}
|
2017-10-31 12:10:49 +03:00
|
|
|
|
|
|
|
_allowFocus() {
|
|
|
|
this._canFocus = true;
|
|
|
|
|
2020-01-15 16:53:51 +03:00
|
|
|
$(window).off(END_ACTIONS, this._allowFocusListener);
|
2017-10-31 12:10:49 +03:00
|
|
|
this._allowFocusListener = null;
|
|
|
|
}
|
2020-01-15 16:53:51 +03:00
|
|
|
}
|
2020-05-18 00:35:53 +03:00
|
|
|
|
|
|
|
export default GhTokenInputSelectMultiple;
|