|
1 | 1 | import type { |
2 | 2 | INotificationProvider, |
3 | | - TNotificationItem, |
4 | 3 | TNotificationOption, |
5 | 4 | TNotificationPosition, |
6 | 5 | TNotificationVariant, |
7 | 6 | } from '@/components/Notification/types'; |
8 | 7 | import Helper from '@/utils/Helper'; |
9 | | -import { reactive } from 'vue'; |
| 8 | +import { type Reactive, reactive } from 'vue'; |
10 | 9 |
|
11 | 10 | export default class NotificationProvider implements INotificationProvider { |
12 | | - private readonly _item: TNotificationItem; |
13 | | - private readonly _positions: TNotificationPosition[] = [ |
14 | | - 'top-right', |
15 | | - 'top-left', |
16 | | - 'top-center', |
17 | | - 'top-full-width', |
18 | | - 'bottom-right', |
19 | | - 'bottom-left', |
20 | | - 'bottom-center', |
21 | | - 'bottom-full-width', |
22 | | - ]; |
| 11 | + private readonly _collection: Reactive<Map<TNotificationPosition, TNotificationOption[]>>; |
23 | 12 |
|
24 | 13 | constructor() { |
25 | | - const item = {} as TNotificationItem; |
26 | | - |
27 | | - for (let i = 0; i <= this._positions.length - 1; i++) { |
28 | | - item[this._positions[i]] = []; |
29 | | - } |
30 | | - |
31 | | - this._item = reactive(item); |
| 14 | + this._collection = reactive(new Map<TNotificationPosition, TNotificationOption[]>()); |
32 | 15 | } |
33 | 16 |
|
34 | | - get notification() { |
35 | | - return this._item; |
| 17 | + get collection(): Map<TNotificationPosition, TNotificationOption[]> { |
| 18 | + return this._collection; |
36 | 19 | } |
37 | 20 |
|
38 | 21 | add(data: string | TNotificationOption): TNotificationOption | null { |
39 | | - const option = this._createOption(data); |
40 | | - const position = option.position as TNotificationPosition; |
41 | | - |
42 | | - if (option.preventDuplicates) { |
43 | | - const keys = this.notification[position].keys(); |
44 | | - |
45 | | - for (const index of keys) { |
46 | | - if ( |
47 | | - this._item[position][index].title === option.title && |
48 | | - this._item[position][index].message === option.message |
49 | | - ) { |
50 | | - console.warn('Duplicate notification', option); |
51 | | - return null; |
| 22 | + const toastOption = this._createOption(data); |
| 23 | + const placement = toastOption.position as TNotificationPosition; |
| 24 | + const toastItems = this._collection.get(placement); |
| 25 | + |
| 26 | + if (!toastItems) { |
| 27 | + this._collection.set(placement, [toastOption]); |
| 28 | + } else { |
| 29 | + if (toastOption.preventDuplicates) { |
| 30 | + for (const toast of toastItems) { |
| 31 | + if ( |
| 32 | + toast.message === toastOption.message && |
| 33 | + toast.title === toastOption.title |
| 34 | + ) { |
| 35 | + console.warn('Duplicate notification', toastOption); |
| 36 | + return null; |
| 37 | + } |
52 | 38 | } |
53 | 39 | } |
| 40 | + |
| 41 | + toastItems.push(toastOption); |
| 42 | + this._collection.set(placement, toastItems); |
54 | 43 | } |
55 | | - this._item[position].push(option); |
56 | 44 |
|
57 | | - return option; |
| 45 | + return toastOption; |
58 | 46 | } |
59 | 47 |
|
60 | 48 | clearAll() { |
61 | | - for (let i = 0; i < this._positions.length; i++) { |
62 | | - this._item[this._positions[i]] = []; |
63 | | - } |
| 49 | + this._collection.clear(); |
64 | 50 | } |
65 | 51 |
|
66 | 52 | close(item: TNotificationOption) { |
67 | 53 | this.remove(item); |
68 | 54 | } |
69 | 55 |
|
| 56 | + private _deleteIfEmpty(placement: TNotificationPosition, data?: TNotificationOption[]): void { |
| 57 | + if (Helper.isEmpty(data)) { |
| 58 | + this._collection.delete(placement); |
| 59 | + } else { |
| 60 | + this._collection.set(placement, data ?? []); |
| 61 | + } |
| 62 | + } |
| 63 | + |
70 | 64 | remove(item: TNotificationOption) { |
71 | 65 | const oid = item.oid as string; |
72 | 66 | const position = item.position as TNotificationPosition; |
73 | | - this._item[position] = this.notification[position].filter((it) => it.oid !== oid); |
| 67 | + const toastItems = this._collection.get(position); |
| 68 | + const results = toastItems?.filter((it) => it.oid !== oid); |
| 69 | + |
| 70 | + this._deleteIfEmpty(position, results); |
74 | 71 | } |
75 | 72 |
|
76 | 73 | removeByType(variant: TNotificationVariant) { |
77 | | - for (let i = 0; i < this._positions.length; i++) { |
78 | | - const keys = this.notification[this._positions[i]].keys(); |
79 | | - |
80 | | - for (const index of keys) { |
81 | | - if (this.notification[this._positions[i]][index].variant === variant) { |
82 | | - this.remove(this.notification[this._positions[i]][index]); |
83 | | - } |
84 | | - } |
85 | | - } |
| 74 | + this._collection.forEach((values, placement) => { |
| 75 | + const results = values.filter((it) => it.variant !== variant); |
| 76 | + this._deleteIfEmpty(placement, results); |
| 77 | + }); |
86 | 78 | } |
87 | 79 |
|
88 | | - error(option: string | TNotificationOption, title?: string): TNotificationOption | null { |
89 | | - return this._doAdd(option, 'error', title); |
| 80 | + error(data: string | TNotificationOption, title?: string): TNotificationOption | null { |
| 81 | + return this._doAdd(data, 'error', title); |
90 | 82 | } |
91 | 83 |
|
92 | | - info(option: string | TNotificationOption, title?: string): TNotificationOption | null { |
93 | | - return this._doAdd(option, 'info', title); |
| 84 | + info(data: string | TNotificationOption, title?: string): TNotificationOption | null { |
| 85 | + return this._doAdd(data, 'info', title); |
94 | 86 | } |
95 | 87 |
|
96 | | - success(option: string | TNotificationOption, title?: string): TNotificationOption | null { |
97 | | - return this._doAdd(option, 'success', title); |
| 88 | + success(data: string | TNotificationOption, title?: string): TNotificationOption | null { |
| 89 | + return this._doAdd(data, 'success', title); |
98 | 90 | } |
99 | 91 |
|
100 | | - warning(option: string | TNotificationOption, title?: string): TNotificationOption | null { |
101 | | - return this._doAdd(option, 'warning', title); |
| 92 | + warning(data: string | TNotificationOption, title?: string): TNotificationOption | null { |
| 93 | + return this._doAdd(data, 'warning', title); |
102 | 94 | } |
103 | 95 |
|
104 | 96 | private _doAdd( |
105 | | - option: string | TNotificationOption, |
| 97 | + data: string | TNotificationOption, |
106 | 98 | variant: TNotificationVariant, |
107 | 99 | title?: string |
108 | 100 | ): TNotificationOption | null { |
109 | | - const data = this._createOption(option); |
110 | | - data.variant = variant; |
111 | | - data.title = title; |
| 101 | + const option = Helper.isObject(data) ? data : ({ message: data } as TNotificationOption); |
| 102 | + option.variant = variant; |
| 103 | + option.title = title || option.title; |
112 | 104 |
|
113 | | - return this.add(data); |
| 105 | + return this.add(option); |
114 | 106 | } |
115 | 107 |
|
116 | 108 | private _createOption(option: string | TNotificationOption): TNotificationOption { |
117 | 109 | const defaultOption = { |
118 | 110 | oid: Helper.uuid(true), |
119 | 111 | clickClose: false, |
120 | 112 | closeButton: true, |
121 | | - // closeOnHover: false, |
| 113 | + iconOff: false, |
122 | 114 | progressBar: false, |
123 | 115 | preventDuplicates: false, |
124 | 116 | position: 'bottom-right' as TNotificationPosition, |
125 | 117 | variant: 'default' as TNotificationVariant, |
126 | 118 | timeout: 6000, |
127 | | - }; |
| 119 | + } as TNotificationOption; |
128 | 120 |
|
129 | 121 | if (Helper.isObject(option) && !Helper.isEmpty(option.message)) { |
130 | 122 | return { |
|
0 commit comments