@veritree/ui 0.66.1 → 0.67.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.
package/package.json
CHANGED
|
@@ -5,19 +5,56 @@
|
|
|
5
5
|
</template>
|
|
6
6
|
|
|
7
7
|
<script>
|
|
8
|
-
let windowResizeTimeout = null;
|
|
9
|
-
|
|
10
8
|
export default {
|
|
11
9
|
name: 'VTCarousel',
|
|
12
10
|
|
|
11
|
+
/**
|
|
12
|
+
* Provides a Vue provide function that exposes a carousel API to child components.
|
|
13
|
+
* The API includes properties related to tracking and observing the carousel state,
|
|
14
|
+
* as well as methods for registering elements and handling observation events.
|
|
15
|
+
*
|
|
16
|
+
* @function
|
|
17
|
+
* @memberof YourComponent
|
|
18
|
+
* @name provide
|
|
19
|
+
* @returns {Object} The carousel API object containing properties and methods for child components.
|
|
20
|
+
*/
|
|
13
21
|
provide() {
|
|
22
|
+
/**
|
|
23
|
+
* Registers the provided element tracker with the component.
|
|
24
|
+
*
|
|
25
|
+
* @function
|
|
26
|
+
* @memberof CarouselAPI
|
|
27
|
+
* @name registerElTracker
|
|
28
|
+
* @param {Object} elTracker - The element tracker object containing dimensions and scroll properties.
|
|
29
|
+
* @returns {void}
|
|
30
|
+
* @private
|
|
31
|
+
*/
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Vue provide function that exposes the carousel API.
|
|
35
|
+
*
|
|
36
|
+
* @function
|
|
37
|
+
* @name apiCarousel
|
|
38
|
+
* @memberof YourComponent
|
|
39
|
+
* @returns {Object} The carousel API object.
|
|
40
|
+
*/
|
|
14
41
|
return {
|
|
15
42
|
apiCarousel: () => {
|
|
43
|
+
/**
|
|
44
|
+
* Registers the provided element tracker with the component.
|
|
45
|
+
*
|
|
46
|
+
* @function
|
|
47
|
+
* @memberof CarouselAPI
|
|
48
|
+
* @name registerElTracker
|
|
49
|
+
* @param {Object} elTracker - The element tracker object containing dimensions and scroll properties.
|
|
50
|
+
* @returns {void}
|
|
51
|
+
*/
|
|
16
52
|
const registerElTracker = (elTracker) => {
|
|
17
53
|
if (!elTracker) return;
|
|
18
54
|
this.elTracker = elTracker;
|
|
19
55
|
};
|
|
20
56
|
|
|
57
|
+
// Expose properties and methods for child components through the carousel API
|
|
21
58
|
return {
|
|
22
59
|
elTracker: this.elTracker,
|
|
23
60
|
hasContentOverflow: this.hasContentOverflow,
|
|
@@ -25,7 +62,7 @@ export default {
|
|
|
25
62
|
hasScrolledToLeftStart: this.hasScrolledToLeftStart,
|
|
26
63
|
isLeftEndReached: this.isLeftEndReached,
|
|
27
64
|
isLeftStartReached: this.isLeftStartReached,
|
|
28
|
-
|
|
65
|
+
onObserveTracker: this.onObserveTracker,
|
|
29
66
|
registerElTracker,
|
|
30
67
|
};
|
|
31
68
|
},
|
|
@@ -42,34 +79,59 @@ export default {
|
|
|
42
79
|
},
|
|
43
80
|
|
|
44
81
|
mounted() {
|
|
45
|
-
this.addEvents();
|
|
46
82
|
this.hasContentOverflow = this.isContentOverflowing();
|
|
47
83
|
},
|
|
48
84
|
|
|
49
85
|
methods: {
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
86
|
+
/**
|
|
87
|
+
* Updates the state or performs actions based on observation events.
|
|
88
|
+
* This method is typically provided globally via Vue provide and is meant to be called
|
|
89
|
+
* in response to observation events (e.g., Resize Observer or Mutation Observer).
|
|
90
|
+
*
|
|
91
|
+
* Note: Ensure that the necessary state or methods (e.g., isContentOverflowing) are accessible
|
|
92
|
+
* in the context where this method is used.
|
|
93
|
+
*
|
|
94
|
+
* @function
|
|
95
|
+
* @memberof VTCarousel
|
|
96
|
+
* @name onObserveTracker
|
|
97
|
+
* @returns {void}
|
|
98
|
+
*/
|
|
99
|
+
onObserveTracker() {
|
|
63
100
|
this.hasContentOverflow = this.isContentOverflowing();
|
|
64
101
|
},
|
|
65
102
|
|
|
66
103
|
/**
|
|
67
|
-
*
|
|
104
|
+
* Determines whether the scroll position has reached the leftmost end of the container.
|
|
105
|
+
* Calculates the distance from the current scroll position to the left end and updates
|
|
106
|
+
* the 'hasScrolledToLeftEnd' property based on the calculated distance.
|
|
68
107
|
*
|
|
69
|
-
* @
|
|
108
|
+
* @function
|
|
109
|
+
* @memberof VTCarousel
|
|
110
|
+
* @name isLeftEndReached
|
|
111
|
+
* @returns {void}
|
|
70
112
|
*/
|
|
71
113
|
isLeftEndReached() {
|
|
114
|
+
/**
|
|
115
|
+
* Represents the tracking element's dimensions and scroll properties.
|
|
116
|
+
*
|
|
117
|
+
* @typedef {Object} ElTracker
|
|
118
|
+
* @property {number} offsetWidth - Width of the container element.
|
|
119
|
+
* @property {number} scrollLeft - Current scroll position from the left.
|
|
120
|
+
* @property {number} scrollWidth - Total width of the content within the container.
|
|
121
|
+
*/
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Object containing dimensions and scroll properties of the tracking element.
|
|
125
|
+
*
|
|
126
|
+
* @type {ElTracker}
|
|
127
|
+
*/
|
|
72
128
|
const { offsetWidth, scrollLeft, scrollWidth } = this.elTracker;
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Calculated distance from the current scroll position to the left end.
|
|
132
|
+
*
|
|
133
|
+
* @type {number}
|
|
134
|
+
*/
|
|
73
135
|
const distanceToLeftEnd = Math.abs(
|
|
74
136
|
scrollWidth - offsetWidth - scrollLeft
|
|
75
137
|
);
|
|
@@ -79,20 +141,37 @@ export default {
|
|
|
79
141
|
},
|
|
80
142
|
|
|
81
143
|
/**
|
|
82
|
-
*
|
|
144
|
+
* Determines whether the scroll position has reached the leftmost starting point of the container.
|
|
145
|
+
* Updates the 'hasScrolledToLeftStart' property based on whether the scroll position is at the beginning.
|
|
83
146
|
*
|
|
84
|
-
* @
|
|
147
|
+
* @function
|
|
148
|
+
* @memberof VTCarousel
|
|
149
|
+
* @name isLeftStartReached
|
|
150
|
+
* @returns {void}
|
|
85
151
|
*/
|
|
86
152
|
isLeftStartReached() {
|
|
87
|
-
|
|
153
|
+
/**
|
|
154
|
+
* Represents the tracking element's scroll position from the left.
|
|
155
|
+
*
|
|
156
|
+
* @type {scrollLeft}
|
|
157
|
+
*/
|
|
158
|
+
const { scrollLeft } = this.elTracker;
|
|
159
|
+
|
|
160
|
+
// Update 'hasScrolledToLeftStart' based on whether the scroll position is at the beginning (0)
|
|
161
|
+
this.hasScrolledToLeftStart = scrollLeft === 0;
|
|
88
162
|
},
|
|
89
163
|
|
|
90
164
|
/**
|
|
91
|
-
*
|
|
165
|
+
* Determines whether the content within the tracked element is overflowing horizontally.
|
|
166
|
+
* It compares the offset width of the element with its scroll width.
|
|
92
167
|
*
|
|
93
|
-
* @
|
|
168
|
+
* @function
|
|
169
|
+
* @memberof VTCarousel
|
|
170
|
+
* @name isContentOverflowing
|
|
171
|
+
* @returns {boolean} True if the content is overflowing horizontally, otherwise false.
|
|
94
172
|
*/
|
|
95
173
|
isContentOverflowing() {
|
|
174
|
+
// Compare the offset width with the scroll width to check for horizontal overflow
|
|
96
175
|
return this.elTracker.offsetWidth !== this.elTracker.scrollWidth;
|
|
97
176
|
},
|
|
98
177
|
},
|
|
@@ -13,21 +13,37 @@ export default {
|
|
|
13
13
|
data() {
|
|
14
14
|
return {
|
|
15
15
|
mutationObserver: null,
|
|
16
|
+
resizeObserver: null,
|
|
16
17
|
};
|
|
17
18
|
},
|
|
18
19
|
|
|
20
|
+
/**
|
|
21
|
+
* Lifecycle hook called before the component is destroyed.
|
|
22
|
+
* Removes event listeners and disconnects observers to prevent memory leaks.
|
|
23
|
+
*
|
|
24
|
+
* @function
|
|
25
|
+
* @memberof VTCarouselTracker
|
|
26
|
+
* @name beforeDestroy
|
|
27
|
+
* @returns {void}
|
|
28
|
+
*/
|
|
19
29
|
beforeDestroy() {
|
|
30
|
+
// Check if the component's root element exists
|
|
20
31
|
if (!this.$el) {
|
|
21
32
|
return;
|
|
22
33
|
}
|
|
23
34
|
|
|
35
|
+
// Remove the 'scrollend' event listener
|
|
24
36
|
this.$el.removeEventListener('scrollend', this.onScroll);
|
|
37
|
+
|
|
38
|
+
// Disconnect the Mutation Observer and Resize Observer to prevent memory leaks
|
|
25
39
|
this.mutationObserver.disconnect(this.$el);
|
|
40
|
+
this.resizeObserver.disconnect(this.$el);
|
|
26
41
|
},
|
|
27
42
|
|
|
28
43
|
mounted() {
|
|
29
44
|
this.registerElTracker();
|
|
30
45
|
this.registerMutationObserver();
|
|
46
|
+
this.registerResizeObserver();
|
|
31
47
|
this.addEventListeners();
|
|
32
48
|
},
|
|
33
49
|
|
|
@@ -40,28 +56,91 @@ export default {
|
|
|
40
56
|
},
|
|
41
57
|
|
|
42
58
|
/**
|
|
43
|
-
*
|
|
44
|
-
*
|
|
59
|
+
* Initializes and registers a Resize Observer to monitor changes in the size of the current element.
|
|
60
|
+
* When a resize event is detected, it triggers the `onObserveTracker` method of the associated carousel API.
|
|
61
|
+
*
|
|
62
|
+
* @function
|
|
63
|
+
* @name registerResizeObserver
|
|
64
|
+
* @returns {void}
|
|
65
|
+
*/
|
|
66
|
+
registerResizeObserver() {
|
|
67
|
+
/**
|
|
68
|
+
* Resize Observer callback function.
|
|
69
|
+
*
|
|
70
|
+
* @callback ResizeObserverCallback
|
|
71
|
+
* @param {Array<ResizeObserverEntry>} entries - An array of resize observer entries.
|
|
72
|
+
*/
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Resize Observer instance.
|
|
76
|
+
*
|
|
77
|
+
* @type {ResizeObserver}
|
|
78
|
+
*/
|
|
79
|
+
this.resizeObserver = new ResizeObserver(() => {
|
|
80
|
+
this.apiCarousel().onObserveTracker();
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
// Start observing the current element's size changes
|
|
84
|
+
this.resizeObserver.observe(this.$el);
|
|
85
|
+
},
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Initializes and registers a Mutation Observer to monitor changes in the child list of the current element.
|
|
89
|
+
* When a mutation event is detected, it triggers the `onObserveTracker` method of the associated carousel API.
|
|
90
|
+
*
|
|
91
|
+
* @function
|
|
92
|
+
* @name registerMutationObserver
|
|
93
|
+
* @returns {void}
|
|
45
94
|
*/
|
|
46
95
|
registerMutationObserver() {
|
|
96
|
+
/**
|
|
97
|
+
* Mutation Observer callback function.
|
|
98
|
+
*
|
|
99
|
+
* @callback MutationObserverCallback
|
|
100
|
+
* @param {Array<MutationRecord>} mutationsList - An array of mutation records.
|
|
101
|
+
* @param {MutationObserver} observer - The Mutation Observer instance.
|
|
102
|
+
*/
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Mutation Observer instance.
|
|
106
|
+
*
|
|
107
|
+
* @type {MutationObserver}
|
|
108
|
+
*/
|
|
47
109
|
this.mutationObserver = new MutationObserver(() => {
|
|
48
|
-
this.apiCarousel().
|
|
110
|
+
this.apiCarousel().onObserveTracker();
|
|
49
111
|
});
|
|
50
112
|
|
|
113
|
+
// Start observing changes in the child list of the current element
|
|
51
114
|
this.mutationObserver.observe(this.$el, {
|
|
52
115
|
childList: true,
|
|
53
116
|
});
|
|
54
117
|
},
|
|
55
118
|
|
|
56
119
|
/**
|
|
57
|
-
* Adds
|
|
58
|
-
*
|
|
120
|
+
* Adds the 'scroll' event listener to the component's root element.
|
|
121
|
+
* The 'scroll' event triggers the 'onScroll' method when the element is scrolled.
|
|
122
|
+
*
|
|
123
|
+
* @function
|
|
124
|
+
* @memberof VTCarouselTracker
|
|
125
|
+
* @name addEventListeners
|
|
126
|
+
* @returns {void}
|
|
59
127
|
*/
|
|
60
128
|
addEventListeners() {
|
|
129
|
+
// Listen for 'scroll' events on the component's root element
|
|
61
130
|
this.$el.addEventListener('scroll', this.onScroll);
|
|
62
131
|
},
|
|
63
132
|
|
|
133
|
+
/**
|
|
134
|
+
* Handles the 'scroll' event by invoking methods related to left end and left start positions.
|
|
135
|
+
* Calls the 'isLeftEndReached' and 'isLeftStartReached' methods from the carousel API.
|
|
136
|
+
*
|
|
137
|
+
* @function
|
|
138
|
+
* @memberof VTCarouselTracker
|
|
139
|
+
* @name onScroll
|
|
140
|
+
* @returns {void}
|
|
141
|
+
*/
|
|
64
142
|
onScroll() {
|
|
143
|
+
// Check and update left end and left start positions based on the scroll event
|
|
65
144
|
this.apiCarousel().isLeftEndReached();
|
|
66
145
|
this.apiCarousel().isLeftStartReached();
|
|
67
146
|
},
|