wacom 20.0.21 → 20.1.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/README.md CHANGED
@@ -1,2345 +1,2345 @@
1
- # Angular (ngx) common
2
-
3
- Module which has common services and components which can be used on all projects.
4
-
5
- ## License
6
-
7
- [MIT](LICENSE)
8
-
9
- ## Instalation
10
-
11
- ```bash
12
- $ npm i --save wacom
13
- ```
14
-
15
- ## Services
16
-
17
- | Name | Description |
18
- | ------------------------------------------------------------------ | :-----------------------------------------------------------------: |
19
- | [**`Core`**](https://www.npmjs.com/package/wacom#core-service) | Common supportive function which can be used in any service |
20
- | [**`Http`**](https://www.npmjs.com/package/wacom#http-service) | Http layer for HttpClient |
21
- | [**`Store`**](https://www.npmjs.com/package/wacom#store-service) | Service will is responsible for keeping information on the device |
22
- | [**`Hash`**](https://www.npmjs.com/package/wacom#hash-service) | Hash management for easily use, storage which stay in url |
23
- | [**`Meta`**](https://www.npmjs.com/package/wacom#meta-service) | Website meta tags management within router |
24
- | [**`UI`**](https://www.npmjs.com/package/wacom#ui-service) | Supportive UI/UX service |
25
- | [**`Crud`**](https://www.npmjs.com/package/wacom#crud-service) | Provides basic CRUD operations for managing data with HTTP services |
26
- | [**`File`**](https://www.npmjs.com/package/wacom#file-service) | Handles file uploads, image processing, and file management tasks |
27
- | [**`Socket`**](https://www.npmjs.com/package/wacom#socket-service) | Manages WebSocket connections and real-time data communication |
28
- | [**`Time`**](https://www.npmjs.com/package/wacom#time-service) | Provides utilities for date and time manipulation and formatting |
29
- | [**`Dom`**](https://www.npmjs.com/package/wacom#dom-service) | Facilitates DOM manipulation and dynamic component loading |
30
-
31
- ## [Core Service](#core-service)
32
-
33
- ### SSR and Platform Services Initialization
34
-
35
- The `CoreService` manages the initialization of various platform-specific services depending on whether the application is running on the server or the client.
36
-
37
- #### Properties
38
-
39
- - `ssr` (boolean): Indicates whether the application is running on the server side.
40
- - `localStorage` (any): Local storage object. Uses a mock object on the server side.
41
- - `navigator` (any): Navigator object. Uses a mock object on the server side.
42
- - `document` (any): Document object. Uses a mock object on the server side.
43
- - `window` (any): Window object. Uses a mock object on the server side.
44
-
45
- ### String Prototype Extension
46
-
47
- The `CoreService` extends the `String` prototype with a `capitalize` method, allowing you to capitalize the first letter of any string instance.
48
-
49
- #### `capitalize(): string`
50
-
51
- Capitalizes the first letter of the string and makes the rest of the string lowercase.
52
-
53
- - **Example**:
54
- const exampleString = "hellO";
55
- console.log(exampleString.capitalize()); // Output: "Hello"
56
-
57
- ### Object to Array Function
58
-
59
- The `CoreService` provides an `ota` method to convert an object to an array. Optionally, it can hold keys instead of values.
60
-
61
- #### `ota(obj: any, holder?: boolean): any[]`
62
-
63
- Converts an object to an array. Optionally holds keys instead of values.
64
-
65
- - **Parameters**:
66
-
67
- - `obj` (any): The object to be converted.
68
- - `holder` (boolean): If true, the keys will be held in the array; otherwise, the values will be held. Default is `false`.
69
-
70
- - **Returns**:
71
-
72
- - `any[]`: The resulting array.
73
-
74
- - **Example**:
75
-
76
- ```Typescript
77
- const exampleObj = { a: 1, b: 2, c: 3 };
78
- const resultValues = coreService.ota(exampleObj);
79
- console.log(resultValues); // Output: [1, 2, 3]
80
- const resultKeys = coreService.ota(exampleObj, true);
81
- console.log(resultKeys); // Output: ['a', 'b', 'c']
82
- ```
83
-
84
- ### Array Splice Function
85
-
86
- The `CoreService` provides a `splice` method to remove elements from one array that are present in another array based on a comparison field.
87
-
88
- #### `splice(removeArray: any[], fromArray: any[], compareField: string = '_id'): any[]`
89
-
90
- Removes elements from `fromArray` that are present in `removeArray` based on a comparison field.
91
-
92
- - **Parameters**:
93
-
94
- - `removeArray` (any[]): The array of elements to remove.
95
- - `fromArray` (any[]): The array from which to remove elements.
96
- - `compareField` (string): The field to use for comparison. Default is `_id`.
97
-
98
- - **Returns**:
99
-
100
- - `any[]`: The modified `fromArray` with elements removed.
101
-
102
- - **Example**:
103
-
104
- ```
105
- const removeArray = [{ _id: '1' }, { _id: '3' }];
106
- const fromArray = [{ _id: '1' }, { _id: '2' }, { _id: '3' }, { _id: '4' }];
107
-
108
- const result = coreService.splice(removeArray, fromArray);
109
- console.log(result); // Output: [{ _id: '2' }, { _id: '4' }]
110
- ```
111
-
112
- ### ID Unification Function
113
-
114
- The `CoreService` provides an `ids2id` method to unite multiple \_id values into a single unique \_id. The resulting \_id is unique regardless of the order of the input \_id values.
115
-
116
- #### `ids2id(...args: string[]): string`
117
-
118
- Unites multiple \_id values into a single unique \_id. The resulting \_id is unique regardless of the order of the input \_id values.
119
-
120
- - **Parameters**:
121
-
122
- - `...args` (string[]): The \_id values to be united.
123
-
124
- - **Returns**:
125
-
126
- - `string`: The unique combined \_id.
127
-
128
- - **Example**:
129
-
130
- ```Typescript
131
- const id1 = "20230101abc";
132
- const id2 = "20230102xyz";
133
- const id3 = "20230101def";
134
-
135
- const result = coreService.ids2id(id1, id2, id3);
136
- console.log(result); // Output will be the ids sorted by the first 8 characters and joined
137
- ```
138
-
139
- ### Delayed Execution Function
140
-
141
- The `CoreService` provides an `afterWhile` method to delay the execution of a callback function for a specified amount of time. If called again within that time, the timer resets.
142
-
143
- #### `afterWhile(doc: string | object | (() => void), cb?: () => void, time: number = 1000): void`
144
-
145
- Delays the execution of a callback function for a specified amount of time. If called again within that time, the timer resets.
146
-
147
- - **Parameters**:
148
-
149
- - `doc` (string | object | (() => void)): A unique identifier for the timer, an object to host the timer, or the callback function.
150
- - `cb` (() => void): The callback function to execute after the delay.
151
- - `time` (number): The delay time in milliseconds. Default is 1000.
152
-
153
- - **Example**:
154
-
155
- ```Typescript
156
- coreService.afterWhile('example', () => {
157
- console.log('This message is delayed by 1 second');
158
- }, 1000);
159
-
160
- const obj = {};
161
- coreService.afterWhile(obj, () => {
162
- console.log('This message is delayed by 1 second and stored in obj.__afterWhile');
163
- }, 1000);
164
-
165
- coreService.afterWhile(() => {
166
- console.log('This message is delayed by 1 second using the default doc "common"');
167
- }, 1000);
168
- ```
169
-
170
- ### Copy Function
171
-
172
- The `CoreService` provides a `copy` method to recursively copy properties from one object to another.
173
-
174
- #### `copy<T>(from: T, to: T): void`
175
-
176
- Recursively copies properties from one object to another.
177
-
178
- - **Parameters**:
179
-
180
- - `from`: The source object from which properties are copied.
181
- - `to`: The target object to which properties are copied.
182
-
183
- - **Example**:
184
-
185
- ```Typescript
186
- const source = { a: 1, b: { c: 2 } };
187
- const target = {};
188
- coreService.copy(source, target);
189
- console.log(target); // Output: { a: 1, b: { c: 2 } }
190
- ```
191
-
192
- ### Device Detection
193
-
194
- The `CoreService` provides methods to detect the client's device type (mobile, tablet, or web).
195
-
196
- #### Properties
197
-
198
- - `device` (string): The detected device type.
199
-
200
- #### Methods
201
-
202
- ##### `detectDevice(): void`
203
-
204
- Detects the device type based on the user agent.
205
-
206
- - **Example**:
207
-
208
- ```Typescript
209
- coreService.detectDevice();
210
- ```
211
-
212
- ##### `isMobile(): boolean`
213
-
214
- Checks if the device is a mobile device.
215
-
216
- - **Returns**:
217
-
218
- - `boolean`: Returns true if the device is a mobile device.
219
-
220
- - **Example**:
221
-
222
- ```Typescript
223
- console.log(coreService.isMobile()); // Output: true or false
224
- ```
225
-
226
- ##### `isTablet(): boolean`
227
-
228
- Checks if the device is a tablet.
229
-
230
- - **Returns**:
231
-
232
- - `boolean`: Returns true if the device is a tablet.
233
-
234
- - **Example**:
235
-
236
- ```Typescript
237
- console.log(coreService.isTablet()); // Output: true or false
238
- ```
239
-
240
- ##### `isWeb(): boolean`
241
-
242
- Checks if the device is a web browser.
243
-
244
- - **Returns**:
245
-
246
- - `boolean`: Returns true if the device is a web browser.
247
-
248
- - **Example**:
249
-
250
- ```Typescript
251
- console.log(coreService.isWeb()); // Output: true or false
252
- ```
253
-
254
- ##### `isAndroid(): boolean`
255
-
256
- Checks if the device is an Android device.
257
-
258
- - **Returns**:
259
-
260
- - `boolean`: Returns true if the device is an Android device.
261
-
262
- - **Example**:
263
-
264
- ```Typescript
265
- console.log(coreService.isAndroid()); // Output: true or false
266
- ```
267
-
268
- ##### `isIos(): boolean`
269
-
270
- Checks if the device is an iOS device.
271
-
272
- - **Returns**:
273
-
274
- - `boolean`: Returns true if the device is an iOS device.
275
-
276
- - **Example**:
277
-
278
- ```Typescript
279
- console.log(coreService.isIos()); // Output: true or false
280
- ```
281
-
282
- ### Version Management
283
-
284
- The `CoreService` provides methods for managing the application's version. The version is dynamically constructed from the app version and the date version.
285
-
286
- #### Properties
287
-
288
- - `version` (string): The combined version string of the application.
289
- - `appVersion` (string): The application version.
290
- - `dateVersion` (string): The date version.
291
-
292
- #### Methods
293
-
294
- ##### `setVersion(): void`
295
-
296
- Sets the combined version string based on `appVersion` and `dateVersion`.
297
-
298
- - **Example**:
299
-
300
- ```Typescript
301
- coreService.setVersion();
302
- ```
303
-
304
- ##### `setAppVersion(appVersion: string): void`
305
-
306
- Sets the app version and updates the combined version string.
307
-
308
- - **Parameters**:
309
-
310
- - `appVersion` (string): The application version to set.
311
-
312
- - **Example**:
313
-
314
- ```Typescript
315
- coreService.setAppVersion('1.2.3');
316
- ```
317
-
318
- ##### `setDateVersion(dateVersion: string): void`
319
-
320
- Sets the date version and updates the combined version string.
321
-
322
- - **Parameters**:
323
-
324
- - `dateVersion` (string): The date version to set.
325
-
326
- - **Example**:
327
-
328
- ```Typescript
329
- coreService.setDateVersion('2023-01-01');
330
- ```
331
-
332
- ### Signal Management
333
-
334
- The `CoreService` provides methods for managing signals (events) to facilitate communication between different parts of the application. This allows multiple components or services to subscribe to signals and be notified when those signals are emitted.
335
-
336
- #### Methods
337
-
338
- ##### `emit(signal: string, data?: any): void`
339
-
340
- Emits a signal, optionally passing data to the listeners.
341
-
342
- - **Parameters**:
343
-
344
- - `signal` (string): The name of the signal to emit.
345
- - `data` (any): Optional data to pass to the listeners.
346
-
347
- - **Example**:
348
- coreService.emit('mySignal', { key: 'value' });
349
-
350
- ##### `on(signal: string): Observable<any>`
351
-
352
- Returns an Observable that emits values when the specified signal is emitted. Multiple components or services can subscribe to this Observable to be notified of the signal.
353
-
354
- - **Parameters**:
355
-
356
- - `signal` (string): The name of the signal to listen for.
357
-
358
- - **Returns**:
359
-
360
- - `Observable<any>`: An Observable that emits when the signal is emitted.
361
-
362
- - **Example**:
363
-
364
- ```Typescript
365
- const subscription = coreService.on('mySignal').subscribe(data => {
366
- console.log('Signal received:', data);
367
- });
368
- // To unsubscribe from the signal
369
- subscription.unsubscribe();
370
- ```
371
-
372
- ##### `off(signal: string): void`
373
-
374
- Completes the Subject for a specific signal, effectively stopping any future emissions. This also unsubscribes all listeners for the signal.
375
-
376
- - **Parameters**:
377
-
378
- - `signal` (string): The name of the signal to stop.
379
-
380
- - **Example**:
381
-
382
- ```Typescript
383
- coreService.off('mySignal');
384
- ```
385
-
386
- #### Example Usage
387
-
388
- Here's an example demonstrating how to use the signal management methods in `CoreService`:
389
-
390
- ```Typescript
391
- import { CoreService } from 'wacom';
392
- import { Component, OnInit, OnDestroy } from '@angular/core';
393
- import { Subscription } from 'rxjs';
394
- export class AppComponent implements OnInit, OnDestroy {
395
- private signalSubscription: Subscription;
396
-
397
- constructor(private coreService: CoreService) {}
398
-
399
- ngOnInit() {
400
- this.setupSignalListeners();
401
- this.coreService.emit('update', { message: 'Data updated' });
402
- }
403
-
404
- setupSignalListeners() {
405
- this.signalSubscription = this.coreService.on('update').subscribe(data => {
406
- this.handleUpdate(data);
407
- });
408
- }
409
-
410
- handleUpdate(data: any) {
411
- console.log('Update signal received:', data);
412
- }
413
-
414
- ngOnDestroy() {
415
- if (this.signalSubscription) {
416
- this.signalSubscription.unsubscribe();
417
- }
418
- this.coreService.off('update');
419
- }
420
- }
421
- ```
422
-
423
- In this example:
424
-
425
- 1. The `on` method returns an `Observable` that emits when the `update` signal is emitted.
426
- 2. The component subscribes to the `Observable` to handle the emitted data.
427
- 3. The `emit` method is used to emit the `update` signal with some data.
428
- 4. The `off` method completes the `Subject` for the `update` signal, stopping any future emissions and cleaning up the signal.
429
-
430
- ### Await Management
431
-
432
- The `CoreService` provides methods for managing the completion of tasks. This is useful in scenarios where you need to wait for certain tasks to be completed before proceeding.
433
-
434
- #### Methods
435
-
436
- ##### `complete(task: string): void`
437
-
438
- Marks a task as complete.
439
-
440
- - **Parameters**:
441
-
442
- - `task` (string): The task to mark as complete, identified by a string.
443
-
444
- - **Example**:
445
- coreService.complete('myTask');
446
-
447
- ##### `onComplete(task: string): Promise<void>`
448
-
449
- Returns a Promise that resolves when the specified task is complete. This is useful for waiting until a task is completed.
450
-
451
- - **Parameters**:
452
-
453
- - `task` (string): The task to watch for completion, identified by a string.
454
-
455
- - **Returns**:
456
-
457
- - `Promise<void>`: A Promise that resolves when the task is complete.
458
-
459
- - **Example**:
460
- coreService.onComplete('myTask').then(() => {
461
- console.log('Task is now complete');
462
- });
463
-
464
- ##### `completed(task: string): boolean`
465
-
466
- Checks if a task is completed.
467
-
468
- - **Parameters**:
469
-
470
- - `task` (string): The task to check, identified by a string.
471
-
472
- - **Returns**:
473
-
474
- - `boolean`: True if the task is completed, false otherwise.
475
-
476
- - **Example**:
477
- if (coreService.completed('myTask')) {
478
- console.log('Task is completed');
479
- } else {
480
- console.log('Task is not yet completed');
481
- }
482
-
483
- #### Example Usage
484
-
485
- Here's an example demonstrating how to use the await management methods in `CoreService`:
486
-
487
- ```Typescript
488
- import { CoreService } from 'wacom';
489
- export class AppComponent {
490
- constructor(private coreService: CoreService) {
491
- this.checkTaskCompletion();
492
- }
493
-
494
- async checkTaskCompletion() {
495
- console.log('Starting task...');
496
-
497
- setTimeout(() => {
498
- this.coreService.complete('task1');
499
- console.log('Task completed');
500
- }, 2000);
501
-
502
- await this.coreService.onComplete('task1');
503
- console.log('Task is now acknowledged as complete');
504
- }
505
- }
506
- ```
507
-
508
- In this example:
509
-
510
- 1. The `complete` method is used to mark a task identified by `'task1'` as complete.
511
- 2. The `onComplete` method returns a Promise that resolves when the task is marked as complete, allowing the code to wait until the task is acknowledged as complete.
512
- 3. The `completed` method checks if a task is completed, returning a boolean value.
513
-
514
- ### Locking Management
515
-
516
- The `CoreService` provides methods for managing locks on resources to prevent concurrent access. This is useful in scenarios where you need to ensure that only one part of your application is accessing or modifying a resource at any given time.
517
-
518
- ### Methods
519
-
520
- #### `lock(which: string): void`
521
-
522
- Locks a resource to prevent concurrent access.
523
-
524
- - **Parameters**:
525
-
526
- - `which` (string): The resource to lock, identified by a string.
527
-
528
- - **Example**:
529
- coreService.lock('myResource');
530
-
531
- #### `unlock(which: string): void`
532
-
533
- Unlocks a resource, allowing other processes or threads to access it.
534
-
535
- - **Parameters**:
536
-
537
- - `which` (string): The resource to unlock, identified by a string.
538
-
539
- - **Example**:
540
- coreService.unlock('myResource');
541
-
542
- #### `onUnlock(which: string): Promise<void>`
543
-
544
- Returns a Promise that resolves when the specified resource is unlocked. This is useful for waiting until a resource becomes available.
545
-
546
- - **Parameters**:
547
-
548
- - `which` (string): The resource to watch for unlocking, identified by a string.
549
-
550
- - **Returns**:
551
-
552
- - `Promise<void>`: A Promise that resolves when the resource is unlocked.
553
-
554
- - **Example**:
555
- coreService.onUnlock('myResource').then(() => {
556
- console.log('Resource is now unlocked');
557
- });
558
-
559
- #### `locked(which: string): boolean`
560
-
561
- Checks if a resource is currently locked.
562
-
563
- - **Parameters**:
564
-
565
- - `which` (string): The resource to check, identified by a string.
566
-
567
- - **Returns**:
568
-
569
- - `boolean`: True if the resource is locked, false otherwise.
570
-
571
- - **Example**:
572
- if (coreService.locked('myResource')) {
573
- console.log('Resource is currently locked');
574
- } else {
575
- console.log('Resource is available');
576
- }
577
-
578
- ### Example Usage
579
-
580
- Here's an example demonstrating how to use the locking management methods in `CoreService`:
581
-
582
- ```Typescript
583
- import { CoreService } from 'wacom';
584
- export class AppComponent {
585
- constructor(private coreService: CoreService) {
586
- this.manageResource();
587
- }
588
-
589
- async manageResource() {
590
- this.coreService.lock('resource1');
591
- console.log('Resource locked');
592
-
593
- setTimeout(() => {
594
- this.coreService.unlock('resource1');
595
- console.log('Resource unlocked');
596
- }, 2000);
597
-
598
- await this.coreService.onUnlock('resource1');
599
- console.log('Resource is now available for use');
600
- }
601
- }
602
- ```
603
-
604
- In this example:
605
-
606
- 1. The `lock` method is used to lock a resource identified by `'resource1'`.
607
- 2. The `unlock` method is called after a timeout to unlock the resource.
608
- 3. The `onUnlock` method returns a Promise that resolves when the resource is unlocked, allowing the code to wait until the resource is available again.
609
-
610
- This ensures controlled access to the resource, preventing race conditions and ensuring data integrity.
611
-
612
- ## [Http Service](#http-service)
613
-
614
- The `HttpService` provides an HTTP layer for `HttpClient` in Angular, supporting both callbacks and observables for various HTTP operations.
615
-
616
- ### Methods
617
-
618
- #### `setUrl(url: string)`
619
-
620
- Sets the base URL for HTTP requests.
621
- **Parameters**:
622
-
623
- - `url` (string): The base URL.
624
-
625
- **Example**:
626
-
627
- ```Typescript
628
- httpService.setUrl('https://api.example.com');
629
- ```
630
-
631
- #### `removeUrl()`
632
-
633
- Removes the base URL for HTTP requests.
634
- **Example**:
635
-
636
- ```Typescript
637
- httpService.removeUrl();
638
- ```
639
-
640
- #### `set(key: string, value: string)`
641
-
642
- Sets a header for HTTP requests.
643
- **Parameters**:
644
-
645
- - `key` (string): The header key.
646
- - `value` (string): The header value.
647
-
648
- **Example**:
649
-
650
- ```Typescript
651
- httpService.set('Authorization', 'Bearer token');
652
- ```
653
-
654
- #### `header(key: string): string`
655
-
656
- Gets the value of a specified header.
657
- **Parameters**:
658
-
659
- - `key` (string): The header key.
660
-
661
- **Returns**:
662
-
663
- - The header value.
664
-
665
- **Example**:
666
-
667
- ```Typescript
668
- const authHeader = httpService.header('Authorization');
669
- ```
670
-
671
- #### `remove(key: string)`
672
-
673
- Removes a specified header.
674
- **Parameters**:
675
-
676
- - `key` (string): The header key.
677
-
678
- **Example**:
679
-
680
- ```Typescript
681
- httpService.remove('Authorization');
682
- ```
683
-
684
- #### `post(url: string, doc: any, callback = (resp: any) => {}, opts: any = {}): Observable<any>`
685
-
686
- Performs a POST request.
687
- **Parameters**:
688
-
689
- - `url` (string): The URL for the request.
690
- - `doc` (any): The request body.
691
- - `callback` (function): The callback function.
692
- - `opts` (any): Additional options.
693
-
694
- **Returns**:
695
-
696
- - An observable for the request.
697
-
698
- **Example**:
699
-
700
- ```Typescript
701
- httpService.post('/endpoint', data, (resp) => {
702
- console.log(resp);
703
- }).subscribe();
704
- ```
705
-
706
- #### `put(url: string, doc: any, callback = (resp: any) => {}, opts: any = {}): Observable<any>`
707
-
708
- Performs a PUT request.
709
- **Parameters**:
710
-
711
- - `url` (string): The URL for the request.
712
- - `doc` (any): The request body.
713
- - `callback` (function): The callback function.
714
- - `opts` (any): Additional options.
715
-
716
- **Returns**:
717
-
718
- - An observable for the request.
719
-
720
- **Example**:
721
-
722
- ```Typescript
723
- httpService.put('/endpoint', data, (resp) => {
724
- console.log(resp);
725
- }).subscribe();
726
- ```
727
-
728
- #### `patch(url: string, doc: any, callback = (resp: any) => {}, opts: any = {}): Observable<any>`
729
-
730
- Performs a PATCH request.
731
- **Parameters**:
732
-
733
- - `url` (string): The URL for the request.
734
- - `doc` (any): The request body.
735
- - `callback` (function): The callback function.
736
- - `opts` (any): Additional options.
737
-
738
- **Returns**:
739
-
740
- - An observable for the request.
741
-
742
- **Example**:
743
-
744
- ```Typescript
745
- httpService.patch('/endpoint', data, (resp) => {
746
- console.log(resp);
747
- }).subscribe();
748
- ```
749
-
750
- #### `delete(url: string, callback = (resp: any) => {}, opts: any = {}): Observable<any>`
751
-
752
- Performs a DELETE request.
753
- **Parameters**:
754
-
755
- - `url` (string): The URL for the request.
756
- - `callback` (function): The callback function.
757
- - `opts` (any): Additional options.
758
-
759
- **Returns**:
760
-
761
- - An observable for the request.
762
-
763
- **Example**:
764
-
765
- ```Typescript
766
- httpService.delete('/endpoint', (resp) => {
767
- console.log(resp);
768
- }).subscribe();
769
- ```
770
-
771
- #### `get(url: string, callback = (resp: any) => {}, opts: any = {}): Observable<any>`
772
-
773
- Performs a GET request.
774
- **Parameters**:
775
-
776
- - `url` (string): The URL for the request.
777
- - `callback` (function): The callback function.
778
- - `opts` (any): Additional options.
779
-
780
- **Returns**:
781
-
782
- - An observable for the request.
783
-
784
- **Example**:
785
-
786
- ```Typescript
787
- httpService.get('/endpoint', (resp) => {
788
- console.log(resp);
789
- }).subscribe();
790
- ```
791
-
792
- #### `clearLocked()`
793
-
794
- Clears all locked requests.
795
- **Example**:
796
-
797
- ```Typescript
798
- httpService.clearLocked();
799
- ```
800
-
801
- #### `lock()`
802
-
803
- Locks the service to prevent further requests.
804
- **Example**:
805
-
806
- ```Typescript
807
- httpService.lock();
808
- ```
809
-
810
- #### `unlock()`
811
-
812
- Unlocks the service to allow requests.
813
- **Example**:
814
-
815
- ```Typescript
816
- httpService.unlock();
817
- ```
818
-
819
- ## [Store Service](#store-service)
820
-
821
- The `StoreService` manages local storage in a configurable manner. It can set, get, and remove items from storage, with support for asynchronous operations and JSON serialization.
822
-
823
- ### Properties
824
-
825
- #### `_prefix: string`
826
-
827
- The prefix for storage keys.
828
-
829
- ### Methods
830
-
831
- #### `setPrefix(prefix: string): void`
832
-
833
- Sets the prefix for storage keys.
834
- **Parameters**:
835
-
836
- - `prefix` (string): The prefix to set.
837
-
838
- **Example**:
839
-
840
- ```Typescript
841
- storeService.setPrefix('app_');
842
- ```
843
-
844
- #### `set(key: string, value: string, callback: () => void = () => {}, errCallback: () => void = () => {}): void`
845
-
846
- Sets a value in storage.
847
-
848
- **Parameters**:
849
-
850
- - `key` (string): The storage key.
851
- - `value` (string): The value to store.
852
- - `callback` (function): The callback to execute on success.
853
- - `errCallback` (function): The callback to execute on error.
854
-
855
- **Example**:
856
-
857
- ```Typescript
858
- storeService.set('key', 'value', () => console.log('Success'), () => console.log('Error'));
859
- ```
860
-
861
- #### `setAsync(key: string, value: string): Promise<boolean>`
862
-
863
- Sets a value in storage asynchronously.
864
-
865
- **Parameters**:
866
-
867
- - `key` (string): The storage key.
868
- - `value` (string): The value to store.
869
-
870
- **Returns**:
871
-
872
- - `Promise<boolean>`: A promise that resolves to a boolean indicating success.
873
-
874
- **Example**:
875
-
876
- ```Typescript
877
- await storeService.setAsync('key', 'value');
878
- ```
879
-
880
- #### `get(key: string, callback: (value: string) => void = () => {}, errCallback: () => void = () => {}): void`
881
-
882
- Gets a value from storage.
883
-
884
- **Parameters**:
885
-
886
- - `key` (string): The storage key.
887
- - `callback` (function): The callback to execute with the retrieved value.
888
- - `errCallback` (function): The callback to execute on error.
889
-
890
- **Example**:
891
-
892
- ```Typescript
893
- storeService.get('key', value => console.log(value), () => console.log('Error'));
894
- ```
895
-
896
- #### `getAsync(key: string): Promise<string>`
897
-
898
- Gets a value from storage asynchronously.
899
-
900
- **Parameters**:
901
-
902
- - `key` (string): The storage key.
903
-
904
- **Returns**:
905
-
906
- - `Promise<string>`: A promise that resolves to the retrieved value.
907
-
908
- **Example**:
909
-
910
- ```Typescript
911
- const value = await storeService.getAsync('key');
912
- ```
913
-
914
- #### `setJson(key: string, value: any, callback: () => void = () => {}, errCallback: () => void = () => {}): void`
915
-
916
- Sets a JSON value in storage.
917
-
918
- **Parameters**:
919
-
920
- - `key` (string): The storage key.
921
- - `value` (any): The value to store.
922
- - `callback` (function): The callback to execute on success.
923
- - `errCallback` (function): The callback to execute on error.
924
-
925
- **Example**:
926
-
927
- ```Typescript
928
- storeService.setJson('key', { data: 'value' }, () => console.log('Success'), () => console.log('Error'));
929
- ```
930
-
931
- #### `setJsonAsync(key: string, value: any): Promise<boolean>`
932
-
933
- Sets a JSON value in storage asynchronously.
934
-
935
- **Parameters**:
936
-
937
- - `key` (string): The storage key.
938
- - `value` (any): The value to store.
939
-
940
- **Returns**:
941
-
942
- - `Promise<boolean>`: A promise that resolves to a boolean indicating success.
943
-
944
- **Example**:
945
-
946
- ```Typescript
947
- await storeService.setJsonAsync('key', { data: 'value' });
948
- ```
949
-
950
- #### `getJson(key: string, callback: (value: any) => void = () => {}, errCallback: () => void = () => {}): void`
951
-
952
- Gets a JSON value from storage.
953
-
954
- **Parameters**:
955
-
956
- - `key` (string): The storage key.
957
- - `callback` (function): The callback to execute with the retrieved value.
958
- - `errCallback` (function): The callback to execute on error.
959
-
960
- **Example**:
961
-
962
- ```Typescript
963
- storeService.getJson('key', value => console.log(value), () => console.log('Error'));
964
- ```
965
-
966
- #### `getJsonAsync<T = any>(key: string): Promise<T | null>`
967
-
968
- Gets a JSON value from storage asynchronously.
969
-
970
- **Parameters**:
971
-
972
- - `key` (string): The storage key.
973
-
974
- **Returns**:
975
-
976
- - `Promise<T | null>`: A promise that resolves to the retrieved value.
977
-
978
- **Example**:
979
-
980
- ```Typescript
981
- const value = await storeService.getJsonAsync('key');
982
- ```
983
-
984
- #### `remove(key: string, callback?: () => void, errCallback?: () => void): Promise<boolean>`
985
-
986
- Removes a value from storage.
987
-
988
- **Parameters**:
989
-
990
- - `key` (string): The storage key.
991
- - `callback` (function): The callback to execute on success.
992
- - `errCallback` (function): The callback to execute on error.
993
-
994
- **Returns**:
995
-
996
- - `Promise<boolean>`: A promise that resolves to a boolean indicating success.
997
-
998
- **Example**:
999
-
1000
- ```Typescript
1001
- await storeService.remove('key', () => console.log('Success'), () => console.log('Error'));
1002
- ```
1003
-
1004
- #### `clear(callback?: () => void, errCallback?: () => void): Promise<boolean>`
1005
-
1006
- Clears all values from storage.
1007
-
1008
- **Parameters**:
1009
-
1010
- - `callback` (function): The callback to execute on success.
1011
- - `errCallback` (function): The callback to execute on error.
1012
-
1013
- **Returns**:
1014
-
1015
- - `Promise<boolean>`: A promise that resolves to a boolean indicating success.
1016
-
1017
- **Example**:
1018
-
1019
- ```Typescript
1020
- await storeService.clear(() => console.log('Success'), () => console.log('Error'));
1021
- ```
1022
-
1023
- #### `applyPrefix(key: string): string`
1024
-
1025
- Applies the configured prefix to a storage key.
1026
-
1027
- **Parameters**:
1028
-
1029
- - `key` (string): The storage key.
1030
-
1031
- **Returns**:
1032
-
1033
- - `string`: The prefixed storage key.
1034
-
1035
- **Example**:
1036
-
1037
- ```Typescript
1038
- const prefixedKey = storeService.applyPrefix('key');
1039
- ```
1040
-
1041
- ## [Hash Service](#hash-service)
1042
-
1043
- The `HashService` manages the URL hash in an Angular application. It can parse, set, get, and clear hash values, providing a simple API for manipulating the URL hash.
1044
-
1045
- ### Properties
1046
-
1047
- #### `hash: { [key: string]: string }`
1048
-
1049
- The object containing the parsed hash values.
1050
-
1051
- ### Methods
1052
-
1053
- #### `initialize(): void`
1054
-
1055
- Initializes the hash service by loading the current hash from the URL.
1056
- **Example**:
1057
-
1058
- ```Typescript
1059
- hashService.initialize();
1060
- ```
1061
-
1062
- #### `load(): void`
1063
-
1064
- Loads the current hash from the URL into the hash object.
1065
-
1066
- **Example**:
1067
-
1068
- ```Typescript
1069
- hashService.load();
1070
- ```
1071
-
1072
- #### `applyReplacements(str: string | undefined): string`
1073
-
1074
- Applies replacements to a given string based on the replacements array.
1075
-
1076
- **Parameters**:
1077
-
1078
- - `str` (string | undefined): The string to apply replacements to.
1079
-
1080
- **Returns**:
1081
-
1082
- - `string`: The string with replacements applied.
1083
-
1084
- **Example**:
1085
-
1086
- ```Typescript
1087
- const result = hashService.applyReplacements('hello%20world');
1088
- ```
1089
-
1090
- #### `on(field: string, cb: (value: string) => void): void`
1091
-
1092
- Executes a callback with the value of a specific hash field once the hash is loaded.
1093
-
1094
- **Parameters**:
1095
-
1096
- - `field` (string): The hash field to get the value for.
1097
- - `cb` (function): The callback to execute with the value.
1098
-
1099
- **Example**:
1100
-
1101
- ```Typescript
1102
- hashService.on('key', value => console.log(value));
1103
- ```
1104
-
1105
- #### `save(): void`
1106
-
1107
- Saves the current hash object to the URL.
1108
-
1109
- **Example**:
1110
-
1111
- ```Typescript
1112
- hashService.save();
1113
- ```
1114
-
1115
- #### `set(field: string, value: string): void`
1116
-
1117
- Sets a value for a specific hash field and updates the URL.
1118
-
1119
- **Parameters**:
1120
-
1121
- - `field` (string): The hash field to set the value for.
1122
- - `value` (string): The value to set.
1123
-
1124
- **Example**:
1125
-
1126
- ```Typescript
1127
- hashService.set('key', 'value');
1128
- ```
1129
-
1130
- #### `get(field: string): string | undefined`
1131
-
1132
- Gets the value of a specific hash field.
1133
-
1134
- **Parameters**:
1135
-
1136
- - `field` (string): The hash field to get the value for.
1137
-
1138
- **Returns**:
1139
-
1140
- - `string | undefined`: The value of the hash field.
1141
-
1142
- **Example**:
1143
-
1144
- ```Typescript
1145
- const value = hashService.get('key');
1146
- ```
1147
-
1148
- #### `clear(field?: string): void`
1149
-
1150
- Clears a specific hash field or all hash fields and updates the URL.
1151
-
1152
- **Parameters**:
1153
-
1154
- - `field` (string | undefined): The hash field to clear. If not provided, clears all hash fields.
1155
-
1156
- **Example**:
1157
-
1158
- ```Typescript
1159
- hashService.clear('key');
1160
- hashService.clear();
1161
- ```
1162
-
1163
- ## [Meta Service](#meta-service)
1164
-
1165
- The `MetaService` manages meta tags and titles in an Angular application. It allows setting defaults, updating meta tags, and configuring titles dynamically.
1166
-
1167
- ### Methods
1168
-
1169
- #### `setDefaults(defaults: { [key: string]: string }): void`
1170
-
1171
- Sets the default meta tags.
1172
-
1173
- **Parameters**:
1174
-
1175
- - `defaults` (object): The default meta tags.
1176
-
1177
- **Example**:
1178
-
1179
- ```Typescript
1180
- metaService.setDefaults({ title: 'Default Title', description: 'Default Description' });
1181
- ```
1182
-
1183
- #### `setTitle(title?: string, titleSuffix?: string): MetaService`
1184
-
1185
- Sets the title and optional title suffix.
1186
-
1187
- **Parameters**:
1188
-
1189
- - `title` (string): The title to set.
1190
- - `titleSuffix` (string): The title suffix to append.
1191
-
1192
- **Returns**:
1193
-
1194
- - `MetaService`: The MetaService instance.
1195
-
1196
- **Example**:
1197
-
1198
- ```Typescript
1199
- metaService.setTitle('My Page Title', ' | My Website');
1200
- ```
1201
-
1202
- #### `setLink(links: { [key: string]: string }): MetaService`
1203
-
1204
- Sets link tags.
1205
-
1206
- **Parameters**:
1207
-
1208
- - `links` (object): The links to set.
1209
-
1210
- **Returns**:
1211
-
1212
- - `MetaService`: The MetaService instance.
1213
-
1214
- **Example**:
1215
-
1216
- ```Typescript
1217
- metaService.setLink({ canonical: 'https://example.com', stylesheet: 'https://example.com/style.css' });
1218
- ```
1219
-
1220
- #### `setTag(tag: string, value: string, prop?: string): MetaService`
1221
-
1222
- Sets a meta tag.
1223
-
1224
- **Parameters**:
1225
-
1226
- - `tag` (string): The meta tag name.
1227
- - `value` (string): The meta tag value.
1228
- - `prop` (string): The meta tag property.
1229
-
1230
- **Returns**:
1231
-
1232
- - `MetaService`: The MetaService instance.
1233
-
1234
- **Example**:
1235
-
1236
- ```Typescript
1237
- metaService.setTag('description', 'This is a description', 'name');
1238
- ```
1239
-
1240
- #### `removeTag(tag: string, prop?: string): void`
1241
-
1242
- Removes a meta tag.
1243
-
1244
- **Parameters**:
1245
-
1246
- - `tag` (string): The meta tag name.
1247
- - `prop` (string): The meta tag property.
1248
-
1249
- **Example**:
1250
-
1251
- ```Typescript
1252
- metaService.removeTag('description', 'name');
1253
- ```
1254
-
1255
- ### Private Methods
1256
-
1257
- #### `_updateMetaTag(tag: string, value: string, prop?: string): void`
1258
-
1259
- Updates a meta tag.
1260
-
1261
- **Parameters**:
1262
-
1263
- - `tag` (string): The meta tag name.
1264
- - `value` (string): The meta tag value.
1265
- - `prop` (string): The meta tag property.
1266
-
1267
- #### `_warnMissingGuard(): void`
1268
-
1269
- Warns about missing meta guards in routes.
1270
-
1271
- **Example**:
1272
-
1273
- ```Typescript
1274
- metaService._warnMissingGuard();
1275
- ```
1276
-
1277
- ## [UI Service](#ui-service)
1278
-
1279
- The `UiService` manages various UI-related tasks in an Angular application, including CSS management, form validation, and generating sample data for UI components.
1280
-
1281
- ### Methods
1282
-
1283
- #### `form(id: string): any`
1284
-
1285
- Manages form states.
1286
-
1287
- **Parameters**:
1288
-
1289
- - `id` (string): The form identifier.
1290
-
1291
- **Returns**:
1292
-
1293
- - `any`: The form state object.
1294
-
1295
- **Example**:
1296
-
1297
- ```Typescript
1298
- const formState = uiService.form('contactForm');
1299
- ```
1300
-
1301
- #### `valid(value: any, kind = 'email', extra = 0): boolean`
1302
-
1303
- Validates input values based on the specified type.
1304
-
1305
- **Parameters**:
1306
-
1307
- - `value` (any): The value to validate.
1308
- - `kind` (string): The type of validation.
1309
- - `extra` (number): Additional validation criteria.
1310
-
1311
- **Returns**:
1312
-
1313
- - `boolean`: True if the value is valid, false otherwise.
1314
-
1315
- **Example**:
1316
-
1317
- ```Typescript
1318
- const isValidEmail = uiService.valid('test@example.com', 'email');
1319
- ```
1320
-
1321
- #### `level(value = ''): number`
1322
-
1323
- Determines the strength of a password.
1324
-
1325
- **Parameters**:
1326
-
1327
- - `value` (string): The password to evaluate.
1328
-
1329
- **Returns**:
1330
-
1331
- - `number`: The strength level of the password.
1332
-
1333
- **Example**:
1334
-
1335
- ```Typescript
1336
- const passwordLevel = uiService.level('Password123!');
1337
- ```
1338
-
1339
- #### `set(variables: { [key: string]: string }, opts: any = {}): void`
1340
-
1341
- Sets multiple CSS variables.
1342
-
1343
- **Parameters**:
1344
-
1345
- - `variables` (object): The CSS variables to set.
1346
- - `opts` (any): Options for setting the variables.
1347
-
1348
- **Example**:
1349
-
1350
- ```Typescript
1351
- uiService.set({ '--primary-color': '#ff0000' }, 'local');
1352
- ```
1353
-
1354
- #### `get(): { [key: string]: string }`
1355
-
1356
- Retrieves the stored CSS variables.
1357
-
1358
- **Returns**:
1359
-
1360
- - `object`: The stored CSS variables.
1361
-
1362
- **Example**:
1363
-
1364
- ```Typescript
1365
- const cssVariables = uiService.get();
1366
- ```
1367
-
1368
- #### `remove(keys: string | string[]): void`
1369
-
1370
- Removes specified CSS variables.
1371
-
1372
- **Parameters**:
1373
-
1374
- - `keys` (string | array): The keys of the CSS variables to remove.
1375
-
1376
- **Example**:
1377
-
1378
- ```Typescript
1379
- uiService.remove('primary-color secondary-color');
1380
- ```
1381
-
1382
- #### `arr(arrLen = 10, type: string = 'number'): any[]`
1383
-
1384
- Generates an array of sample data.
1385
-
1386
- **Parameters**:
1387
-
1388
- - `arrLen` (number): The length of the array.
1389
- - `type` (string): The type of data to generate.
1390
-
1391
- **Returns**:
1392
-
1393
- - `array`: An array of sample data.
1394
-
1395
- **Example**:
1396
-
1397
- ```Typescript
1398
- const sampleArray = uiService.arr(5, 'text');
1399
- ```
1400
-
1401
- #### `text(length = 10): string`
1402
-
1403
- Generates a random text string.
1404
-
1405
- **Parameters**:
1406
-
1407
- - `length` (number): The length of the text string.
1408
-
1409
- **Returns**:
1410
-
1411
- - `string`: A random text string.
1412
-
1413
- **Example**:
1414
-
1415
- ```Typescript
1416
- const randomText = uiService.text(15);
1417
- ```
1418
-
1419
- ## [Crud Service](#crud-service)
1420
-
1421
- The `CrudService` is designed to manage CRUD (Create, Read, Update, Delete) operations in an Angular application. It interacts with an API, stores documents locally, and provides methods for handling various CRUD operations. It should be extended by specific services that manage different document types.
1422
-
1423
- ### Methods
1424
-
1425
- ---
1426
-
1427
- #### `new(doc: Document = {}): Document`
1428
-
1429
- Creates a new document with a temporary ID and status flags.
1430
-
1431
- **Parameters**:
1432
-
1433
- - `doc` (Document, optional): A base document to initialize.
1434
-
1435
- **Returns**:
1436
-
1437
- - `Document`: A new document instance with default properties.
1438
-
1439
- **Example**:
1440
- const newDoc = workService.new();
1441
-
1442
- ---
1443
-
1444
- #### `doc(_id: string): Document`
1445
-
1446
- Retrieves a document by its ID. If the document doesn't exist, a new one is created.
1447
-
1448
- **Parameters**:
1449
-
1450
- - `_id` (string): The document ID.
1451
-
1452
- **Returns**:
1453
-
1454
- - `Document`: The document instance.
1455
-
1456
- **Example**:
1457
- const doc = workService.doc('12345');
1458
-
1459
- ---
1460
-
1461
- #### `addDoc(doc: Document): void`
1462
-
1463
- Adds a new document or updates an existing document in the local store. It will attempt to update the document if it already exists in the collection.
1464
-
1465
- **Parameters**:
1466
-
1467
- - `doc` (Document): The document to add or update.
1468
-
1469
- **Example**:
1470
- workService.addDoc(doc);
1471
-
1472
- ---
1473
-
1474
- #### `addDocs(docs: Document[]): void`
1475
-
1476
- Adds multiple documents to the service. Each document will either be added or updated depending on whether it already exists.
1477
-
1478
- **Parameters**:
1479
-
1480
- - `docs` (Document[]): The array of documents to add.
1481
-
1482
- **Example**:
1483
- workService.addDocs(docs);
1484
-
1485
- ---
1486
-
1487
- #### `setDocs(): void`
1488
-
1489
- Saves the current state of documents to local storage.
1490
-
1491
- **Example**:
1492
- workService.setDocs();
1493
-
1494
- ---
1495
-
1496
- #### `getDocs(): Document[]`
1497
-
1498
- Retrieves the current list of documents stored locally.
1499
-
1500
- **Returns**:
1501
-
1502
- - `Document[]`: The list of documents.
1503
-
1504
- **Example**:
1505
- const docs = workService.getDocs();
1506
-
1507
- ---
1508
-
1509
- #### `setPerPage(_perPage: number): void`
1510
-
1511
- Sets the number of documents to display per page for pagination.
1512
-
1513
- **Parameters**:
1514
-
1515
- - `_perPage` (number): The number of documents per page.
1516
-
1517
- **Example**:
1518
- workService.setPerPage(10);
1519
-
1520
- ---
1521
-
1522
- #### `get(config: GetConfig = {}, options: CrudOptions<Document> = {}): Observable<Document[]>`
1523
-
1524
- Fetches a list of documents from the API with optional pagination and other settings.
1525
-
1526
- **Parameters**:
1527
-
1528
- - `config` (object, optional): The configuration for pagination (`page` and `perPage`).
1529
- - `options` (CrudOptions<Document>, optional): Options for callbacks and error handling.
1530
-
1531
- **Returns**:
1532
-
1533
- - `Observable<Document[]>`: An observable of the retrieved documents.
1534
-
1535
- **Example**:
1536
- workService.get({ page: 1 }, { callback: (docs) => console.log(docs) });
1537
-
1538
- ---
1539
-
1540
- #### `create(doc: Document, options: CrudOptions<Document> = {}): Observable<Document> | void`
1541
-
1542
- Creates a new document in the API and adds it to the local store. The document is only created once.
1543
-
1544
- **Parameters**:
1545
-
1546
- - `doc` (Document): The document to create.
1547
- - `options` (CrudOptions<Document>, optional): Options for callbacks and error handling.
1548
-
1549
- **Returns**:
1550
-
1551
- - `Observable<Document> | void`: An observable of the created document or void if the document was already created.
1552
-
1553
- **Example**:
1554
- workService.create(newDoc, { callback: (doc) => console.log(doc) });
1555
-
1556
- ---
1557
-
1558
- #### `fetch(query: object = {}, options: CrudOptions<Document> = {}): Observable<Document>`
1559
-
1560
- Fetches a document from the API based on a query object and adds it to the local store.
1561
-
1562
- **Parameters**:
1563
-
1564
- - `query` (object, optional): The query object to filter the documents.
1565
- - `options` (CrudOptions<Document>, optional): Options for callbacks and error handling.
1566
-
1567
- **Returns**:
1568
-
1569
- - `Observable<Document>`: An observable of the fetched document.
1570
-
1571
- **Example**:
1572
- workService.fetch({ name: 'example' }, { callback: (doc) => console.log(doc) });
1573
-
1574
- ---
1575
-
1576
- #### `updateAfterWhile(doc: Document, options: CrudOptions<Document> = {}): void`
1577
-
1578
- Updates a document after a specified delay using a core service function to handle the delay.
1579
-
1580
- **Parameters**:
1581
-
1582
- - `doc` (Document): The document to update.
1583
- - `options` (CrudOptions<Document>, optional): Options for callbacks and error handling.
1584
-
1585
- **Example**:
1586
- workService.updateAfterWhile(doc, { callback: (doc) => console.log(doc) });
1587
-
1588
- ---
1589
-
1590
- #### `update(doc: Document, options: CrudOptions<Document> = {}): Observable<Document>`
1591
-
1592
- Updates a document in the API and reflects the changes locally.
1593
-
1594
- **Parameters**:
1595
-
1596
- - `doc` (Document): The document to update.
1597
- - `options` (CrudOptions<Document>, optional): Options for callbacks and error handling.
1598
-
1599
- **Returns**:
1600
-
1601
- - `Observable<Document>`: An observable of the updated document.
1602
-
1603
- **Example**:
1604
- workService.update(doc, { callback: (doc) => console.log(doc) });
1605
-
1606
- ---
1607
-
1608
- #### `unique(doc: Document, options: CrudOptions<Document> = {}): Observable<Document>`
1609
-
1610
- Unique update a document field in the API.
1611
-
1612
- **Parameters**:
1613
-
1614
- - `doc` (Document): The document to update.
1615
- - `options` (CrudOptions<Document>, optional): Optional callback and error handling configuration.
1616
-
1617
- **Returns**:
1618
-
1619
- - `Observable<Document>`: An observable that resolves with the updated document.
1620
-
1621
- **Example**:
1622
- workService.unique(doc, { callback: (doc) => console.log('Document updated', doc) });
1623
-
1624
- ---
1625
-
1626
- #### `delete(doc: Document, options: CrudOptions<Document> = {}): Observable<Document>`
1627
-
1628
- Deletes a document from the API and updates the local store.
1629
-
1630
- **Parameters**:
1631
-
1632
- - `doc` (Document): The document to delete.
1633
- - `options` (CrudOptions<Document>, optional): Options for callbacks and error handling.
1634
-
1635
- **Returns**:
1636
-
1637
- - `Observable<Document>`: An observable of the deleted document.
1638
-
1639
- **Example**:
1640
- workService.delete(doc, { callback: (doc) => console.log(doc) });
1641
-
1642
- ---
1643
-
1644
- ### Interfaces
1645
-
1646
- ---
1647
-
1648
- #### `CrudDocument`
1649
-
1650
- Represents a CRUD document.
1651
-
1652
- **Properties**:
1653
-
1654
- - `_id` (string): The document ID.
1655
- - `__created` (boolean): Indicates if the document is created.
1656
- - `__modified` (boolean): Indicates if the document is modified.
1657
-
1658
- **Example**:
1659
- interface CrudDocument {
1660
- \_id: string;
1661
- **created: boolean;
1662
- **modified: boolean;
1663
- }
1664
-
1665
- ---
1666
-
1667
- ### Code sample use
1668
-
1669
- ```typescript
1670
- import { Injectable } from "@angular/core";
1671
- import { AlertService, CoreService, HttpService, StoreService, CrudService, CrudDocument } from "wacom";
1672
-
1673
- export interface Work extends CrudDocument {
1674
- name: string;
1675
- description: string;
1676
- }
1677
-
1678
- @Injectable({
1679
- providedIn: "root",
1680
- })
1681
- export class WorkService extends CrudService<Work> {
1682
- works: Work[] = this.getDocs();
1683
-
1684
- constructor(_http: HttpService, _store: StoreService, _alert: AlertService, _core: CoreService) {
1685
- super(
1686
- {
1687
- name: "work",
1688
- },
1689
- _http,
1690
- _store,
1691
- _alert,
1692
- _core
1693
- );
1694
-
1695
- this.get();
1696
- }
1697
- }
1698
- ```
1699
-
1700
- ## [File Service](#file-service)
1701
-
1702
- The `FileService` is designed to handle file uploads, image processing, and file management tasks in an Angular application. It interacts with the `HttpService` to send files to the server and provides utilities for image resizing and validation.
1703
-
1704
- ### Methods
1705
-
1706
- #### `add(opts: FileOptions | string): void | (() => void)`
1707
-
1708
- Adds a file input configuration.
1709
- **Parameters**:
1710
-
1711
- - `opts` (FileOptions | string): The file options or a string ID.
1712
-
1713
- **Example**:
1714
-
1715
- ```Typescript
1716
- fs.add({
1717
- id: 'fileInput',
1718
- type: 'image',
1719
- resize: 200,
1720
- cb: (dataUrl, file) => {
1721
- console.log('File processed:', dataUrl, file);
1722
- },
1723
- save: true,
1724
- });
1725
- ```
1726
-
1727
- #### `change(event: Event, info: FileOptions): void`
1728
-
1729
- Handles file input change event.
1730
- **Parameters**:
1731
-
1732
- - `event` (Event): The input change event.
1733
- - `info` (FileOptions): The file options.
1734
-
1735
- **Example**:
1736
-
1737
- ```Typescript
1738
- <input type="file" (change)="fs.change($event, fileOptions)">
1739
- ```
1740
-
1741
- #### `remove(part: string, url: string, opts: any = {}, cb: (resp: any) => void = () => {}): void | (() => void)`
1742
-
1743
- Removes a file.
1744
-
1745
- **Parameters**:
1746
-
1747
- - `part` (string): The part of the API.
1748
- - `url` (string): The URL of the file.
1749
- - `opts` (object): Additional options.
1750
- - `cb` (function): The callback function.
1751
-
1752
- **Example**:
1753
-
1754
- ```Typescript
1755
- fs.remove('images', 'https://example.com/image.jpg', {}, (resp) => {
1756
- console.log('File removed:', resp);
1757
- });
1758
- ```
1759
-
1760
- #### `uploadFiles(info: FileOptions, files: File[], cb: (resp: any) => void = () => {}): void`
1761
-
1762
- Uploads files to the server.
1763
- **Parameters**:
1764
-
1765
- - `info` (FileOptions): The file options.
1766
- - `files` (File[]): The files to upload.
1767
- - `cb` (function): The callback function.
1768
-
1769
- **Example**:
1770
-
1771
- ```Typescript
1772
- const files = document.getElementById('fileInput').files;
1773
- fs.uploadFiles(fileOptions, files, (resp) => {
1774
- console.log('Files uploaded:', resp);
1775
- });
1776
- ```
1777
-
1778
- #### `image(info: FileOptions, cb: (resp: any) => void = () => {}): void | (() => void)`
1779
-
1780
- Uploads an image to the server.
1781
- **Parameters**:
1782
-
1783
- - `info` (FileOptions): The file options.
1784
- - `cb` (function): The callback function.
1785
-
1786
- **Example**:
1787
-
1788
- ```Typescript
1789
- fs.image({
1790
- api: '/api/upload',
1791
- part: 'images',
1792
- name: 'profilePic',
1793
- }, (resp) => {
1794
- console.log('Image uploaded:', resp);
1795
- });
1796
- ```
1797
-
1798
- ### Interfaces
1799
-
1800
- #### `FileOptions`
1801
-
1802
- Represents the file options for uploading and processing files.
1803
- **Properties**:
1804
-
1805
- - `id` (string): The unique ID for the file input.
1806
- - `type` (string): The type of file ('image' or 'file').
1807
- - `resize` (number | object): The dimensions for resizing the image.
1808
- - `multiple` (boolean): Indicates if multiple files can be uploaded.
1809
- - `multiple_cb` (function): Callback function for multiple files.
1810
- - `cb` (function): Callback function for file processing.
1811
- - `save` (boolean): Indicates if the file should be saved.
1812
- - `complete` (function): Function to call when the file is saved.
1813
- - `api` (string): The API endpoint for uploading the file.
1814
- - `part` (string): The part of the API.
1815
- - `name` (string): The name of the file.
1816
- - `body` (function | object): Function or object to generate the request body.
1817
- - `resp` (function): Function to handle the response.
1818
- - `append` (function): Function to append files to FormData.
1819
- - `multiple_files` (array): Array of multiple files.
1820
- - `multiple_counter` (number): Counter for multiple files.
1821
- - `url` (string): The URL for the file.
1822
-
1823
- **Example**:
1824
-
1825
- ```Typescript
1826
- const fileOptions: FileOptions = {
1827
- id: 'fileInput',
1828
- type: 'image',
1829
- resize: { width: 200, height: 200 },
1830
- multiple: true,
1831
- multiple_cb: (files) => {
1832
- console.log('Multiple files processed:', files);
1833
- },
1834
- cb: (dataUrl, file) => {
1835
- console.log('File processed:', dataUrl, file);
1836
- },
1837
- save: true,
1838
- api: '/api/upload',
1839
- part: 'images',
1840
- name: 'profilePic',
1841
- body: () => ({ userId: 123 }),
1842
- resp: (response) => {
1843
- console.log('Server response:', response);
1844
- },
1845
- };
1846
- ```
1847
-
1848
- ## [Socket Service](#socket-service)
1849
-
1850
- The `SocketService` manages WebSocket connections using `socket.io`. It handles setting up the connection, listening for events, and emitting messages.
1851
-
1852
- ### Methods
1853
-
1854
- #### `setUrl(url: string): void`
1855
-
1856
- Sets the URL for the WebSocket connection and reloads the socket.
1857
- **Parameters**:
1858
-
1859
- - `url` (string): The URL of the WebSocket server.
1860
-
1861
- **Example**:
1862
-
1863
- ```Typescript
1864
- socketService.setUrl('https://example.com');
1865
- ```
1866
-
1867
- #### `on(to: string, cb: (message: any) => void = () => {}): void`
1868
-
1869
- Subscribes to a WebSocket event.
1870
-
1871
- **Parameters**:
1872
-
1873
- - `to` (string): The event to subscribe to.
1874
- - `cb` (function): The callback function to execute when the event is received.
1875
-
1876
- **Example**:
1877
-
1878
- ```Typescript
1879
- socketService.on('message', (msg) => {
1880
- console.log('Received message:', msg);
1881
- });
1882
- ```
1883
-
1884
- #### `emit(to: string, message: any, room: any = false): void`
1885
-
1886
- Emits a message to a WebSocket event.
1887
-
1888
- **Parameters**:
1889
-
1890
- - `to` (string): The event to emit the message to.
1891
- - `message` (any): The message to emit.
1892
- - `room` (any): Optional room to emit the message to.
1893
-
1894
- **Example**:
1895
-
1896
- ```Typescript
1897
- socketService.emit('message', { text: 'Hello, World!' });
1898
- ```
1899
-
1900
- ### Usage Example
1901
-
1902
- ```typescript
1903
- import { SocketService } from "wacom";
1904
-
1905
- @Component({
1906
- selector: "app-root",
1907
- templateUrl: "./app.component.html",
1908
- styleUrls: ["./app.component.css"],
1909
- })
1910
- export class AppComponent {
1911
- constructor(private socketService: SocketService) {
1912
- this.socketService.setUrl("https://example.com");
1913
- this.socketService.on("connect", () => {
1914
- console.log("Connected to WebSocket");
1915
- });
1916
- this.socketService.on("message", (msg) => {
1917
- console.log("Received message:", msg);
1918
- });
1919
- }
1920
-
1921
- sendMessage() {
1922
- this.socketService.emit("message", { text: "Hello, World!" });
1923
- }
1924
- }
1925
- ```
1926
-
1927
- ## [Time Service](#time-service)
1928
-
1929
- The `TimeService` provides comprehensive date and time management, including timezone handling, formatting dates, and utility functions for calendar operations.
1930
-
1931
- ### Methods
1932
-
1933
- #### `getDayName(date: Date, format: 'short' | 'long' = 'long'): string`
1934
-
1935
- Returns the name of the day of the week for a given date.
1936
-
1937
- **Parameters**:
1938
-
1939
- - `date` (Date): The date for which to get the day of the week.
1940
- - `format` ('short' | 'long'): The format in which to return the day name. Default is 'long'.
1941
-
1942
- **Returns**:
1943
-
1944
- - The name of the day of the week.
1945
-
1946
- **Example**:
1947
-
1948
- ```Typescript
1949
- const dayName = timeService.getDayName(new Date(), 'short');
1950
- console.log(dayName); // Output: 'Mon'
1951
- ```
1952
-
1953
- #### `formatDate(date: Date, format: string = 'mediumDate', timezone: string = 'UTC'): string`
1954
-
1955
- Formats a date according to the specified format and timezone.
1956
-
1957
- **Parameters**:
1958
-
1959
- - `date` (Date): The date to format.
1960
- - `format` (string): The format string (see Angular DatePipe documentation for format options).
1961
- - `timezone` (string): The timezone to use for formatting.
1962
-
1963
- **Returns**:
1964
-
1965
- - The formatted date string.
1966
-
1967
- **Example**:
1968
-
1969
- ```Typescript
1970
- const formattedDate = timeService.formatDate(new Date(), 'fullDate', 'America/New_York');
1971
- console.log(formattedDate); // Output: 'Monday, January 1, 2023'
1972
- ```
1973
-
1974
- #### `convertToTimezone(date: Date, timezone: string): Date`
1975
-
1976
- Converts a date to a different timezone.
1977
- **Parameters**:
1978
-
1979
- - `date` (Date): The date to convert.
1980
- - `timezone` (string): The timezone to convert to.
1981
-
1982
- **Returns**:
1983
-
1984
- - The date in the new timezone.
1985
-
1986
- **Example**:
1987
-
1988
- ```Typescript
1989
- const dateInTimezone = timeService.convertToTimezone(new Date(), 'Asia/Tokyo');
1990
- console.log(dateInTimezone);
1991
- ```
1992
-
1993
- #### `startOfDay(date: Date): Date`
1994
-
1995
- Returns the start of the day for a given date.
1996
- **Parameters**:
1997
-
1998
- - `date` (Date): The date for which to get the start of the day.
1999
-
2000
- **Returns**:
2001
-
2002
- - The start of the day (midnight) for the given date.
2003
-
2004
- **Example**:
2005
-
2006
- ```Typescript
2007
- const startOfDay = timeService.startOfDay(new Date());
2008
- console.log(startOfDay); // Output: '2023-01-01T00:00:00.000Z'
2009
- ```
2010
-
2011
- #### `endOfDay(date: Date): Date`
2012
-
2013
- Returns the end of the day for a given date.
2014
- **Parameters**:
2015
-
2016
- - `date` (Date): The date for which to get the end of the day.
2017
-
2018
- **Returns**:
2019
-
2020
- - The end of the day (one millisecond before midnight) for the given date.
2021
-
2022
- **Example**:
2023
-
2024
- ```Typescript
2025
- const endOfDay = timeService.endOfDay(new Date());
2026
- console.log(endOfDay); // Output: '2023-01-01T23:59:59.999Z'
2027
- ```
2028
-
2029
- #### `getDaysInMonth(month: number, year: number): number`
2030
-
2031
- Returns the number of days in a given month and year.
2032
- **Parameters**:
2033
-
2034
- - `month` (number): The month (0-11).
2035
- - `year` (number): The year.
2036
-
2037
- **Returns**:
2038
-
2039
- - The number of days in the month.
2040
-
2041
- **Example**:
2042
-
2043
- ```Typescript
2044
- const daysInMonth = timeService.getDaysInMonth(1, 2023);
2045
- console.log(daysInMonth); // Output: 28
2046
- ```
2047
-
2048
- #### `isLeapYear(year: number): boolean`
2049
-
2050
- Checks if a given year is a leap year.
2051
- **Parameters**:
2052
-
2053
- - `year` (number): The year to check.
2054
-
2055
- **Returns**:
2056
-
2057
- - True if the year is a leap year, false otherwise.
2058
-
2059
- **Example**:
2060
-
2061
- ```Typescript
2062
- const isLeap = timeService.isLeapYear(2024);
2063
- console.log(isLeap); // Output: true
2064
- ```
2065
-
2066
- #### `addDays(date: Date, days: number): Date`
2067
-
2068
- Adds a specified number of days to a date.
2069
- **Parameters**:
2070
-
2071
- - `date` (Date): The date to which to add days.
2072
- - `days` (number): The number of days to add.
2073
-
2074
- **Returns**:
2075
-
2076
- - The new date with the added days.
2077
-
2078
- **Example**:
2079
-
2080
- ```Typescript
2081
- const newDate = timeService.addDays(new Date(), 10);
2082
- console.log(newDate);
2083
- ```
2084
-
2085
- #### `addMonths(date: Date, months: number): Date`
2086
-
2087
- Adds a specified number of months to a date.
2088
- **Parameters**:
2089
-
2090
- - `date` (Date): The date to which to add months.
2091
- - `months` (number): The number of months to add.
2092
-
2093
- **Returns**:
2094
-
2095
- - The new date with the added months.
2096
-
2097
- **Example**:
2098
-
2099
- ```Typescript
2100
- const newDate = timeService.addMonths(new Date(), 2);
2101
- console.log(newDate);
2102
- ```
2103
-
2104
- #### `addYears(date: Date, years: number): Date`
2105
-
2106
- Adds a specified number of years to a date.
2107
- **Parameters**:
2108
-
2109
- - `date` (Date): The date to which to add years.
2110
- - `years` (number): The number of years to add.
2111
-
2112
- **Returns**:
2113
-
2114
- - The new date with the added years.
2115
-
2116
- **Example**:
2117
-
2118
- ```Typescript
2119
- const newDate = timeService.addYears(new Date(), 5);
2120
- console.log(newDate);
2121
- ```
2122
-
2123
- #### `subtractDays(date: Date, days: number): Date`
2124
-
2125
- Subtracts a specified number of days from a date.
2126
- **Parameters**:
2127
-
2128
- - `date` (Date): The date from which to subtract days.
2129
- - `days` (number): The number of days to subtract.
2130
-
2131
- **Returns**:
2132
-
2133
- - The new date with the subtracted days.
2134
-
2135
- **Example**:
2136
-
2137
- ```Typescript
2138
- const newDate = timeService.subtractDays(new Date(), 5);
2139
- console.log(newDate);
2140
- ```
2141
-
2142
- #### `subtractMonths(date: Date, months: number): Date`
2143
-
2144
- Subtracts a specified number of months from a date.
2145
- **Parameters**:
2146
-
2147
- - `date` (Date): The date from which to subtract months.
2148
- - `months` (number): The number of months to subtract.
2149
-
2150
- **Returns**:
2151
-
2152
- - The new date with the subtracted months.
2153
-
2154
- **Example**:
2155
-
2156
- ```Typescript
2157
- const newDate = timeService.subtractMonths(new Date(), 3);
2158
- console.log(newDate);
2159
- ```
2160
-
2161
- #### `subtractYears(date: Date, years: number): Date`
2162
-
2163
- Subtracts a specified number of years from a date.
2164
- **Parameters**:
2165
-
2166
- - `date` (Date): The date from which to subtract years.
2167
- - `years` (number): The number of years to subtract.
2168
-
2169
- **Returns**:
2170
-
2171
- - The new date with the subtracted years.
2172
-
2173
- **Example**:
2174
-
2175
- ```Typescript
2176
- const newDate = timeService.subtractYears(new Date(), 2);
2177
- console.log(newDate);
2178
- ```
2179
-
2180
- #### `isSameDay(date1: Date, date2: Date): boolean`
2181
-
2182
- Checks if two dates are on the same day.
2183
- **Parameters**:
2184
-
2185
- - `date1` (Date): The first date.
2186
- - `date2` (Date): The second date.
2187
-
2188
- **Returns**:
2189
-
2190
- - True if the dates are on the same day, false otherwise.
2191
-
2192
- **Example**:
2193
-
2194
- ```Typescript
2195
- const sameDay = timeService.isSameDay(new Date(), new Date());
2196
- console.log(sameDay); // Output: true
2197
- ```
2198
-
2199
- #### `getWeekNumber(date: Date): number`
2200
-
2201
- Returns the ISO week number for a given date.
2202
-
2203
- **Parameters**:
2204
-
2205
- - `date` (Date): The date for which to get the week number.
2206
-
2207
- **Returns**:
2208
-
2209
- - The ISO week number (1-53).
2210
-
2211
- **Example**:
2212
-
2213
- ```Typescript
2214
- const weekNumber = timeService.getWeekNumber(new Date());
2215
- console.log(weekNumber); // Output: 35 (example)
2216
- ```
2217
-
2218
- #### `getWeeksInMonth(month: number, year: number): number`
2219
-
2220
- Returns the number of weeks in a month for a given month and year.
2221
-
2222
- **Parameters**:
2223
-
2224
- - `month` (number): The month (0-11).
2225
- - `year` (number): The year.
2226
-
2227
- **Returns**:
2228
-
2229
- - The number of weeks in the month.
2230
-
2231
- **Example**:
2232
-
2233
- ```Typescript
2234
- const weeksInMonth = timeService.getWeeksInMonth(2, 2025);
2235
- console.log(weeksInMonth); // Output: 6 (example for March 2025)
2236
- ```
2237
-
2238
- ## [Dom Service](#dom-service)
2239
-
2240
- The `DomService` facilitates DOM manipulation and dynamic component loading in Angular applications.
2241
-
2242
- ### Methods
2243
-
2244
- #### `appendById(component: any, options: any = {}, id: string): { nativeElement: HTMLElement, componentRef: ComponentRef<any> }`
2245
-
2246
- Appends a component to a specified element by ID.
2247
-
2248
- **Parameters**:
2249
-
2250
- - `component` (any): The component to append.
2251
- - `options` (any): The options to project into the component.
2252
- - `id` (string): The ID of the element to append the component to.
2253
-
2254
- **Returns**:
2255
-
2256
- - An object containing the native element and the component reference.
2257
-
2258
- **Example**:
2259
-
2260
- ```Typescript
2261
- const result = domService.appendById(MyComponent, { inputProp: 'value' }, 'elementId');
2262
- console.log(result.nativeElement); // Output: The native DOM element
2263
- console.log(result.componentRef); // Output: The component reference
2264
- ```
2265
-
2266
- #### `appendComponent(component: any, options: any = {}, element: HTMLElement = this.core.document.body): { nativeElement: HTMLElement, componentRef: ComponentRef<any> }`
2267
-
2268
- Appends a component to a specified element or to the body.
2269
- **Parameters**:
2270
-
2271
- - `component` (any): The component to append.
2272
- - `options` (any): The options to project into the component.
2273
- - `element` (HTMLElement): The element to append the component to. Defaults to body.
2274
-
2275
- **Returns**:
2276
-
2277
- - An object containing the native element and the component reference.
2278
-
2279
- **Example**:
2280
-
2281
- ```Typescript
2282
- const result = domService.appendComponent(MyComponent, { inputProp: 'value' });
2283
- console.log(result.nativeElement); // Output: The native DOM element
2284
- console.log(result.componentRef); // Output: The component reference
2285
- ```
2286
-
2287
- #### `getComponentRef(component: any, options: any = {}): ComponentRef<any>`
2288
-
2289
- Gets a reference to a dynamically created component.
2290
- **Parameters**:
2291
-
2292
- - `component` (any): The component to create.
2293
- - `options` (any): The options to project into the component.
2294
-
2295
- **Returns**:
2296
-
2297
- - The component reference.
2298
-
2299
- **Example**:
2300
-
2301
- ```Typescript
2302
- const componentRef = domService.getComponentRef(MyComponent, { inputProp: 'value' });
2303
- console.log(componentRef); // Output: The component reference
2304
- ```
2305
-
2306
- #### `projectComponentInputs(component: ComponentRef<any>, options: any): ComponentRef<any>`
2307
-
2308
- Projects the inputs onto the component.
2309
- **Parameters**:
2310
-
2311
- - `component` (ComponentRef<any>): The component reference.
2312
- - `options` (any): The options to project into the component.
2313
-
2314
- **Returns**:
2315
-
2316
- - The component reference with the projected inputs.
2317
-
2318
- **Example**:
2319
-
2320
- ```Typescript
2321
- const componentRef = domService.getComponentRef(MyComponent);
2322
- domService.projectComponentInputs(componentRef, { inputProp: 'value' });
2323
- console.log(componentRef.instance.inputProp); // Output: 'value'
2324
- ```
2325
-
2326
- ### Usage Example
2327
-
2328
- ```Typescript
2329
- import { DomService } from './dom.service';
2330
-
2331
- @Component({
2332
- selector: 'app-root',
2333
- templateUrl: './app.component.html',
2334
- styleUrls: ['./app.component.css']
2335
- })
2336
- export class AppComponent {
2337
- constructor(private domService: DomService) {}
2338
-
2339
- addComponent() {
2340
- const result = this.domService.appendById(MyComponent, { inputProp: 'value' }, 'elementId');
2341
- console.log(result.nativeElement); // Output: The native DOM element
2342
- console.log(result.componentRef); // Output: The component reference
2343
- }
2344
- }
2345
- ```
1
+ # Angular (ngx) common
2
+
3
+ Module which has common services and components which can be used on all projects.
4
+
5
+ ## License
6
+
7
+ [MIT](LICENSE)
8
+
9
+ ## Instalation
10
+
11
+ ```bash
12
+ $ npm i --save wacom
13
+ ```
14
+
15
+ ## Services
16
+
17
+ | Name | Description |
18
+ | ------------------------------------------------------------------ | :-----------------------------------------------------------------: |
19
+ | [**`Core`**](https://www.npmjs.com/package/wacom#core-service) | Common supportive function which can be used in any service |
20
+ | [**`Http`**](https://www.npmjs.com/package/wacom#http-service) | Http layer for HttpClient |
21
+ | [**`Store`**](https://www.npmjs.com/package/wacom#store-service) | Service will is responsible for keeping information on the device |
22
+ | [**`Hash`**](https://www.npmjs.com/package/wacom#hash-service) | Hash management for easily use, storage which stay in url |
23
+ | [**`Meta`**](https://www.npmjs.com/package/wacom#meta-service) | Website meta tags management within router |
24
+ | [**`UI`**](https://www.npmjs.com/package/wacom#ui-service) | Supportive UI/UX service |
25
+ | [**`Crud`**](https://www.npmjs.com/package/wacom#crud-service) | Provides basic CRUD operations for managing data with HTTP services |
26
+ | [**`File`**](https://www.npmjs.com/package/wacom#file-service) | Handles file uploads, image processing, and file management tasks |
27
+ | [**`Socket`**](https://www.npmjs.com/package/wacom#socket-service) | Manages WebSocket connections and real-time data communication |
28
+ | [**`Time`**](https://www.npmjs.com/package/wacom#time-service) | Provides utilities for date and time manipulation and formatting |
29
+ | [**`Dom`**](https://www.npmjs.com/package/wacom#dom-service) | Facilitates DOM manipulation and dynamic component loading |
30
+
31
+ ## [Core Service](#core-service)
32
+
33
+ ### SSR and Platform Services Initialization
34
+
35
+ The `CoreService` manages the initialization of various platform-specific services depending on whether the application is running on the server or the client.
36
+
37
+ #### Properties
38
+
39
+ - `ssr` (boolean): Indicates whether the application is running on the server side.
40
+ - `localStorage` (any): Local storage object. Uses a mock object on the server side.
41
+ - `navigator` (any): Navigator object. Uses a mock object on the server side.
42
+ - `document` (any): Document object. Uses a mock object on the server side.
43
+ - `window` (any): Window object. Uses a mock object on the server side.
44
+
45
+ ### String Prototype Extension
46
+
47
+ The `CoreService` extends the `String` prototype with a `capitalize` method, allowing you to capitalize the first letter of any string instance.
48
+
49
+ #### `capitalize(): string`
50
+
51
+ Capitalizes the first letter of the string and makes the rest of the string lowercase.
52
+
53
+ - **Example**:
54
+ const exampleString = "hellO";
55
+ console.log(exampleString.capitalize()); // Output: "Hello"
56
+
57
+ ### Object to Array Function
58
+
59
+ The `CoreService` provides an `ota` method to convert an object to an array. Optionally, it can hold keys instead of values.
60
+
61
+ #### `ota(obj: any, holder?: boolean): any[]`
62
+
63
+ Converts an object to an array. Optionally holds keys instead of values.
64
+
65
+ - **Parameters**:
66
+
67
+ - `obj` (any): The object to be converted.
68
+ - `holder` (boolean): If true, the keys will be held in the array; otherwise, the values will be held. Default is `false`.
69
+
70
+ - **Returns**:
71
+
72
+ - `any[]`: The resulting array.
73
+
74
+ - **Example**:
75
+
76
+ ```Typescript
77
+ const exampleObj = { a: 1, b: 2, c: 3 };
78
+ const resultValues = coreService.ota(exampleObj);
79
+ console.log(resultValues); // Output: [1, 2, 3]
80
+ const resultKeys = coreService.ota(exampleObj, true);
81
+ console.log(resultKeys); // Output: ['a', 'b', 'c']
82
+ ```
83
+
84
+ ### Array Splice Function
85
+
86
+ The `CoreService` provides a `splice` method to remove elements from one array that are present in another array based on a comparison field.
87
+
88
+ #### `splice(removeArray: any[], fromArray: any[], compareField: string = '_id'): any[]`
89
+
90
+ Removes elements from `fromArray` that are present in `removeArray` based on a comparison field.
91
+
92
+ - **Parameters**:
93
+
94
+ - `removeArray` (any[]): The array of elements to remove.
95
+ - `fromArray` (any[]): The array from which to remove elements.
96
+ - `compareField` (string): The field to use for comparison. Default is `_id`.
97
+
98
+ - **Returns**:
99
+
100
+ - `any[]`: The modified `fromArray` with elements removed.
101
+
102
+ - **Example**:
103
+
104
+ ```
105
+ const removeArray = [{ _id: '1' }, { _id: '3' }];
106
+ const fromArray = [{ _id: '1' }, { _id: '2' }, { _id: '3' }, { _id: '4' }];
107
+
108
+ const result = coreService.splice(removeArray, fromArray);
109
+ console.log(result); // Output: [{ _id: '2' }, { _id: '4' }]
110
+ ```
111
+
112
+ ### ID Unification Function
113
+
114
+ The `CoreService` provides an `ids2id` method to unite multiple \_id values into a single unique \_id. The resulting \_id is unique regardless of the order of the input \_id values.
115
+
116
+ #### `ids2id(...args: string[]): string`
117
+
118
+ Unites multiple \_id values into a single unique \_id. The resulting \_id is unique regardless of the order of the input \_id values.
119
+
120
+ - **Parameters**:
121
+
122
+ - `...args` (string[]): The \_id values to be united.
123
+
124
+ - **Returns**:
125
+
126
+ - `string`: The unique combined \_id.
127
+
128
+ - **Example**:
129
+
130
+ ```Typescript
131
+ const id1 = "20230101abc";
132
+ const id2 = "20230102xyz";
133
+ const id3 = "20230101def";
134
+
135
+ const result = coreService.ids2id(id1, id2, id3);
136
+ console.log(result); // Output will be the ids sorted by the first 8 characters and joined
137
+ ```
138
+
139
+ ### Delayed Execution Function
140
+
141
+ The `CoreService` provides an `afterWhile` method to delay the execution of a callback function for a specified amount of time. If called again within that time, the timer resets.
142
+
143
+ #### `afterWhile(doc: string | object | (() => void), cb?: () => void, time: number = 1000): void`
144
+
145
+ Delays the execution of a callback function for a specified amount of time. If called again within that time, the timer resets.
146
+
147
+ - **Parameters**:
148
+
149
+ - `doc` (string | object | (() => void)): A unique identifier for the timer, an object to host the timer, or the callback function.
150
+ - `cb` (() => void): The callback function to execute after the delay.
151
+ - `time` (number): The delay time in milliseconds. Default is 1000.
152
+
153
+ - **Example**:
154
+
155
+ ```Typescript
156
+ coreService.afterWhile('example', () => {
157
+ console.log('This message is delayed by 1 second');
158
+ }, 1000);
159
+
160
+ const obj = {};
161
+ coreService.afterWhile(obj, () => {
162
+ console.log('This message is delayed by 1 second and stored in obj.__afterWhile');
163
+ }, 1000);
164
+
165
+ coreService.afterWhile(() => {
166
+ console.log('This message is delayed by 1 second using the default doc "common"');
167
+ }, 1000);
168
+ ```
169
+
170
+ ### Copy Function
171
+
172
+ The `CoreService` provides a `copy` method to recursively copy properties from one object to another.
173
+
174
+ #### `copy<T>(from: T, to: T): void`
175
+
176
+ Recursively copies properties from one object to another.
177
+
178
+ - **Parameters**:
179
+
180
+ - `from`: The source object from which properties are copied.
181
+ - `to`: The target object to which properties are copied.
182
+
183
+ - **Example**:
184
+
185
+ ```Typescript
186
+ const source = { a: 1, b: { c: 2 } };
187
+ const target = {};
188
+ coreService.copy(source, target);
189
+ console.log(target); // Output: { a: 1, b: { c: 2 } }
190
+ ```
191
+
192
+ ### Device Detection
193
+
194
+ The `CoreService` provides methods to detect the client's device type (mobile, tablet, or web).
195
+
196
+ #### Properties
197
+
198
+ - `device` (string): The detected device type.
199
+
200
+ #### Methods
201
+
202
+ ##### `detectDevice(): void`
203
+
204
+ Detects the device type based on the user agent.
205
+
206
+ - **Example**:
207
+
208
+ ```Typescript
209
+ coreService.detectDevice();
210
+ ```
211
+
212
+ ##### `isMobile(): boolean`
213
+
214
+ Checks if the device is a mobile device.
215
+
216
+ - **Returns**:
217
+
218
+ - `boolean`: Returns true if the device is a mobile device.
219
+
220
+ - **Example**:
221
+
222
+ ```Typescript
223
+ console.log(coreService.isMobile()); // Output: true or false
224
+ ```
225
+
226
+ ##### `isTablet(): boolean`
227
+
228
+ Checks if the device is a tablet.
229
+
230
+ - **Returns**:
231
+
232
+ - `boolean`: Returns true if the device is a tablet.
233
+
234
+ - **Example**:
235
+
236
+ ```Typescript
237
+ console.log(coreService.isTablet()); // Output: true or false
238
+ ```
239
+
240
+ ##### `isWeb(): boolean`
241
+
242
+ Checks if the device is a web browser.
243
+
244
+ - **Returns**:
245
+
246
+ - `boolean`: Returns true if the device is a web browser.
247
+
248
+ - **Example**:
249
+
250
+ ```Typescript
251
+ console.log(coreService.isWeb()); // Output: true or false
252
+ ```
253
+
254
+ ##### `isAndroid(): boolean`
255
+
256
+ Checks if the device is an Android device.
257
+
258
+ - **Returns**:
259
+
260
+ - `boolean`: Returns true if the device is an Android device.
261
+
262
+ - **Example**:
263
+
264
+ ```Typescript
265
+ console.log(coreService.isAndroid()); // Output: true or false
266
+ ```
267
+
268
+ ##### `isIos(): boolean`
269
+
270
+ Checks if the device is an iOS device.
271
+
272
+ - **Returns**:
273
+
274
+ - `boolean`: Returns true if the device is an iOS device.
275
+
276
+ - **Example**:
277
+
278
+ ```Typescript
279
+ console.log(coreService.isIos()); // Output: true or false
280
+ ```
281
+
282
+ ### Version Management
283
+
284
+ The `CoreService` provides methods for managing the application's version. The version is dynamically constructed from the app version and the date version.
285
+
286
+ #### Properties
287
+
288
+ - `version` (string): The combined version string of the application.
289
+ - `appVersion` (string): The application version.
290
+ - `dateVersion` (string): The date version.
291
+
292
+ #### Methods
293
+
294
+ ##### `setVersion(): void`
295
+
296
+ Sets the combined version string based on `appVersion` and `dateVersion`.
297
+
298
+ - **Example**:
299
+
300
+ ```Typescript
301
+ coreService.setVersion();
302
+ ```
303
+
304
+ ##### `setAppVersion(appVersion: string): void`
305
+
306
+ Sets the app version and updates the combined version string.
307
+
308
+ - **Parameters**:
309
+
310
+ - `appVersion` (string): The application version to set.
311
+
312
+ - **Example**:
313
+
314
+ ```Typescript
315
+ coreService.setAppVersion('1.2.3');
316
+ ```
317
+
318
+ ##### `setDateVersion(dateVersion: string): void`
319
+
320
+ Sets the date version and updates the combined version string.
321
+
322
+ - **Parameters**:
323
+
324
+ - `dateVersion` (string): The date version to set.
325
+
326
+ - **Example**:
327
+
328
+ ```Typescript
329
+ coreService.setDateVersion('2023-01-01');
330
+ ```
331
+
332
+ ### Signal Management
333
+
334
+ The `CoreService` provides methods for managing signals (events) to facilitate communication between different parts of the application. This allows multiple components or services to subscribe to signals and be notified when those signals are emitted.
335
+
336
+ #### Methods
337
+
338
+ ##### `emit(signal: string, data?: any): void`
339
+
340
+ Emits a signal, optionally passing data to the listeners.
341
+
342
+ - **Parameters**:
343
+
344
+ - `signal` (string): The name of the signal to emit.
345
+ - `data` (any): Optional data to pass to the listeners.
346
+
347
+ - **Example**:
348
+ coreService.emit('mySignal', { key: 'value' });
349
+
350
+ ##### `on(signal: string): Observable<any>`
351
+
352
+ Returns an Observable that emits values when the specified signal is emitted. Multiple components or services can subscribe to this Observable to be notified of the signal.
353
+
354
+ - **Parameters**:
355
+
356
+ - `signal` (string): The name of the signal to listen for.
357
+
358
+ - **Returns**:
359
+
360
+ - `Observable<any>`: An Observable that emits when the signal is emitted.
361
+
362
+ - **Example**:
363
+
364
+ ```Typescript
365
+ const subscription = coreService.on('mySignal').subscribe(data => {
366
+ console.log('Signal received:', data);
367
+ });
368
+ // To unsubscribe from the signal
369
+ subscription.unsubscribe();
370
+ ```
371
+
372
+ ##### `off(signal: string): void`
373
+
374
+ Completes the Subject for a specific signal, effectively stopping any future emissions. This also unsubscribes all listeners for the signal.
375
+
376
+ - **Parameters**:
377
+
378
+ - `signal` (string): The name of the signal to stop.
379
+
380
+ - **Example**:
381
+
382
+ ```Typescript
383
+ coreService.off('mySignal');
384
+ ```
385
+
386
+ #### Example Usage
387
+
388
+ Here's an example demonstrating how to use the signal management methods in `CoreService`:
389
+
390
+ ```Typescript
391
+ import { CoreService } from 'wacom';
392
+ import { Component, OnInit, OnDestroy } from '@angular/core';
393
+ import { Subscription } from 'rxjs';
394
+ export class AppComponent implements OnInit, OnDestroy {
395
+ private signalSubscription: Subscription;
396
+
397
+ constructor(private coreService: CoreService) {}
398
+
399
+ ngOnInit() {
400
+ this.setupSignalListeners();
401
+ this.coreService.emit('update', { message: 'Data updated' });
402
+ }
403
+
404
+ setupSignalListeners() {
405
+ this.signalSubscription = this.coreService.on('update').subscribe(data => {
406
+ this.handleUpdate(data);
407
+ });
408
+ }
409
+
410
+ handleUpdate(data: any) {
411
+ console.log('Update signal received:', data);
412
+ }
413
+
414
+ ngOnDestroy() {
415
+ if (this.signalSubscription) {
416
+ this.signalSubscription.unsubscribe();
417
+ }
418
+ this.coreService.off('update');
419
+ }
420
+ }
421
+ ```
422
+
423
+ In this example:
424
+
425
+ 1. The `on` method returns an `Observable` that emits when the `update` signal is emitted.
426
+ 2. The component subscribes to the `Observable` to handle the emitted data.
427
+ 3. The `emit` method is used to emit the `update` signal with some data.
428
+ 4. The `off` method completes the `Subject` for the `update` signal, stopping any future emissions and cleaning up the signal.
429
+
430
+ ### Await Management
431
+
432
+ The `CoreService` provides methods for managing the completion of tasks. This is useful in scenarios where you need to wait for certain tasks to be completed before proceeding.
433
+
434
+ #### Methods
435
+
436
+ ##### `complete(task: string): void`
437
+
438
+ Marks a task as complete.
439
+
440
+ - **Parameters**:
441
+
442
+ - `task` (string): The task to mark as complete, identified by a string.
443
+
444
+ - **Example**:
445
+ coreService.complete('myTask');
446
+
447
+ ##### `onComplete(task: string): Promise<void>`
448
+
449
+ Returns a Promise that resolves when the specified task is complete. This is useful for waiting until a task is completed.
450
+
451
+ - **Parameters**:
452
+
453
+ - `task` (string): The task to watch for completion, identified by a string.
454
+
455
+ - **Returns**:
456
+
457
+ - `Promise<void>`: A Promise that resolves when the task is complete.
458
+
459
+ - **Example**:
460
+ coreService.onComplete('myTask').then(() => {
461
+ console.log('Task is now complete');
462
+ });
463
+
464
+ ##### `completed(task: string): boolean`
465
+
466
+ Checks if a task is completed.
467
+
468
+ - **Parameters**:
469
+
470
+ - `task` (string): The task to check, identified by a string.
471
+
472
+ - **Returns**:
473
+
474
+ - `boolean`: True if the task is completed, false otherwise.
475
+
476
+ - **Example**:
477
+ if (coreService.completed('myTask')) {
478
+ console.log('Task is completed');
479
+ } else {
480
+ console.log('Task is not yet completed');
481
+ }
482
+
483
+ #### Example Usage
484
+
485
+ Here's an example demonstrating how to use the await management methods in `CoreService`:
486
+
487
+ ```Typescript
488
+ import { CoreService } from 'wacom';
489
+ export class AppComponent {
490
+ constructor(private coreService: CoreService) {
491
+ this.checkTaskCompletion();
492
+ }
493
+
494
+ async checkTaskCompletion() {
495
+ console.log('Starting task...');
496
+
497
+ setTimeout(() => {
498
+ this.coreService.complete('task1');
499
+ console.log('Task completed');
500
+ }, 2000);
501
+
502
+ await this.coreService.onComplete('task1');
503
+ console.log('Task is now acknowledged as complete');
504
+ }
505
+ }
506
+ ```
507
+
508
+ In this example:
509
+
510
+ 1. The `complete` method is used to mark a task identified by `'task1'` as complete.
511
+ 2. The `onComplete` method returns a Promise that resolves when the task is marked as complete, allowing the code to wait until the task is acknowledged as complete.
512
+ 3. The `completed` method checks if a task is completed, returning a boolean value.
513
+
514
+ ### Locking Management
515
+
516
+ The `CoreService` provides methods for managing locks on resources to prevent concurrent access. This is useful in scenarios where you need to ensure that only one part of your application is accessing or modifying a resource at any given time.
517
+
518
+ ### Methods
519
+
520
+ #### `lock(which: string): void`
521
+
522
+ Locks a resource to prevent concurrent access.
523
+
524
+ - **Parameters**:
525
+
526
+ - `which` (string): The resource to lock, identified by a string.
527
+
528
+ - **Example**:
529
+ coreService.lock('myResource');
530
+
531
+ #### `unlock(which: string): void`
532
+
533
+ Unlocks a resource, allowing other processes or threads to access it.
534
+
535
+ - **Parameters**:
536
+
537
+ - `which` (string): The resource to unlock, identified by a string.
538
+
539
+ - **Example**:
540
+ coreService.unlock('myResource');
541
+
542
+ #### `onUnlock(which: string): Promise<void>`
543
+
544
+ Returns a Promise that resolves when the specified resource is unlocked. This is useful for waiting until a resource becomes available.
545
+
546
+ - **Parameters**:
547
+
548
+ - `which` (string): The resource to watch for unlocking, identified by a string.
549
+
550
+ - **Returns**:
551
+
552
+ - `Promise<void>`: A Promise that resolves when the resource is unlocked.
553
+
554
+ - **Example**:
555
+ coreService.onUnlock('myResource').then(() => {
556
+ console.log('Resource is now unlocked');
557
+ });
558
+
559
+ #### `locked(which: string): boolean`
560
+
561
+ Checks if a resource is currently locked.
562
+
563
+ - **Parameters**:
564
+
565
+ - `which` (string): The resource to check, identified by a string.
566
+
567
+ - **Returns**:
568
+
569
+ - `boolean`: True if the resource is locked, false otherwise.
570
+
571
+ - **Example**:
572
+ if (coreService.locked('myResource')) {
573
+ console.log('Resource is currently locked');
574
+ } else {
575
+ console.log('Resource is available');
576
+ }
577
+
578
+ ### Example Usage
579
+
580
+ Here's an example demonstrating how to use the locking management methods in `CoreService`:
581
+
582
+ ```Typescript
583
+ import { CoreService } from 'wacom';
584
+ export class AppComponent {
585
+ constructor(private coreService: CoreService) {
586
+ this.manageResource();
587
+ }
588
+
589
+ async manageResource() {
590
+ this.coreService.lock('resource1');
591
+ console.log('Resource locked');
592
+
593
+ setTimeout(() => {
594
+ this.coreService.unlock('resource1');
595
+ console.log('Resource unlocked');
596
+ }, 2000);
597
+
598
+ await this.coreService.onUnlock('resource1');
599
+ console.log('Resource is now available for use');
600
+ }
601
+ }
602
+ ```
603
+
604
+ In this example:
605
+
606
+ 1. The `lock` method is used to lock a resource identified by `'resource1'`.
607
+ 2. The `unlock` method is called after a timeout to unlock the resource.
608
+ 3. The `onUnlock` method returns a Promise that resolves when the resource is unlocked, allowing the code to wait until the resource is available again.
609
+
610
+ This ensures controlled access to the resource, preventing race conditions and ensuring data integrity.
611
+
612
+ ## [Http Service](#http-service)
613
+
614
+ The `HttpService` provides an HTTP layer for `HttpClient` in Angular, supporting both callbacks and observables for various HTTP operations.
615
+
616
+ ### Methods
617
+
618
+ #### `setUrl(url: string)`
619
+
620
+ Sets the base URL for HTTP requests.
621
+ **Parameters**:
622
+
623
+ - `url` (string): The base URL.
624
+
625
+ **Example**:
626
+
627
+ ```Typescript
628
+ httpService.setUrl('https://api.example.com');
629
+ ```
630
+
631
+ #### `removeUrl()`
632
+
633
+ Removes the base URL for HTTP requests.
634
+ **Example**:
635
+
636
+ ```Typescript
637
+ httpService.removeUrl();
638
+ ```
639
+
640
+ #### `set(key: string, value: string)`
641
+
642
+ Sets a header for HTTP requests.
643
+ **Parameters**:
644
+
645
+ - `key` (string): The header key.
646
+ - `value` (string): The header value.
647
+
648
+ **Example**:
649
+
650
+ ```Typescript
651
+ httpService.set('Authorization', 'Bearer token');
652
+ ```
653
+
654
+ #### `header(key: string): string`
655
+
656
+ Gets the value of a specified header.
657
+ **Parameters**:
658
+
659
+ - `key` (string): The header key.
660
+
661
+ **Returns**:
662
+
663
+ - The header value.
664
+
665
+ **Example**:
666
+
667
+ ```Typescript
668
+ const authHeader = httpService.header('Authorization');
669
+ ```
670
+
671
+ #### `remove(key: string)`
672
+
673
+ Removes a specified header.
674
+ **Parameters**:
675
+
676
+ - `key` (string): The header key.
677
+
678
+ **Example**:
679
+
680
+ ```Typescript
681
+ httpService.remove('Authorization');
682
+ ```
683
+
684
+ #### `post(url: string, doc: any, callback = (resp: any) => {}, opts: any = {}): Observable<any>`
685
+
686
+ Performs a POST request.
687
+ **Parameters**:
688
+
689
+ - `url` (string): The URL for the request.
690
+ - `doc` (any): The request body.
691
+ - `callback` (function): The callback function.
692
+ - `opts` (any): Additional options.
693
+
694
+ **Returns**:
695
+
696
+ - An observable for the request.
697
+
698
+ **Example**:
699
+
700
+ ```Typescript
701
+ httpService.post('/endpoint', data, (resp) => {
702
+ console.log(resp);
703
+ }).subscribe();
704
+ ```
705
+
706
+ #### `put(url: string, doc: any, callback = (resp: any) => {}, opts: any = {}): Observable<any>`
707
+
708
+ Performs a PUT request.
709
+ **Parameters**:
710
+
711
+ - `url` (string): The URL for the request.
712
+ - `doc` (any): The request body.
713
+ - `callback` (function): The callback function.
714
+ - `opts` (any): Additional options.
715
+
716
+ **Returns**:
717
+
718
+ - An observable for the request.
719
+
720
+ **Example**:
721
+
722
+ ```Typescript
723
+ httpService.put('/endpoint', data, (resp) => {
724
+ console.log(resp);
725
+ }).subscribe();
726
+ ```
727
+
728
+ #### `patch(url: string, doc: any, callback = (resp: any) => {}, opts: any = {}): Observable<any>`
729
+
730
+ Performs a PATCH request.
731
+ **Parameters**:
732
+
733
+ - `url` (string): The URL for the request.
734
+ - `doc` (any): The request body.
735
+ - `callback` (function): The callback function.
736
+ - `opts` (any): Additional options.
737
+
738
+ **Returns**:
739
+
740
+ - An observable for the request.
741
+
742
+ **Example**:
743
+
744
+ ```Typescript
745
+ httpService.patch('/endpoint', data, (resp) => {
746
+ console.log(resp);
747
+ }).subscribe();
748
+ ```
749
+
750
+ #### `delete(url: string, callback = (resp: any) => {}, opts: any = {}): Observable<any>`
751
+
752
+ Performs a DELETE request.
753
+ **Parameters**:
754
+
755
+ - `url` (string): The URL for the request.
756
+ - `callback` (function): The callback function.
757
+ - `opts` (any): Additional options.
758
+
759
+ **Returns**:
760
+
761
+ - An observable for the request.
762
+
763
+ **Example**:
764
+
765
+ ```Typescript
766
+ httpService.delete('/endpoint', (resp) => {
767
+ console.log(resp);
768
+ }).subscribe();
769
+ ```
770
+
771
+ #### `get(url: string, callback = (resp: any) => {}, opts: any = {}): Observable<any>`
772
+
773
+ Performs a GET request.
774
+ **Parameters**:
775
+
776
+ - `url` (string): The URL for the request.
777
+ - `callback` (function): The callback function.
778
+ - `opts` (any): Additional options.
779
+
780
+ **Returns**:
781
+
782
+ - An observable for the request.
783
+
784
+ **Example**:
785
+
786
+ ```Typescript
787
+ httpService.get('/endpoint', (resp) => {
788
+ console.log(resp);
789
+ }).subscribe();
790
+ ```
791
+
792
+ #### `clearLocked()`
793
+
794
+ Clears all locked requests.
795
+ **Example**:
796
+
797
+ ```Typescript
798
+ httpService.clearLocked();
799
+ ```
800
+
801
+ #### `lock()`
802
+
803
+ Locks the service to prevent further requests.
804
+ **Example**:
805
+
806
+ ```Typescript
807
+ httpService.lock();
808
+ ```
809
+
810
+ #### `unlock()`
811
+
812
+ Unlocks the service to allow requests.
813
+ **Example**:
814
+
815
+ ```Typescript
816
+ httpService.unlock();
817
+ ```
818
+
819
+ ## [Store Service](#store-service)
820
+
821
+ The `StoreService` manages local storage in a configurable manner. It can set, get, and remove items from storage, with support for asynchronous operations and JSON serialization.
822
+
823
+ ### Properties
824
+
825
+ #### `_prefix: string`
826
+
827
+ The prefix for storage keys.
828
+
829
+ ### Methods
830
+
831
+ #### `setPrefix(prefix: string): void`
832
+
833
+ Sets the prefix for storage keys.
834
+ **Parameters**:
835
+
836
+ - `prefix` (string): The prefix to set.
837
+
838
+ **Example**:
839
+
840
+ ```Typescript
841
+ storeService.setPrefix('app_');
842
+ ```
843
+
844
+ #### `set(key: string, value: string, callback: () => void = () => {}, errCallback: () => void = () => {}): void`
845
+
846
+ Sets a value in storage.
847
+
848
+ **Parameters**:
849
+
850
+ - `key` (string): The storage key.
851
+ - `value` (string): The value to store.
852
+ - `callback` (function): The callback to execute on success.
853
+ - `errCallback` (function): The callback to execute on error.
854
+
855
+ **Example**:
856
+
857
+ ```Typescript
858
+ storeService.set('key', 'value', () => console.log('Success'), () => console.log('Error'));
859
+ ```
860
+
861
+ #### `setAsync(key: string, value: string): Promise<boolean>`
862
+
863
+ Sets a value in storage asynchronously.
864
+
865
+ **Parameters**:
866
+
867
+ - `key` (string): The storage key.
868
+ - `value` (string): The value to store.
869
+
870
+ **Returns**:
871
+
872
+ - `Promise<boolean>`: A promise that resolves to a boolean indicating success.
873
+
874
+ **Example**:
875
+
876
+ ```Typescript
877
+ await storeService.setAsync('key', 'value');
878
+ ```
879
+
880
+ #### `get(key: string, callback: (value: string) => void = () => {}, errCallback: () => void = () => {}): void`
881
+
882
+ Gets a value from storage.
883
+
884
+ **Parameters**:
885
+
886
+ - `key` (string): The storage key.
887
+ - `callback` (function): The callback to execute with the retrieved value.
888
+ - `errCallback` (function): The callback to execute on error.
889
+
890
+ **Example**:
891
+
892
+ ```Typescript
893
+ storeService.get('key', value => console.log(value), () => console.log('Error'));
894
+ ```
895
+
896
+ #### `getAsync(key: string): Promise<string>`
897
+
898
+ Gets a value from storage asynchronously.
899
+
900
+ **Parameters**:
901
+
902
+ - `key` (string): The storage key.
903
+
904
+ **Returns**:
905
+
906
+ - `Promise<string>`: A promise that resolves to the retrieved value.
907
+
908
+ **Example**:
909
+
910
+ ```Typescript
911
+ const value = await storeService.getAsync('key');
912
+ ```
913
+
914
+ #### `setJson(key: string, value: any, callback: () => void = () => {}, errCallback: () => void = () => {}): void`
915
+
916
+ Sets a JSON value in storage.
917
+
918
+ **Parameters**:
919
+
920
+ - `key` (string): The storage key.
921
+ - `value` (any): The value to store.
922
+ - `callback` (function): The callback to execute on success.
923
+ - `errCallback` (function): The callback to execute on error.
924
+
925
+ **Example**:
926
+
927
+ ```Typescript
928
+ storeService.setJson('key', { data: 'value' }, () => console.log('Success'), () => console.log('Error'));
929
+ ```
930
+
931
+ #### `setJsonAsync(key: string, value: any): Promise<boolean>`
932
+
933
+ Sets a JSON value in storage asynchronously.
934
+
935
+ **Parameters**:
936
+
937
+ - `key` (string): The storage key.
938
+ - `value` (any): The value to store.
939
+
940
+ **Returns**:
941
+
942
+ - `Promise<boolean>`: A promise that resolves to a boolean indicating success.
943
+
944
+ **Example**:
945
+
946
+ ```Typescript
947
+ await storeService.setJsonAsync('key', { data: 'value' });
948
+ ```
949
+
950
+ #### `getJson(key: string, callback: (value: any) => void = () => {}, errCallback: () => void = () => {}): void`
951
+
952
+ Gets a JSON value from storage.
953
+
954
+ **Parameters**:
955
+
956
+ - `key` (string): The storage key.
957
+ - `callback` (function): The callback to execute with the retrieved value.
958
+ - `errCallback` (function): The callback to execute on error.
959
+
960
+ **Example**:
961
+
962
+ ```Typescript
963
+ storeService.getJson('key', value => console.log(value), () => console.log('Error'));
964
+ ```
965
+
966
+ #### `getJsonAsync<T = any>(key: string): Promise<T | null>`
967
+
968
+ Gets a JSON value from storage asynchronously.
969
+
970
+ **Parameters**:
971
+
972
+ - `key` (string): The storage key.
973
+
974
+ **Returns**:
975
+
976
+ - `Promise<T | null>`: A promise that resolves to the retrieved value.
977
+
978
+ **Example**:
979
+
980
+ ```Typescript
981
+ const value = await storeService.getJsonAsync('key');
982
+ ```
983
+
984
+ #### `remove(key: string, callback?: () => void, errCallback?: () => void): Promise<boolean>`
985
+
986
+ Removes a value from storage.
987
+
988
+ **Parameters**:
989
+
990
+ - `key` (string): The storage key.
991
+ - `callback` (function): The callback to execute on success.
992
+ - `errCallback` (function): The callback to execute on error.
993
+
994
+ **Returns**:
995
+
996
+ - `Promise<boolean>`: A promise that resolves to a boolean indicating success.
997
+
998
+ **Example**:
999
+
1000
+ ```Typescript
1001
+ await storeService.remove('key', () => console.log('Success'), () => console.log('Error'));
1002
+ ```
1003
+
1004
+ #### `clear(callback?: () => void, errCallback?: () => void): Promise<boolean>`
1005
+
1006
+ Clears all values from storage.
1007
+
1008
+ **Parameters**:
1009
+
1010
+ - `callback` (function): The callback to execute on success.
1011
+ - `errCallback` (function): The callback to execute on error.
1012
+
1013
+ **Returns**:
1014
+
1015
+ - `Promise<boolean>`: A promise that resolves to a boolean indicating success.
1016
+
1017
+ **Example**:
1018
+
1019
+ ```Typescript
1020
+ await storeService.clear(() => console.log('Success'), () => console.log('Error'));
1021
+ ```
1022
+
1023
+ #### `applyPrefix(key: string): string`
1024
+
1025
+ Applies the configured prefix to a storage key.
1026
+
1027
+ **Parameters**:
1028
+
1029
+ - `key` (string): The storage key.
1030
+
1031
+ **Returns**:
1032
+
1033
+ - `string`: The prefixed storage key.
1034
+
1035
+ **Example**:
1036
+
1037
+ ```Typescript
1038
+ const prefixedKey = storeService.applyPrefix('key');
1039
+ ```
1040
+
1041
+ ## [Hash Service](#hash-service)
1042
+
1043
+ The `HashService` manages the URL hash in an Angular application. It can parse, set, get, and clear hash values, providing a simple API for manipulating the URL hash.
1044
+
1045
+ ### Properties
1046
+
1047
+ #### `hash: { [key: string]: string }`
1048
+
1049
+ The object containing the parsed hash values.
1050
+
1051
+ ### Methods
1052
+
1053
+ #### `initialize(): void`
1054
+
1055
+ Initializes the hash service by loading the current hash from the URL.
1056
+ **Example**:
1057
+
1058
+ ```Typescript
1059
+ hashService.initialize();
1060
+ ```
1061
+
1062
+ #### `load(): void`
1063
+
1064
+ Loads the current hash from the URL into the hash object.
1065
+
1066
+ **Example**:
1067
+
1068
+ ```Typescript
1069
+ hashService.load();
1070
+ ```
1071
+
1072
+ #### `applyReplacements(str: string | undefined): string`
1073
+
1074
+ Applies replacements to a given string based on the replacements array.
1075
+
1076
+ **Parameters**:
1077
+
1078
+ - `str` (string | undefined): The string to apply replacements to.
1079
+
1080
+ **Returns**:
1081
+
1082
+ - `string`: The string with replacements applied.
1083
+
1084
+ **Example**:
1085
+
1086
+ ```Typescript
1087
+ const result = hashService.applyReplacements('hello%20world');
1088
+ ```
1089
+
1090
+ #### `on(field: string, cb: (value: string) => void): void`
1091
+
1092
+ Executes a callback with the value of a specific hash field once the hash is loaded.
1093
+
1094
+ **Parameters**:
1095
+
1096
+ - `field` (string): The hash field to get the value for.
1097
+ - `cb` (function): The callback to execute with the value.
1098
+
1099
+ **Example**:
1100
+
1101
+ ```Typescript
1102
+ hashService.on('key', value => console.log(value));
1103
+ ```
1104
+
1105
+ #### `save(): void`
1106
+
1107
+ Saves the current hash object to the URL.
1108
+
1109
+ **Example**:
1110
+
1111
+ ```Typescript
1112
+ hashService.save();
1113
+ ```
1114
+
1115
+ #### `set(field: string, value: string): void`
1116
+
1117
+ Sets a value for a specific hash field and updates the URL.
1118
+
1119
+ **Parameters**:
1120
+
1121
+ - `field` (string): The hash field to set the value for.
1122
+ - `value` (string): The value to set.
1123
+
1124
+ **Example**:
1125
+
1126
+ ```Typescript
1127
+ hashService.set('key', 'value');
1128
+ ```
1129
+
1130
+ #### `get(field: string): string | undefined`
1131
+
1132
+ Gets the value of a specific hash field.
1133
+
1134
+ **Parameters**:
1135
+
1136
+ - `field` (string): The hash field to get the value for.
1137
+
1138
+ **Returns**:
1139
+
1140
+ - `string | undefined`: The value of the hash field.
1141
+
1142
+ **Example**:
1143
+
1144
+ ```Typescript
1145
+ const value = hashService.get('key');
1146
+ ```
1147
+
1148
+ #### `clear(field?: string): void`
1149
+
1150
+ Clears a specific hash field or all hash fields and updates the URL.
1151
+
1152
+ **Parameters**:
1153
+
1154
+ - `field` (string | undefined): The hash field to clear. If not provided, clears all hash fields.
1155
+
1156
+ **Example**:
1157
+
1158
+ ```Typescript
1159
+ hashService.clear('key');
1160
+ hashService.clear();
1161
+ ```
1162
+
1163
+ ## [Meta Service](#meta-service)
1164
+
1165
+ The `MetaService` manages meta tags and titles in an Angular application. It allows setting defaults, updating meta tags, and configuring titles dynamically.
1166
+
1167
+ ### Methods
1168
+
1169
+ #### `setDefaults(defaults: { [key: string]: string }): void`
1170
+
1171
+ Sets the default meta tags.
1172
+
1173
+ **Parameters**:
1174
+
1175
+ - `defaults` (object): The default meta tags.
1176
+
1177
+ **Example**:
1178
+
1179
+ ```Typescript
1180
+ metaService.setDefaults({ title: 'Default Title', description: 'Default Description' });
1181
+ ```
1182
+
1183
+ #### `setTitle(title?: string, titleSuffix?: string): MetaService`
1184
+
1185
+ Sets the title and optional title suffix.
1186
+
1187
+ **Parameters**:
1188
+
1189
+ - `title` (string): The title to set.
1190
+ - `titleSuffix` (string): The title suffix to append.
1191
+
1192
+ **Returns**:
1193
+
1194
+ - `MetaService`: The MetaService instance.
1195
+
1196
+ **Example**:
1197
+
1198
+ ```Typescript
1199
+ metaService.setTitle('My Page Title', ' | My Website');
1200
+ ```
1201
+
1202
+ #### `setLink(links: { [key: string]: string }): MetaService`
1203
+
1204
+ Sets link tags.
1205
+
1206
+ **Parameters**:
1207
+
1208
+ - `links` (object): The links to set.
1209
+
1210
+ **Returns**:
1211
+
1212
+ - `MetaService`: The MetaService instance.
1213
+
1214
+ **Example**:
1215
+
1216
+ ```Typescript
1217
+ metaService.setLink({ canonical: 'https://example.com', stylesheet: 'https://example.com/style.css' });
1218
+ ```
1219
+
1220
+ #### `setTag(tag: string, value: string, prop?: string): MetaService`
1221
+
1222
+ Sets a meta tag.
1223
+
1224
+ **Parameters**:
1225
+
1226
+ - `tag` (string): The meta tag name.
1227
+ - `value` (string): The meta tag value.
1228
+ - `prop` (string): The meta tag property.
1229
+
1230
+ **Returns**:
1231
+
1232
+ - `MetaService`: The MetaService instance.
1233
+
1234
+ **Example**:
1235
+
1236
+ ```Typescript
1237
+ metaService.setTag('description', 'This is a description', 'name');
1238
+ ```
1239
+
1240
+ #### `removeTag(tag: string, prop?: string): void`
1241
+
1242
+ Removes a meta tag.
1243
+
1244
+ **Parameters**:
1245
+
1246
+ - `tag` (string): The meta tag name.
1247
+ - `prop` (string): The meta tag property.
1248
+
1249
+ **Example**:
1250
+
1251
+ ```Typescript
1252
+ metaService.removeTag('description', 'name');
1253
+ ```
1254
+
1255
+ ### Private Methods
1256
+
1257
+ #### `_updateMetaTag(tag: string, value: string, prop?: string): void`
1258
+
1259
+ Updates a meta tag.
1260
+
1261
+ **Parameters**:
1262
+
1263
+ - `tag` (string): The meta tag name.
1264
+ - `value` (string): The meta tag value.
1265
+ - `prop` (string): The meta tag property.
1266
+
1267
+ #### `_warnMissingGuard(): void`
1268
+
1269
+ Warns about missing meta guards in routes.
1270
+
1271
+ **Example**:
1272
+
1273
+ ```Typescript
1274
+ metaService._warnMissingGuard();
1275
+ ```
1276
+
1277
+ ## [UI Service](#ui-service)
1278
+
1279
+ The `UiService` manages various UI-related tasks in an Angular application, including CSS management, form validation, and generating sample data for UI components.
1280
+
1281
+ ### Methods
1282
+
1283
+ #### `form(id: string): any`
1284
+
1285
+ Manages form states.
1286
+
1287
+ **Parameters**:
1288
+
1289
+ - `id` (string): The form identifier.
1290
+
1291
+ **Returns**:
1292
+
1293
+ - `any`: The form state object.
1294
+
1295
+ **Example**:
1296
+
1297
+ ```Typescript
1298
+ const formState = uiService.form('contactForm');
1299
+ ```
1300
+
1301
+ #### `valid(value: any, kind = 'email', extra = 0): boolean`
1302
+
1303
+ Validates input values based on the specified type.
1304
+
1305
+ **Parameters**:
1306
+
1307
+ - `value` (any): The value to validate.
1308
+ - `kind` (string): The type of validation.
1309
+ - `extra` (number): Additional validation criteria.
1310
+
1311
+ **Returns**:
1312
+
1313
+ - `boolean`: True if the value is valid, false otherwise.
1314
+
1315
+ **Example**:
1316
+
1317
+ ```Typescript
1318
+ const isValidEmail = uiService.valid('test@example.com', 'email');
1319
+ ```
1320
+
1321
+ #### `level(value = ''): number`
1322
+
1323
+ Determines the strength of a password.
1324
+
1325
+ **Parameters**:
1326
+
1327
+ - `value` (string): The password to evaluate.
1328
+
1329
+ **Returns**:
1330
+
1331
+ - `number`: The strength level of the password.
1332
+
1333
+ **Example**:
1334
+
1335
+ ```Typescript
1336
+ const passwordLevel = uiService.level('Password123!');
1337
+ ```
1338
+
1339
+ #### `set(variables: { [key: string]: string }, opts: any = {}): void`
1340
+
1341
+ Sets multiple CSS variables.
1342
+
1343
+ **Parameters**:
1344
+
1345
+ - `variables` (object): The CSS variables to set.
1346
+ - `opts` (any): Options for setting the variables.
1347
+
1348
+ **Example**:
1349
+
1350
+ ```Typescript
1351
+ uiService.set({ '--primary-color': '#ff0000' }, 'local');
1352
+ ```
1353
+
1354
+ #### `get(): { [key: string]: string }`
1355
+
1356
+ Retrieves the stored CSS variables.
1357
+
1358
+ **Returns**:
1359
+
1360
+ - `object`: The stored CSS variables.
1361
+
1362
+ **Example**:
1363
+
1364
+ ```Typescript
1365
+ const cssVariables = uiService.get();
1366
+ ```
1367
+
1368
+ #### `remove(keys: string | string[]): void`
1369
+
1370
+ Removes specified CSS variables.
1371
+
1372
+ **Parameters**:
1373
+
1374
+ - `keys` (string | array): The keys of the CSS variables to remove.
1375
+
1376
+ **Example**:
1377
+
1378
+ ```Typescript
1379
+ uiService.remove('primary-color secondary-color');
1380
+ ```
1381
+
1382
+ #### `arr(arrLen = 10, type: string = 'number'): any[]`
1383
+
1384
+ Generates an array of sample data.
1385
+
1386
+ **Parameters**:
1387
+
1388
+ - `arrLen` (number): The length of the array.
1389
+ - `type` (string): The type of data to generate.
1390
+
1391
+ **Returns**:
1392
+
1393
+ - `array`: An array of sample data.
1394
+
1395
+ **Example**:
1396
+
1397
+ ```Typescript
1398
+ const sampleArray = uiService.arr(5, 'text');
1399
+ ```
1400
+
1401
+ #### `text(length = 10): string`
1402
+
1403
+ Generates a random text string.
1404
+
1405
+ **Parameters**:
1406
+
1407
+ - `length` (number): The length of the text string.
1408
+
1409
+ **Returns**:
1410
+
1411
+ - `string`: A random text string.
1412
+
1413
+ **Example**:
1414
+
1415
+ ```Typescript
1416
+ const randomText = uiService.text(15);
1417
+ ```
1418
+
1419
+ ## [Crud Service](#crud-service)
1420
+
1421
+ The `CrudService` is designed to manage CRUD (Create, Read, Update, Delete) operations in an Angular application. It interacts with an API, stores documents locally, and provides methods for handling various CRUD operations. It should be extended by specific services that manage different document types.
1422
+
1423
+ ### Methods
1424
+
1425
+ ---
1426
+
1427
+ #### `new(doc: Document = {}): Document`
1428
+
1429
+ Creates a new document with a temporary ID and status flags.
1430
+
1431
+ **Parameters**:
1432
+
1433
+ - `doc` (Document, optional): A base document to initialize.
1434
+
1435
+ **Returns**:
1436
+
1437
+ - `Document`: A new document instance with default properties.
1438
+
1439
+ **Example**:
1440
+ const newDoc = workService.new();
1441
+
1442
+ ---
1443
+
1444
+ #### `doc(_id: string): Document`
1445
+
1446
+ Retrieves a document by its ID. If the document doesn't exist, a new one is created.
1447
+
1448
+ **Parameters**:
1449
+
1450
+ - `_id` (string): The document ID.
1451
+
1452
+ **Returns**:
1453
+
1454
+ - `Document`: The document instance.
1455
+
1456
+ **Example**:
1457
+ const doc = workService.doc('12345');
1458
+
1459
+ ---
1460
+
1461
+ #### `addDoc(doc: Document): void`
1462
+
1463
+ Adds a new document or updates an existing document in the local store. It will attempt to update the document if it already exists in the collection.
1464
+
1465
+ **Parameters**:
1466
+
1467
+ - `doc` (Document): The document to add or update.
1468
+
1469
+ **Example**:
1470
+ workService.addDoc(doc);
1471
+
1472
+ ---
1473
+
1474
+ #### `addDocs(docs: Document[]): void`
1475
+
1476
+ Adds multiple documents to the service. Each document will either be added or updated depending on whether it already exists.
1477
+
1478
+ **Parameters**:
1479
+
1480
+ - `docs` (Document[]): The array of documents to add.
1481
+
1482
+ **Example**:
1483
+ workService.addDocs(docs);
1484
+
1485
+ ---
1486
+
1487
+ #### `setDocs(): void`
1488
+
1489
+ Saves the current state of documents to local storage.
1490
+
1491
+ **Example**:
1492
+ workService.setDocs();
1493
+
1494
+ ---
1495
+
1496
+ #### `getDocs(): Document[]`
1497
+
1498
+ Retrieves the current list of documents stored locally.
1499
+
1500
+ **Returns**:
1501
+
1502
+ - `Document[]`: The list of documents.
1503
+
1504
+ **Example**:
1505
+ const docs = workService.getDocs();
1506
+
1507
+ ---
1508
+
1509
+ #### `setPerPage(_perPage: number): void`
1510
+
1511
+ Sets the number of documents to display per page for pagination.
1512
+
1513
+ **Parameters**:
1514
+
1515
+ - `_perPage` (number): The number of documents per page.
1516
+
1517
+ **Example**:
1518
+ workService.setPerPage(10);
1519
+
1520
+ ---
1521
+
1522
+ #### `get(config: GetConfig = {}, options: CrudOptions<Document> = {}): Observable<Document[]>`
1523
+
1524
+ Fetches a list of documents from the API with optional pagination and other settings.
1525
+
1526
+ **Parameters**:
1527
+
1528
+ - `config` (object, optional): The configuration for pagination (`page` and `perPage`).
1529
+ - `options` (CrudOptions<Document>, optional): Options for callbacks and error handling.
1530
+
1531
+ **Returns**:
1532
+
1533
+ - `Observable<Document[]>`: An observable of the retrieved documents.
1534
+
1535
+ **Example**:
1536
+ workService.get({ page: 1 }, { callback: (docs) => console.log(docs) });
1537
+
1538
+ ---
1539
+
1540
+ #### `create(doc: Document, options: CrudOptions<Document> = {}): Observable<Document> | void`
1541
+
1542
+ Creates a new document in the API and adds it to the local store. The document is only created once.
1543
+
1544
+ **Parameters**:
1545
+
1546
+ - `doc` (Document): The document to create.
1547
+ - `options` (CrudOptions<Document>, optional): Options for callbacks and error handling.
1548
+
1549
+ **Returns**:
1550
+
1551
+ - `Observable<Document> | void`: An observable of the created document or void if the document was already created.
1552
+
1553
+ **Example**:
1554
+ workService.create(newDoc, { callback: (doc) => console.log(doc) });
1555
+
1556
+ ---
1557
+
1558
+ #### `fetch(query: object = {}, options: CrudOptions<Document> = {}): Observable<Document>`
1559
+
1560
+ Fetches a document from the API based on a query object and adds it to the local store.
1561
+
1562
+ **Parameters**:
1563
+
1564
+ - `query` (object, optional): The query object to filter the documents.
1565
+ - `options` (CrudOptions<Document>, optional): Options for callbacks and error handling.
1566
+
1567
+ **Returns**:
1568
+
1569
+ - `Observable<Document>`: An observable of the fetched document.
1570
+
1571
+ **Example**:
1572
+ workService.fetch({ name: 'example' }, { callback: (doc) => console.log(doc) });
1573
+
1574
+ ---
1575
+
1576
+ #### `updateAfterWhile(doc: Document, options: CrudOptions<Document> = {}): void`
1577
+
1578
+ Updates a document after a specified delay using a core service function to handle the delay.
1579
+
1580
+ **Parameters**:
1581
+
1582
+ - `doc` (Document): The document to update.
1583
+ - `options` (CrudOptions<Document>, optional): Options for callbacks and error handling.
1584
+
1585
+ **Example**:
1586
+ workService.updateAfterWhile(doc, { callback: (doc) => console.log(doc) });
1587
+
1588
+ ---
1589
+
1590
+ #### `update(doc: Document, options: CrudOptions<Document> = {}): Observable<Document>`
1591
+
1592
+ Updates a document in the API and reflects the changes locally.
1593
+
1594
+ **Parameters**:
1595
+
1596
+ - `doc` (Document): The document to update.
1597
+ - `options` (CrudOptions<Document>, optional): Options for callbacks and error handling.
1598
+
1599
+ **Returns**:
1600
+
1601
+ - `Observable<Document>`: An observable of the updated document.
1602
+
1603
+ **Example**:
1604
+ workService.update(doc, { callback: (doc) => console.log(doc) });
1605
+
1606
+ ---
1607
+
1608
+ #### `unique(doc: Document, options: CrudOptions<Document> = {}): Observable<Document>`
1609
+
1610
+ Unique update a document field in the API.
1611
+
1612
+ **Parameters**:
1613
+
1614
+ - `doc` (Document): The document to update.
1615
+ - `options` (CrudOptions<Document>, optional): Optional callback and error handling configuration.
1616
+
1617
+ **Returns**:
1618
+
1619
+ - `Observable<Document>`: An observable that resolves with the updated document.
1620
+
1621
+ **Example**:
1622
+ workService.unique(doc, { callback: (doc) => console.log('Document updated', doc) });
1623
+
1624
+ ---
1625
+
1626
+ #### `delete(doc: Document, options: CrudOptions<Document> = {}): Observable<Document>`
1627
+
1628
+ Deletes a document from the API and updates the local store.
1629
+
1630
+ **Parameters**:
1631
+
1632
+ - `doc` (Document): The document to delete.
1633
+ - `options` (CrudOptions<Document>, optional): Options for callbacks and error handling.
1634
+
1635
+ **Returns**:
1636
+
1637
+ - `Observable<Document>`: An observable of the deleted document.
1638
+
1639
+ **Example**:
1640
+ workService.delete(doc, { callback: (doc) => console.log(doc) });
1641
+
1642
+ ---
1643
+
1644
+ ### Interfaces
1645
+
1646
+ ---
1647
+
1648
+ #### `CrudDocument`
1649
+
1650
+ Represents a CRUD document.
1651
+
1652
+ **Properties**:
1653
+
1654
+ - `_id` (string): The document ID.
1655
+ - `__created` (boolean): Indicates if the document is created.
1656
+ - `__modified` (boolean): Indicates if the document is modified.
1657
+
1658
+ **Example**:
1659
+ interface CrudDocument {
1660
+ \_id: string;
1661
+ **created: boolean;
1662
+ **modified: boolean;
1663
+ }
1664
+
1665
+ ---
1666
+
1667
+ ### Code sample use
1668
+
1669
+ ```typescript
1670
+ import { Injectable } from "@angular/core";
1671
+ import { AlertService, CoreService, HttpService, StoreService, CrudService, CrudDocument } from "wacom";
1672
+
1673
+ export interface Work extends CrudDocument {
1674
+ name: string;
1675
+ description: string;
1676
+ }
1677
+
1678
+ @Injectable({
1679
+ providedIn: "root",
1680
+ })
1681
+ export class WorkService extends CrudService<Work> {
1682
+ works: Work[] = this.getDocs();
1683
+
1684
+ constructor(_http: HttpService, _store: StoreService, _alert: AlertService, _core: CoreService) {
1685
+ super(
1686
+ {
1687
+ name: "work",
1688
+ },
1689
+ _http,
1690
+ _store,
1691
+ _alert,
1692
+ _core
1693
+ );
1694
+
1695
+ this.get();
1696
+ }
1697
+ }
1698
+ ```
1699
+
1700
+ ## [File Service](#file-service)
1701
+
1702
+ The `FileService` is designed to handle file uploads, image processing, and file management tasks in an Angular application. It interacts with the `HttpService` to send files to the server and provides utilities for image resizing and validation.
1703
+
1704
+ ### Methods
1705
+
1706
+ #### `add(opts: FileOptions | string): void | (() => void)`
1707
+
1708
+ Adds a file input configuration.
1709
+ **Parameters**:
1710
+
1711
+ - `opts` (FileOptions | string): The file options or a string ID.
1712
+
1713
+ **Example**:
1714
+
1715
+ ```Typescript
1716
+ fs.add({
1717
+ id: 'fileInput',
1718
+ type: 'image',
1719
+ resize: 200,
1720
+ cb: (dataUrl, file) => {
1721
+ console.log('File processed:', dataUrl, file);
1722
+ },
1723
+ save: true,
1724
+ });
1725
+ ```
1726
+
1727
+ #### `change(event: Event, info: FileOptions): void`
1728
+
1729
+ Handles file input change event.
1730
+ **Parameters**:
1731
+
1732
+ - `event` (Event): The input change event.
1733
+ - `info` (FileOptions): The file options.
1734
+
1735
+ **Example**:
1736
+
1737
+ ```Typescript
1738
+ <input type="file" (change)="fs.change($event, fileOptions)">
1739
+ ```
1740
+
1741
+ #### `remove(part: string, url: string, opts: any = {}, cb: (resp: any) => void = () => {}): void | (() => void)`
1742
+
1743
+ Removes a file.
1744
+
1745
+ **Parameters**:
1746
+
1747
+ - `part` (string): The part of the API.
1748
+ - `url` (string): The URL of the file.
1749
+ - `opts` (object): Additional options.
1750
+ - `cb` (function): The callback function.
1751
+
1752
+ **Example**:
1753
+
1754
+ ```Typescript
1755
+ fs.remove('images', 'https://example.com/image.jpg', {}, (resp) => {
1756
+ console.log('File removed:', resp);
1757
+ });
1758
+ ```
1759
+
1760
+ #### `uploadFiles(info: FileOptions, files: File[], cb: (resp: any) => void = () => {}): void`
1761
+
1762
+ Uploads files to the server.
1763
+ **Parameters**:
1764
+
1765
+ - `info` (FileOptions): The file options.
1766
+ - `files` (File[]): The files to upload.
1767
+ - `cb` (function): The callback function.
1768
+
1769
+ **Example**:
1770
+
1771
+ ```Typescript
1772
+ const files = document.getElementById('fileInput').files;
1773
+ fs.uploadFiles(fileOptions, files, (resp) => {
1774
+ console.log('Files uploaded:', resp);
1775
+ });
1776
+ ```
1777
+
1778
+ #### `image(info: FileOptions, cb: (resp: any) => void = () => {}): void | (() => void)`
1779
+
1780
+ Uploads an image to the server.
1781
+ **Parameters**:
1782
+
1783
+ - `info` (FileOptions): The file options.
1784
+ - `cb` (function): The callback function.
1785
+
1786
+ **Example**:
1787
+
1788
+ ```Typescript
1789
+ fs.image({
1790
+ api: '/api/upload',
1791
+ part: 'images',
1792
+ name: 'profilePic',
1793
+ }, (resp) => {
1794
+ console.log('Image uploaded:', resp);
1795
+ });
1796
+ ```
1797
+
1798
+ ### Interfaces
1799
+
1800
+ #### `FileOptions`
1801
+
1802
+ Represents the file options for uploading and processing files.
1803
+ **Properties**:
1804
+
1805
+ - `id` (string): The unique ID for the file input.
1806
+ - `type` (string): The type of file ('image' or 'file').
1807
+ - `resize` (number | object): The dimensions for resizing the image.
1808
+ - `multiple` (boolean): Indicates if multiple files can be uploaded.
1809
+ - `multiple_cb` (function): Callback function for multiple files.
1810
+ - `cb` (function): Callback function for file processing.
1811
+ - `save` (boolean): Indicates if the file should be saved.
1812
+ - `complete` (function): Function to call when the file is saved.
1813
+ - `api` (string): The API endpoint for uploading the file.
1814
+ - `part` (string): The part of the API.
1815
+ - `name` (string): The name of the file.
1816
+ - `body` (function | object): Function or object to generate the request body.
1817
+ - `resp` (function): Function to handle the response.
1818
+ - `append` (function): Function to append files to FormData.
1819
+ - `multiple_files` (array): Array of multiple files.
1820
+ - `multiple_counter` (number): Counter for multiple files.
1821
+ - `url` (string): The URL for the file.
1822
+
1823
+ **Example**:
1824
+
1825
+ ```Typescript
1826
+ const fileOptions: FileOptions = {
1827
+ id: 'fileInput',
1828
+ type: 'image',
1829
+ resize: { width: 200, height: 200 },
1830
+ multiple: true,
1831
+ multiple_cb: (files) => {
1832
+ console.log('Multiple files processed:', files);
1833
+ },
1834
+ cb: (dataUrl, file) => {
1835
+ console.log('File processed:', dataUrl, file);
1836
+ },
1837
+ save: true,
1838
+ api: '/api/upload',
1839
+ part: 'images',
1840
+ name: 'profilePic',
1841
+ body: () => ({ userId: 123 }),
1842
+ resp: (response) => {
1843
+ console.log('Server response:', response);
1844
+ },
1845
+ };
1846
+ ```
1847
+
1848
+ ## [Socket Service](#socket-service)
1849
+
1850
+ The `SocketService` manages WebSocket connections using `socket.io`. It handles setting up the connection, listening for events, and emitting messages.
1851
+
1852
+ ### Methods
1853
+
1854
+ #### `setUrl(url: string): void`
1855
+
1856
+ Sets the URL for the WebSocket connection and reloads the socket.
1857
+ **Parameters**:
1858
+
1859
+ - `url` (string): The URL of the WebSocket server.
1860
+
1861
+ **Example**:
1862
+
1863
+ ```Typescript
1864
+ socketService.setUrl('https://example.com');
1865
+ ```
1866
+
1867
+ #### `on(to: string, cb: (message: any) => void = () => {}): void`
1868
+
1869
+ Subscribes to a WebSocket event.
1870
+
1871
+ **Parameters**:
1872
+
1873
+ - `to` (string): The event to subscribe to.
1874
+ - `cb` (function): The callback function to execute when the event is received.
1875
+
1876
+ **Example**:
1877
+
1878
+ ```Typescript
1879
+ socketService.on('message', (msg) => {
1880
+ console.log('Received message:', msg);
1881
+ });
1882
+ ```
1883
+
1884
+ #### `emit(to: string, message: any, room: any = false): void`
1885
+
1886
+ Emits a message to a WebSocket event.
1887
+
1888
+ **Parameters**:
1889
+
1890
+ - `to` (string): The event to emit the message to.
1891
+ - `message` (any): The message to emit.
1892
+ - `room` (any): Optional room to emit the message to.
1893
+
1894
+ **Example**:
1895
+
1896
+ ```Typescript
1897
+ socketService.emit('message', { text: 'Hello, World!' });
1898
+ ```
1899
+
1900
+ ### Usage Example
1901
+
1902
+ ```typescript
1903
+ import { SocketService } from "wacom";
1904
+
1905
+ @Component({
1906
+ selector: "app-root",
1907
+ templateUrl: "./app.component.html",
1908
+ styleUrls: ["./app.component.css"],
1909
+ })
1910
+ export class AppComponent {
1911
+ constructor(private socketService: SocketService) {
1912
+ this.socketService.setUrl("https://example.com");
1913
+ this.socketService.on("connect", () => {
1914
+ console.log("Connected to WebSocket");
1915
+ });
1916
+ this.socketService.on("message", (msg) => {
1917
+ console.log("Received message:", msg);
1918
+ });
1919
+ }
1920
+
1921
+ sendMessage() {
1922
+ this.socketService.emit("message", { text: "Hello, World!" });
1923
+ }
1924
+ }
1925
+ ```
1926
+
1927
+ ## [Time Service](#time-service)
1928
+
1929
+ The `TimeService` provides comprehensive date and time management, including timezone handling, formatting dates, and utility functions for calendar operations.
1930
+
1931
+ ### Methods
1932
+
1933
+ #### `getDayName(date: Date, format: 'short' | 'long' = 'long'): string`
1934
+
1935
+ Returns the name of the day of the week for a given date.
1936
+
1937
+ **Parameters**:
1938
+
1939
+ - `date` (Date): The date for which to get the day of the week.
1940
+ - `format` ('short' | 'long'): The format in which to return the day name. Default is 'long'.
1941
+
1942
+ **Returns**:
1943
+
1944
+ - The name of the day of the week.
1945
+
1946
+ **Example**:
1947
+
1948
+ ```Typescript
1949
+ const dayName = timeService.getDayName(new Date(), 'short');
1950
+ console.log(dayName); // Output: 'Mon'
1951
+ ```
1952
+
1953
+ #### `formatDate(date: Date, format: string = 'mediumDate', timezone: string = 'UTC'): string`
1954
+
1955
+ Formats a date according to the specified format and timezone.
1956
+
1957
+ **Parameters**:
1958
+
1959
+ - `date` (Date): The date to format.
1960
+ - `format` (string): The format string (see Angular DatePipe documentation for format options).
1961
+ - `timezone` (string): The timezone to use for formatting.
1962
+
1963
+ **Returns**:
1964
+
1965
+ - The formatted date string.
1966
+
1967
+ **Example**:
1968
+
1969
+ ```Typescript
1970
+ const formattedDate = timeService.formatDate(new Date(), 'fullDate', 'America/New_York');
1971
+ console.log(formattedDate); // Output: 'Monday, January 1, 2023'
1972
+ ```
1973
+
1974
+ #### `convertToTimezone(date: Date, timezone: string): Date`
1975
+
1976
+ Converts a date to a different timezone.
1977
+ **Parameters**:
1978
+
1979
+ - `date` (Date): The date to convert.
1980
+ - `timezone` (string): The timezone to convert to.
1981
+
1982
+ **Returns**:
1983
+
1984
+ - The date in the new timezone.
1985
+
1986
+ **Example**:
1987
+
1988
+ ```Typescript
1989
+ const dateInTimezone = timeService.convertToTimezone(new Date(), 'Asia/Tokyo');
1990
+ console.log(dateInTimezone);
1991
+ ```
1992
+
1993
+ #### `startOfDay(date: Date): Date`
1994
+
1995
+ Returns the start of the day for a given date.
1996
+ **Parameters**:
1997
+
1998
+ - `date` (Date): The date for which to get the start of the day.
1999
+
2000
+ **Returns**:
2001
+
2002
+ - The start of the day (midnight) for the given date.
2003
+
2004
+ **Example**:
2005
+
2006
+ ```Typescript
2007
+ const startOfDay = timeService.startOfDay(new Date());
2008
+ console.log(startOfDay); // Output: '2023-01-01T00:00:00.000Z'
2009
+ ```
2010
+
2011
+ #### `endOfDay(date: Date): Date`
2012
+
2013
+ Returns the end of the day for a given date.
2014
+ **Parameters**:
2015
+
2016
+ - `date` (Date): The date for which to get the end of the day.
2017
+
2018
+ **Returns**:
2019
+
2020
+ - The end of the day (one millisecond before midnight) for the given date.
2021
+
2022
+ **Example**:
2023
+
2024
+ ```Typescript
2025
+ const endOfDay = timeService.endOfDay(new Date());
2026
+ console.log(endOfDay); // Output: '2023-01-01T23:59:59.999Z'
2027
+ ```
2028
+
2029
+ #### `getDaysInMonth(month: number, year: number): number`
2030
+
2031
+ Returns the number of days in a given month and year.
2032
+ **Parameters**:
2033
+
2034
+ - `month` (number): The month (0-11).
2035
+ - `year` (number): The year.
2036
+
2037
+ **Returns**:
2038
+
2039
+ - The number of days in the month.
2040
+
2041
+ **Example**:
2042
+
2043
+ ```Typescript
2044
+ const daysInMonth = timeService.getDaysInMonth(1, 2023);
2045
+ console.log(daysInMonth); // Output: 28
2046
+ ```
2047
+
2048
+ #### `isLeapYear(year: number): boolean`
2049
+
2050
+ Checks if a given year is a leap year.
2051
+ **Parameters**:
2052
+
2053
+ - `year` (number): The year to check.
2054
+
2055
+ **Returns**:
2056
+
2057
+ - True if the year is a leap year, false otherwise.
2058
+
2059
+ **Example**:
2060
+
2061
+ ```Typescript
2062
+ const isLeap = timeService.isLeapYear(2024);
2063
+ console.log(isLeap); // Output: true
2064
+ ```
2065
+
2066
+ #### `addDays(date: Date, days: number): Date`
2067
+
2068
+ Adds a specified number of days to a date.
2069
+ **Parameters**:
2070
+
2071
+ - `date` (Date): The date to which to add days.
2072
+ - `days` (number): The number of days to add.
2073
+
2074
+ **Returns**:
2075
+
2076
+ - The new date with the added days.
2077
+
2078
+ **Example**:
2079
+
2080
+ ```Typescript
2081
+ const newDate = timeService.addDays(new Date(), 10);
2082
+ console.log(newDate);
2083
+ ```
2084
+
2085
+ #### `addMonths(date: Date, months: number): Date`
2086
+
2087
+ Adds a specified number of months to a date.
2088
+ **Parameters**:
2089
+
2090
+ - `date` (Date): The date to which to add months.
2091
+ - `months` (number): The number of months to add.
2092
+
2093
+ **Returns**:
2094
+
2095
+ - The new date with the added months.
2096
+
2097
+ **Example**:
2098
+
2099
+ ```Typescript
2100
+ const newDate = timeService.addMonths(new Date(), 2);
2101
+ console.log(newDate);
2102
+ ```
2103
+
2104
+ #### `addYears(date: Date, years: number): Date`
2105
+
2106
+ Adds a specified number of years to a date.
2107
+ **Parameters**:
2108
+
2109
+ - `date` (Date): The date to which to add years.
2110
+ - `years` (number): The number of years to add.
2111
+
2112
+ **Returns**:
2113
+
2114
+ - The new date with the added years.
2115
+
2116
+ **Example**:
2117
+
2118
+ ```Typescript
2119
+ const newDate = timeService.addYears(new Date(), 5);
2120
+ console.log(newDate);
2121
+ ```
2122
+
2123
+ #### `subtractDays(date: Date, days: number): Date`
2124
+
2125
+ Subtracts a specified number of days from a date.
2126
+ **Parameters**:
2127
+
2128
+ - `date` (Date): The date from which to subtract days.
2129
+ - `days` (number): The number of days to subtract.
2130
+
2131
+ **Returns**:
2132
+
2133
+ - The new date with the subtracted days.
2134
+
2135
+ **Example**:
2136
+
2137
+ ```Typescript
2138
+ const newDate = timeService.subtractDays(new Date(), 5);
2139
+ console.log(newDate);
2140
+ ```
2141
+
2142
+ #### `subtractMonths(date: Date, months: number): Date`
2143
+
2144
+ Subtracts a specified number of months from a date.
2145
+ **Parameters**:
2146
+
2147
+ - `date` (Date): The date from which to subtract months.
2148
+ - `months` (number): The number of months to subtract.
2149
+
2150
+ **Returns**:
2151
+
2152
+ - The new date with the subtracted months.
2153
+
2154
+ **Example**:
2155
+
2156
+ ```Typescript
2157
+ const newDate = timeService.subtractMonths(new Date(), 3);
2158
+ console.log(newDate);
2159
+ ```
2160
+
2161
+ #### `subtractYears(date: Date, years: number): Date`
2162
+
2163
+ Subtracts a specified number of years from a date.
2164
+ **Parameters**:
2165
+
2166
+ - `date` (Date): The date from which to subtract years.
2167
+ - `years` (number): The number of years to subtract.
2168
+
2169
+ **Returns**:
2170
+
2171
+ - The new date with the subtracted years.
2172
+
2173
+ **Example**:
2174
+
2175
+ ```Typescript
2176
+ const newDate = timeService.subtractYears(new Date(), 2);
2177
+ console.log(newDate);
2178
+ ```
2179
+
2180
+ #### `isSameDay(date1: Date, date2: Date): boolean`
2181
+
2182
+ Checks if two dates are on the same day.
2183
+ **Parameters**:
2184
+
2185
+ - `date1` (Date): The first date.
2186
+ - `date2` (Date): The second date.
2187
+
2188
+ **Returns**:
2189
+
2190
+ - True if the dates are on the same day, false otherwise.
2191
+
2192
+ **Example**:
2193
+
2194
+ ```Typescript
2195
+ const sameDay = timeService.isSameDay(new Date(), new Date());
2196
+ console.log(sameDay); // Output: true
2197
+ ```
2198
+
2199
+ #### `getWeekNumber(date: Date): number`
2200
+
2201
+ Returns the ISO week number for a given date.
2202
+
2203
+ **Parameters**:
2204
+
2205
+ - `date` (Date): The date for which to get the week number.
2206
+
2207
+ **Returns**:
2208
+
2209
+ - The ISO week number (1-53).
2210
+
2211
+ **Example**:
2212
+
2213
+ ```Typescript
2214
+ const weekNumber = timeService.getWeekNumber(new Date());
2215
+ console.log(weekNumber); // Output: 35 (example)
2216
+ ```
2217
+
2218
+ #### `getWeeksInMonth(month: number, year: number): number`
2219
+
2220
+ Returns the number of weeks in a month for a given month and year.
2221
+
2222
+ **Parameters**:
2223
+
2224
+ - `month` (number): The month (0-11).
2225
+ - `year` (number): The year.
2226
+
2227
+ **Returns**:
2228
+
2229
+ - The number of weeks in the month.
2230
+
2231
+ **Example**:
2232
+
2233
+ ```Typescript
2234
+ const weeksInMonth = timeService.getWeeksInMonth(2, 2025);
2235
+ console.log(weeksInMonth); // Output: 6 (example for March 2025)
2236
+ ```
2237
+
2238
+ ## [Dom Service](#dom-service)
2239
+
2240
+ The `DomService` facilitates DOM manipulation and dynamic component loading in Angular applications.
2241
+
2242
+ ### Methods
2243
+
2244
+ #### `appendById(component: any, options: any = {}, id: string): { nativeElement: HTMLElement, componentRef: ComponentRef<any> }`
2245
+
2246
+ Appends a component to a specified element by ID.
2247
+
2248
+ **Parameters**:
2249
+
2250
+ - `component` (any): The component to append.
2251
+ - `options` (any): The options to project into the component.
2252
+ - `id` (string): The ID of the element to append the component to.
2253
+
2254
+ **Returns**:
2255
+
2256
+ - An object containing the native element and the component reference.
2257
+
2258
+ **Example**:
2259
+
2260
+ ```Typescript
2261
+ const result = domService.appendById(MyComponent, { inputProp: 'value' }, 'elementId');
2262
+ console.log(result.nativeElement); // Output: The native DOM element
2263
+ console.log(result.componentRef); // Output: The component reference
2264
+ ```
2265
+
2266
+ #### `appendComponent(component: any, options: any = {}, element: HTMLElement = this.core.document.body): { nativeElement: HTMLElement, componentRef: ComponentRef<any> }`
2267
+
2268
+ Appends a component to a specified element or to the body.
2269
+ **Parameters**:
2270
+
2271
+ - `component` (any): The component to append.
2272
+ - `options` (any): The options to project into the component.
2273
+ - `element` (HTMLElement): The element to append the component to. Defaults to body.
2274
+
2275
+ **Returns**:
2276
+
2277
+ - An object containing the native element and the component reference.
2278
+
2279
+ **Example**:
2280
+
2281
+ ```Typescript
2282
+ const result = domService.appendComponent(MyComponent, { inputProp: 'value' });
2283
+ console.log(result.nativeElement); // Output: The native DOM element
2284
+ console.log(result.componentRef); // Output: The component reference
2285
+ ```
2286
+
2287
+ #### `getComponentRef(component: any, options: any = {}): ComponentRef<any>`
2288
+
2289
+ Gets a reference to a dynamically created component.
2290
+ **Parameters**:
2291
+
2292
+ - `component` (any): The component to create.
2293
+ - `options` (any): The options to project into the component.
2294
+
2295
+ **Returns**:
2296
+
2297
+ - The component reference.
2298
+
2299
+ **Example**:
2300
+
2301
+ ```Typescript
2302
+ const componentRef = domService.getComponentRef(MyComponent, { inputProp: 'value' });
2303
+ console.log(componentRef); // Output: The component reference
2304
+ ```
2305
+
2306
+ #### `projectComponentInputs(component: ComponentRef<any>, options: any): ComponentRef<any>`
2307
+
2308
+ Projects the inputs onto the component.
2309
+ **Parameters**:
2310
+
2311
+ - `component` (ComponentRef<any>): The component reference.
2312
+ - `options` (any): The options to project into the component.
2313
+
2314
+ **Returns**:
2315
+
2316
+ - The component reference with the projected inputs.
2317
+
2318
+ **Example**:
2319
+
2320
+ ```Typescript
2321
+ const componentRef = domService.getComponentRef(MyComponent);
2322
+ domService.projectComponentInputs(componentRef, { inputProp: 'value' });
2323
+ console.log(componentRef.instance.inputProp); // Output: 'value'
2324
+ ```
2325
+
2326
+ ### Usage Example
2327
+
2328
+ ```Typescript
2329
+ import { DomService } from './dom.service';
2330
+
2331
+ @Component({
2332
+ selector: 'app-root',
2333
+ templateUrl: './app.component.html',
2334
+ styleUrls: ['./app.component.css']
2335
+ })
2336
+ export class AppComponent {
2337
+ constructor(private domService: DomService) {}
2338
+
2339
+ addComponent() {
2340
+ const result = this.domService.appendById(MyComponent, { inputProp: 'value' }, 'elementId');
2341
+ console.log(result.nativeElement); // Output: The native DOM element
2342
+ console.log(result.componentRef); // Output: The component reference
2343
+ }
2344
+ }
2345
+ ```