Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(235)

Side by Side Diff: third_party/polymer/v0_8/components/paper-drawer-panel/paper-drawer-panel.html

Issue 1082403004: Import Polymer 0.8 and several key elements. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Also remove polymer/explainer Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 <!--
2 Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
3 This code may only be used under the BSD style license found at http://polymer.g ithub.io/LICENSE.txt
4 The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
5 The complete set of contributors may be found at http://polymer.github.io/CONTRI BUTORS.txt
6 Code distributed by Google as part of the polymer project is also
7 subject to an additional IP rights grant found at http://polymer.github.io/PATEN TS.txt
8 -->
9
10 <link rel="import" href="../polymer/polymer.html">
11 <link rel="import" href="../iron-media-query/iron-media-query.html">
12 <link rel="import" href="../iron-selector/iron-selector.html">
13
14 <!--
15 `paper-drawer-panel` contains a drawer panel and a main panel. The drawer
16 and the main panel are side-by-side with drawer on the left. When the browser
17 window size is smaller than the `responsiveWidth`, `paper-drawer-panel`
18 changes to narrow layout. In narrow layout, the drawer will be stacked on top
19 of the main panel. The drawer will slide in/out to hide/reveal the main
20 panel.
21
22 Use the attribute `drawer` to indicate that the element is the drawer panel and
23 `main` to indicate that the element is the main panel.
24
25 Example:
26
27 <paper-drawer-panel>
28 <div drawer> Drawer panel... </div>
29 <div main> Main panel... </div>
30 </paper-drawer-panel>
31
32 The drawer and the main panels are not scrollable. You can set CSS overflow
33 property on the elements to make them scrollable or use `paper-header-panel`.
34
35 Example:
36
37 <paper-drawer-panel>
38 <paper-header-panel drawer>
39 <paper-toolbar></paper-toolbar>
40 <div> Drawer content... </div>
41 </paper-header-panel>
42 <paper-header-panel main>
43 <paper-toolbar></paper-toolbar>
44 <div> Main content... </div>
45 </paper-header-panel>
46 </paper-drawer-panel>
47
48 An element that should toggle the drawer will automatically do so if it's
49 given the `paper-drawer-toggle` attribute. Also this element will automatically
50 be hidden in wide layout.
51
52 Example:
53
54 <paper-drawer-panel>
55 <paper-header-panel drawer>
56 <paper-toolbar>
57 <div>Application</div>
58 </paper-toolbar>
59 <div> Drawer content... </div>
60 </paper-header-panel>
61 <paper-header-panel main>
62 <paper-toolbar>
63 <paper-icon-button icon="menu" paper-drawer-toggle></paper-icon-button >
64 <div>Title</div>
65 </paper-toolbar>
66 <div> Main content... </div>
67 </paper-header-panel>
68 </paper-drawer-panel>
69
70 To position the drawer to the right, add `rightDrawer` attribute.
71
72 <paper-drawer-panel rightDrawer>
73 <div drawer> Drawer panel... </div>
74 <div main> Main panel... </div>
75 </paper-drawer-panel>
76
77 @group Polymer Elements
78 @element paper-drawer-panel
79 @homepage github.io
80 -->
81
82 <dom-module id="paper-drawer-panel">
83
84 <style>
85 :host {
86 display: block;
87 position: absolute;
88 top: 0;
89 left: 0;
90 width: 100%;
91 height: 100%;
92 overflow: hidden;
93 }
94
95 iron-selector > #drawer {
96 position: absolute;
97 top: 0;
98 left: 0;
99 height: 100%;
100 will-change: transform;
101 box-sizing: border-box;
102 -moz-box-sizing: border-box;
103 }
104
105 .transition > #drawer {
106 transition: -webkit-transform ease-in-out 0.3s, width ease-in-out 0.3s;
107 transition: transform ease-in-out 0.3s, width ease-in-out 0.3s;
108 }
109
110 /*
111 right-drawer: make drawer on the right side
112 */
113 .right-drawer > #drawer {
114 left: auto;
115 right: 0;
116 }
117
118 paper-drawer-panel #drawer > * {
119 position: absolute;
120 top: 0;
121 left: 0;
122 width: 100%;
123 height: 100%;
124 box-sizing: border-box;
125 -moz-box-sizing: border-box;
126 }
127
128 iron-selector > #main {
129 position: absolute;
130 top: 0;
131 right: 0;
132 bottom: 0;
133 }
134
135 .transition > #main {
136 transition: left ease-in-out 0.3s, padding ease-in-out 0.3s;
137 }
138
139 .right-drawer > #main {
140 left: 0;
141 }
142
143 .right-drawer.transition > #main {
144 transition: right ease-in-out 0.3s, padding ease-in-out 0.3s;
145 }
146
147 #main > [main] {
148 height: 100%;
149 }
150
151 #scrim {
152 position: absolute;
153 top: 0;
154 right: 0;
155 bottom: 0;
156 left: 0;
157 background-color: rgba(0, 0, 0, 0.3);
158 visibility: hidden;
159 opacity: 0;
160 transition: opacity ease-in-out 0.38s, visibility ease-in-out 0.38s;
161 }
162
163 #edgeSwipeOverlay {
164 position: absolute;
165 top: 0;
166 bottom: 0;
167 left: 0;
168 width: 20px;
169 }
170
171 .right-drawer > #main > #edgeSwipeOverlay {
172 right: 0;
173 left: auto;
174 }
175
176 /*
177 narrow layout
178 */
179 .narrow-layout > #drawer.iron-selected {
180 box-shadow: 2px 2px 4px rgba(0, 0, 0, 0.15);
181 }
182
183 .right-drawer.narrow-layout > #drawer.iron-selected {
184 box-shadow: -2px 2px 4px rgba(0, 0, 0, 0.15);
185 }
186
187 .narrow-layout > #drawer > ::content[select="[drawer]"] > * {
188 border: 0;
189 }
190
191 .narrow-layout > #drawer:not(.iron-selected) {
192 -webkit-transform: translateX(-100%);
193 transform: translateX(-100%);
194 }
195
196 .right-drawer.narrow-layout > #drawer:not(.iron-selected) {
197 left: auto;
198 -webkit-transform: translateX(100%);
199 transform: translateX(100%);
200 }
201
202 .narrow-layout > #main {
203 left: 0 !important;
204 padding: 0;
205 }
206
207 .right-drawer.narrow-layout > #main {
208 left: 0;
209 right: 0;
210 padding: 0;
211 }
212
213 .narrow-layout > #main:not(.iron-selected) > #scrim,
214 .dragging #scrim {
215 visibility: visible;
216 opacity: 1;
217 }
218
219 .narrow-layout > #main > * {
220 margin: 0;
221 min-height: 100%;
222 left: 0;
223 right: 0;
224 box-sizing: border-box;
225 -moz-box-sizing: border-box;
226 }
227
228 iron-selector:not(.narrow-layout) #main ::content [paper-drawer-toggle] {
229 display: none;
230 }
231 </style>
232
233 <template>
234 <iron-media-query
235 on-query-matches-changed="onQueryMatchesChanged"
236 query="[[_computeMediaQuery(forceNarrow, responsiveWidth)]]">
237 </iron-media-query>
238
239 <iron-selector
240 attr-for-selected="id"
241 class$="[[_computeIronSelectorClass(narrow, transition, dragging, rightD rawer)]]"
242 on-iron-activate="onSelect"
243 selected="[[selected]]">
244
245 <div id="main" style$="[[_computeMainStyle(narrow, rightDrawer, drawerWidt h)]]">
246 <content select="[main]"></content>
247 <div id="scrim"></div>
248 <div id="edgeSwipeOverlay"
249 hidden$="[[_computeSwipeOverlayHidden(narrow, disableEdgeSwipe)]]">
250 </div>
251 </div>
252
253 <div id="drawer" style$="[[_computeDrawerStyle(drawerWidth)]]">
254 <content select="[drawer]"></content>
255 </div>
256
257 </iron-selector>
258 </template>
259
260 </dom-module>
261
262 <script>
263
264 (function() {
265
266 'use strict';
267
268 function classNames(obj) {
269 var classNames = [];
270 for (var key in obj) {
271 if (obj.hasOwnProperty(key) && obj[key]) {
272 classNames.push(key);
273 }
274 }
275
276 return classNames.join(' ');
277 }
278
279 Polymer({
280
281 is: 'paper-drawer-panel',
282
283 /**
284 * Fired when the narrow layout changes.
285 *
286 * @event paper-responsive-change
287 * @param {Object} detail
288 * @param {boolean} detail.narrow true if the panel is in narrow layout.
289 */
290
291 /**
292 * Fired when the selected panel changes.
293 *
294 * Listening for this event is an alternative to observing changes in the `selected` attribute.
295 * This event is fired both when a panel is selected and deselected.
296 * The `isSelected` detail property contains the selection state.
297 *
298 * @event paper-select
299 * @param {Object} detail
300 * @param {boolean} detail.isSelected true for selection and false for des election
301 * @param {Object} detail.item the panel that the event refers to
302 */
303
304 properties: {
305
306 /**
307 * The panel to be selected when `paper-drawer-panel` changes to narrow
308 * layout.
309 *
310 * @attribute defaultSelected
311 * @type string
312 * @default 'main'
313 */
314 defaultSelected: {
315 type: String,
316 value: 'main'
317 },
318
319 /**
320 * If true, swipe from the edge is disable.
321 *
322 * @attribute disableEdgeSwipe
323 * @type boolean
324 * @default false
325 */
326 disableEdgeSwipe: Boolean,
327
328 /**
329 * If true, swipe to open/close the drawer is disabled.
330 *
331 * @attribute disableSwipe
332 * @type boolean
333 * @default false
334 */
335 disableSwipe: Boolean,
336
337 // Whether the user is dragging the drawer interactively.
338 dragging: {
339 value: false
340 },
341
342 /**
343 * Width of the drawer panel.
344 *
345 * @attribute drawerWidth
346 * @type string
347 * @default '256px'
348 */
349 drawerWidth: {
350 type: String,
351 value: '256px'
352 },
353
354 // How many pixels on the side of the screen are sensitive to edge
355 // swipes and peek.
356 edgeSwipeSensitivity: {
357 value: 30
358 },
359
360 /**
361 * If true, ignore `responsiveWidth` setting and force the narrow layout .
362 *
363 * @attribute forceNarrow
364 * @type boolean
365 * @default false
366 */
367 forceNarrow: {
368 observer: 'forceNarrowChanged',
369 type: Boolean,
370 value: false
371 },
372
373 // Whether the browser has support for the transform CSS property.
374 hasTransform: {
375 value: function() {
376 return 'transform' in this.style;
377 }
378 },
379
380 // Whether the browser has support for the will-change CSS property.
381 hasWillChange: {
382 value: function() {
383 return 'willChange' in this.style;
384 }
385 },
386
387 /**
388 * Returns true if the panel is in narrow layout. This is useful if you
389 * need to show/hide elements based on the layout.
390 *
391 * @attribute narrow
392 * @type boolean
393 * @default false
394 */
395 narrow: {
396 reflectToAttribute: true,
397 type: Boolean,
398 value: false
399 },
400
401 // Whether the drawer is peeking out from the edge.
402 peeking: false,
403
404 /**
405 * Max-width when the panel changes to narrow layout.
406 *
407 * @attribute responsiveWidth
408 * @type string
409 * @default '640px'
410 */
411 responsiveWidth: {
412 type: String,
413 value: '640px'
414 },
415
416 /**
417 * If true, position the drawer to the right.
418 *
419 * @attribute rightDrawer
420 * @type boolean
421 * @default false
422 */
423 rightDrawer: {
424 type: Boolean,
425 value: false
426 },
427
428 /**
429 * The panel that is being selected. `drawer` for the drawer panel and
430 * `main` for the main panel.
431 *
432 * @attribute selected
433 * @type string
434 * @default null
435 */
436 selected: {
437 reflectToAttribute: true,
438 type: String,
439 value: null
440 },
441
442 /**
443 * The attribute on elements that should toggle the drawer on tap, also elements will
444 * automatically be hidden in wide layout.
445 */
446 drawerToggleAttribute: {
447 value: 'paper-drawer-toggle'
448 },
449
450 /**
451 * Whether the transition is enabled.
452 */
453 transition: false,
454
455 /**
456 * Starting X coordinate of a tracking gesture. It is non-null only betw een trackStart and
457 * trackEnd events.
458 */
459 _startX: {
460 value: null
461 }
462
463 },
464
465 listeners: {
466 click: 'onClick',
467 track: 'onTrack'
468
469 // TODO: Implement tap handlers when taps are supported.
470 //
471 // down: 'downHandler',
472 // up: 'upHandler'
473 },
474
475 _computeIronSelectorClass: function(narrow, transition, dragging, rightDra wer) {
476 return classNames({
477 dragging: dragging,
478 'narrow-layout': narrow,
479 'right-drawer': rightDrawer,
480 transition: transition
481 });
482 },
483
484 _computeDrawerStyle: function(drawerWidth) {
485 return 'width:' + drawerWidth + ';';
486 },
487
488 _computeMainStyle: function(narrow, rightDrawer, drawerWidth) {
489 var style = '';
490
491 style += 'left:' + ((narrow || rightDrawer) ? '0' : drawerWidth) + ';'
492
493 if (rightDrawer) {
494 style += 'right:' + (narrow ? '' : drawerWidth) + ';';
495 } else {
496 style += 'right:;'
497 }
498
499 return style;
500 },
501
502 _computeMediaQuery: function(forceNarrow, responsiveWidth) {
503 return forceNarrow ? '' : '(max-width: ' + responsiveWidth + ')';
504 },
505
506 _computeSwipeOverlayHidden: function(narrow, disableEdgeSwipe) {
507 return !narrow || disableEdgeSwipe;
508 },
509
510 onTrack: function(event) {
511 switch (event.detail.state) {
512 case 'end':
513 this.trackEnd(event);
514 break;
515 case 'move':
516 this.trackX(event);
517 break;
518 case 'start':
519 this.trackStart(event);
520 break;
521 }
522 },
523
524 ready: function() {
525 // Avoid transition at the beginning e.g. page loads and enable
526 // transitions only after the element is rendered and ready.
527 this.transition = true;
528 },
529
530 /**
531 * Toggles the panel open and closed.
532 *
533 * @method togglePanel
534 */
535 togglePanel: function() {
536 if (this.isMainSelected()) {
537 this.openDrawer();
538 } else {
539 this.closeDrawer();
540 }
541 },
542
543 /**
544 * Opens the drawer.
545 *
546 * @method openDrawer
547 */
548 openDrawer: function() {
549 this.selected = 'drawer';
550 },
551
552 /**
553 * Closes the drawer.
554 *
555 * @method closeDrawer
556 */
557 closeDrawer: function() {
558 this.selected = 'main';
559 },
560
561 _responsiveChange: function(narrow) {
562 this.narrow = narrow;
563
564 if (this.narrow) {
565 this.selected = this.defaultSelected;
566 }
567
568 this.setAttribute('touch-action', this.swipeAllowed() ? 'pan-y' : '');
569 this.fire('paper-responsive-change', {narrow: this.narrow});
570 },
571
572 onQueryMatchesChanged: function(e) {
573 this._responsiveChange(e.detail.value);
574 },
575
576 forceNarrowChanged: function() {
577 this._responsiveChange(this.forceNarrow);
578 },
579
580 swipeAllowed: function() {
581 return this.narrow && !this.disableSwipe;
582 },
583
584 isMainSelected: function() {
585 return this.selected === 'main';
586 },
587
588 startEdgePeek: function() {
589 this.width = this.$.drawer.offsetWidth;
590 this.moveDrawer(this.translateXForDeltaX(this.rightDrawer ?
591 -this.edgeSwipeSensitivity : this.edgeSwipeSensitivity));
592 this.peeking = true;
593 },
594
595 stopEdgePeek: function() {
596 if (this.peeking) {
597 this.peeking = false;
598 this.moveDrawer(null);
599 }
600 },
601
602 // TODO: Implement tap handlers when taps are supported.
603 //
604 // downHandler: function(e) {
605 // if (!this.dragging && this.isMainSelected() && this.isEdgeTouch(e)) {
606 // this.startEdgePeek();
607 // }
608 // },
609 //
610 // upHandler: function(e) {
611 // this.stopEdgePeek();
612 // },
613
614 onClick: function(e) {
615 var isTargetToggleElement = e.target &&
616 this.drawerToggleAttribute &&
617 e.target.hasAttribute(this.drawerToggleAttribute);
618
619 if (isTargetToggleElement) {
620 this.togglePanel();
621 }
622 },
623
624 isEdgeTouch: function(event) {
625 var x = event.detail.x;
626
627 return !this.disableEdgeSwipe && this.swipeAllowed() &&
628 (this.rightDrawer ?
629 x >= this.offsetWidth - this.edgeSwipeSensitivity :
630 x <= this.edgeSwipeSensitivity);
631 },
632
633 trackStart: function(event) {
634 if (this.swipeAllowed()) {
635 this.dragging = true;
636 this._startX = event.detail.x;
637
638 if (this.isMainSelected()) {
639 this.dragging = this.peeking || this.isEdgeTouch(event);
640 }
641
642 if (this.dragging) {
643 this.width = this.$.drawer.offsetWidth;
644 this.transition = false;
645
646 // TODO: Re-enable when tap gestures are implemented.
647 //
648 // e.preventTap();
649 }
650 }
651 },
652
653 translateXForDeltaX: function(deltaX) {
654 var isMain = this.isMainSelected();
655
656 if (this.rightDrawer) {
657 return Math.max(0, isMain ? this.width + deltaX : deltaX);
658 } else {
659 return Math.min(0, isMain ? deltaX - this.width : deltaX);
660 }
661 },
662
663 trackX: function(event) {
664 var dx = event.detail.x - this._startX;
665
666 if (this.dragging) {
667 if (this.peeking) {
668 if (Math.abs(dx) <= this.edgeSwipeSensitivity) {
669 // Ignore trackx until we move past the edge peek.
670 return;
671 }
672
673 this.peeking = false;
674 }
675
676 this.moveDrawer(this.translateXForDeltaX(dx));
677 }
678 },
679
680 trackEnd: function(event) {
681 if (this.dragging) {
682 var xDirection = (event.detail.x - this._startX) > 0;
683
684 this.dragging = false;
685 this._startX = null;
686 this.transition = true;
687 this.moveDrawer(null);
688
689 if (this.rightDrawer) {
690 this[(xDirection > 0) ? 'closeDrawer' : 'openDrawer']();
691 } else {
692 this[(xDirection > 0) ? 'openDrawer' : 'closeDrawer']();
693 }
694 }
695 },
696
697 transformForTranslateX: function(translateX) {
698 if (translateX === null) {
699 return '';
700 }
701
702 return this.hasWillChange ? 'translateX(' + translateX + 'px)' :
703 'translate3d(' + translateX + 'px, 0, 0)';
704 },
705
706 moveDrawer: function(translateX) {
707 var s = this.$.drawer.style;
708
709 if (this.hasTransform) {
710 s.transform = this.transformForTranslateX(translateX);
711 } else {
712 s.webkitTransform = this.transformForTranslateX(translateX);
713 }
714 },
715
716 onSelect: function(e) {
717 e.preventDefault();
718 this.selected = e.detail.selected;
719 }
720
721 });
722
723 }());
724
725 </script>
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698