forked from jebaGem/AngularRedux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauto-suggest.component.ts
272 lines (237 loc) · 7.99 KB
/
auto-suggest.component.ts
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
import {
Component, OnInit, ViewEncapsulation,
Input, Output, EventEmitter, forwardRef, OnChanges, SimpleChanges,
HostListener,
ViewChild,
ElementRef,
AfterViewInit
} from '@angular/core';
import {
FormBuilder, FormGroup, Validators, FormControl,
FormArray, AbstractControl
} from '@angular/forms';
import { ApiMasterAssetTypes } from '../../core/account/interfaces/api-master-asset';
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
import { findIndex, isNil } from 'lodash';
import { SearchFilterPipe } from './filter.pipe';
import { filter, first, startWith, delay, tap } from 'rxjs/operators';
import { EuppConfigureTranslateService } from '../translation/eupp-configure-translate.service';
import { IRootState } from '../../store/root.state';
import { select } from '@angular-redux/store';
import { Observable } from 'rxjs/Observable';
import { RoutesWorkflowService } from '../routes/routes-workflow.service';
enum HandleKeyboard {
UP = 'ArrowUp',
DOWN = 'ArrowDown',
ENTER = 'Enter',
ESC = 'Escape'
}
@Component({
selector: 'app-auto-suggest',
templateUrl: './auto-suggest.component.html',
styleUrls: ['./auto-suggest.component.scss'],
encapsulation: ViewEncapsulation.None,
providers: [
{
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => AutoSuggestComponent),
multi: true
}
]
})
export class AutoSuggestComponent implements OnInit,
OnChanges, ControlValueAccessor, AfterViewInit {
@Input() indexProperty: string;
@Input('value') _value; // tslint:disable-line:no-input-rename
@Input() brandModels: ApiMasterAssetTypes.IModel[];
@Input() searchProperty: string;
@Output() selectedAssetFromList = new EventEmitter();
@Output() change = new EventEmitter();
@ViewChild('autoSuggestSearch') autoSuggestSearchElem: ElementRef;
@select((state: IRootState) => state.routesReducer.steps[2].locked)
stepLocked$: Observable<boolean>;
stateForm: FormGroup;
showDropDown = false;
selectedId: string;
selectedIndex = 0;
filteredList: ApiMasterAssetTypes.IModel[] = [];
dropdownTopPosition = '-250px';
onChange(e) { }
onTouched: any = () => { };
get value() { return this._value; }
set value(val) {
this._value = val;
this.setSearchText(val);
this.onChange(val);
this.onTouched();
}
get query(): string { return this.stateForm.value.search; }
get searchInput(): AbstractControl { return this.stateForm.controls.search; }
constructor(private fb: FormBuilder, private filterPipe: SearchFilterPipe,
private _stepLogic: RoutesWorkflowService,
private _euppConfigureTranslateService: EuppConfigureTranslateService) {
this.initForm();
this.initLockedStepListener();
}
ngOnInit() {
if (this.brandModels) {
this.selectedId = this.brandModels[0].modelId;
this._setFilteredList();
}
}
private initLockedStepListener() {
this._stepLogic.step$.subscribe(steps => {
if (steps[0] && steps[0].locked) {
this.stateForm.disable();
}
});
}
ngAfterViewInit() {
// This is really challenging and know bug in angular 5
// https://blog.angular-university.io/angular-debugging/
// ngAfterViewInit always throw error when there is a change in DOM
// So to solve thi setTimeout is required
setTimeout(() => {
this.autoSuggestSearchElem.nativeElement.focus();
}, 1);
}
// ngOnChanges listens to async props, once they are changed this function is executed
// preventing to subscribing in each component
ngOnChanges(changes: SimpleChanges) {
if (!isNil(this.brandModels) && !isNil(this.value)) {
this.value = this.value;
}
if (changes.brandModels.currentValue.length === 1) {
this.searchInput.disable();
} else if (this.searchInput.disabled) {
this.searchInput.enable();
}
}
rowHasError() {
const row = this.stateForm.controls.search;
return (row && row.touched && row.errors) || (row && row.touched && !this.value);
}
registerOnChange(fn) {
this.onChange = fn;
}
registerOnTouched(fn) {
this.onTouched = fn;
}
writeValue(value) {
if (this.value !== value) {
this.value = value;
this.change.emit(this.value);
}
}
initForm(): FormGroup {
return this.stateForm = this.fb.group({
search: [null, [Validators.required]]
});
}
selectValue(value) {
this.setSearchText(value[this.indexProperty]);
this.writeValue(value[this.indexProperty]);
this.showDropDown = false;
}
closeDropDown() {
if (this.showDropDown) {
this.showDropDown = false;
}
}
openDropDown() {
this.onTouched();
if (this.filteredList.length > 0) {
this.showDropDown = true;
} else {
this.showDropDown = false;
}
}
setSearchText(searchIndex: string) {
if (searchIndex) {
const foundVal = this.brandModels.find(x => x[this.indexProperty] === searchIndex);
this.stateForm.patchValue({ 'search': foundVal[this.searchProperty] });
} else {
this.stateForm.patchValue({ 'search': null });
}
}
// close list when the surrounding document is scrolled
// NOTE: necessary so that the position: fixed list will not move around the screen
@HostListener('document:scroll') onScroll() {
this.closeDropDown();
}
// handle multiple autosuggest lines
// NOTE: necessary so that the position:fixed list will not open at the same position
@HostListener('click', ['$event']) onClickInside(e) {
if (e.target.id === 'autoSuggestSearch') {
this._setDropdownPosition(e.target);
}
}
@HostListener('keypress', ['$event']) onKeyPressEvent(e) {
if (e.target.id === 'autoSuggestSearch') {
this._setDropdownPosition(e.target);
}
}
// handle keyboard navigation and scroll
keyNavigate(e: KeyboardEvent) {
// keyboard "scroll"
// NOTE: will not work in Safari and IE
if (this.selectedIndex !== 0) {
const itemNode = document.getElementsByClassName('selected')[0];
if (itemNode) {
itemNode.scrollIntoView({ block: 'center', inline: 'nearest' });
}
}
if (e.key === HandleKeyboard.UP && this.selectedIndex !== 0) {
this.selectedIndex--;
}
if (e.key === HandleKeyboard.DOWN && this.selectedIndex !== this.filteredList.length - 1) {
if (!this.showDropDown) {
this.openDropDown();
} else {
this.selectedIndex++;
}
}
if (e.key === HandleKeyboard.ESC) {
this.closeDropDown();
}
this.selectedId = this.filteredList[this.selectedIndex].modelId;
}
// handle selection by pressing Enter
keyConfirm(e: KeyboardEvent) {
e.preventDefault();
this.stateForm.patchValue({ 'search': this.filteredList[this.selectedIndex].modelName });
this.value = this.filteredList[this.selectedIndex].modelId;
this.closeDropDown();
}
// adjusts selectedIndex when the list changes
refineKeySelected() {
this._setFilteredList();
if (this.query) {
if (this.filteredList.length) {
const indexInFiltered = findIndex(this.filteredList,
{ modelId: this.brandModels[this.selectedIndex].modelId });
if (indexInFiltered < 0) {
this.selectedIndex = 0;
this.selectedId = this.filteredList[0].modelId;
} else {
this.selectedIndex = indexInFiltered;
}
}
} else {
this.value = null;
}
}
private _setFilteredList() {
this.filteredList =
this.filterPipe.transform(this.brandModels, this.query, this.searchProperty);
}
private _setDropdownPosition(basis: Element) {
if (basis) {
const boxBoundaries = basis.getBoundingClientRect();
this.dropdownTopPosition = (boxBoundaries.top + 50) + 'px';
}
}
getControls(frmGrp: FormGroup, key: string) {
return (<FormArray>frmGrp.controls[key]).controls;
}
}