| OLD | NEW |
| (Empty) |
| 1 <!-- | |
| 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 | |
| 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 <!-- | |
| 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 | |
| 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 | |
| 15 of the main panel. The drawer will be slided in/out to hide/reveal the main | |
| 16 panel. | |
| 17 | |
| 18 Use the attribute `drawer` to indicate the element is a drawer panel and | |
| 19 `main` to indicate is a main panel. | |
| 20 | |
| 21 Example: | |
| 22 | |
| 23 <core-drawer-panel> | |
| 24 <div drawer> Drawer panel... </div> | |
| 25 <div main> Main panel... </div> | |
| 26 </core-drawer-panel> | |
| 27 | |
| 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`. | |
| 30 | |
| 31 Example: | |
| 32 | |
| 33 <core-drawer-panel> | |
| 34 <core-header-panel drawer> | |
| 35 <core-toolbar></core-toolbar> | |
| 36 <div> Drawer content... </div> | |
| 37 </core-header-panel> | |
| 38 <core-header-panel main> | |
| 39 <core-toolbar></core-toolbar> | |
| 40 <div> Main content... </div> | |
| 41 </core-header-panel> | |
| 42 </core-drawer-panel> | |
| 43 | |
| 44 To position the drawer to the right, add `rightDrawer` attribute. | |
| 45 | |
| 46 <core-drawer-panel rightDrawer> | |
| 47 <div drawer> Drawer panel... </div> | |
| 48 <div main> Main panel... </div> | |
| 49 </core-drawer-panel> | |
| 50 | |
| 51 @group Polymer Core Elements | |
| 52 @element core-drawer-panel | |
| 53 @homepage github.io | |
| 54 --> | |
| 55 | |
| 56 <link rel="import" href="../core-media-query/core-media-query.html"> | |
| 57 <link rel="import" href="../core-selector/core-selector.html"> | |
| 58 | |
| 59 <polymer-element name="core-drawer-panel" touch-action="auto"> | |
| 60 <template> | |
| 61 | |
| 62 <link rel="stylesheet" href="core-drawer-panel.css"> | |
| 63 | |
| 64 <core-media-query query="max-width: {{responsiveWidth}}" queryMatches="{{query
Matches}}"></core-media-query> | |
| 65 | |
| 66 <core-selector class="{{ {'narrow-layout' : queryMatches, transition : transit
ion, dragging : dragging, 'right-drawer': rightDrawer} | tokenList }}" valueattr
="id" selected="{{selected}}"> | |
| 67 | |
| 68 <div id="main" _style="left: {{ narrow || rightDrawer ? '0' : drawerWidth }}
; right: {{ rightDrawer ? (narrow ? '' : drawerWidth) : '' }};"> | |
| 69 <content select="[main]"></content> | |
| 70 <div id="scrim" on-tap="{{togglePanel}}"></div> | |
| 71 </div> | |
| 72 | |
| 73 <div id="drawer" _style="width: {{ drawerWidth }}"> | |
| 74 <content select="[drawer]"></content> | |
| 75 </div> | |
| 76 | |
| 77 </core-selector> | |
| 78 | |
| 79 </template> | |
| 80 <script> | |
| 81 | |
| 82 Polymer('core-drawer-panel', { | |
| 83 | |
| 84 /** | |
| 85 * Fired when the narrow layout changes. | |
| 86 * | |
| 87 * @event core-responsive-change | |
| 88 * @param {Object} detail | |
| 89 * @param {boolean} detail.narrow true if the panel is in narrow layout. | |
| 90 */ | |
| 91 | |
| 92 publish: { | |
| 93 | |
| 94 /** | |
| 95 * Width of the drawer panel. | |
| 96 * | |
| 97 * @attribute drawerWidth | |
| 98 * @type string | |
| 99 * @default '256px' | |
| 100 */ | |
| 101 drawerWidth: '256px', | |
| 102 | |
| 103 /** | |
| 104 * Max-width when the panel changes to narrow layout. | |
| 105 * | |
| 106 * @attribute responsiveWidth | |
| 107 * @type string | |
| 108 * @default '640px' | |
| 109 */ | |
| 110 responsiveWidth: '640px', | |
| 111 | |
| 112 /** | |
| 113 * The panel that is being selected. `drawer` for the drawer panel and | |
| 114 * `main` for the main panel. | |
| 115 * | |
| 116 * @attribute selected | |
| 117 * @type string | |
| 118 * @default null | |
| 119 */ | |
| 120 selected: {value: null, reflect: true}, | |
| 121 | |
| 122 /** | |
| 123 * The panel to be selected when `core-drawer-panel` changes to narrow | |
| 124 * layout. | |
| 125 * | |
| 126 * @attribute defaultSelected | |
| 127 * @type string | |
| 128 * @default 'main' | |
| 129 */ | |
| 130 defaultSelected: 'main', | |
| 131 | |
| 132 /** | |
| 133 * Returns true if the panel is in narrow layout. This is useful if you | |
| 134 * need to show/hide elements based on the layout. | |
| 135 * | |
| 136 * @attribute narrow | |
| 137 * @type boolean | |
| 138 * @default false | |
| 139 */ | |
| 140 narrow: {value: false, reflect: true}, | |
| 141 | |
| 142 /** | |
| 143 * If true, position the drawer to the right. | |
| 144 * | |
| 145 * @attribute rightDrawer | |
| 146 * @type boolean | |
| 147 * @default false | |
| 148 */ | |
| 149 rightDrawer: false, | |
| 150 | |
| 151 /** | |
| 152 * If true, swipe to open/close the drawer is disabled. | |
| 153 * | |
| 154 * @attribute disableSwipe | |
| 155 * @type boolean | |
| 156 * @default false | |
| 157 */ | |
| 158 disableSwipe: false | |
| 159 }, | |
| 160 | |
| 161 eventDelegates: { | |
| 162 trackstart: 'trackStart', | |
| 163 trackx: 'trackx', | |
| 164 trackend: 'trackEnd', | |
| 165 down: 'touchStart', | |
| 166 up: 'touchEnd' | |
| 167 }, | |
| 168 | |
| 169 // Whether the transition is enabled. | |
| 170 transition: false, | |
| 171 | |
| 172 // How many pixels on the side of the screen are sensitive to edge swipes an
d peek. | |
| 173 edgeSwipeSensitivity: 15, | |
| 174 | |
| 175 // Whether the drawer is peeking out from the edge. | |
| 176 peeking: false, | |
| 177 | |
| 178 // Whether the user is dragging the drawer interactively. | |
| 179 dragging: false, | |
| 180 | |
| 181 // Whether the browser has support for the transform CSS property. | |
| 182 hasTransform: true, | |
| 183 | |
| 184 // Whether the browser has support for the will-change CSS property. | |
| 185 hasWillChange: true, | |
| 186 | |
| 187 created: function() { | |
| 188 this.hasTransform = 'transform' in this.style; | |
| 189 this.hasWillChange = 'willChange' in this.style; | |
| 190 }, | |
| 191 | |
| 192 domReady: function() { | |
| 193 // to avoid transition at the beginning e.g. page loads | |
| 194 // NOTE: domReady is already raf delayed and delaying another frame | |
| 195 // ensures a layout has occurred. | |
| 196 this.async(function() { | |
| 197 this.transition = true; | |
| 198 }); | |
| 199 }, | |
| 200 | |
| 201 /** | |
| 202 * Toggles the panel open and closed. | |
| 203 * | |
| 204 * @method togglePanel | |
| 205 */ | |
| 206 togglePanel: function() { | |
| 207 this.selected = this.selected === 'main' ? 'drawer' : 'main'; | |
| 208 }, | |
| 209 | |
| 210 /** | |
| 211 * Opens the drawer. | |
| 212 * | |
| 213 * @method openDrawer | |
| 214 */ | |
| 215 openDrawer: function() { | |
| 216 this.selected = 'drawer'; | |
| 217 }, | |
| 218 | |
| 219 /** | |
| 220 * Closes the drawer. | |
| 221 * | |
| 222 * @method closeDrawer | |
| 223 */ | |
| 224 closeDrawer: function() { | |
| 225 this.selected = 'main'; | |
| 226 }, | |
| 227 | |
| 228 queryMatchesChanged: function() { | |
| 229 if (this.queryMatches) { | |
| 230 this.selected = this.defaultSelected; | |
| 231 } | |
| 232 this.narrow = this.queryMatches; | |
| 233 this.setAttribute('touch-action', this.swipeAllowed() ? 'pan-y' : ''); | |
| 234 this.fire('core-responsive-change', {narrow: this.narrow}); | |
| 235 }, | |
| 236 | |
| 237 swipeAllowed: function() { | |
| 238 return this.narrow && !this.disableSwipe; | |
| 239 }, | |
| 240 | |
| 241 startEdgePeek: function() { | |
| 242 this.width = this.$.drawer.offsetWidth; | |
| 243 this.moveDrawer(this.translateXForDeltaX(this.rightDrawer ? | |
| 244 -this.edgeSwipeSensitivity : this.edgeSwipeSensitivity)); | |
| 245 this.peeking = true; | |
| 246 }, | |
| 247 | |
| 248 stopEdgePeak: function() { | |
| 249 if (this.peeking) { | |
| 250 this.peeking = false; | |
| 251 this.moveDrawer(null); | |
| 252 } | |
| 253 }, | |
| 254 | |
| 255 touchStart: function(e) { | |
| 256 if (!this.dragging && this.selected === 'main' && this.isEdgeTouch(e)) | |
| 257 this.startEdgePeek(); | |
| 258 }, | |
| 259 | |
| 260 touchEnd: function(e) { | |
| 261 this.stopEdgePeak(); | |
| 262 }, | |
| 263 | |
| 264 isEdgeTouch: function(e) { | |
| 265 return this.swipeAllowed() && (this.rightDrawer ? | |
| 266 e.pageX >= this.offsetWidth - this.edgeSwipeSensitivity : | |
| 267 e.pageX <= this.edgeSwipeSensitivity); | |
| 268 }, | |
| 269 | |
| 270 // swipe support for the drawer, inspired by | |
| 271 // https://github.com/Polymer/core-drawer-panel/pull/6 | |
| 272 trackStart : function(e) { | |
| 273 if (this.swipeAllowed()) { | |
| 274 this.dragging = true; | |
| 275 | |
| 276 if (this.selected === 'main') | |
| 277 this.dragging = this.peeking || this.isEdgeTouch(e); | |
| 278 | |
| 279 if (this.dragging) { | |
| 280 this.width = this.$.drawer.offsetWidth; | |
| 281 this.transition = false; | |
| 282 e.preventTap(); | |
| 283 } | |
| 284 } | |
| 285 }, | |
| 286 | |
| 287 translateXForDeltaX: function(deltaX) { | |
| 288 if (this.rightDrawer) { | |
| 289 return Math.max(0, (this.selected === 'main') ? this.width + deltaX : de
ltaX); | |
| 290 } else { | |
| 291 return Math.min(0, (this.selected === 'main') ? deltaX - this.width : de
ltaX); | |
| 292 } | |
| 293 }, | |
| 294 | |
| 295 trackx : function(e) { | |
| 296 if (this.dragging) { | |
| 297 if (this.peeking) { | |
| 298 if (Math.abs(e.dx) <= this.edgeSwipeSensitivity) | |
| 299 return; // Ignore trackx until we move past the edge peek. | |
| 300 this.peeking = false; | |
| 301 } | |
| 302 this.moveDrawer(this.translateXForDeltaX(e.dx)); | |
| 303 } | |
| 304 }, | |
| 305 | |
| 306 trackEnd : function(e) { | |
| 307 if (this.dragging) { | |
| 308 this.dragging = false; | |
| 309 this.transition = true; | |
| 310 this.moveDrawer(null); | |
| 311 | |
| 312 if (this.rightDrawer) { | |
| 313 this.selected = e.xDirection > 0 ? 'main' : 'drawer'; | |
| 314 } else { | |
| 315 this.selected = e.xDirection > 0 ? 'drawer' : 'main'; | |
| 316 } | |
| 317 } | |
| 318 }, | |
| 319 | |
| 320 transformForTranslateX: function (translateX) { | |
| 321 if (translateX === null) | |
| 322 return ''; | |
| 323 return this.hasWillChange ? 'translateX(' + translateX + 'px)' : 'translat
e3d(' + translateX + 'px, 0, 0)'; | |
| 324 }, | |
| 325 | |
| 326 moveDrawer: function(translateX) { | |
| 327 var s = this.$.drawer.style; | |
| 328 | |
| 329 if (this.hasTransform) { | |
| 330 s.transform = this.transformForTranslateX(translateX); | |
| 331 } else { | |
| 332 s.webkitTransform = this.transformForTranslateX(translateX); | |
| 333 } | |
| 334 }, | |
| 335 | |
| 336 }); | |
| 337 | |
| 338 </script> | |
| 339 </polymer-element> | |
| OLD | NEW |