| 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 }, | |
| 215 | |
| 216 /** | |
| 217 * Width of the drawer panel. | |
| 218 */ | |
| 219 drawerWidth: { | |
| 220 type: String, | |
| 221 value: '256px' | |
| 222 }, | |
| 223 | |
| 224 /** | |
| 225 * How many pixels on the side of the screen are sensitive to edge | |
| 226 * swipes and peek. | |
| 227 */ | |
| 228 edgeSwipeSensitivity: { | |
| 229 type: Number, | |
| 230 value: 30 | |
| 231 }, | |
| 232 | |
| 233 /** | |
| 234 * If true, ignore `responsiveWidth` setting and force the narrow layout
. | |
| 235 */ | |
| 236 forceNarrow: { | |
| 237 type: Boolean, | |
| 238 value: false | |
| 239 }, | |
| 240 | |
| 241 /** | |
| 242 * Whether the browser has support for the transform CSS property. | |
| 243 */ | |
| 244 hasTransform: { | |
| 245 type: Boolean, | |
| 246 value: function() { | |
| 247 return 'transform' in this.style; | |
| 248 } | |
| 249 }, | |
| 250 | |
| 251 /** | |
| 252 * Whether the browser has support for the will-change CSS property. | |
| 253 */ | |
| 254 hasWillChange: { | |
| 255 type: Boolean, | |
| 256 value: function() { | |
| 257 return 'willChange' in this.style; | |
| 258 } | |
| 259 }, | |
| 260 | |
| 261 /** | |
| 262 * Returns true if the panel is in narrow layout. This is useful if you | |
| 263 * need to show/hide elements based on the layout. | |
| 264 */ | |
| 265 narrow: { | |
| 266 reflectToAttribute: true, | |
| 267 type: Boolean, | |
| 268 value: false, | |
| 269 notify: true | |
| 270 }, | |
| 271 | |
| 272 /** | |
| 273 * Whether the drawer is peeking out from the edge. | |
| 274 */ | |
| 275 peeking: { | |
| 276 type: Boolean, | |
| 277 value: false | |
| 278 }, | |
| 279 | |
| 280 /** | |
| 281 * Max-width when the panel changes to narrow layout. | |
| 282 */ | |
| 283 responsiveWidth: { | |
| 284 type: String, | |
| 285 value: '640px' | |
| 286 }, | |
| 287 | |
| 288 /** | |
| 289 * If true, position the drawer to the right. | |
| 290 */ | |
| 291 rightDrawer: { | |
| 292 type: Boolean, | |
| 293 value: false | |
| 294 }, | |
| 295 | |
| 296 /** | |
| 297 * The panel that is being selected. `drawer` for the drawer panel and | |
| 298 * `main` for the main panel. | |
| 299 */ | |
| 300 selected: { | |
| 301 reflectToAttribute: true, | |
| 302 type: String, | |
| 303 value: null | |
| 304 }, | |
| 305 | |
| 306 /** | |
| 307 * The attribute on elements that should toggle the drawer on tap, also
elements will | |
| 308 * automatically be hidden in wide layout. | |
| 309 */ | |
| 310 drawerToggleAttribute: { | |
| 311 type: String, | |
| 312 value: 'paper-drawer-toggle' | |
| 313 }, | |
| 314 | |
| 315 /** | |
| 316 * Whether the transition is enabled. | |
| 317 */ | |
| 318 transition: { | |
| 319 type: Boolean, | |
| 320 value: false | |
| 321 }, | |
| 322 | |
| 323 }, | |
| 324 | |
| 325 listeners: { | |
| 326 tap: '_onTap', | |
| 327 track: '_onTrack', | |
| 328 down: '_downHandler', | |
| 329 up: '_upHandler' | |
| 330 }, | |
| 331 | |
| 332 observers: [ | |
| 333 '_forceNarrowChanged(forceNarrow, defaultSelected)' | |
| 334 ], | |
| 335 | |
| 336 /** | |
| 337 * Toggles the panel open and closed. | |
| 338 * | |
| 339 * @method togglePanel | |
| 340 */ | |
| 341 togglePanel: function() { | |
| 342 if (this._isMainSelected()) { | |
| 343 this.openDrawer(); | |
| 344 } else { | |
| 345 this.closeDrawer(); | |
| 346 } | |
| 347 }, | |
| 348 | |
| 349 /** | |
| 350 * Opens the drawer. | |
| 351 * | |
| 352 * @method openDrawer | |
| 353 */ | |
| 354 openDrawer: function() { | |
| 355 this.selected = 'drawer'; | |
| 356 }, | |
| 357 | |
| 358 /** | |
| 359 * Closes the drawer. | |
| 360 * | |
| 361 * @method closeDrawer | |
| 362 */ | |
| 363 closeDrawer: function() { | |
| 364 this.selected = 'main'; | |
| 365 }, | |
| 366 | |
| 367 ready: function() { | |
| 368 // Avoid transition at the beginning e.g. page loads and enable | |
| 369 // transitions only after the element is rendered and ready. | |
| 370 this.transition = true; | |
| 371 }, | |
| 372 | |
| 373 _computeIronSelectorClass: function(narrow, transition, dragging, rightDra
wer) { | |
| 374 return classNames({ | |
| 375 dragging: dragging, | |
| 376 'narrow-layout': narrow, | |
| 377 'right-drawer': rightDrawer, | |
| 378 'left-drawer': !rightDrawer, | |
| 379 transition: transition | |
| 380 }); | |
| 381 }, | |
| 382 | |
| 383 _computeDrawerStyle: function(drawerWidth) { | |
| 384 return 'width:' + drawerWidth + ';'; | |
| 385 }, | |
| 386 | |
| 387 _computeMainStyle: function(narrow, rightDrawer, drawerWidth) { | |
| 388 var style = ''; | |
| 389 | |
| 390 style += 'left:' + ((narrow || rightDrawer) ? '0' : drawerWidth) + ';'; | |
| 391 | |
| 392 if (rightDrawer) { | |
| 393 style += 'right:' + (narrow ? '' : drawerWidth) + ';'; | |
| 394 } else { | |
| 395 style += 'right:;'; | |
| 396 } | |
| 397 | |
| 398 return style; | |
| 399 }, | |
| 400 | |
| 401 _computeMediaQuery: function(forceNarrow, responsiveWidth) { | |
| 402 return forceNarrow ? '' : '(max-width: ' + responsiveWidth + ')'; | |
| 403 }, | |
| 404 | |
| 405 _computeSwipeOverlayHidden: function(narrow, disableEdgeSwipe) { | |
| 406 return !narrow || disableEdgeSwipe; | |
| 407 }, | |
| 408 | |
| 409 _onTrack: function(e) { | |
| 410 if (sharedPanel && this !== sharedPanel) { | |
| 411 return; | |
| 412 } | |
| 413 switch (e.detail.state) { | |
| 414 case 'start': | |
| 415 this._trackStart(e); | |
| 416 break; | |
| 417 case 'track': | |
| 418 this._trackX(e); | |
| 419 break; | |
| 420 case 'end': | |
| 421 this._trackEnd(e); | |
| 422 break; | |
| 423 } | |
| 424 | |
| 425 }, | |
| 426 | |
| 427 _responsiveChange: function(narrow) { | |
| 428 this.narrow = narrow; | |
| 429 | |
| 430 if (this.narrow) { | |
| 431 this.selected = this.defaultSelected; | |
| 432 } | |
| 433 | |
| 434 this.setScrollDirection(this._swipeAllowed() ? 'y' : 'all'); | |
| 435 this.fire('paper-responsive-change', {narrow: this.narrow}); | |
| 436 }, | |
| 437 | |
| 438 _onQueryMatchesChanged: function(e) { | |
| 439 this._responsiveChange(e.detail.value); | |
| 440 }, | |
| 441 | |
| 442 _forceNarrowChanged: function() { | |
| 443 // set the narrow mode only if we reached the `responsiveWidth` | |
| 444 this._responsiveChange(this.forceNarrow || this.$.mq.queryMatches); | |
| 445 }, | |
| 446 | |
| 447 _swipeAllowed: function() { | |
| 448 return this.narrow && !this.disableSwipe; | |
| 449 }, | |
| 450 | |
| 451 _isMainSelected: function() { | |
| 452 return this.selected === 'main'; | |
| 453 }, | |
| 454 | |
| 455 _startEdgePeek: function() { | |
| 456 this.width = this.$.drawer.offsetWidth; | |
| 457 this._moveDrawer(this._translateXForDeltaX(this.rightDrawer ? | |
| 458 -this.edgeSwipeSensitivity : this.edgeSwipeSensitivity)); | |
| 459 this.peeking = true; | |
| 460 }, | |
| 461 | |
| 462 _stopEdgePeek: function() { | |
| 463 if (this.peeking) { | |
| 464 this.peeking = false; | |
| 465 this._moveDrawer(null); | |
| 466 } | |
| 467 }, | |
| 468 | |
| 469 _downHandler: function(e) { | |
| 470 if (!this.dragging && this._isMainSelected() && this._isEdgeTouch(e) &&
!sharedPanel) { | |
| 471 this._startEdgePeek(); | |
| 472 // grab this panel | |
| 473 sharedPanel = this; | |
| 474 } | |
| 475 }, | |
| 476 | |
| 477 _upHandler: function() { | |
| 478 this._stopEdgePeek(); | |
| 479 // release the panel | |
| 480 sharedPanel = null; | |
| 481 }, | |
| 482 | |
| 483 _onTap: function(e) { | |
| 484 var targetElement = Polymer.dom(e).localTarget; | |
| 485 var isTargetToggleElement = targetElement && | |
| 486 this.drawerToggleAttribute && | |
| 487 targetElement.hasAttribute(this.drawerToggleAttribute); | |
| 488 | |
| 489 if (isTargetToggleElement) { | |
| 490 this.togglePanel(); | |
| 491 } | |
| 492 }, | |
| 493 | |
| 494 _isEdgeTouch: function(e) { | |
| 495 var x = e.detail.x; | |
| 496 | |
| 497 return !this.disableEdgeSwipe && this._swipeAllowed() && | |
| 498 (this.rightDrawer ? | |
| 499 x >= this.offsetWidth - this.edgeSwipeSensitivity : | |
| 500 x <= this.edgeSwipeSensitivity); | |
| 501 }, | |
| 502 | |
| 503 _trackStart: function() { | |
| 504 if (this._swipeAllowed()) { | |
| 505 sharedPanel = this; | |
| 506 this.dragging = true; | |
| 507 | |
| 508 if (this._isMainSelected()) { | |
| 509 this.dragging = this.peeking || this._isEdgeTouch(event); | |
| 510 } | |
| 511 | |
| 512 if (this.dragging) { | |
| 513 this.width = this.$.drawer.offsetWidth; | |
| 514 this.transition = false; | |
| 515 } | |
| 516 } | |
| 517 }, | |
| 518 | |
| 519 _translateXForDeltaX: function(deltaX) { | |
| 520 var isMain = this._isMainSelected(); | |
| 521 | |
| 522 if (this.rightDrawer) { | |
| 523 return Math.max(0, isMain ? this.width + deltaX : deltaX); | |
| 524 } else { | |
| 525 return Math.min(0, isMain ? deltaX - this.width : deltaX); | |
| 526 } | |
| 527 }, | |
| 528 | |
| 529 _trackX: function(e) { | |
| 530 if (this.dragging) { | |
| 531 var dx = e.detail.dx; | |
| 532 | |
| 533 if (this.peeking) { | |
| 534 if (Math.abs(dx) <= this.edgeSwipeSensitivity) { | |
| 535 // Ignore trackx until we move past the edge peek. | |
| 536 return; | |
| 537 } | |
| 538 this.peeking = false; | |
| 539 } | |
| 540 | |
| 541 this._moveDrawer(this._translateXForDeltaX(dx)); | |
| 542 } | |
| 543 }, | |
| 544 | |
| 545 _trackEnd: function(e) { | |
| 546 if (this.dragging) { | |
| 547 var xDirection = e.detail.dx > 0; | |
| 548 | |
| 549 this.dragging = false; | |
| 550 this.transition = true; | |
| 551 sharedPanel = null; | |
| 552 this._moveDrawer(null); | |
| 553 | |
| 554 if (this.rightDrawer) { | |
| 555 this[xDirection ? 'closeDrawer' : 'openDrawer'](); | |
| 556 } else { | |
| 557 this[xDirection ? 'openDrawer' : 'closeDrawer'](); | |
| 558 } | |
| 559 } | |
| 560 }, | |
| 561 | |
| 562 _transformForTranslateX: function(translateX) { | |
| 563 if (translateX === null) { | |
| 564 return ''; | |
| 565 } | |
| 566 | |
| 567 return this.hasWillChange ? 'translateX(' + translateX + 'px)' : | |
| 568 'translate3d(' + translateX + 'px, 0, 0)'; | |
| 569 }, | |
| 570 | |
| 571 _moveDrawer: function(translateX) { | |
| 572 var s = this.$.drawer.style; | |
| 573 | |
| 574 if (this.hasTransform) { | |
| 575 s.transform = this._transformForTranslateX(translateX); | |
| 576 } else { | |
| 577 s.webkitTransform = this._transformForTranslateX(translateX); | |
| 578 } | |
| 579 } | |
| 580 | |
| 581 }); | |
| 582 | |
| 583 }()); | |
| 584 | |
| 585 </script> | |
| OLD | NEW |