-
Notifications
You must be signed in to change notification settings - Fork 379
Expand file tree
/
Copy pathtrigger.js
More file actions
129 lines (118 loc) · 4.38 KB
/
trigger.js
File metadata and controls
129 lines (118 loc) · 4.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import Component from '@ember/component';
import { get } from '@ember/object';
import { computed } from '@ember/object';
import { inject } from '@ember/service';
import { scheduleOnce } from '@ember/runloop';
import { assert } from '@ember/debug';
import { isBlank } from '@ember/utils';
import { htmlSafe } from '@ember/string';
import layout from '../../templates/components/power-select-multiple/trigger';
const ua = window && window.navigator ? window.navigator.userAgent : '';
const isIE = ua.indexOf('MSIE ') > -1 || ua.indexOf('Trident/') > -1;
const isTouchDevice = !!window && 'ontouchstart' in window;
export default Component.extend({
tagName: '',
layout,
textMeasurer: inject(),
_lastIsOpen: false,
// Lifecycle hooks
didInsertElement() {
this._super(...arguments);
let select = this.get('select');
this.input = document.getElementById(`ember-power-select-trigger-multiple-input-${select.uniqueId}`);
let inputStyle = this.input ? window.getComputedStyle(this.input) : null;
this.inputFont = inputStyle ? `${ inputStyle.fontStyle } ${ inputStyle.fontVariant} ${ inputStyle.fontWeight } ${ inputStyle.fontSize}/${ inputStyle.lineHeight } ${ inputStyle.fontFamily }` : null;
let optionsList = document.getElementById(`ember-power-select-multiple-options-${select.uniqueId}`);
let chooseOption = (e) => {
let selectedIndex = e.target.getAttribute('data-selected-index');
if (selectedIndex) {
e.stopPropagation();
e.preventDefault();
let select = this.get('select');
let object = this.selectedObject(select.selected, selectedIndex);
select.actions.choose(object);
}
};
if (isTouchDevice) {
optionsList.addEventListener('touchstart', chooseOption);
}
optionsList.addEventListener('mousedown', chooseOption);
},
didReceiveAttrs() {
let oldSelect = this.get('oldSelect') || {};
let select = this.set('oldSelect', this.get('select'));
if (oldSelect.isOpen && !select.isOpen) {
scheduleOnce('actions', null, select.actions.search, '');
}
},
// CPs
triggerMultipleInputStyle: computed('select.{searchText.length,selected.length}', function() {
let select = this.get('select');
scheduleOnce('actions', select.actions.reposition);
if (!select.selected || select.selected.length === 0) {
return htmlSafe('width: 100%;');
} else {
let textWidth = 0;
if (this.inputFont) {
textWidth = this.get('textMeasurer').width(select.searchText, this.inputFont);
}
return htmlSafe(`width: ${textWidth + 25}px`);
}
}),
maybePlaceholder: computed('placeholder', 'select.selected.length', function() {
if (isIE) {
return;
}
let select = this.get('select');
return (!select.selected || get(select.selected, 'length') === 0) ? (this.get('placeholder') || '') : '';
}),
// Actions
actions: {
onInput(e) {
let action = this.get('onInput');
if (action && action(e) === false) {
return;
}
this.get('select').actions.open(e);
},
onKeydown(e) {
let { onKeydown, select } = this.getProperties('onKeydown', 'select');
if (onKeydown && onKeydown(e) === false) {
e.stopPropagation();
return false;
}
if (e.keyCode === 8) {
e.stopPropagation();
if (isBlank(e.target.value)) {
let lastSelection = select.selected[select.selected.length - 1];
if (lastSelection) {
select.actions.select(this.get('buildSelection')(lastSelection, select), e);
if (typeof lastSelection === 'string') {
select.actions.search(lastSelection);
} else {
let searchField = this.get('searchField');
assert('`{{power-select-multiple}}` requires a `searchField` when the options are not strings to remove options using backspace', searchField);
select.actions.search(get(lastSelection, searchField));
}
select.actions.open(e);
}
}
} else if (e.keyCode >= 48 && e.keyCode <= 90 || e.keyCode === 32) { // Keys 0-9, a-z or SPACE
e.stopPropagation();
}
},
onMousedown(e) {
if (this.input.value) {
e.stopPropagation();
}
}
},
// Methods
selectedObject(list, index) {
if (list.objectAt) {
return list.objectAt(index);
} else {
return get(list, index);
}
}
});