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
/
paginationDirective.js
251 lines (204 loc) · 7.37 KB
/
paginationDirective.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
(function() {
'use strict';
angular.module('material.components.tabs')
.directive('mdTabsPagination', TabPaginationDirective);
function TabPaginationDirective($mdConstant, $window, $$rAF, $$q, $timeout, $mdMedia) {
// Must match (2 * width of paginators) in scss
var PAGINATORS_WIDTH = (8 * 4) * 2;
return {
restrict: 'A',
require: '^mdTabs',
link: postLink
};
function postLink(scope, element, attr, tabsCtrl) {
var tabs = element[0].getElementsByTagName('md-tab');
var debouncedUpdatePagination = $$rAF.throttle(updatePagination);
var tabsParent = element.children();
var locked = false;
var state = scope.pagination = {
page: -1,
active: false,
clickNext: function() { locked || userChangePage(+1); },
clickPrevious: function() { locked || userChangePage(-1); }
};
scope.$on('$mdTabsChanged', debouncedUpdatePagination);
angular.element($window).on('resize', debouncedUpdatePagination);
scope.$on('$destroy', function() {
angular.element($window).off('resize', debouncedUpdatePagination);
});
scope.$watch(function() { return tabsCtrl.tabToFocus; }, onTabFocus);
// Make sure we don't focus an element on the next page
// before it's in view
function onTabFocus(tab, oldTab) {
if (!tab) return;
var pageIndex = getPageForTab(tab);
if (!state.active || pageIndex === state.page) {
tab.element.focus();
} else {
// Go to the new page, wait for the page transition to end, then focus.
oldTab && oldTab.element.blur();
setPage(pageIndex).then(function() {
locked = false;
tab.element.focus();
});
}
}
// Called when page is changed by a user action (click)
function userChangePage(increment) {
var sizeData = state.tabData;
var newPage = Math.max(0, Math.min(sizeData.pages.length - 1, state.page + increment));
var newTabIndex = sizeData.pages[newPage][ increment > 0 ? 'firstTabIndex' : 'lastTabIndex' ];
var newTab = tabsCtrl.itemAt(newTabIndex);
locked = true;
onTabFocus(newTab);
}
function updatePagination() {
if (!element.prop('offsetParent')) {
var watcher = waitForVisible();
return;
}
var tabs = element.find('md-tab');
disablePagination();
var sizeData = state.tabData = calculateTabData();
var needPagination = state.active = sizeData.pages.length > 1;
if (needPagination) { enablePagination(); }
scope.$evalAsync(function () { scope.$broadcast('$mdTabsPaginationChanged'); });
function enablePagination() {
tabsParent.css('width', '9999px');
//-- apply filler margins
angular.forEach(sizeData.tabs, function (tab) {
angular.element(tab.element).css('margin-left', tab.filler + 'px');
});
setPage(getPageForTab(tabsCtrl.getSelectedItem()));
}
function disablePagination() {
slideTabButtons(0);
tabsParent.css('width', '');
tabs.css('width', '');
tabs.css('margin-left', '');
state.page = null;
state.active = false;
}
function waitForVisible() {
return watcher || scope.$watch(
function () {
$timeout(function () {
if (element[0].offsetParent) {
if (angular.isFunction(watcher)) {
watcher();
}
debouncedUpdatePagination();
watcher = null;
}
}, 0, false);
}
);
}
}
function slideTabButtons(x) {
if (tabsCtrl.pagingOffset === x) {
// Resolve instantly if no change
return $$q.when();
}
var deferred = $$q.defer();
tabsCtrl.$$pagingOffset = x;
tabsParent.css($mdConstant.CSS.TRANSFORM, 'translate3d(' + x + 'px,0,0)');
tabsParent.on($mdConstant.CSS.TRANSITIONEND, onTabsParentTransitionEnd);
return deferred.promise;
function onTabsParentTransitionEnd(ev) {
// Make sure this event didn't bubble up from an animation in a child element.
if (ev.target === tabsParent[0]) {
tabsParent.off($mdConstant.CSS.TRANSITIONEND, onTabsParentTransitionEnd);
deferred.resolve();
}
}
}
function shouldStretchTabs() {
switch (scope.stretchTabs) {
case 'never': return false;
case 'always': return true;
default: return $mdMedia('sm');
}
}
function calculateTabData(noAdjust) {
var clientWidth = element.parent().prop('offsetWidth');
var tabsWidth = clientWidth - PAGINATORS_WIDTH - 1;
var $tabs = angular.element(tabs);
var totalWidth = 0;
var max = 0;
var tabData = [];
var pages = [];
var currentPage;
$tabs.css('max-width', '');
angular.forEach(tabs, function (tab, index) {
var tabWidth = Math.min(tabsWidth, tab.offsetWidth);
var data = {
element: tab,
left: totalWidth,
width: tabWidth,
right: totalWidth + tabWidth,
filler: 0
};
//-- This calculates the page for each tab. The first page will use the clientWidth, which
// does not factor in the pagination items. After the first page, tabsWidth is used
// because at this point, we know that the pagination buttons will be shown.
data.page = Math.ceil(data.right / ( pages.length === 1 && index === tabs.length - 1 ? clientWidth : tabsWidth )) - 1;
if (data.page >= pages.length) {
data.filler = (tabsWidth * data.page) - data.left;
data.right += data.filler;
data.left += data.filler;
currentPage = {
left: data.left,
firstTabIndex: index,
lastTabIndex: index,
tabs: [ data ]
};
pages.push(currentPage);
} else {
currentPage.lastTabIndex = index;
currentPage.tabs.push(data);
}
totalWidth = data.right;
max = Math.max(max, tabWidth);
tabData.push(data);
});
$tabs.css('max-width', tabsWidth + 'px');
if (!noAdjust && shouldStretchTabs()) {
return adjustForStretchedTabs();
} else {
return {
width: totalWidth,
max: max,
tabs: tabData,
pages: pages,
tabElements: tabs
};
}
function adjustForStretchedTabs() {
var canvasWidth = pages.length === 1 ? clientWidth : tabsWidth;
var tabsPerPage = Math.min(Math.floor(canvasWidth / max), tabs.length);
var tabWidth = Math.floor(canvasWidth / tabsPerPage);
$tabs.css('width', tabWidth + 'px');
return calculateTabData(true);
}
}
function getPageForTab(tab) {
var tabIndex = tabsCtrl.indexOf(tab);
if (tabIndex === -1) return 0;
var sizeData = state.tabData;
return sizeData ? sizeData.tabs[tabIndex].page : 0;
}
function setPage(page) {
if (page === state.page) return;
var lastPage = state.tabData.pages.length - 1;
if (page < 0) page = 0;
if (page > lastPage) page = lastPage;
state.hasPrev = page > 0;
state.hasNext = page < lastPage;
state.page = page;
scope.$broadcast('$mdTabsPaginationChanged');
return slideTabButtons(-state.tabData.pages[page].left);
}
}
}
})();