vgapp 0.5.9 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,16 +1,60 @@
1
1
  import {mergeDeepObject, normalizeData} from "../functions";
2
+ import Selectors from "../dom/selectors";
2
3
 
3
4
  /**
4
5
  * Класс Placement, определяет и устанавливает местоположение элемента на странице.
5
- * TODO класс не дописан
6
+ * TODO класс не дописан, не определяет сверху и снизу
6
7
  */
7
8
 
9
+ const CLASS_NAME_RIGHT = 'right';
10
+ const CLASS_NAME_LEFT = 'left';
11
+
8
12
  class Placement {
9
13
  constructor(arg = {}) {
10
14
  this.params = mergeDeepObject({
11
15
  element: null,
12
16
  drop: null
13
17
  }, arg);
18
+
19
+ this._drop = null;
20
+ this.drop = this.params.drop;
21
+
22
+ this._element = null;
23
+ this.element = this.params.element;
24
+ }
25
+
26
+ get drop() {
27
+ return this._drop;
28
+ }
29
+
30
+ set drop(el) {
31
+ if (!el) return;
32
+ this._drop = el;
33
+ }
34
+
35
+ get element() {
36
+ return this._element;
37
+ }
38
+
39
+ set element(el) {
40
+ if (!el) {
41
+ if (this.drop) {
42
+ this._element = this.drop.parentNode;
43
+ }
44
+ }
45
+
46
+ this._element = el;
47
+ }
48
+
49
+ _setPlacement() {
50
+ this.drop.classList.remove(CLASS_NAME_RIGHT);
51
+ this.drop.classList.remove(CLASS_NAME_LEFT);
52
+
53
+ if (this._isElementInViewport(this.drop)) {
54
+ this.drop.classList.add(CLASS_NAME_LEFT);
55
+ } else {
56
+ this.drop.classList.add(CLASS_NAME_RIGHT);
57
+ }
14
58
  }
15
59
 
16
60
  _getPlacement() {
@@ -54,6 +98,19 @@ class Placement {
54
98
  left: left
55
99
  }
56
100
  }
101
+
102
+ _isElementInViewport(element) {
103
+ const rect = element.getBoundingClientRect();
104
+ const viewportWidth = window.innerWidth || document.documentElement.clientWidth;
105
+ const viewportHeight = window.innerHeight || document.documentElement.clientHeight;
106
+
107
+ return (
108
+ rect.top >= 0 &&
109
+ rect.left >= 0 &&
110
+ rect.bottom <= viewportHeight &&
111
+ rect.right <= viewportWidth
112
+ );
113
+ }
57
114
  }
58
115
 
59
116
  export default Placement;