vgapp 1.4.0 → 1.4.2

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.
@@ -1,89 +1,116 @@
1
- import { execute } from "../functions";
2
- import Html from "../components/templater";
3
- import { Classes } from "../dom/manipulator";
4
- import ScrollBarHelper from "./scrollbar";
5
-
6
- const NAME = 'backdrop';
7
- const CLASS_NAME = 'vg-backdrop';
8
- const CLASS_NAME_FADE = 'fade';
9
- const CLASS_NAME_SHOW = 'show';
10
-
11
- const backdropDelay = 150;
12
-
13
- class Backdrop {
14
- static get _rootEl() {
15
- return document.body;
16
- }
17
-
18
- static _scrollbar = new ScrollBarHelper();
19
- static _backdrop = null;
20
-
21
- /**
22
- * Показывает бэкдроп
23
- * @param {Function} callback - вызывается после отображения
24
- */
25
- static show(callback) {
26
- if (!this._backdrop) {
27
- this._backdrop = this._rootEl.querySelector(`.${CLASS_NAME}`);
28
- }
29
-
30
- if (this._backdrop) {
31
- this._backdrop.remove();
32
- }
33
-
34
- this._append();
35
- execute(callback);
36
- }
37
-
38
- /**
39
- * Скрывает бэкдроп
40
- * @param {Function} callback - вызывается после скрытия
41
- */
42
- static hide(callback) {
43
- if (!this._backdrop) {
44
- // На всякий случай проверим, есть ли элемент в DOM
45
- this._backdrop = this._rootEl.querySelector(`.${CLASS_NAME}`);
46
- }
47
-
48
- if (!this._backdrop) return;
49
-
50
- this._destroy().then(execute.bind(null, callback));
51
- }
52
-
53
- /**
54
- * Создаёт и добавляет элемент бэкдропа
55
- * @private
56
- */
57
- static _append() {
58
- const html = Html('dom');
59
- this._backdrop = html.div({ class: CLASS_NAME });
60
-
61
- this._rootEl.appendChild(this._backdrop);
62
- requestAnimationFrame(() => {
63
- Classes.add(this._backdrop, CLASS_NAME_SHOW);
64
- setTimeout(() => {
65
- Classes.add(this._backdrop, CLASS_NAME_FADE);
66
- }, backdropDelay);
67
- });
68
- }
69
-
70
- /**
71
- * Удаляет бэкдроп с анимацией
72
- * @returns {Promise}
73
- * @private
74
- */
75
- static _destroy() {
76
- return new Promise((resolve) => {
77
- Classes.remove(this._backdrop, CLASS_NAME_FADE);
78
- setTimeout(() => {
79
- Classes.remove(this._backdrop, CLASS_NAME_SHOW);
80
- this._backdrop.remove();
81
- this._backdrop = null;
82
- this._scrollbar.reset();
83
- resolve();
84
- }, backdropDelay);
85
- });
86
- }
87
- }
88
-
89
- export default Backdrop;
1
+ import { execute } from "../functions";
2
+ import Html from "../components/templater";
3
+ import { Classes } from "../dom/manipulator";
4
+ import ScrollBarHelper from "./scrollbar";
5
+
6
+ const NAME = 'backdrop';
7
+ const CLASS_NAME = 'vg-backdrop';
8
+ const CLASS_NAME_FADE = 'fade';
9
+ const CLASS_NAME_SHOW = 'show';
10
+
11
+ const backdropDelay = 150;
12
+
13
+ class Backdrop {
14
+ static get _rootEl() {
15
+ return document.body;
16
+ }
17
+
18
+ static _scrollbar = new ScrollBarHelper();
19
+ static _backdrop = null;
20
+ static _backdrops = [];
21
+
22
+ static getElement() {
23
+ this._syncBackdrops();
24
+ return this._backdrop;
25
+ }
26
+
27
+ static isActive() {
28
+ this._syncBackdrops();
29
+ return this._backdrops.length > 0;
30
+ }
31
+
32
+ /**
33
+ * Показывает новый backdrop и кладёт его поверх предыдущих.
34
+ * @param {Function} callback - Вызывается после добавления backdrop в DOM.
35
+ */
36
+ static show(callback) {
37
+ const backdrop = this._append();
38
+ execute(callback, [backdrop]);
39
+ }
40
+
41
+ /**
42
+ * Скрывает только верхний backdrop.
43
+ * @param {Function} callback - Вызывается после скрытия.
44
+ */
45
+ static hide(callback, backdrop = null) {
46
+ this._syncBackdrops();
47
+
48
+ const targetBackdrop = backdrop && backdrop.isConnected ? backdrop : this._backdrop;
49
+
50
+ if (!targetBackdrop) {
51
+ execute(callback);
52
+ return;
53
+ }
54
+
55
+ this._destroy(targetBackdrop).then(execute.bind(null, callback));
56
+ }
57
+
58
+ /**
59
+ * Создаёт и добавляет backdrop.
60
+ * @returns {HTMLElement}
61
+ * @private
62
+ */
63
+ static _append() {
64
+ const html = Html('dom');
65
+ const backdrop = html.div({ class: CLASS_NAME });
66
+
67
+ this._rootEl.appendChild(backdrop);
68
+ this._backdrops.push(backdrop);
69
+ this._backdrop = backdrop;
70
+
71
+ requestAnimationFrame(() => {
72
+ Classes.add(backdrop, CLASS_NAME_SHOW);
73
+ setTimeout(() => {
74
+ if (backdrop.isConnected) {
75
+ Classes.add(backdrop, CLASS_NAME_FADE);
76
+ }
77
+ }, backdropDelay);
78
+ });
79
+
80
+ return backdrop;
81
+ }
82
+
83
+ /**
84
+ * Удаляет backdrop с анимацией.
85
+ * @param {HTMLElement} backdrop
86
+ * @returns {Promise}
87
+ * @private
88
+ */
89
+ static _destroy(backdrop) {
90
+ return new Promise((resolve) => {
91
+ Classes.remove(backdrop, CLASS_NAME_FADE);
92
+ setTimeout(() => {
93
+ Classes.remove(backdrop, CLASS_NAME_SHOW);
94
+ backdrop.remove();
95
+ this._backdrops = this._backdrops.filter(item => item !== backdrop && item.isConnected);
96
+ this._backdrop = this._backdrops.length ? this._backdrops[this._backdrops.length - 1] : null;
97
+
98
+ if (!this._backdrop) {
99
+ this._scrollbar.reset();
100
+ }
101
+
102
+ resolve();
103
+ }, backdropDelay);
104
+ });
105
+ }
106
+
107
+ static _syncBackdrops() {
108
+ this._backdrops = this._backdrops.filter(item => item && item.isConnected);
109
+ if (!this._backdrops.length) {
110
+ this._backdrops = Array.from(this._rootEl.querySelectorAll(`.${CLASS_NAME}`));
111
+ }
112
+ this._backdrop = this._backdrops.length ? this._backdrops[this._backdrops.length - 1] : null;
113
+ }
114
+ }
115
+
116
+ export default Backdrop;