wacom 18.2.2 → 18.2.4

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