| OLD | NEW |
| 1 <!-- | 1 <!-- |
| 2 Copyright (c) 2014 The Polymer Project Authors. All rights reserved. | 2 Copyright (c) 2014 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 | 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 | 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 | 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 | 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 | 7 subject to an additional IP rights grant found at http://polymer.github.io/PATEN
TS.txt |
| 8 --> | 8 --> |
| 9 | 9 |
| 10 <!-- | 10 <!-- |
| 11 `core-drawer-panel` contains a drawer panel and a main panel. The drawer | 11 `core-drawer-panel` contains a drawer panel and a main panel. The drawer |
| 12 and the main panel are side-by-side with drawer on the left. When browser | 12 and the main panel are side-by-side with drawer on the left. When the browser |
| 13 window size is smaller than the `responsiveWidth`, `core-drawer-panel` | 13 window size is smaller than the `responsiveWidth`, `core-drawer-panel` |
| 14 changes to narrow layout. In narrow layout, the drawer will be stacked on top | 14 changes to narrow layout. In narrow layout, the drawer will be stacked on top |
| 15 of the main panel. The drawer will be slided in/out to hide/reveal the main | 15 of the main panel. The drawer will slide in/out to hide/reveal the main |
| 16 panel. | 16 panel. |
| 17 | 17 |
| 18 Use the attribute `drawer` to indicate the element is a drawer panel and | 18 Use the attribute `drawer` to indicate that the element is the drawer panel and |
| 19 `main` to indicate is a main panel. | 19 `main` to indicate that the element is the main panel. |
| 20 | 20 |
| 21 Example: | 21 Example: |
| 22 | 22 |
| 23 <core-drawer-panel> | 23 <core-drawer-panel> |
| 24 <div drawer> Drawer panel... </div> | 24 <div drawer> Drawer panel... </div> |
| 25 <div main> Main panel... </div> | 25 <div main> Main panel... </div> |
| 26 </core-drawer-panel> | 26 </core-drawer-panel> |
| 27 | 27 |
| 28 The drawer and the main panels are not scrollable. You can set CSS overflow | 28 The drawer and the main panels are not scrollable. You can set CSS overflow |
| 29 property on the elements to make them scrollable or use `core-header-panel`. | 29 property on the elements to make them scrollable or use `core-header-panel`. |
| 30 | 30 |
| 31 Example: | 31 Example: |
| 32 | 32 |
| 33 <core-drawer-panel> | 33 <core-drawer-panel> |
| 34 <core-header-panel drawer> | 34 <core-header-panel drawer> |
| 35 <core-toolbar></core-toolbar> | 35 <core-toolbar></core-toolbar> |
| 36 <div> Drawer content... </div> | 36 <div> Drawer content... </div> |
| 37 </core-header-panel> | 37 </core-header-panel> |
| 38 <core-header-panel main> | 38 <core-header-panel main> |
| 39 <core-toolbar></core-toolbar> | 39 <core-toolbar></core-toolbar> |
| 40 <div> Main content... </div> | 40 <div> Main content... </div> |
| 41 </core-header-panel> | 41 </core-header-panel> |
| 42 </core-drawer-panel> | 42 </core-drawer-panel> |
| 43 | 43 |
| 44 An element that should toggle the drawer will automatically do so if it's |
| 45 given the `core-drawer-toggle` attribute. Also this element will automatically |
| 46 be hidden in wide layout. |
| 47 |
| 48 Example: |
| 49 |
| 50 <core-drawer-panel> |
| 51 <core-header-panel drawer> |
| 52 <core-toolbar> |
| 53 <div>Application</div> |
| 54 </core-toolbar> |
| 55 <div> Drawer content... </div> |
| 56 </core-header-panel> |
| 57 <core-header-panel main> |
| 58 <core-toolbar> |
| 59 <core-icon-button icon="menu" core-drawer-toggle></core-icon-button> |
| 60 <div>Title</div> |
| 61 </core-toolbar> |
| 62 <div> Main content... </div> |
| 63 </core-header-panel> |
| 64 </core-drawer-panel> |
| 65 |
| 44 To position the drawer to the right, add `rightDrawer` attribute. | 66 To position the drawer to the right, add `rightDrawer` attribute. |
| 45 | 67 |
| 46 <core-drawer-panel rightDrawer> | 68 <core-drawer-panel rightDrawer> |
| 47 <div drawer> Drawer panel... </div> | 69 <div drawer> Drawer panel... </div> |
| 48 <div main> Main panel... </div> | 70 <div main> Main panel... </div> |
| 49 </core-drawer-panel> | 71 </core-drawer-panel> |
| 50 | 72 |
| 51 @group Polymer Core Elements | 73 @group Polymer Core Elements |
| 52 @element core-drawer-panel | 74 @element core-drawer-panel |
| 53 @homepage github.io | 75 @homepage github.io |
| 54 --> | 76 --> |
| 55 | 77 |
| 56 <link rel="import" href="../core-media-query/core-media-query.html"> | 78 <link rel="import" href="../core-media-query/core-media-query.html"> |
| 57 <link rel="import" href="../core-selector/core-selector.html"> | 79 <link rel="import" href="../core-selector/core-selector.html"> |
| 58 | 80 |
| 59 <polymer-element name="core-drawer-panel" touch-action="auto"> | 81 <polymer-element name="core-drawer-panel" touch-action="auto"> |
| 60 <template> | 82 <template> |
| 61 | 83 |
| 62 <link rel="stylesheet" href="core-drawer-panel.css"> | 84 <link rel="stylesheet" href="core-drawer-panel.css"> |
| 63 | 85 |
| 64 <core-media-query query="max-width: {{responsiveWidth}}" queryMatches="{{query
Matches}}"></core-media-query> | 86 <core-media-query query="max-width: {{forceNarrow ? '' : responsiveWidth}}" qu
eryMatches="{{queryMatches}}"></core-media-query> |
| 65 | 87 |
| 66 <core-selector class="{{ {'narrow-layout' : queryMatches, transition : transit
ion, dragging : dragging, 'right-drawer': rightDrawer} | tokenList }}" valueattr
="id" selected="{{selected}}"> | 88 <core-selector class="{{ {'narrow-layout' : narrow, transition : transition, d
ragging : dragging, 'right-drawer': rightDrawer} | tokenList }}" valueattr="id"
selected="{{selected}}"> |
| 67 | 89 |
| 68 <div id="main" _style="left: {{ narrow || rightDrawer ? '0' : drawerWidth }}
; right: {{ rightDrawer ? (narrow ? '' : drawerWidth) : '' }};"> | 90 <div id="main" _style="left: {{ narrow || rightDrawer ? '0' : drawerWidth }}
; right: {{ rightDrawer ? (narrow ? '' : drawerWidth) : '' }};"> |
| 69 <content select="[main]"></content> | 91 <content select="[main]"></content> |
| 70 <div id="scrim" on-tap="{{togglePanel}}"></div> | 92 <div id="scrim" on-tap="{{togglePanel}}"></div> |
| 71 </div> | 93 </div> |
| 72 | 94 |
| 73 <div id="drawer" _style="width: {{ drawerWidth }}"> | 95 <div id="drawer" _style="width: {{ drawerWidth }}"> |
| 74 <content select="[drawer]"></content> | 96 <content select="[drawer]"></content> |
| 75 </div> | 97 </div> |
| 76 | 98 |
| 77 </core-selector> | 99 </core-selector> |
| 78 | 100 |
| 79 </template> | 101 </template> |
| 80 <script> | 102 <script> |
| 81 | 103 |
| 82 Polymer('core-drawer-panel', { | 104 Polymer('core-drawer-panel', { |
| 83 | 105 |
| 84 /** | 106 /** |
| 85 * Fired when the narrow layout changes. | 107 * Fired when the narrow layout changes. |
| 86 * | 108 * |
| 87 * @event core-responsive-change | 109 * @event core-responsive-change |
| 88 * @param {Object} detail | 110 * @param {Object} detail |
| 89 * @param {boolean} detail.narrow true if the panel is in narrow layout. | 111 * @param {boolean} detail.narrow true if the panel is in narrow layout. |
| 90 */ | 112 */ |
| 113 |
| 114 /** |
| 115 * Fired when the selected panel changes. |
| 116 * |
| 117 * Listening for this event is an alternative to observing changes in the `s
elected` attribute. |
| 118 * This event is fired both when a panel is selected and deselected. |
| 119 * The `isSelected` detail property contains the selection state. |
| 120 * |
| 121 * @event core-select |
| 122 * @param {Object} detail |
| 123 * @param {boolean} detail.isSelected true for selection and false for desel
ection |
| 124 * @param {Object} detail.item the panel that the event refers to |
| 125 */ |
| 91 | 126 |
| 92 publish: { | 127 publish: { |
| 93 | 128 |
| 94 /** | 129 /** |
| 95 * Width of the drawer panel. | 130 * Width of the drawer panel. |
| 96 * | 131 * |
| 97 * @attribute drawerWidth | 132 * @attribute drawerWidth |
| 98 * @type string | 133 * @type string |
| 99 * @default '256px' | 134 * @default '256px' |
| 100 */ | 135 */ |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 148 */ | 183 */ |
| 149 rightDrawer: false, | 184 rightDrawer: false, |
| 150 | 185 |
| 151 /** | 186 /** |
| 152 * If true, swipe to open/close the drawer is disabled. | 187 * If true, swipe to open/close the drawer is disabled. |
| 153 * | 188 * |
| 154 * @attribute disableSwipe | 189 * @attribute disableSwipe |
| 155 * @type boolean | 190 * @type boolean |
| 156 * @default false | 191 * @default false |
| 157 */ | 192 */ |
| 158 disableSwipe: false | 193 disableSwipe: false, |
| 194 |
| 195 /** |
| 196 * If true, ignore `responsiveWidth` setting and force the narrow layout. |
| 197 * |
| 198 * @attribute forceNarrow |
| 199 * @type boolean |
| 200 * @default false |
| 201 */ |
| 202 forceNarrow: false |
| 159 }, | 203 }, |
| 160 | 204 |
| 161 eventDelegates: { | 205 eventDelegates: { |
| 162 trackstart: 'trackStart', | 206 trackstart: 'trackStart', |
| 163 trackx: 'trackx', | 207 trackx: 'trackx', |
| 164 trackend: 'trackEnd', | 208 trackend: 'trackEnd', |
| 165 down: 'touchStart', | 209 down: 'downHandler', |
| 166 up: 'touchEnd' | 210 up: 'upHandler', |
| 211 tap: 'tapHandler' |
| 167 }, | 212 }, |
| 168 | 213 |
| 169 // Whether the transition is enabled. | 214 // Whether the transition is enabled. |
| 170 transition: false, | 215 transition: false, |
| 171 | 216 |
| 172 // How many pixels on the side of the screen are sensitive to edge swipes an
d peek. | 217 // How many pixels on the side of the screen are sensitive to edge swipes an
d peek. |
| 173 edgeSwipeSensitivity: 15, | 218 edgeSwipeSensitivity: 15, |
| 174 | 219 |
| 175 // Whether the drawer is peeking out from the edge. | 220 // Whether the drawer is peeking out from the edge. |
| 176 peeking: false, | 221 peeking: false, |
| 177 | 222 |
| 178 // Whether the user is dragging the drawer interactively. | 223 // Whether the user is dragging the drawer interactively. |
| 179 dragging: false, | 224 dragging: false, |
| 180 | 225 |
| 181 // Whether the browser has support for the transform CSS property. | 226 // Whether the browser has support for the transform CSS property. |
| 182 hasTransform: true, | 227 hasTransform: true, |
| 183 | 228 |
| 184 // Whether the browser has support for the will-change CSS property. | 229 // Whether the browser has support for the will-change CSS property. |
| 185 hasWillChange: true, | 230 hasWillChange: true, |
| 231 |
| 232 // The attribute on elements that should toggle the drawer on tap, also |
| 233 // elements will automatically be hidden in wide layout. |
| 234 toggleAttribute: 'core-drawer-toggle', |
| 186 | 235 |
| 187 created: function() { | 236 created: function() { |
| 188 this.hasTransform = 'transform' in this.style; | 237 this.hasTransform = 'transform' in this.style; |
| 189 this.hasWillChange = 'willChange' in this.style; | 238 this.hasWillChange = 'willChange' in this.style; |
| 190 }, | 239 }, |
| 191 | 240 |
| 192 domReady: function() { | 241 domReady: function() { |
| 193 // to avoid transition at the beginning e.g. page loads | 242 // to avoid transition at the beginning e.g. page loads |
| 194 // NOTE: domReady is already raf delayed and delaying another frame | 243 // NOTE: domReady is already raf delayed and delaying another frame |
| 195 // ensures a layout has occurred. | 244 // ensures a layout has occurred. |
| 196 this.async(function() { | 245 this.async(function() { |
| 197 this.transition = true; | 246 this.transition = true; |
| 198 }); | 247 }); |
| 199 }, | 248 }, |
| 200 | 249 |
| 201 /** | 250 /** |
| 202 * Toggles the panel open and closed. | 251 * Toggles the panel open and closed. |
| 203 * | 252 * |
| 204 * @method togglePanel | 253 * @method togglePanel |
| 205 */ | 254 */ |
| 206 togglePanel: function() { | 255 togglePanel: function() { |
| 207 this.selected = this.selected === 'main' ? 'drawer' : 'main'; | 256 this.selected = this.isMainSelected() ? 'drawer' : 'main'; |
| 208 }, | 257 }, |
| 209 | 258 |
| 210 /** | 259 /** |
| 211 * Opens the drawer. | 260 * Opens the drawer. |
| 212 * | 261 * |
| 213 * @method openDrawer | 262 * @method openDrawer |
| 214 */ | 263 */ |
| 215 openDrawer: function() { | 264 openDrawer: function() { |
| 216 this.selected = 'drawer'; | 265 this.selected = 'drawer'; |
| 217 }, | 266 }, |
| 218 | 267 |
| 219 /** | 268 /** |
| 220 * Closes the drawer. | 269 * Closes the drawer. |
| 221 * | 270 * |
| 222 * @method closeDrawer | 271 * @method closeDrawer |
| 223 */ | 272 */ |
| 224 closeDrawer: function() { | 273 closeDrawer: function() { |
| 225 this.selected = 'main'; | 274 this.selected = 'main'; |
| 226 }, | 275 }, |
| 227 | 276 |
| 228 queryMatchesChanged: function() { | 277 queryMatchesChanged: function() { |
| 229 if (this.queryMatches) { | 278 this.narrow = this.queryMatches || this.forceNarrow; |
| 279 if (this.narrow) { |
| 230 this.selected = this.defaultSelected; | 280 this.selected = this.defaultSelected; |
| 231 } | 281 } |
| 232 this.narrow = this.queryMatches; | |
| 233 this.setAttribute('touch-action', this.swipeAllowed() ? 'pan-y' : ''); | 282 this.setAttribute('touch-action', this.swipeAllowed() ? 'pan-y' : ''); |
| 234 this.fire('core-responsive-change', {narrow: this.narrow}); | 283 this.fire('core-responsive-change', {narrow: this.narrow}); |
| 235 }, | 284 }, |
| 285 |
| 286 forceNarrowChanged: function() { |
| 287 this.queryMatchesChanged(); |
| 288 }, |
| 236 | 289 |
| 237 swipeAllowed: function() { | 290 swipeAllowed: function() { |
| 238 return this.narrow && !this.disableSwipe; | 291 return this.narrow && !this.disableSwipe; |
| 239 }, | 292 }, |
| 293 |
| 294 isMainSelected: function() { |
| 295 return this.selected === 'main'; |
| 296 }, |
| 240 | 297 |
| 241 startEdgePeek: function() { | 298 startEdgePeek: function() { |
| 242 this.width = this.$.drawer.offsetWidth; | 299 this.width = this.$.drawer.offsetWidth; |
| 243 this.moveDrawer(this.translateXForDeltaX(this.rightDrawer ? | 300 this.moveDrawer(this.translateXForDeltaX(this.rightDrawer ? |
| 244 -this.edgeSwipeSensitivity : this.edgeSwipeSensitivity)); | 301 -this.edgeSwipeSensitivity : this.edgeSwipeSensitivity)); |
| 245 this.peeking = true; | 302 this.peeking = true; |
| 246 }, | 303 }, |
| 247 | 304 |
| 248 stopEdgePeak: function() { | 305 stopEdgePeak: function() { |
| 249 if (this.peeking) { | 306 if (this.peeking) { |
| 250 this.peeking = false; | 307 this.peeking = false; |
| 251 this.moveDrawer(null); | 308 this.moveDrawer(null); |
| 252 } | 309 } |
| 253 }, | 310 }, |
| 254 | 311 |
| 255 touchStart: function(e) { | 312 downHandler: function(e) { |
| 256 if (!this.dragging && this.selected === 'main' && this.isEdgeTouch(e)) | 313 if (!this.dragging && this.isMainSelected() && this.isEdgeTouch(e)) { |
| 257 this.startEdgePeek(); | 314 this.startEdgePeek(); |
| 315 } |
| 258 }, | 316 }, |
| 259 | 317 |
| 260 touchEnd: function(e) { | 318 upHandler: function(e) { |
| 261 this.stopEdgePeak(); | 319 this.stopEdgePeak(); |
| 262 }, | 320 }, |
| 321 |
| 322 tapHandler: function(e) { |
| 323 if (e.target && this.toggleAttribute && |
| 324 e.target.hasAttribute(this.toggleAttribute)) { |
| 325 this.togglePanel(); |
| 326 } |
| 327 }, |
| 263 | 328 |
| 264 isEdgeTouch: function(e) { | 329 isEdgeTouch: function(e) { |
| 265 return this.swipeAllowed() && (this.rightDrawer ? | 330 return this.swipeAllowed() && (this.rightDrawer ? |
| 266 e.pageX >= this.offsetWidth - this.edgeSwipeSensitivity : | 331 e.pageX >= this.offsetWidth - this.edgeSwipeSensitivity : |
| 267 e.pageX <= this.edgeSwipeSensitivity); | 332 e.pageX <= this.edgeSwipeSensitivity); |
| 268 }, | 333 }, |
| 269 | 334 |
| 270 // swipe support for the drawer, inspired by | |
| 271 // https://github.com/Polymer/core-drawer-panel/pull/6 | |
| 272 trackStart : function(e) { | 335 trackStart : function(e) { |
| 273 if (this.swipeAllowed()) { | 336 if (this.swipeAllowed()) { |
| 274 this.dragging = true; | 337 this.dragging = true; |
| 275 | 338 |
| 276 if (this.selected === 'main') | 339 if (this.isMainSelected()) { |
| 277 this.dragging = this.peeking || this.isEdgeTouch(e); | 340 this.dragging = this.peeking || this.isEdgeTouch(e); |
| 341 } |
| 278 | 342 |
| 279 if (this.dragging) { | 343 if (this.dragging) { |
| 280 this.width = this.$.drawer.offsetWidth; | 344 this.width = this.$.drawer.offsetWidth; |
| 281 this.transition = false; | 345 this.transition = false; |
| 282 e.preventTap(); | 346 e.preventTap(); |
| 283 } | 347 } |
| 284 } | 348 } |
| 285 }, | 349 }, |
| 286 | 350 |
| 287 translateXForDeltaX: function(deltaX) { | 351 translateXForDeltaX: function(deltaX) { |
| 352 var isMain = this.isMainSelected(); |
| 288 if (this.rightDrawer) { | 353 if (this.rightDrawer) { |
| 289 return Math.max(0, (this.selected === 'main') ? this.width + deltaX : de
ltaX); | 354 return Math.max(0, isMain ? this.width + deltaX : deltaX); |
| 290 } else { | 355 } else { |
| 291 return Math.min(0, (this.selected === 'main') ? deltaX - this.width : de
ltaX); | 356 return Math.min(0, isMain ? deltaX - this.width : deltaX); |
| 292 } | 357 } |
| 293 }, | 358 }, |
| 294 | 359 |
| 295 trackx : function(e) { | 360 trackx : function(e) { |
| 296 if (this.dragging) { | 361 if (this.dragging) { |
| 297 if (this.peeking) { | 362 if (this.peeking) { |
| 298 if (Math.abs(e.dx) <= this.edgeSwipeSensitivity) | 363 if (Math.abs(e.dx) <= this.edgeSwipeSensitivity) { |
| 299 return; // Ignore trackx until we move past the edge peek. | 364 return; // Ignore trackx until we move past the edge peek. |
| 365 } |
| 300 this.peeking = false; | 366 this.peeking = false; |
| 301 } | 367 } |
| 302 this.moveDrawer(this.translateXForDeltaX(e.dx)); | 368 this.moveDrawer(this.translateXForDeltaX(e.dx)); |
| 303 } | 369 } |
| 304 }, | 370 }, |
| 305 | 371 |
| 306 trackEnd : function(e) { | 372 trackEnd : function(e) { |
| 307 if (this.dragging) { | 373 if (this.dragging) { |
| 308 this.dragging = false; | 374 this.dragging = false; |
| 309 this.transition = true; | 375 this.transition = true; |
| 310 this.moveDrawer(null); | 376 this.moveDrawer(null); |
| 311 | 377 |
| 312 if (this.rightDrawer) { | 378 if (this.rightDrawer) { |
| 313 this.selected = e.xDirection > 0 ? 'main' : 'drawer'; | 379 this.selected = e.xDirection > 0 ? 'main' : 'drawer'; |
| 314 } else { | 380 } else { |
| 315 this.selected = e.xDirection > 0 ? 'drawer' : 'main'; | 381 this.selected = e.xDirection > 0 ? 'drawer' : 'main'; |
| 316 } | 382 } |
| 317 } | 383 } |
| 318 }, | 384 }, |
| 319 | 385 |
| 320 transformForTranslateX: function (translateX) { | 386 transformForTranslateX: function(translateX) { |
| 321 if (translateX === null) | 387 if (translateX === null) { |
| 322 return ''; | 388 return ''; |
| 323 return this.hasWillChange ? 'translateX(' + translateX + 'px)' : 'translat
e3d(' + translateX + 'px, 0, 0)'; | 389 } |
| 390 return this.hasWillChange ? 'translateX(' + translateX + 'px)' : |
| 391 'translate3d(' + translateX + 'px, 0, 0)'; |
| 324 }, | 392 }, |
| 325 | 393 |
| 326 moveDrawer: function(translateX) { | 394 moveDrawer: function(translateX) { |
| 327 var s = this.$.drawer.style; | 395 var s = this.$.drawer.style; |
| 328 | 396 |
| 329 if (this.hasTransform) { | 397 if (this.hasTransform) { |
| 330 s.transform = this.transformForTranslateX(translateX); | 398 s.transform = this.transformForTranslateX(translateX); |
| 331 } else { | 399 } else { |
| 332 s.webkitTransform = this.transformForTranslateX(translateX); | 400 s.webkitTransform = this.transformForTranslateX(translateX); |
| 333 } | 401 } |
| 334 }, | 402 } |
| 335 | 403 |
| 336 }); | 404 }); |
| 337 | 405 |
| 338 </script> | 406 </script> |
| 339 </polymer-element> | 407 </polymer-element> |
| OLD | NEW |