OLD | NEW |
(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 `right-drawer` attribute. |
| 71 |
| 72 <paper-drawer-panel right-drawer> |
| 73 <div drawer> Drawer panel... </div> |
| 74 <div main> Main panel... </div> |
| 75 </paper-drawer-panel> |
| 76 |
| 77 Styling paper-drawer-panel: |
| 78 |
| 79 To change the main container: |
| 80 paper-drawer-panel { |
| 81 --paper-drawer-panel-main-container: { |
| 82 background-color: gray; |
| 83 }; |
| 84 } |
| 85 |
| 86 To change the drawer container when it's in the left side: |
| 87 paper-drawer-panel { |
| 88 --paper-drawer-panel-left-drawer-container: { |
| 89 background-color: white; |
| 90 }; |
| 91 } |
| 92 |
| 93 To change the drawer container when it's in the right side: |
| 94 |
| 95 paper-drawer-panel { |
| 96 --paper-drawer-panel-right-drawer-container: { |
| 97 background-color: white; |
| 98 }; |
| 99 } |
| 100 |
| 101 @group Paper elements |
| 102 @element paper-drawer-panel |
| 103 @demo demo/index.html |
| 104 @hero hero.svg |
| 105 --> |
| 106 |
| 107 <dom-module id="paper-drawer-panel"> |
| 108 <link rel="import" type="css" href="paper-drawer-panel.css"> |
| 109 |
| 110 <template> |
| 111 <iron-media-query |
| 112 id="mq" |
| 113 on-query-matches-changed="_onQueryMatchesChanged" |
| 114 query="[[_computeMediaQuery(forceNarrow, responsiveWidth)]]"> |
| 115 </iron-media-query> |
| 116 |
| 117 <iron-selector |
| 118 attr-for-selected="id" |
| 119 class$="[[_computeIronSelectorClass(narrow, transition, dragging, rightD
rawer)]]" |
| 120 activate-event="" |
| 121 selected="[[selected]]"> |
| 122 |
| 123 <div id="main" style$="[[_computeMainStyle(narrow, rightDrawer, drawerWidt
h)]]"> |
| 124 <content select="[main]"></content> |
| 125 <div id="scrim" on-tap="closeDrawer"></div> |
| 126 </div> |
| 127 |
| 128 <div id="drawer" style$="[[_computeDrawerStyle(drawerWidth)]]"> |
| 129 <content select="[drawer]"></content> |
| 130 </div> |
| 131 |
| 132 </iron-selector> |
| 133 </template> |
| 134 |
| 135 </dom-module> |
| 136 |
| 137 <script> |
| 138 |
| 139 (function() { |
| 140 |
| 141 'use strict'; |
| 142 |
| 143 // this would be the only `paper-drawer-panel` in |
| 144 // the whole app that can be in `dragging` state |
| 145 var sharedPanel = null; |
| 146 |
| 147 function classNames(obj) { |
| 148 var classes = []; |
| 149 for (var key in obj) { |
| 150 if (obj.hasOwnProperty(key) && obj[key]) { |
| 151 classes.push(key); |
| 152 } |
| 153 } |
| 154 |
| 155 return classes.join(' '); |
| 156 } |
| 157 |
| 158 Polymer({ |
| 159 |
| 160 is: 'paper-drawer-panel', |
| 161 |
| 162 /** |
| 163 * Fired when the narrow layout changes. |
| 164 * |
| 165 * @event paper-responsive-change {{narrow: boolean}} detail - |
| 166 * narrow: true if the panel is in narrow layout. |
| 167 */ |
| 168 |
| 169 /** |
| 170 * Fired when the selected panel changes. |
| 171 * |
| 172 * Listening for this event is an alternative to observing changes in the
`selected` attribute. |
| 173 * This event is fired both when a panel is selected and deselected. |
| 174 * The `isSelected` detail property contains the selection state. |
| 175 * |
| 176 * @event paper-select {{isSelected: boolean, item: Object}} detail - |
| 177 * isSelected: True for selection and false for deselection. |
| 178 * item: The panel that the event refers to. |
| 179 */ |
| 180 |
| 181 properties: { |
| 182 |
| 183 /** |
| 184 * The panel to be selected when `paper-drawer-panel` changes to narrow |
| 185 * layout. |
| 186 */ |
| 187 defaultSelected: { |
| 188 type: String, |
| 189 value: 'main' |
| 190 }, |
| 191 |
| 192 /** |
| 193 * If true, swipe from the edge is disable. |
| 194 */ |
| 195 disableEdgeSwipe: { |
| 196 type: Boolean, |
| 197 value: false |
| 198 }, |
| 199 |
| 200 /** |
| 201 * If true, swipe to open/close the drawer is disabled. |
| 202 */ |
| 203 disableSwipe: { |
| 204 type: Boolean, |
| 205 value: false |
| 206 }, |
| 207 |
| 208 /** |
| 209 * Whether the user is dragging the drawer interactively. |
| 210 */ |
| 211 dragging: { |
| 212 type: Boolean, |
| 213 value: false, |
| 214 readOnly: true, |
| 215 notify: true |
| 216 }, |
| 217 |
| 218 /** |
| 219 * Width of the drawer panel. |
| 220 */ |
| 221 drawerWidth: { |
| 222 type: String, |
| 223 value: '256px' |
| 224 }, |
| 225 |
| 226 /** |
| 227 * How many pixels on the side of the screen are sensitive to edge |
| 228 * swipes and peek. |
| 229 */ |
| 230 edgeSwipeSensitivity: { |
| 231 type: Number, |
| 232 value: 30 |
| 233 }, |
| 234 |
| 235 /** |
| 236 * If true, ignore `responsiveWidth` setting and force the narrow layout
. |
| 237 */ |
| 238 forceNarrow: { |
| 239 type: Boolean, |
| 240 value: false |
| 241 }, |
| 242 |
| 243 /** |
| 244 * Whether the browser has support for the transform CSS property. |
| 245 */ |
| 246 hasTransform: { |
| 247 type: Boolean, |
| 248 value: function() { |
| 249 return 'transform' in this.style; |
| 250 } |
| 251 }, |
| 252 |
| 253 /** |
| 254 * Whether the browser has support for the will-change CSS property. |
| 255 */ |
| 256 hasWillChange: { |
| 257 type: Boolean, |
| 258 value: function() { |
| 259 return 'willChange' in this.style; |
| 260 } |
| 261 }, |
| 262 |
| 263 /** |
| 264 * Returns true if the panel is in narrow layout. This is useful if you |
| 265 * need to show/hide elements based on the layout. |
| 266 */ |
| 267 narrow: { |
| 268 reflectToAttribute: true, |
| 269 type: Boolean, |
| 270 value: false, |
| 271 readOnly: true, |
| 272 notify: true |
| 273 }, |
| 274 |
| 275 /** |
| 276 * Whether the drawer is peeking out from the edge. |
| 277 */ |
| 278 peeking: { |
| 279 type: Boolean, |
| 280 value: false, |
| 281 readOnly: true, |
| 282 notify: true |
| 283 }, |
| 284 |
| 285 /** |
| 286 * Max-width when the panel changes to narrow layout. |
| 287 */ |
| 288 responsiveWidth: { |
| 289 type: String, |
| 290 value: '640px' |
| 291 }, |
| 292 |
| 293 /** |
| 294 * If true, position the drawer to the right. |
| 295 */ |
| 296 rightDrawer: { |
| 297 type: Boolean, |
| 298 value: false |
| 299 }, |
| 300 |
| 301 /** |
| 302 * The panel that is being selected. `drawer` for the drawer panel and |
| 303 * `main` for the main panel. |
| 304 */ |
| 305 selected: { |
| 306 reflectToAttribute: true, |
| 307 notify: true, |
| 308 type: String, |
| 309 value: null |
| 310 }, |
| 311 |
| 312 /** |
| 313 * The attribute on elements that should toggle the drawer on tap, also
elements will |
| 314 * automatically be hidden in wide layout. |
| 315 */ |
| 316 drawerToggleAttribute: { |
| 317 type: String, |
| 318 value: 'paper-drawer-toggle' |
| 319 }, |
| 320 |
| 321 /** |
| 322 * Whether the transition is enabled. |
| 323 */ |
| 324 transition: { |
| 325 type: Boolean, |
| 326 value: false |
| 327 }, |
| 328 |
| 329 }, |
| 330 |
| 331 listeners: { |
| 332 tap: '_onTap', |
| 333 track: '_onTrack', |
| 334 down: '_downHandler', |
| 335 up: '_upHandler' |
| 336 }, |
| 337 |
| 338 observers: [ |
| 339 '_forceNarrowChanged(forceNarrow, defaultSelected)' |
| 340 ], |
| 341 |
| 342 /** |
| 343 * Toggles the panel open and closed. |
| 344 * |
| 345 * @method togglePanel |
| 346 */ |
| 347 togglePanel: function() { |
| 348 if (this._isMainSelected()) { |
| 349 this.openDrawer(); |
| 350 } else { |
| 351 this.closeDrawer(); |
| 352 } |
| 353 }, |
| 354 |
| 355 /** |
| 356 * Opens the drawer. |
| 357 * |
| 358 * @method openDrawer |
| 359 */ |
| 360 openDrawer: function() { |
| 361 this.selected = 'drawer'; |
| 362 }, |
| 363 |
| 364 /** |
| 365 * Closes the drawer. |
| 366 * |
| 367 * @method closeDrawer |
| 368 */ |
| 369 closeDrawer: function() { |
| 370 this.selected = 'main'; |
| 371 }, |
| 372 |
| 373 ready: function() { |
| 374 // Avoid transition at the beginning e.g. page loads and enable |
| 375 // transitions only after the element is rendered and ready. |
| 376 this.transition = true; |
| 377 }, |
| 378 |
| 379 _computeIronSelectorClass: function(narrow, transition, dragging, rightDra
wer) { |
| 380 return classNames({ |
| 381 dragging: dragging, |
| 382 'narrow-layout': narrow, |
| 383 'right-drawer': rightDrawer, |
| 384 'left-drawer': !rightDrawer, |
| 385 transition: transition |
| 386 }); |
| 387 }, |
| 388 |
| 389 _computeDrawerStyle: function(drawerWidth) { |
| 390 return 'width:' + drawerWidth + ';'; |
| 391 }, |
| 392 |
| 393 _computeMainStyle: function(narrow, rightDrawer, drawerWidth) { |
| 394 var style = ''; |
| 395 |
| 396 style += 'left:' + ((narrow || rightDrawer) ? '0' : drawerWidth) + ';'; |
| 397 |
| 398 if (rightDrawer) { |
| 399 style += 'right:' + (narrow ? '' : drawerWidth) + ';'; |
| 400 } else { |
| 401 style += 'right:;'; |
| 402 } |
| 403 |
| 404 return style; |
| 405 }, |
| 406 |
| 407 _computeMediaQuery: function(forceNarrow, responsiveWidth) { |
| 408 return forceNarrow ? '' : '(max-width: ' + responsiveWidth + ')'; |
| 409 }, |
| 410 |
| 411 _computeSwipeOverlayHidden: function(narrow, disableEdgeSwipe) { |
| 412 return !narrow || disableEdgeSwipe; |
| 413 }, |
| 414 |
| 415 _onTrack: function(e) { |
| 416 if (sharedPanel && this !== sharedPanel) { |
| 417 return; |
| 418 } |
| 419 switch (e.detail.state) { |
| 420 case 'start': |
| 421 this._trackStart(e); |
| 422 break; |
| 423 case 'track': |
| 424 this._trackX(e); |
| 425 break; |
| 426 case 'end': |
| 427 this._trackEnd(e); |
| 428 break; |
| 429 } |
| 430 |
| 431 }, |
| 432 |
| 433 _responsiveChange: function(narrow) { |
| 434 this._setNarrow(narrow); |
| 435 |
| 436 if (this.narrow) { |
| 437 this.selected = this.defaultSelected; |
| 438 } |
| 439 |
| 440 this.setScrollDirection(this._swipeAllowed() ? 'y' : 'all'); |
| 441 this.fire('paper-responsive-change', {narrow: this.narrow}); |
| 442 }, |
| 443 |
| 444 _onQueryMatchesChanged: function(e) { |
| 445 this._responsiveChange(e.detail.value); |
| 446 }, |
| 447 |
| 448 _forceNarrowChanged: function() { |
| 449 // set the narrow mode only if we reached the `responsiveWidth` |
| 450 this._responsiveChange(this.forceNarrow || this.$.mq.queryMatches); |
| 451 }, |
| 452 |
| 453 _swipeAllowed: function() { |
| 454 return this.narrow && !this.disableSwipe; |
| 455 }, |
| 456 |
| 457 _isMainSelected: function() { |
| 458 return this.selected === 'main'; |
| 459 }, |
| 460 |
| 461 _startEdgePeek: function() { |
| 462 this.width = this.$.drawer.offsetWidth; |
| 463 this._moveDrawer(this._translateXForDeltaX(this.rightDrawer ? |
| 464 -this.edgeSwipeSensitivity : this.edgeSwipeSensitivity)); |
| 465 this._setPeeking(true); |
| 466 }, |
| 467 |
| 468 _stopEdgePeek: function() { |
| 469 if (this.peeking) { |
| 470 this._setPeeking(false); |
| 471 this._moveDrawer(null); |
| 472 } |
| 473 }, |
| 474 |
| 475 _downHandler: function(e) { |
| 476 if (!this.dragging && this._isMainSelected() && this._isEdgeTouch(e) &&
!sharedPanel) { |
| 477 this._startEdgePeek(); |
| 478 // grab this panel |
| 479 sharedPanel = this; |
| 480 } |
| 481 }, |
| 482 |
| 483 _upHandler: function() { |
| 484 this._stopEdgePeek(); |
| 485 // release the panel |
| 486 sharedPanel = null; |
| 487 }, |
| 488 |
| 489 _onTap: function(e) { |
| 490 var targetElement = Polymer.dom(e).localTarget; |
| 491 var isTargetToggleElement = targetElement && |
| 492 this.drawerToggleAttribute && |
| 493 targetElement.hasAttribute(this.drawerToggleAttribute); |
| 494 |
| 495 if (isTargetToggleElement) { |
| 496 this.togglePanel(); |
| 497 } |
| 498 }, |
| 499 |
| 500 _isEdgeTouch: function(e) { |
| 501 var x = e.detail.x; |
| 502 |
| 503 return !this.disableEdgeSwipe && this._swipeAllowed() && |
| 504 (this.rightDrawer ? |
| 505 x >= this.offsetWidth - this.edgeSwipeSensitivity : |
| 506 x <= this.edgeSwipeSensitivity); |
| 507 }, |
| 508 |
| 509 _trackStart: function(event) { |
| 510 if (this._swipeAllowed()) { |
| 511 sharedPanel = this; |
| 512 this._setDragging(true); |
| 513 |
| 514 if (this._isMainSelected()) { |
| 515 this._setDragging(this.peeking || this._isEdgeTouch(event)); |
| 516 } |
| 517 |
| 518 if (this.dragging) { |
| 519 this.width = this.$.drawer.offsetWidth; |
| 520 this.transition = false; |
| 521 } |
| 522 } |
| 523 }, |
| 524 |
| 525 _translateXForDeltaX: function(deltaX) { |
| 526 var isMain = this._isMainSelected(); |
| 527 |
| 528 if (this.rightDrawer) { |
| 529 return Math.max(0, isMain ? this.width + deltaX : deltaX); |
| 530 } else { |
| 531 return Math.min(0, isMain ? deltaX - this.width : deltaX); |
| 532 } |
| 533 }, |
| 534 |
| 535 _trackX: function(e) { |
| 536 if (this.dragging) { |
| 537 var dx = e.detail.dx; |
| 538 |
| 539 if (this.peeking) { |
| 540 if (Math.abs(dx) <= this.edgeSwipeSensitivity) { |
| 541 // Ignore trackx until we move past the edge peek. |
| 542 return; |
| 543 } |
| 544 this._setPeeking(false); |
| 545 } |
| 546 |
| 547 this._moveDrawer(this._translateXForDeltaX(dx)); |
| 548 } |
| 549 }, |
| 550 |
| 551 _trackEnd: function(e) { |
| 552 if (this.dragging) { |
| 553 var xDirection = e.detail.dx > 0; |
| 554 |
| 555 this._setDragging(false); |
| 556 this.transition = true; |
| 557 sharedPanel = null; |
| 558 this._moveDrawer(null); |
| 559 |
| 560 if (this.rightDrawer) { |
| 561 this[xDirection ? 'closeDrawer' : 'openDrawer'](); |
| 562 } else { |
| 563 this[xDirection ? 'openDrawer' : 'closeDrawer'](); |
| 564 } |
| 565 } |
| 566 }, |
| 567 |
| 568 _transformForTranslateX: function(translateX) { |
| 569 if (translateX === null) { |
| 570 return ''; |
| 571 } |
| 572 |
| 573 return this.hasWillChange ? 'translateX(' + translateX + 'px)' : |
| 574 'translate3d(' + translateX + 'px, 0, 0)'; |
| 575 }, |
| 576 |
| 577 _moveDrawer: function(translateX) { |
| 578 var s = this.$.drawer.style; |
| 579 |
| 580 if (this.hasTransform) { |
| 581 s.transform = this._transformForTranslateX(translateX); |
| 582 } else { |
| 583 s.webkitTransform = this._transformForTranslateX(translateX); |
| 584 } |
| 585 } |
| 586 |
| 587 }); |
| 588 |
| 589 }()); |
| 590 |
| 591 </script> |
OLD | NEW |