This repository has been archived by the owner on Mar 13, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathpaper-tabs.html
353 lines (287 loc) · 9.79 KB
/
paper-tabs.html
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
<!--
Copyright (c) 2014 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
-->
<!--
`paper-tabs` is a `core-selector` styled to look like tabs. Tabs make it easy to
explore and switch between different views or functional aspects of an app, or
to browse categorized data sets.
Use `selected` property to get or set the selected tab.
Example:
<paper-tabs selected="0">
<paper-tab>TAB 1</paper-tab>
<paper-tab>TAB 2</paper-tab>
<paper-tab>TAB 3</paper-tab>
</paper-tabs>
See <a href="#paper-tab">paper-tab</a> for more information about
`paper-tab`.
A common usage for `paper-tabs` is to use it along with `core-pages` to switch
between different views.
<paper-tabs selected="{{selected}}">
<paper-tab>Tab 1</paper-tab>
<paper-tab>Tab 2</paper-tab>
<paper-tab>Tab 3</paper-tab>
</paper-tabs>
<core-pages selected="{{selected}}">
<div>Page 1</div>
<div>Page 2</div>
<div>Page 3</div>
</core-pages>
`paper-tabs` adapt to mobile/narrow layout when there is a `core-narrow` class set
on itself or any of its ancestors.
To use links in tabs, add `link` attribute to `paper-tabs` and put an `<a>`
element in `paper-tab`.
Example:
<paper-tabs selected="0" link>
<paper-tab>
<a href="#link1" horizontal center-center layout>TAB ONE</a>
</paper-tab>
<paper-tab>
<a href="#link2" horizontal center-center layout>TAB TWO</a>
</paper-tab>
<paper-tab>
<a href="#link3" horizontal center-center layout>TAB THREE</a>
</paper-tab>
</paper-tabs>
Styling tabs:
To change the sliding bar color:
paper-tabs.pink::shadow #selectionBar {
background-color: #ff4081;
}
To change the ink ripple color:
paper-tabs.pink paper-tab::shadow #ink {
color: #ff4081;
}
@group Paper Elements
@element paper-tabs
@extends core-selector
@mixins Polymer.CoreResizable https://github.com/polymer/core-resizable
@homepage github.io
-->
<link rel="import" href="../core-selector/core-selector.html">
<link rel="import" href="../paper-icon-button/paper-icon-button.html">
<link rel="import" href="../core-resizable/core-resizable.html">
<link rel="import" href="paper-tab.html">
<polymer-element name="paper-tabs" extends="core-selector" attributes="noink nobar noslide scrollable hideScrollButton alignBottom" role="tablist" horizontal center layout>
<template>
<link rel="stylesheet" href="paper-tabs.css">
<div class="scroll-button" hidden?="{{!scrollable || hideScrollButton}}">
<paper-icon-button icon="chevron-left" class="{{ {hidden: leftHidden} | tokenList }}" on-down="{{holdLeft}}" on-up="{{releaseHold}}"></paper-icon-button>
</div>
<div id="tabsContainer" class="{{ {scrollable: scrollable} | tokenList }}" flex on-scroll="{{scroll}}" on-trackstart="{{trackStart}}">
<div id="tabsContent" horizontal layout?="{{!scrollable}}">
<shadow></shadow>
<div id="selectionBar" hidden?="{{nobar}}" class="{{ {alignBottom: alignBottom} | tokenList }}" on-transitionend="{{barTransitionEnd}}"></div>
</div>
</div>
<div class="scroll-button" hidden?="{{!scrollable || hideScrollButton}}">
<paper-icon-button icon="chevron-right" class="{{ {hidden: rightHidden} | tokenList }}" on-down="{{holdRight}}" on-up="{{releaseHold}}"></paper-icon-button>
</div>
</template>
<script>
Polymer(Polymer.mixin({
/**
* If true, ink ripple effect is disabled.
*
* @attribute noink
* @type boolean
* @default false
*/
noink: false,
/**
* If true, the bottom bar to indicate the selected tab will not be shown.
*
* @attribute nobar
* @type boolean
* @default false
*/
nobar: false,
/**
* If true, the slide effect for the bottom bar is disabled.
*
* @attribute noslide
* @type boolean
* @default false
*/
noslide: false,
/**
* If true, tabs are scrollable and the tab width is based on the label width.
*
* @attribute scrollable
* @type boolean
* @default false
*/
scrollable: false,
/**
* If true, dragging on the tabs to scroll is disabled.
*
* @attribute disableDrag
* @type boolean
* @default false
*/
disableDrag: false,
/**
* If true, scroll buttons (left/right arrow) will be hidden for scrollable tabs.
*
* @attribute hideScrollButton
* @type boolean
* @default false
*/
hideScrollButton: false,
/**
* If true, the tabs are aligned to bottom (the selection bar appears at the top).
*
@attribute alignBottom
@type boolean
@default false
*/
alignBottom: false,
eventDelegates: {
'core-resize': 'resizeHandler'
},
activateEvent: 'tap',
step: 10,
holdDelay: 10,
ready: function() {
this.super();
this._trackxHandler = this.trackx.bind(this);
Polymer.addEventListener(this.$.tabsContainer, 'trackx', this._trackxHandler);
this._tabsObserver = new MutationObserver(this.updateBar.bind(this));
},
domReady: function() {
this.async('resizeHandler');
this._tabsObserver.observe(this, {childList: true, subtree: true, characterData: true});
},
attached: function() {
this.resizableAttachedHandler();
},
detached: function() {
Polymer.removeEventListener(this.$.tabsContainer, 'trackx', this._trackxHandler);
this._tabsObserver.disconnect();
this.resizableDetachedHandler();
},
trackStart: function(e) {
if (!this.scrollable || this.disableDrag) {
return;
}
var t = e.target;
if (t && t.cancelRipple) {
t.cancelRipple();
}
this._startx = this.$.tabsContainer.scrollLeft;
e.preventTap();
},
trackx: function(e) {
if (!this.scrollable || this.disableDrag) {
return;
}
this.$.tabsContainer.scrollLeft = this._startx - e.dx;
},
resizeHandler: function() {
this.scroll();
this.updateBar();
},
scroll: function() {
if (!this.scrollable) {
return;
}
var tc = this.$.tabsContainer;
var l = tc.scrollLeft;
this.leftHidden = l === 0;
this.rightHidden = l === Math.max(0, (tc.scrollWidth - tc.clientWidth));
},
holdLeft: function() {
this.holdJob = setInterval(this.scrollToLeft.bind(this), this.holdDelay);
},
holdRight: function() {
this.holdJob = setInterval(this.scrollToRight.bind(this), this.holdDelay);
},
releaseHold: function() {
clearInterval(this.holdJob);
this.holdJob = null;
},
scrollToLeft: function() {
this.$.tabsContainer.scrollLeft -= this.step;
},
scrollToRight: function() {
this.$.tabsContainer.scrollLeft += this.step;
},
/**
* Invoke this to update the size and position of the bottom bar. Usually
* you only need to call this if the `paper-tabs` is initially hidden and
* later becomes visible.
*
* @method updateBar
*/
updateBar: function() {
this.async('selectedItemChanged');
},
selectedItemChanged: function(old) {
var oldIndex = this.selectedIndex;
this.super(arguments);
var s = this.$.selectionBar.style;
if (!this.selectedItem) {
s.width = 0;
s.left = 0;
return;
}
var r = this.$.tabsContent.getBoundingClientRect();
this._w = r.width;
this._l = r.left;
r = this.selectedItem.getBoundingClientRect();
this._sw = r.width;
this._sl = r.left;
this._sOffsetLeft = this._sl - this._l;
if (this.noslide || old == null) {
this.positionBarForSelected();
return;
}
var oldRect = old.getBoundingClientRect();
var m = 5;
this.$.selectionBar.classList.add('expand');
if (oldIndex < this.selectedIndex) {
s.width = this.calcPercent(this._sl + this._sw - oldRect.left) - m + '%';
this._transitionCounter = 1;
} else {
s.width = this.calcPercent(oldRect.left + oldRect.width - this._sl) - m + '%';
s.left = this.calcPercent(this._sOffsetLeft) + m + '%';
this._transitionCounter = 2;
}
if (this.scrollable) {
this.scrollToSelectedIfNeeded();
}
},
scrollToSelectedIfNeeded: function() {
var scrollLeft = this.$.tabsContainer.scrollLeft;
// scroll to selected if needed
if (this._sOffsetLeft + this._sw < scrollLeft ||
this._sOffsetLeft - scrollLeft > this.$.tabsContainer.offsetWidth) {
this.$.tabsContainer.scrollLeft = this._sOffsetLeft;
}
},
positionBarForSelected: function() {
var s = this.$.selectionBar.style;
s.width = this.calcPercent(this._sw) + '%';
s.left = this.calcPercent(this._sOffsetLeft) + '%';
},
calcPercent: function(w) {
return 100 * w / this._w;
},
barTransitionEnd: function(e) {
this._transitionCounter--;
var cl = this.$.selectionBar.classList;
if (cl.contains('expand') && !this._transitionCounter) {
cl.remove('expand');
cl.add('contract');
this.positionBarForSelected();
} else if (cl.contains('contract')) {
cl.remove('contract');
}
}
}, Polymer.CoreResizable));
</script>
</polymer-element>