aprog 2.2.3 → 2.2.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -0
- package/index.js +4 -2
- package/package.json +1 -1
- package/src/classes/AbortError.js +11 -0
- package/src/classes/SmartWrapper.js +160 -139
- package/src/classes/StateWrapper.js +53 -53
- package/src/classes/Toast.js +158 -0
- package/src/constructs/ButtonConstructor.js +36 -35
- package/src/constructs/InputConstructor.js +43 -42
- package/src/constructs/SelectConstructor.js +36 -35
- package/src/general.js +1302 -1254
- package/src/helper.js +92 -0
- package/src/html.js +64 -64
package/README.md
CHANGED
|
@@ -182,6 +182,7 @@ onInput: (event) => {
|
|
|
182
182
|
|
|
183
183
|
- Додано безплатний чат із `Gemini`
|
|
184
184
|
- Додано методи збереження зашифрованих даних `saveItem` та `loadItem`
|
|
185
|
+
- Додвано `Wrapper` та `Toast`
|
|
185
186
|
- З часом будуть додаватися можливості створення додаткових інструментів
|
|
186
187
|
|
|
187
188
|
|
package/index.js
CHANGED
|
@@ -23,6 +23,8 @@ export {
|
|
|
23
23
|
} from './src/html.js';
|
|
24
24
|
export { prism } from "./src/prism.js";
|
|
25
25
|
export { uniquePage } from "./src/pages.js";
|
|
26
|
-
export { state, wrap } from "./src/helper.js";
|
|
27
|
-
export { default as
|
|
26
|
+
export { state, wrap, toast, abort, useTableDrag } from "./src/helper.js";
|
|
27
|
+
export { default as AbortError } from './src/classes/AbortError.js';
|
|
28
28
|
export { default as SmartWrapper } from './src/classes/SmartWrapper.js';
|
|
29
|
+
export { default as StateWrapper } from './src/classes/StateWrapper.js';
|
|
30
|
+
export { default as Toast } from './src/classes/Toast.js';
|
package/package.json
CHANGED
|
@@ -1,159 +1,180 @@
|
|
|
1
1
|
class SmartWrapper {
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
constructor(value) {
|
|
3
|
+
this._value = value;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Отримати значення по ключу
|
|
8
|
+
* @param {string|number} key
|
|
9
|
+
* @param {*} def
|
|
10
|
+
* @returns {SmartWrapper}
|
|
11
|
+
*/
|
|
12
|
+
get(key, def = null) {
|
|
13
|
+
if (this._value === null || this._value === undefined) {
|
|
14
|
+
return new SmartWrapper(def);
|
|
4
15
|
}
|
|
5
16
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
* @returns {SmartWrapper}
|
|
11
|
-
*/
|
|
12
|
-
get(key, def = null) {
|
|
13
|
-
if (this._value === null || this._value === undefined) {
|
|
14
|
-
return new SmartWrapper(def);
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
if (typeof this._value === 'object') {
|
|
18
|
-
if (Array.isArray(this._value)) {
|
|
19
|
-
return new SmartWrapper(this._value[key] ?? def);
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
if (Object.prototype.hasOwnProperty.call(this._value, key)) {
|
|
23
|
-
return new SmartWrapper(this._value[key]);
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
return new SmartWrapper(def);
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Отримати вкладене значення за шляхом 'a.b.c'
|
|
32
|
-
* @param {string} path
|
|
33
|
-
* @param {*} def
|
|
34
|
-
* @returns {SmartWrapper}
|
|
35
|
-
*/
|
|
36
|
-
path(path, def = null) {
|
|
37
|
-
const keys = path.split('.');
|
|
38
|
-
let current = new SmartWrapper(this._value);
|
|
39
|
-
for (const key of keys) {
|
|
40
|
-
current = current.get(key);
|
|
41
|
-
}
|
|
42
|
-
return current.isNull ? new SmartWrapper(def) : current;
|
|
43
|
-
}
|
|
17
|
+
if (typeof this._value === 'object') {
|
|
18
|
+
if (Array.isArray(this._value)) {
|
|
19
|
+
return new SmartWrapper(this._value[key] ?? def);
|
|
20
|
+
}
|
|
44
21
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
*/
|
|
50
|
-
has(key) {
|
|
51
|
-
if (this._value === null || this._value === undefined) return false;
|
|
52
|
-
if (Array.isArray(this._value)) return key in this._value;
|
|
53
|
-
if (typeof this._value === 'object') return Object.prototype.hasOwnProperty.call(this._value, key);
|
|
54
|
-
return false;
|
|
22
|
+
if (Object.prototype.hasOwnProperty.call(this._value, key)) {
|
|
23
|
+
const val = this._value[key];
|
|
24
|
+
return new SmartWrapper(val !== null && val !== undefined ? val : def);
|
|
25
|
+
}
|
|
55
26
|
}
|
|
56
27
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
28
|
+
return new SmartWrapper(def);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Отримати значення по ключу з value
|
|
33
|
+
* @param {string|number} key
|
|
34
|
+
* @param {*} def
|
|
35
|
+
* @returns {*}
|
|
36
|
+
*/
|
|
37
|
+
getValue(key, def = null) {
|
|
38
|
+
return this.get(key, def)._value;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Отримати вкладене значення за шляхом 'a.b.c' з value
|
|
43
|
+
* @param {string} path
|
|
44
|
+
* @param {*} def
|
|
45
|
+
* @returns {*}
|
|
46
|
+
*/
|
|
47
|
+
pathValue(path, def = null) {
|
|
48
|
+
return this.path(path, def)._value;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Отримати вкладене значення за шляхом 'a.b.c'
|
|
53
|
+
* @param {string} path
|
|
54
|
+
* @param {*} def
|
|
55
|
+
* @returns {SmartWrapper}
|
|
56
|
+
*/
|
|
57
|
+
path(path, def = null) {
|
|
58
|
+
const keys = path.split('.');
|
|
59
|
+
let current = new SmartWrapper(this._value);
|
|
60
|
+
for (const key of keys) {
|
|
61
|
+
current = current.get(key);
|
|
75
62
|
}
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
63
|
+
return current.isNull ? new SmartWrapper(def) : current;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Перевірити чи існує ключ
|
|
68
|
+
* @param {string|number} key
|
|
69
|
+
* @returns {boolean}
|
|
70
|
+
*/
|
|
71
|
+
has(key) {
|
|
72
|
+
if (this._value === null || this._value === undefined) return false;
|
|
73
|
+
if (Array.isArray(this._value)) return key in this._value;
|
|
74
|
+
if (typeof this._value === 'object') return Object.prototype.hasOwnProperty.call(this._value, key);
|
|
75
|
+
return false;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Повернути тільки вказані ключі
|
|
80
|
+
* @param {string[]} keys
|
|
81
|
+
* @returns {SmartWrapper}
|
|
82
|
+
*/
|
|
83
|
+
only(keys = []) {
|
|
84
|
+
if (typeof this._value !== 'object' || this._value === null) {
|
|
85
|
+
return new SmartWrapper({});
|
|
88
86
|
}
|
|
89
87
|
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
map(callback) {
|
|
96
|
-
if (Array.isArray(this._value)) {
|
|
97
|
-
return new SmartWrapper(this._value.map(callback));
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
if (typeof this._value === 'object' && this._value !== null) {
|
|
101
|
-
return new SmartWrapper(
|
|
102
|
-
Object.entries(this._value).map(([key, val]) => callback(val, key))
|
|
103
|
-
);
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
return new SmartWrapper([]);
|
|
88
|
+
const result = {};
|
|
89
|
+
for (const key of keys) {
|
|
90
|
+
if (Object.prototype.hasOwnProperty.call(this._value, key)) {
|
|
91
|
+
result[key] = this._value[key];
|
|
92
|
+
}
|
|
107
93
|
}
|
|
108
94
|
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
95
|
+
return new SmartWrapper(result);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Встановити нове значення по ключу
|
|
100
|
+
* @param {string|number} key
|
|
101
|
+
* @param {*} val
|
|
102
|
+
* @returns {SmartWrapper}
|
|
103
|
+
*/
|
|
104
|
+
set(key, val) {
|
|
105
|
+
if (typeof this._value === 'object' && this._value !== null) {
|
|
106
|
+
return new SmartWrapper({...this._value, [key]: val});
|
|
115
107
|
}
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
* @returns {Array}
|
|
128
|
-
*/
|
|
129
|
-
get toArray() {
|
|
130
|
-
if (Array.isArray(this._value)) return this._value;
|
|
131
|
-
if (typeof this._value === 'object' && this._value !== null) return Object.values(this._value);
|
|
132
|
-
return [];
|
|
108
|
+
return new SmartWrapper({[key]: val});
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Пройтись по значеннях як по масиву або об'єкту
|
|
113
|
+
* @param {function(*, string|number): *} callback
|
|
114
|
+
* @returns {SmartWrapper}
|
|
115
|
+
*/
|
|
116
|
+
map(callback) {
|
|
117
|
+
if (Array.isArray(this._value)) {
|
|
118
|
+
return new SmartWrapper(this._value.map(callback));
|
|
133
119
|
}
|
|
134
120
|
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
get toObject() {
|
|
140
|
-
if (typeof this._value === 'object' && this._value !== null) return this._value;
|
|
141
|
-
return {};
|
|
121
|
+
if (typeof this._value === 'object' && this._value !== null) {
|
|
122
|
+
return new SmartWrapper(
|
|
123
|
+
Object.entries(this._value).map(([key, val]) => callback(val, key))
|
|
124
|
+
);
|
|
142
125
|
}
|
|
143
126
|
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
127
|
+
return new SmartWrapper([]);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Отримати реальне значення
|
|
132
|
+
* @returns {*}
|
|
133
|
+
*/
|
|
134
|
+
get val() {
|
|
135
|
+
return this._value;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Чи є значення null або undefined
|
|
140
|
+
* @returns {boolean}
|
|
141
|
+
*/
|
|
142
|
+
get isNull() {
|
|
143
|
+
return this._value === null || this._value === undefined;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Привести до масиву (рекурсивно)
|
|
148
|
+
* @returns {Array}
|
|
149
|
+
*/
|
|
150
|
+
get toArray() {
|
|
151
|
+
if (Array.isArray(this._value)) return this._value;
|
|
152
|
+
if (typeof this._value === 'object' && this._value !== null) return Object.values(this._value);
|
|
153
|
+
return [];
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Привести до об'єкта (рекурсивно)
|
|
158
|
+
* @returns {Object}
|
|
159
|
+
*/
|
|
160
|
+
get toObject() {
|
|
161
|
+
if (typeof this._value === 'object' && this._value !== null) return this._value;
|
|
162
|
+
return {};
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Привести до рядка
|
|
167
|
+
* @returns {string}
|
|
168
|
+
*/
|
|
169
|
+
get toString() {
|
|
170
|
+
if (typeof this._value === 'string') return this._value;
|
|
171
|
+
if (typeof this._value === 'number' || typeof this._value === 'boolean') {
|
|
172
|
+
return String(this._value);
|
|
156
173
|
}
|
|
174
|
+
if (Array.isArray(this._value)) return JSON.stringify(this._value);
|
|
175
|
+
if (typeof this._value === 'object' && this._value !== null) return JSON.stringify(Object.values(this._value));
|
|
176
|
+
return '';
|
|
177
|
+
}
|
|
157
178
|
}
|
|
158
179
|
|
|
159
180
|
export default SmartWrapper;
|
|
@@ -1,57 +1,57 @@
|
|
|
1
1
|
class StateWrapper {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
2
|
+
#value;
|
|
3
|
+
|
|
4
|
+
constructor(initialValue) {
|
|
5
|
+
this.#value = initialValue;
|
|
6
|
+
|
|
7
|
+
const handler = {
|
|
8
|
+
get: (target, prop, receiver) => {
|
|
9
|
+
if (prop === 'val') return target.#value;
|
|
10
|
+
|
|
11
|
+
if (prop === 'set') {
|
|
12
|
+
return (newValue) => {
|
|
13
|
+
target.#value = typeof newValue === 'function' ? newValue(target.#value) : newValue;
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
if (prop === 'merge') {
|
|
18
|
+
return (patch) => {
|
|
19
|
+
if (typeof target.#value === 'object' && target.#value !== null) {
|
|
20
|
+
target.#value = {...target.#value, ...patch};
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
if (prop === 'toJSON') return () => target.#value;
|
|
26
|
+
if (prop === 'toString') return () => String(target.#value);
|
|
27
|
+
if (prop === 'valueOf') return () => target.#value;
|
|
28
|
+
|
|
29
|
+
if (
|
|
30
|
+
typeof target.#value === 'object'
|
|
31
|
+
&& target.#value !== null
|
|
32
|
+
&& prop in target.#value
|
|
33
|
+
) {
|
|
34
|
+
return target.#value[prop];
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return Reflect.get(target, prop, receiver);
|
|
38
|
+
},
|
|
39
|
+
|
|
40
|
+
set: (target, prop, newVal) => {
|
|
41
|
+
if (typeof target.#value === 'object' && target.#value !== null) {
|
|
42
|
+
target.#value[prop] = newVal;
|
|
43
|
+
return true;
|
|
44
|
+
}
|
|
45
|
+
return false;
|
|
46
|
+
},
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
return new Proxy(this, handler);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
valueOf() {
|
|
53
|
+
return this.#value;
|
|
54
|
+
}
|
|
55
55
|
}
|
|
56
56
|
|
|
57
57
|
export default StateWrapper;
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import {div, span} from "../general.js";
|
|
2
|
+
|
|
3
|
+
class Toast {
|
|
4
|
+
constructor({
|
|
5
|
+
position = "top-right", // top-left | top-right | bottom-left | bottom-right
|
|
6
|
+
gap = "10px",
|
|
7
|
+
offset = "20px",
|
|
8
|
+
zIndex = 2147483647
|
|
9
|
+
} = {}) {
|
|
10
|
+
this.position = position;
|
|
11
|
+
this.gap = gap;
|
|
12
|
+
this.zIndex = zIndex;
|
|
13
|
+
this.offset = offset;
|
|
14
|
+
this.container = this.#getOrCreateContainer();
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
#icons = {
|
|
18
|
+
info: `
|
|
19
|
+
<svg style="flex: 0 0 auto" width="18" height="18" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
|
|
20
|
+
<path d="M12 17V11" stroke="#ffffff" stroke-width="2" stroke-linecap="round"></path>
|
|
21
|
+
<circle cx="1" cy="1" r="1" transform="matrix(1 0 0 -1 11 9)" fill="#ffffff"></circle>
|
|
22
|
+
<path d="M2 12C2 7.28595 2 4.92893 3.46447 3.46447C4.92893 2 7.28595 2 12 2C16.714 2 19.0711 2 20.5355 3.46447C22 4.92893 22 7.28595 22 12C22 16.714 22 19.0711 20.5355 20.5355C19.0711 22 16.714 22 12 22C7.28595 22 4.92893 22 3.46447 20.5355C2 19.0711 2 16.714 2 12Z" stroke="#ffffff" stroke-width="2"></path>
|
|
23
|
+
</svg>
|
|
24
|
+
`,
|
|
25
|
+
|
|
26
|
+
success: `
|
|
27
|
+
<svg style="flex: 0 0 auto" width="18" height="18" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
|
|
28
|
+
<path d="M9.78133 3.89027C10.3452 3.40974 10.6271 3.16948 10.9219 3.02859C11.6037 2.70271 12.3963 2.70271 13.0781 3.02859C13.3729 3.16948 13.6548 3.40974 14.2187 3.89027C14.4431 4.08152 14.5553 4.17715 14.6752 4.25747C14.9499 4.4416 15.2584 4.56939 15.5828 4.63344C15.7244 4.66139 15.8713 4.67312 16.1653 4.69657C16.9038 4.7555 17.273 4.78497 17.5811 4.89378C18.2936 5.14546 18.8541 5.70591 19.1058 6.41844C19.2146 6.72651 19.244 7.09576 19.303 7.83426C19.3264 8.12819 19.3381 8.27515 19.3661 8.41669C19.4301 8.74114 19.5579 9.04965 19.7421 9.32437C19.8224 9.44421 19.918 9.55642 20.1093 9.78084C20.5898 10.3447 20.8301 10.6267 20.971 10.9214C21.2968 11.6032 21.2968 12.3958 20.971 13.0776C20.8301 13.3724 20.5898 13.6543 20.1093 14.2182C19.918 14.4426 19.8224 14.5548 19.7421 14.6747C19.5579 14.9494 19.4301 15.2579 19.3661 15.5824C19.3381 15.7239 19.3264 15.8709 19.303 16.1648C19.244 16.9033 19.2146 17.2725 19.1058 17.5806C18.8541 18.2931 18.2936 18.8536 17.5811 19.1053C17.273 19.2141 16.9038 19.2435 16.1653 19.3025C15.8713 19.3259 15.7244 19.3377 15.5828 19.3656C15.2584 19.4297 14.9499 19.5574 14.6752 19.7416C14.5553 19.8219 14.4431 19.9175 14.2187 20.1088C13.6548 20.5893 13.3729 20.8296 13.0781 20.9705C12.3963 21.2963 11.6037 21.2963 10.9219 20.9705C10.6271 20.8296 10.3452 20.5893 9.78133 20.1088C9.55691 19.9175 9.44469 19.8219 9.32485 19.7416C9.05014 19.5574 8.74163 19.4297 8.41718 19.3656C8.27564 19.3377 8.12868 19.3259 7.83475 19.3025C7.09625 19.2435 6.72699 19.2141 6.41893 19.1053C5.7064 18.8536 5.14594 18.2931 4.89427 17.5806C4.78546 17.2725 4.75599 16.9033 4.69706 16.1648C4.6736 15.8709 4.66188 15.7239 4.63393 15.5824C4.56988 15.2579 4.44209 14.9494 4.25796 14.6747C4.17764 14.5548 4.08201 14.4426 3.89076 14.2182C3.41023 13.6543 3.16997 13.3724 3.02907 13.0776C2.7032 12.3958 2.7032 11.6032 3.02907 10.9214C3.16997 10.6266 3.41023 10.3447 3.89076 9.78084C4.08201 9.55642 4.17764 9.44421 4.25796 9.32437C4.44209 9.04965 4.56988 8.74114 4.63393 8.41669C4.66188 8.27515 4.6736 8.12819 4.69706 7.83426C4.75599 7.09576 4.78546 6.72651 4.89427 6.41844C5.14594 5.70591 5.7064 5.14546 6.41893 4.89378C6.72699 4.78497 7.09625 4.7555 7.83475 4.69657C8.12868 4.67312 8.27564 4.66139 8.41718 4.63344C8.74163 4.56939 9.05014 4.4416 9.32485 4.25747C9.4447 4.17715 9.55691 4.08152 9.78133 3.89027Z" stroke="#ffffff" stroke-width="2"></path>
|
|
29
|
+
<path d="M8.5 12.5L10.5 14.5L15.5 9.5" stroke="#ffffff" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"></path>
|
|
30
|
+
</svg>
|
|
31
|
+
`,
|
|
32
|
+
|
|
33
|
+
warning: `
|
|
34
|
+
<svg style="flex: 0 0 auto" width="18" height="18" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
|
|
35
|
+
<path d="M3 10.4167C3 7.21907 3 5.62028 3.37752 5.08241C3.75503 4.54454 5.25832 4.02996 8.26491 3.00079L8.83772 2.80472C10.405 2.26824 11.1886 2 12 2C12.8114 2 13.595 2.26824 15.1623 2.80472L15.7351 3.00079C18.7417 4.02996 20.245 4.54454 20.6225 5.08241C21 5.62028 21 7.21907 21 10.4167C21 10.8996 21 11.4234 21 11.9914C21 17.6294 16.761 20.3655 14.1014 21.5273C13.38 21.8424 13.0193 22 12 22C10.9807 22 10.62 21.8424 9.89856 21.5273C7.23896 20.3655 3 17.6294 3 11.9914C3 11.4234 3 10.8996 3 10.4167Z" stroke="#ffffff" stroke-width="2"></path>
|
|
36
|
+
<path d="M12 8V12" stroke="#ffffff" stroke-width="2" stroke-linecap="round"></path>
|
|
37
|
+
<circle cx="12" cy="15" r="1" fill="#ffffff"></circle>
|
|
38
|
+
</svg>
|
|
39
|
+
`,
|
|
40
|
+
|
|
41
|
+
error: `
|
|
42
|
+
<svg style="flex: 0 0 auto" width="18" height="18" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><g id="SVGRepo_bgCarrier" stroke-width="0"></g><g id="SVGRepo_tracerCarrier" stroke-linecap="round" stroke-linejoin="round"></g><g id="SVGRepo_iconCarrier">
|
|
43
|
+
<circle cx="12" cy="12" r="10" stroke="#ffffff" stroke-width="2"></circle>
|
|
44
|
+
<path d="M14.5 9.50002L9.5 14.5M9.49998 9.5L14.5 14.5" stroke="#ffffff" stroke-width="2" stroke-linecap="round"></path>
|
|
45
|
+
</svg>
|
|
46
|
+
`
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
#defaults = {
|
|
50
|
+
success: "#28a745",
|
|
51
|
+
error: "#dc3545",
|
|
52
|
+
warning: "#ffc107",
|
|
53
|
+
info: "#007bff"
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
#getOrCreateContainer() {
|
|
57
|
+
const existing = document.querySelector(`[data-toast-position="${this.position}"]`);
|
|
58
|
+
if (existing) return existing;
|
|
59
|
+
|
|
60
|
+
const container = div({
|
|
61
|
+
"data-toast-position": this.position,
|
|
62
|
+
style: this.#positionStyles(),
|
|
63
|
+
});
|
|
64
|
+
document.body.appendChild(container);
|
|
65
|
+
return container;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
#positionStyles() {
|
|
69
|
+
const base = {
|
|
70
|
+
position: "fixed",
|
|
71
|
+
display: "flex",
|
|
72
|
+
flexDirection: "column",
|
|
73
|
+
gap: this.gap,
|
|
74
|
+
zIndex: this.zIndex,
|
|
75
|
+
};
|
|
76
|
+
const pos = this.position.split("-");
|
|
77
|
+
const style = {...base, alignItems: pos[1] === "left" ? "flex-start" : "flex-end"};
|
|
78
|
+
style[pos[0]] = this.offset;
|
|
79
|
+
style[pos[1]] = this.offset;
|
|
80
|
+
return style;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
show(message, {
|
|
84
|
+
type = "info", // success | info | warning | error
|
|
85
|
+
duration = 3000,
|
|
86
|
+
relay = 0,
|
|
87
|
+
fontSize = "14px",
|
|
88
|
+
color = "#fff",
|
|
89
|
+
background = null,
|
|
90
|
+
borderRadius = "0.5rem",
|
|
91
|
+
shadow = "0 3px 8px rgba(0,0,0,0.3)"
|
|
92
|
+
} = {}) {
|
|
93
|
+
const bg = background ?? this.#defaults[type] ?? "#007bff";
|
|
94
|
+
|
|
95
|
+
const toast = div({
|
|
96
|
+
style: {
|
|
97
|
+
minWidth: "180px",
|
|
98
|
+
maxWidth: "300px",
|
|
99
|
+
padding: "8px 12px",
|
|
100
|
+
display: "flex",
|
|
101
|
+
alignItems: "center",
|
|
102
|
+
gap: "10px",
|
|
103
|
+
fontSize,
|
|
104
|
+
color,
|
|
105
|
+
background: bg,
|
|
106
|
+
borderRadius,
|
|
107
|
+
boxShadow: shadow,
|
|
108
|
+
opacity: "0",
|
|
109
|
+
transform: "translateY(-10px)",
|
|
110
|
+
transition: "all 0.4s ease",
|
|
111
|
+
cursor: "pointer"
|
|
112
|
+
},
|
|
113
|
+
value: [
|
|
114
|
+
this.#icons[type] || this.#icons.info,
|
|
115
|
+
span({style: {whiteSpace: "normal"}, value: message})
|
|
116
|
+
]
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
setTimeout(() => this.container.appendChild(toast), relay);
|
|
120
|
+
|
|
121
|
+
// Анімація появи
|
|
122
|
+
requestAnimationFrame(() => {
|
|
123
|
+
toast.style.opacity = "1";
|
|
124
|
+
toast.style.transform = "translateY(0)";
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
toast.addEventListener("click", () => this.#remove(toast));
|
|
128
|
+
setTimeout(() => this.#remove(toast), duration);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
success(msg, opt = {}) {
|
|
132
|
+
this.show(msg, {...opt, type: "success"});
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
info(msg, opt = {}) {
|
|
136
|
+
this.show(msg, {...opt, type: "info"});
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
warning(msg, opt = {}) {
|
|
140
|
+
this.show(msg, {...opt, type: "warning"});
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
error(msg, opt = {}) {
|
|
144
|
+
this.show(msg, {...opt, type: "error"});
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
#remove(toast) {
|
|
148
|
+
toast.style.opacity = "0";
|
|
149
|
+
toast.style.transform = "translateY(-20px)";
|
|
150
|
+
setTimeout(() => toast.remove(), 300);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
clear() {
|
|
154
|
+
this.container.innerHTML = "";
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
export default Toast;
|