Blame view

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