@sourceloop/search-client 9.0.1 → 11.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.prettierignore +2 -0
- package/.prettierrc +7 -0
- package/CHANGELOG.md +123 -0
- package/README.md +26 -3
- package/bundle-element.ts +42 -0
- package/karma.conf.js +56 -0
- package/ng-package.json +7 -0
- package/package.json +28 -27
- package/src/favicon.ico +0 -0
- package/src/index.html +12 -0
- package/src/lib/lib-configuration.ts +112 -0
- package/src/lib/search/mocks/search.component.mock.ts +117 -0
- package/src/lib/search/promise-api-adapter.service.ts +31 -0
- package/src/lib/search/search.component.html +191 -0
- package/src/lib/search/search.component.scss +339 -0
- package/src/lib/search/search.component.spec.ts +306 -0
- package/src/lib/search/search.component.ts +457 -0
- package/src/lib/search-element.module.ts +49 -0
- package/src/lib/types.ts +85 -0
- package/src/main.ts +9 -0
- package/src/polyfills.ts +53 -0
- package/src/public-api.ts +10 -0
- package/src/styles.scss +4 -0
- package/src/test.ts +32 -0
- package/tsconfig.app.json +15 -0
- package/tsconfig.lib.json +20 -0
- package/tsconfig.lib.prod.json +10 -0
- package/tsconfig.spec.json +18 -0
- package/fesm2022/sourceloop-search-client.mjs +0 -564
- package/fesm2022/sourceloop-search-client.mjs.map +0 -1
- package/index.d.ts +0 -182
- /package/{assets → src/assets}/icomoon/fonts/icomoon.ttf +0 -0
- /package/{assets → src/assets}/icomoon/style.css +0 -0
|
@@ -0,0 +1,457 @@
|
|
|
1
|
+
// Copyright (c) 2023 Sourcefuse Technologies
|
|
2
|
+
//
|
|
3
|
+
// This software is released under the MIT License.
|
|
4
|
+
// https://opensource.org/licenses/MIT
|
|
5
|
+
import {
|
|
6
|
+
ChangeDetectorRef,
|
|
7
|
+
Component,
|
|
8
|
+
computed,
|
|
9
|
+
effect,
|
|
10
|
+
ElementRef,
|
|
11
|
+
Inject,
|
|
12
|
+
input,
|
|
13
|
+
OnDestroy,
|
|
14
|
+
OnInit,
|
|
15
|
+
Optional,
|
|
16
|
+
output,
|
|
17
|
+
PLATFORM_ID,
|
|
18
|
+
SimpleChanges,
|
|
19
|
+
TemplateRef,
|
|
20
|
+
ViewChild,
|
|
21
|
+
} from '@angular/core';
|
|
22
|
+
import {Configuration} from '../lib-configuration';
|
|
23
|
+
import {Subject} from 'rxjs';
|
|
24
|
+
import {debounceTime, tap} from 'rxjs/operators';
|
|
25
|
+
import {
|
|
26
|
+
ControlValueAccessor,
|
|
27
|
+
FormsModule,
|
|
28
|
+
NG_VALUE_ACCESSOR,
|
|
29
|
+
} from '@angular/forms';
|
|
30
|
+
import {
|
|
31
|
+
CustomSearchEvent,
|
|
32
|
+
ISearchService,
|
|
33
|
+
ISearchQuery,
|
|
34
|
+
SEARCH_SERVICE_TOKEN,
|
|
35
|
+
DEBOUNCE_TIME,
|
|
36
|
+
DEFAULT_LIMIT,
|
|
37
|
+
DEFAULT_LIMIT_TYPE,
|
|
38
|
+
DEFAULT_OFFSET,
|
|
39
|
+
DEFAULT_SAVE_IN_RECENTS,
|
|
40
|
+
DEFAULT_ORDER,
|
|
41
|
+
IReturnType,
|
|
42
|
+
RecentSearchEvent,
|
|
43
|
+
TypeEvent,
|
|
44
|
+
ItemClickedEvent,
|
|
45
|
+
IModel,
|
|
46
|
+
ISearchServiceWithPromises,
|
|
47
|
+
isApiServiceWithPromise,
|
|
48
|
+
} from '../types';
|
|
49
|
+
import {CommonModule, isPlatformBrowser} from '@angular/common';
|
|
50
|
+
import {PromiseApiAdapterService} from './promise-api-adapter.service';
|
|
51
|
+
import {MatFormFieldModule} from '@angular/material/form-field';
|
|
52
|
+
import {MatIconModule} from '@angular/material/icon';
|
|
53
|
+
import {MatSelectModule} from '@angular/material/select';
|
|
54
|
+
import {MatInputModule} from '@angular/material/input';
|
|
55
|
+
const ALL_LABEL = 'All';
|
|
56
|
+
@Component({
|
|
57
|
+
selector: 'sourceloop-search',
|
|
58
|
+
standalone: true,
|
|
59
|
+
templateUrl: './search.component.html',
|
|
60
|
+
styleUrls: ['./search.component.scss'],
|
|
61
|
+
providers: [
|
|
62
|
+
{
|
|
63
|
+
provide: NG_VALUE_ACCESSOR,
|
|
64
|
+
useExisting: SearchComponent,
|
|
65
|
+
multi: true,
|
|
66
|
+
},
|
|
67
|
+
],
|
|
68
|
+
imports: [
|
|
69
|
+
CommonModule,
|
|
70
|
+
FormsModule,
|
|
71
|
+
MatIconModule,
|
|
72
|
+
MatSelectModule,
|
|
73
|
+
MatFormFieldModule,
|
|
74
|
+
MatInputModule,
|
|
75
|
+
],
|
|
76
|
+
})
|
|
77
|
+
export class SearchComponent<T extends IReturnType>
|
|
78
|
+
implements OnInit, OnDestroy, ControlValueAccessor
|
|
79
|
+
{
|
|
80
|
+
readonly cfg = computed(() => {
|
|
81
|
+
const cfg = this.config();
|
|
82
|
+
if (!cfg) {
|
|
83
|
+
throw new Error('SearchComponent: config input is required');
|
|
84
|
+
}
|
|
85
|
+
return cfg;
|
|
86
|
+
});
|
|
87
|
+
config = input<Configuration<T>>();
|
|
88
|
+
searchProvider = input<ISearchService<T> | ISearchServiceWithPromises<T>>();
|
|
89
|
+
|
|
90
|
+
titleTemplate = input<TemplateRef<any> | undefined>();
|
|
91
|
+
subtitleTemplate = input<TemplateRef<any> | undefined>();
|
|
92
|
+
|
|
93
|
+
customAllLabel = input<string>(ALL_LABEL);
|
|
94
|
+
showOnlySearchResultOverlay = input<boolean>(false);
|
|
95
|
+
|
|
96
|
+
customSearchEvent = input<CustomSearchEvent>({
|
|
97
|
+
searchValue: '',
|
|
98
|
+
modelName: ALL_LABEL,
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
clicked = output<ItemClickedEvent<T>>();
|
|
102
|
+
searched = output<RecentSearchEvent>();
|
|
103
|
+
|
|
104
|
+
searchBoxInput = '';
|
|
105
|
+
suggestionsDisplay = false;
|
|
106
|
+
categoryDisplay = false;
|
|
107
|
+
searching = false;
|
|
108
|
+
suggestions: T[] = [];
|
|
109
|
+
recentSearches: ISearchQuery[] = [];
|
|
110
|
+
category: string = ALL_LABEL;
|
|
111
|
+
searchRequest$ = new Subject<{input: string; event: Event}>();
|
|
112
|
+
|
|
113
|
+
private searchService!: ISearchService<T>;
|
|
114
|
+
|
|
115
|
+
onChange: (value: string | undefined) => void = () => {};
|
|
116
|
+
onTouched: () => void = () => {};
|
|
117
|
+
disabled = false;
|
|
118
|
+
|
|
119
|
+
@ViewChild('searchInput') public searchInputElement!: ElementRef;
|
|
120
|
+
|
|
121
|
+
constructor(
|
|
122
|
+
@Inject(SEARCH_SERVICE_TOKEN)
|
|
123
|
+
@Optional()
|
|
124
|
+
searchService: ISearchService<T>,
|
|
125
|
+
// tslint:disable-next-line:ban-types
|
|
126
|
+
@Inject(PLATFORM_ID)
|
|
127
|
+
private readonly platformId: Object,
|
|
128
|
+
private readonly cdr: ChangeDetectorRef,
|
|
129
|
+
private readonly promiseAdapter: PromiseApiAdapterService<T>,
|
|
130
|
+
) {
|
|
131
|
+
if (searchService) {
|
|
132
|
+
this.searchService = searchService;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
effect(() => {
|
|
136
|
+
const cfg = this.config();
|
|
137
|
+
if (!cfg) return;
|
|
138
|
+
|
|
139
|
+
if (cfg.models) {
|
|
140
|
+
cfg.models.unshift({
|
|
141
|
+
name: ALL_LABEL,
|
|
142
|
+
displayName: this.customAllLabel(),
|
|
143
|
+
});
|
|
144
|
+
} else {
|
|
145
|
+
cfg.models = [
|
|
146
|
+
{
|
|
147
|
+
name: ALL_LABEL,
|
|
148
|
+
displayName: this.customAllLabel(),
|
|
149
|
+
},
|
|
150
|
+
];
|
|
151
|
+
}
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
effect(() => {
|
|
155
|
+
let provider = this.searchProvider();
|
|
156
|
+
if (!provider) return;
|
|
157
|
+
|
|
158
|
+
if (isApiServiceWithPromise(provider)) {
|
|
159
|
+
provider = this.promiseAdapter.adapt(provider);
|
|
160
|
+
}
|
|
161
|
+
this.searchService = provider;
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
effect(() => {
|
|
165
|
+
const event = this.customSearchEvent();
|
|
166
|
+
if (!event) return;
|
|
167
|
+
|
|
168
|
+
if (event.searchValue !== undefined) {
|
|
169
|
+
this.searchBoxInput = event.searchValue;
|
|
170
|
+
this.searchOnCustomEventValueChange(this.searchBoxInput);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
if (event.modelName) {
|
|
174
|
+
this.setCategory(event.modelName);
|
|
175
|
+
}
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
ngOnInit(): void {
|
|
180
|
+
this.searchRequest$
|
|
181
|
+
.pipe(
|
|
182
|
+
tap(v => (this.suggestions = [])),
|
|
183
|
+
debounceTime(DEBOUNCE_TIME),
|
|
184
|
+
)
|
|
185
|
+
.subscribe((value: TypeEvent) => {
|
|
186
|
+
this.searched.emit({
|
|
187
|
+
event: value.event,
|
|
188
|
+
keyword: value.input,
|
|
189
|
+
category: this.category,
|
|
190
|
+
});
|
|
191
|
+
this.getSuggestions(value);
|
|
192
|
+
this.cdr.markForCheck();
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
// ControlValueAccessor Implementation
|
|
197
|
+
writeValue(value: string): void {
|
|
198
|
+
this.searchBoxInput = value;
|
|
199
|
+
}
|
|
200
|
+
// When the value in the UI is changed, this method will invoke a callback function
|
|
201
|
+
registerOnChange(fn: (value: string | undefined) => void): void {
|
|
202
|
+
this.onChange = fn;
|
|
203
|
+
}
|
|
204
|
+
registerOnTouched(fn: () => void): void {
|
|
205
|
+
this.onTouched = fn;
|
|
206
|
+
}
|
|
207
|
+
setDisabledState?(isDisabled: boolean): void {
|
|
208
|
+
this.disabled = isDisabled;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
getSuggestions(eventValue: TypeEvent) {
|
|
212
|
+
const cfg = this.config();
|
|
213
|
+
if (!cfg) return;
|
|
214
|
+
eventValue.input = eventValue.input.trim();
|
|
215
|
+
if (!eventValue.input.length) {
|
|
216
|
+
return;
|
|
217
|
+
}
|
|
218
|
+
const order = cfg.order ?? DEFAULT_ORDER;
|
|
219
|
+
const orderString = order.join(' ');
|
|
220
|
+
// let orderString = '';
|
|
221
|
+
// order.forEach(preference => (orderString = `${orderString}${preference} `));
|
|
222
|
+
|
|
223
|
+
let saveInRecents = cfg.saveInRecents ?? DEFAULT_SAVE_IN_RECENTS;
|
|
224
|
+
if (cfg.saveInRecents && cfg.saveInRecentsOnlyOnEnter) {
|
|
225
|
+
if (
|
|
226
|
+
!eventValue.event ||
|
|
227
|
+
(eventValue.event instanceof KeyboardEvent &&
|
|
228
|
+
eventValue.event.key === 'Enter')
|
|
229
|
+
) {
|
|
230
|
+
saveInRecents = true; // save in recents only on enter or change in category
|
|
231
|
+
} else {
|
|
232
|
+
// do not save in recent search on typing
|
|
233
|
+
saveInRecents = false;
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
const requestParameters: ISearchQuery = {
|
|
237
|
+
match: eventValue.input,
|
|
238
|
+
sources: this._categoryToSourceName(this.category),
|
|
239
|
+
limit: cfg.limit ?? DEFAULT_LIMIT,
|
|
240
|
+
limitByType: cfg.limitByType ?? DEFAULT_LIMIT_TYPE,
|
|
241
|
+
order: orderString,
|
|
242
|
+
offset: cfg.offset ?? DEFAULT_OFFSET,
|
|
243
|
+
};
|
|
244
|
+
|
|
245
|
+
this.searching = true;
|
|
246
|
+
this.cdr.markForCheck();
|
|
247
|
+
this.searchService
|
|
248
|
+
.searchApiRequest(requestParameters, saveInRecents)
|
|
249
|
+
.subscribe(
|
|
250
|
+
(value: T[]) => {
|
|
251
|
+
this.suggestions = value;
|
|
252
|
+
this.searching = false;
|
|
253
|
+
this.cdr.markForCheck();
|
|
254
|
+
},
|
|
255
|
+
(_error: Error) => {
|
|
256
|
+
this.suggestions = [];
|
|
257
|
+
this.searching = false;
|
|
258
|
+
this.cdr.markForCheck();
|
|
259
|
+
},
|
|
260
|
+
);
|
|
261
|
+
}
|
|
262
|
+
getRecentSearches() {
|
|
263
|
+
const cfg = this.config();
|
|
264
|
+
if (!cfg || cfg.hideRecentSearch) return;
|
|
265
|
+
this.searchService.recentSearchApiRequest?.().subscribe(
|
|
266
|
+
(value: ISearchQuery[]) => {
|
|
267
|
+
this.recentSearches = value;
|
|
268
|
+
this.cdr.markForCheck();
|
|
269
|
+
},
|
|
270
|
+
(_error: Error) => {
|
|
271
|
+
this.recentSearches = [];
|
|
272
|
+
this.cdr.markForCheck();
|
|
273
|
+
},
|
|
274
|
+
);
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
hitSearchApi(event?: Event) {
|
|
278
|
+
// this will happen only in case user searches something and
|
|
279
|
+
// then erases it, we need to update recent search
|
|
280
|
+
const cfg = this.config();
|
|
281
|
+
if (!cfg) return;
|
|
282
|
+
if (!this.searchBoxInput) {
|
|
283
|
+
this.suggestions = [];
|
|
284
|
+
this.getRecentSearches();
|
|
285
|
+
return;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
// no debounce time needed in case of searchOnlyOnEnter
|
|
289
|
+
if (cfg.searchOnlyOnEnter) {
|
|
290
|
+
if (!event || (event instanceof KeyboardEvent && event.key === 'Enter')) {
|
|
291
|
+
this.getSuggestions({input: this.searchBoxInput, event});
|
|
292
|
+
}
|
|
293
|
+
return;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
// no debounce time needed in case of change in category
|
|
297
|
+
if (!event) {
|
|
298
|
+
this.getSuggestions({input: this.searchBoxInput, event});
|
|
299
|
+
return;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
this.searchRequest$.next({
|
|
303
|
+
input: this.searchBoxInput,
|
|
304
|
+
event,
|
|
305
|
+
});
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
populateValue(suggestion: T, event: MouseEvent) {
|
|
309
|
+
const cfg = this.config();
|
|
310
|
+
if (!cfg) return;
|
|
311
|
+
this.searchBoxInput = String(suggestion[cfg.displayPropertyName]);
|
|
312
|
+
// converted to string to assign value to searchBoxInput
|
|
313
|
+
// this.searchBoxInput = value;
|
|
314
|
+
this.suggestionsDisplay = false;
|
|
315
|
+
// ngModelChange doesn't detect change in value
|
|
316
|
+
// when populated from outside, hence calling manually
|
|
317
|
+
this.onChange(this.searchBoxInput);
|
|
318
|
+
// need to do this to show more search options for selected
|
|
319
|
+
//suggestion - just in case user reopens search input
|
|
320
|
+
this.getSuggestions({input: this.searchBoxInput, event});
|
|
321
|
+
this.clicked.emit({item: suggestion, event});
|
|
322
|
+
}
|
|
323
|
+
populateValueRecentSearch(recentSearch: ISearchQuery, event: MouseEvent) {
|
|
324
|
+
event.stopPropagation();
|
|
325
|
+
event.preventDefault();
|
|
326
|
+
const value = recentSearch['match'];
|
|
327
|
+
this.searchBoxInput = value;
|
|
328
|
+
this.suggestionsDisplay = false;
|
|
329
|
+
this.onChange(this.searchBoxInput);
|
|
330
|
+
// need to do this to show more search options for selected
|
|
331
|
+
// suggestion - just in case user reopens search input
|
|
332
|
+
this.getSuggestions({input: this.searchBoxInput, event});
|
|
333
|
+
this.focusInput();
|
|
334
|
+
this.showSuggestions();
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
fetchModelImageUrlFromSuggestion(suggestion: T) {
|
|
338
|
+
const modelName = suggestion[
|
|
339
|
+
'source' as unknown as keyof T
|
|
340
|
+
] as unknown as string;
|
|
341
|
+
let url: string | undefined;
|
|
342
|
+
this.config()?.models.forEach(model => {
|
|
343
|
+
if (model.name === modelName && model.imageUrl) {
|
|
344
|
+
url = model.imageUrl;
|
|
345
|
+
}
|
|
346
|
+
});
|
|
347
|
+
return url;
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
boldString(str: T[keyof T] | string, substr: string) {
|
|
351
|
+
const strRegExp = new RegExp(`(${substr})`, 'gi');
|
|
352
|
+
const stringToMakeBold: string = str as unknown as string;
|
|
353
|
+
return stringToMakeBold.replace(strRegExp, `<b>$1</b>`);
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
hideSuggestions() {
|
|
357
|
+
this.suggestionsDisplay = false;
|
|
358
|
+
this.onTouched();
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
showSuggestions() {
|
|
362
|
+
this.suggestionsDisplay = true;
|
|
363
|
+
this.getRecentSearches();
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
focusInput() {
|
|
367
|
+
if (
|
|
368
|
+
isPlatformBrowser(this.platformId) &&
|
|
369
|
+
!this.showOnlySearchResultOverlay()
|
|
370
|
+
) {
|
|
371
|
+
this.searchInputElement.nativeElement.focus();
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
setCategory(category: string) {
|
|
376
|
+
this.category = category;
|
|
377
|
+
this.categoryDisplay = false;
|
|
378
|
+
if (this.searchBoxInput) {
|
|
379
|
+
this.hitSearchApi();
|
|
380
|
+
this.focusInput();
|
|
381
|
+
this.showSuggestions();
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
showCategory() {
|
|
386
|
+
this.categoryDisplay = !this.categoryDisplay;
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
hideCategory() {
|
|
390
|
+
this.categoryDisplay = false;
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
resetInput() {
|
|
394
|
+
this.searchBoxInput = '';
|
|
395
|
+
this.suggestions = [];
|
|
396
|
+
this.suggestionsDisplay = true;
|
|
397
|
+
this.focusInput();
|
|
398
|
+
// ngModelChange doesn't detect change in value
|
|
399
|
+
// when populated from outside, hence calling manually
|
|
400
|
+
this.onChange(this.searchBoxInput);
|
|
401
|
+
this.getRecentSearches();
|
|
402
|
+
}
|
|
403
|
+
ngOnDestroy() {
|
|
404
|
+
this.searchRequest$.unsubscribe();
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
_categoryToSourceName(category: string) {
|
|
408
|
+
if ([ALL_LABEL, this.customAllLabel()].includes(category)) {
|
|
409
|
+
return [];
|
|
410
|
+
} else {
|
|
411
|
+
return [category];
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
getModelFromModelName(name: string) {
|
|
415
|
+
return this.config()?.models.find(item => item.name === name) as IModel;
|
|
416
|
+
}
|
|
417
|
+
getModelsWithSuggestions() {
|
|
418
|
+
const modelsWithSuggestions: {model: IModel; items: T[]}[] = [];
|
|
419
|
+
const sources: string[] = [];
|
|
420
|
+
this.suggestions.forEach(suggestion => {
|
|
421
|
+
if (sources.indexOf(suggestion['source']) >= 0) {
|
|
422
|
+
modelsWithSuggestions.every(modelWithSuggestions => {
|
|
423
|
+
if (modelWithSuggestions.model.name === suggestion['source']) {
|
|
424
|
+
modelWithSuggestions.items.push(suggestion);
|
|
425
|
+
return false;
|
|
426
|
+
}
|
|
427
|
+
return true;
|
|
428
|
+
});
|
|
429
|
+
} else {
|
|
430
|
+
const model = this.getModelFromModelName(suggestion['source']);
|
|
431
|
+
modelsWithSuggestions.push({model, items: [suggestion]});
|
|
432
|
+
sources.push(suggestion['source']);
|
|
433
|
+
}
|
|
434
|
+
});
|
|
435
|
+
return modelsWithSuggestions;
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
searchOnCustomEventValueChange(value: string) {
|
|
439
|
+
if (value?.length) {
|
|
440
|
+
this.showSuggestions();
|
|
441
|
+
this.hitSearchApi();
|
|
442
|
+
} else {
|
|
443
|
+
this.hideSuggestions();
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
private _isCustomSearchEventChange(
|
|
448
|
+
changes: SimpleChanges,
|
|
449
|
+
propertyName: string,
|
|
450
|
+
) {
|
|
451
|
+
return (
|
|
452
|
+
!changes.customSearchEvent?.previousValue ||
|
|
453
|
+
changes.customSearchEvent?.previousValue[propertyName] !==
|
|
454
|
+
changes.customSearchEvent?.currentValue[propertyName]
|
|
455
|
+
);
|
|
456
|
+
}
|
|
457
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
// Copyright (c) 2023 Sourcefuse Technologies
|
|
2
|
+
//
|
|
3
|
+
// This software is released under the MIT License.
|
|
4
|
+
// https://opensource.org/licenses/MIT
|
|
5
|
+
import {CommonModule} from '@angular/common';
|
|
6
|
+
import {HttpClientModule} from '@angular/common/http';
|
|
7
|
+
import {Injector, NgModule} from '@angular/core';
|
|
8
|
+
import {FlexLayoutModule} from '@angular/flex-layout';
|
|
9
|
+
import {FormsModule} from '@angular/forms';
|
|
10
|
+
import {MatFormFieldModule} from '@angular/material/form-field';
|
|
11
|
+
import {MatIconModule} from '@angular/material/icon';
|
|
12
|
+
import {MatInputModule} from '@angular/material/input';
|
|
13
|
+
import {MatSelectModule} from '@angular/material/select';
|
|
14
|
+
import {MatButtonModule} from '@angular/material/button';
|
|
15
|
+
import {createCustomElement} from '@angular/elements';
|
|
16
|
+
import {SearchComponent} from './search/search.component';
|
|
17
|
+
import {BrowserModule} from '@angular/platform-browser';
|
|
18
|
+
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
|
|
19
|
+
import {Configuration} from './lib-configuration';
|
|
20
|
+
|
|
21
|
+
@NgModule({
|
|
22
|
+
imports: [
|
|
23
|
+
BrowserModule,
|
|
24
|
+
BrowserAnimationsModule,
|
|
25
|
+
CommonModule,
|
|
26
|
+
FormsModule,
|
|
27
|
+
HttpClientModule,
|
|
28
|
+
MatSelectModule,
|
|
29
|
+
MatFormFieldModule,
|
|
30
|
+
MatIconModule,
|
|
31
|
+
MatInputModule,
|
|
32
|
+
MatButtonModule,
|
|
33
|
+
FlexLayoutModule,
|
|
34
|
+
SearchComponent,
|
|
35
|
+
],
|
|
36
|
+
})
|
|
37
|
+
export class SearchElementModule {
|
|
38
|
+
constructor(private injector: Injector) {}
|
|
39
|
+
ngDoBootstrap() {
|
|
40
|
+
const webComponent = createCustomElement(SearchComponent, {
|
|
41
|
+
injector: this.injector,
|
|
42
|
+
});
|
|
43
|
+
customElements.define('sourceloop-search-element', webComponent);
|
|
44
|
+
// to export the Configuration class for vanilla JS projects
|
|
45
|
+
Object.assign(window, {
|
|
46
|
+
SearchConfiguration: Configuration,
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
}
|
package/src/lib/types.ts
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
// Copyright (c) 2023 Sourcefuse Technologies
|
|
2
|
+
//
|
|
3
|
+
// This software is released under the MIT License.
|
|
4
|
+
// https://opensource.org/licenses/MIT
|
|
5
|
+
import {InjectionToken} from '@angular/core';
|
|
6
|
+
import {Observable} from 'rxjs';
|
|
7
|
+
|
|
8
|
+
export interface ISearchQuery {
|
|
9
|
+
match: string;
|
|
10
|
+
limit: number | null;
|
|
11
|
+
order: string | null;
|
|
12
|
+
limitByType: boolean | null;
|
|
13
|
+
offset: number | null;
|
|
14
|
+
sources: string[] | null;
|
|
15
|
+
}
|
|
16
|
+
export interface IModel {
|
|
17
|
+
name: string;
|
|
18
|
+
displayName: string;
|
|
19
|
+
imageUrl?: string;
|
|
20
|
+
icon?: string;
|
|
21
|
+
}
|
|
22
|
+
export interface IReturnType {
|
|
23
|
+
rank: number;
|
|
24
|
+
source: string;
|
|
25
|
+
}
|
|
26
|
+
export interface IDefaultReturnType extends IReturnType {
|
|
27
|
+
name: string;
|
|
28
|
+
description: string;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export interface ISearchService<T extends IReturnType> {
|
|
32
|
+
searchApiRequest(
|
|
33
|
+
requestParameters: ISearchQuery,
|
|
34
|
+
saveInRecents: boolean,
|
|
35
|
+
): Observable<T[]>;
|
|
36
|
+
recentSearchApiRequest?(): Observable<ISearchQuery[]>;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface ISearchServiceWithPromises<T extends IReturnType> {
|
|
40
|
+
searchApiRequestWithPromise(
|
|
41
|
+
requestParameters: ISearchQuery,
|
|
42
|
+
saveInRecents: boolean,
|
|
43
|
+
): Promise<T[]>;
|
|
44
|
+
recentSearchApiRequestWithPromise?(): Promise<ISearchQuery[]>;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function isApiServiceWithPromise(
|
|
48
|
+
service:
|
|
49
|
+
| ISearchService<IReturnType>
|
|
50
|
+
| ISearchServiceWithPromises<IReturnType>,
|
|
51
|
+
): service is ISearchServiceWithPromises<IReturnType> {
|
|
52
|
+
return !!(service as ISearchServiceWithPromises<IReturnType>)
|
|
53
|
+
.searchApiRequestWithPromise;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// cant use T extends IReturnType here
|
|
57
|
+
export const SEARCH_SERVICE_TOKEN: InjectionToken<ISearchService<IReturnType>> =
|
|
58
|
+
new InjectionToken<ISearchService<IReturnType>>('Search_Service_Token');
|
|
59
|
+
|
|
60
|
+
export type RecentSearchEvent = {
|
|
61
|
+
event?: Event;
|
|
62
|
+
keyword: string;
|
|
63
|
+
category: string;
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
export type ItemClickedEvent<T> = {
|
|
67
|
+
event: MouseEvent;
|
|
68
|
+
item: T;
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
export type TypeEvent = {
|
|
72
|
+
event?: Event;
|
|
73
|
+
input: string;
|
|
74
|
+
};
|
|
75
|
+
// IRequestParameters default values
|
|
76
|
+
export const DEFAULT_LIMIT = 20;
|
|
77
|
+
export const DEFAULT_LIMIT_TYPE = false;
|
|
78
|
+
export const DEFAULT_ORDER = [];
|
|
79
|
+
export const DEBOUNCE_TIME = 1000;
|
|
80
|
+
export const DEFAULT_OFFSET = 0;
|
|
81
|
+
export const DEFAULT_SAVE_IN_RECENTS = true;
|
|
82
|
+
export type CustomSearchEvent = {
|
|
83
|
+
searchValue: string;
|
|
84
|
+
modelName: string;
|
|
85
|
+
};
|
package/src/main.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import {enableProdMode} from '@angular/core';
|
|
2
|
+
import {platformBrowserDynamic} from '@angular/platform-browser-dynamic';
|
|
3
|
+
import {SearchElementModule} from './lib/search-element.module';
|
|
4
|
+
|
|
5
|
+
enableProdMode();
|
|
6
|
+
|
|
7
|
+
platformBrowserDynamic()
|
|
8
|
+
.bootstrapModule(SearchElementModule)
|
|
9
|
+
.catch(err => console.error(err));
|
package/src/polyfills.ts
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file includes polyfills needed by Angular and is loaded before the app.
|
|
3
|
+
* You can add your own extra polyfills to this file.
|
|
4
|
+
*
|
|
5
|
+
* This file is divided into 2 sections:
|
|
6
|
+
* 1. Browser polyfills. These are applied before loading ZoneJS and are sorted by browsers.
|
|
7
|
+
* 2. Application imports. Files imported after ZoneJS that should be loaded before your main
|
|
8
|
+
* file.
|
|
9
|
+
*
|
|
10
|
+
* The current setup is for so-called "evergreen" browsers; the last versions of browsers that
|
|
11
|
+
* automatically update themselves. This includes recent versions of Safari, Chrome (including
|
|
12
|
+
* Opera), Edge on the desktop, and iOS and Chrome on mobile.
|
|
13
|
+
*
|
|
14
|
+
* Learn more in https://angular.io/guide/browser-support
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
/***************************************************************************************************
|
|
18
|
+
* BROWSER POLYFILLS
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* By default, zone.js will patch all possible macroTask and DomEvents
|
|
23
|
+
* user can disable parts of macroTask/DomEvents patch by setting following flags
|
|
24
|
+
* because those flags need to be set before `zone.js` being loaded, and webpack
|
|
25
|
+
* will put import in the top of bundle, so user need to create a separate file
|
|
26
|
+
* in this directory (for example: zone-flags.ts), and put the following flags
|
|
27
|
+
* into that file, and then add the following code before importing zone.js.
|
|
28
|
+
* import './zone-flags';
|
|
29
|
+
*
|
|
30
|
+
* The flags allowed in zone-flags.ts are listed here.
|
|
31
|
+
*
|
|
32
|
+
* The following flags will work for all browsers.
|
|
33
|
+
*
|
|
34
|
+
* (window as any).__Zone_disable_requestAnimationFrame = true; // disable patch requestAnimationFrame
|
|
35
|
+
* (window as any).__Zone_disable_on_property = true; // disable patch onProperty such as onclick
|
|
36
|
+
* (window as any).__zone_symbol__UNPATCHED_EVENTS = ['scroll', 'mousemove']; // disable patch specified eventNames
|
|
37
|
+
*
|
|
38
|
+
* in IE/Edge developer tools, the addEventListener will also be wrapped by zone.js
|
|
39
|
+
* with the following flag, it will bypass `zone.js` patch for IE/Edge
|
|
40
|
+
*
|
|
41
|
+
* (window as any).__Zone_enable_cross_context_check = true;
|
|
42
|
+
*
|
|
43
|
+
*/
|
|
44
|
+
|
|
45
|
+
/***************************************************************************************************
|
|
46
|
+
* Zone JS is required by default for Angular itself.
|
|
47
|
+
*/
|
|
48
|
+
import 'zone.js'; // Included with Angular CLI.
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
/***************************************************************************************************
|
|
52
|
+
* APPLICATION IMPORTS
|
|
53
|
+
*/
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
// Copyright (c) 2023 Sourcefuse Technologies
|
|
2
|
+
//
|
|
3
|
+
// This software is released under the MIT License.
|
|
4
|
+
// https://opensource.org/licenses/MIT
|
|
5
|
+
/*
|
|
6
|
+
* Public API Surface of my-lib
|
|
7
|
+
*/
|
|
8
|
+
export * from './lib/search/search.component';
|
|
9
|
+
export * from './lib/lib-configuration';
|
|
10
|
+
export * from './lib/types';
|
package/src/styles.scss
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
@import 'https://fonts.googleapis.com/icon?family=Material+Icons+Round';
|
|
2
|
+
@import 'https://fonts.googleapis.com/css2?family=Roboto:wght@300;400;500&display=swap';
|
|
3
|
+
@import '../../../node_modules/@angular/material/prebuilt-themes/indigo-pink.css';
|
|
4
|
+
@import '../src/assets/icomoon/style.css';
|
package/src/test.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
// Copyright (c) 2023 Sourcefuse Technologies
|
|
2
|
+
//
|
|
3
|
+
// This software is released under the MIT License.
|
|
4
|
+
// https://opensource.org/licenses/MIT
|
|
5
|
+
// This file is required by karma.conf.js and loads recursively all the .spec and framework files
|
|
6
|
+
import 'zone.js';
|
|
7
|
+
import 'zone.js/testing';
|
|
8
|
+
import {getTestBed} from '@angular/core/testing';
|
|
9
|
+
import {
|
|
10
|
+
BrowserDynamicTestingModule,
|
|
11
|
+
platformBrowserDynamicTesting,
|
|
12
|
+
} from '@angular/platform-browser-dynamic/testing'; // NOSONAR
|
|
13
|
+
|
|
14
|
+
declare const require: {
|
|
15
|
+
context(
|
|
16
|
+
path: string,
|
|
17
|
+
deep?: boolean,
|
|
18
|
+
filter?: RegExp,
|
|
19
|
+
): {
|
|
20
|
+
keys(): string[];
|
|
21
|
+
<T>(id: string): T;
|
|
22
|
+
};
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
// First, initialize the Angular testing environment.
|
|
26
|
+
getTestBed().initTestEnvironment(
|
|
27
|
+
BrowserDynamicTestingModule,
|
|
28
|
+
platformBrowserDynamicTesting(),
|
|
29
|
+
{
|
|
30
|
+
teardown: {destroyAfterEach: false},
|
|
31
|
+
},
|
|
32
|
+
);
|