This repository has been archived by the owner on Sep 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
tabsPaginationService.js
112 lines (98 loc) · 3.06 KB
/
tabsPaginationService.js
1
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
angular
.module('material.components.tabs')
.service('MdTabsPaginationService', MdTabsPaginationService);
/**
* @private
* @module material.components.tabs
* @name MdTabsPaginationService
* @description Provides many standalone functions to ease in pagination calculations.
*
* Most functions accept the elements and the current offset.
*
* The `elements` parameter is typically the value returned from the `getElements()` function of the
* tabsController.
*
* The `offset` parameter is always positive regardless of LTR or RTL (we simply make the LTR one
* negative when we apply our transform). This is typically the `ctrl.leftOffset` variable in the
* tabsController.
*
* @returns MdTabsPaginationService
* @constructor
*/
function MdTabsPaginationService() {
return {
decreasePageOffset: decreasePageOffset,
increasePageOffset: increasePageOffset,
getTabOffsets: getTabOffsets,
getTotalTabsWidth: getTotalTabsWidth
};
/**
* Returns the offset for the next decreasing page.
*
* @param elements
* @param currentOffset
* @returns {number}
*/
function decreasePageOffset(elements, currentOffset) {
var canvas = elements.canvas,
tabOffsets = getTabOffsets(elements),
i, firstVisibleTabOffset;
// Find the first fully visible tab in offset range
for (i = 0; i < tabOffsets.length; i++) {
if (tabOffsets[i] >= currentOffset) {
firstVisibleTabOffset = tabOffsets[i];
break;
}
}
// Return (the first visible tab offset - the tabs container width) without going negative
return Math.max(0, firstVisibleTabOffset - canvas.clientWidth);
}
/**
* Returns the offset for the next increasing page.
*
* @param elements
* @param currentOffset
* @returns {number}
*/
function increasePageOffset(elements, currentOffset) {
var canvas = elements.canvas,
maxOffset = getTotalTabsWidth(elements) - canvas.clientWidth,
tabOffsets = getTabOffsets(elements),
i, firstHiddenTabOffset;
// Find the first partially (or fully) invisible tab
for (i = 0; i < tabOffsets.length, tabOffsets[i] <= currentOffset + canvas.clientWidth; i++) {
firstHiddenTabOffset = tabOffsets[i];
}
// Return the offset of the first hidden tab, or the maximum offset (whichever is smaller)
return Math.min(maxOffset, firstHiddenTabOffset);
}
/**
* Returns the offsets of all of the tabs based on their widths.
*
* @param elements
* @returns {number[]}
*/
function getTabOffsets(elements) {
var i, tab, currentOffset = 0, offsets = [];
for (i = 0; i < elements.tabs.length; i++) {
tab = elements.tabs[i];
offsets.push(currentOffset);
currentOffset += tab.offsetWidth;
}
return offsets;
}
/**
* Sum the width of all tabs.
*
* @param elements
* @returns {number}
*/
function getTotalTabsWidth(elements) {
var sum = 0, i, tab;
for (i = 0; i < elements.tabs.length; i++) {
tab = elements.tabs[i];
sum += tab.offsetWidth;
}
return sum;
}
}