Blame view

bower_components/paper-scroll-header-panel/paper-scroll-header-panel.html 16 KB
73bcce88   luigser   COMPONENTS
1
2
3
4
5
6
7
8
9
10
11
12
13
14
  <!--
  @license
  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-resizable-behavior/iron-resizable-behavior.html">
  
  <!--
0a86ccc8   Luigi Serra   graph and datalet...
15
16
  Material design: [Scrolling techniques](https://www.google.com/design/spec/patterns/scrolling-techniques.html)
  
73bcce88   luigser   COMPONENTS
17
18
19
20
21
22
23
24
25
26
  `paper-scroll-header-panel` contains a header section and a content section.  The
  header is initially on the top part of the view but it scrolls away with the
  rest of the scrollable content.  Upon scrolling slightly up at any point, the
  header scrolls back into view.  This saves screen space and allows users to
  access important controls by easily moving them back to the view.
  
  __Important:__ The `paper-scroll-header-panel` will not display if its parent does not have a height.
  
  Using [layout classes](https://www.polymer-project.org/1.0/docs/migration.html#layout-attributes) or custom properties, you can easily make the `paper-scroll-header-panel` fill the screen
  
0a86ccc8   Luigi Serra   graph and datalet...
27
28
29
30
31
32
33
34
35
  ```html
  <body class="fullbleed layout vertical">
    <paper-scroll-header-panel class="flex">
      <paper-toolbar>
        <div>Hello World!</div>
      </paper-toolbar>
    </paper-scroll-header-panel>
  </body>
  ```
73bcce88   luigser   COMPONENTS
36
37
38
  
  or, if you would prefer to do it in CSS, just give `html`, `body`, and `paper-scroll-header-panel` a height of 100%:
  
0a86ccc8   Luigi Serra   graph and datalet...
39
40
41
42
43
44
45
46
47
  ```css
  html, body {
    height: 100%;
    margin: 0;
  }
  paper-scroll-header-panel {
    height: 100%;
  }
  ```
73bcce88   luigser   COMPONENTS
48
49
50
51
  
  `paper-scroll-header-panel` works well with `paper-toolbar` but can use any element
  that represents a header by adding a `paper-header` class to it.
  
0a86ccc8   Luigi Serra   graph and datalet...
52
53
54
55
56
57
  ```html
  <paper-scroll-header-panel>
    <paper-toolbar>Header</paper-toolbar>
    <div>Content goes here...</div>
  </paper-scroll-header-panel>
  ```
73bcce88   luigser   COMPONENTS
58
  
0a86ccc8   Luigi Serra   graph and datalet...
59
60
  ### Styling
  =======
73bcce88   luigser   COMPONENTS
61
  
0a86ccc8   Luigi Serra   graph and datalet...
62
  The following custom properties and mixins are available for styling:
73bcce88   luigser   COMPONENTS
63
  
0a86ccc8   Luigi Serra   graph and datalet...
64
65
66
67
68
  Custom property | Description | Default
  ----------------|-------------|----------
  --paper-scroll-header-panel-full-header | To change background for toolbar when it is at its full size | {}
  --paper-scroll-header-panel-condensed-header | To change the background for toolbar when it is condensed | {}
  --paper-scroll-header-container | To override or add container styles | {}
73bcce88   luigser   COMPONENTS
69
70
71
  
  @group Paper Element
  @element paper-scroll-header-panel
0a86ccc8   Luigi Serra   graph and datalet...
72
73
74
75
76
77
78
79
80
81
  @demo demo/transform-header-1.html Transform header 1
  @demo demo/transform-header-2.html Transform header 2
  @demo demo/transform-header-3.html Transform header 3
  @demo demo/transform-header-4.html Transform header 4
  @demo demo/transform-header-5.html Transform header 5
  @demo demo/transform-header-6.html Transform header 6
  @demo demo/keep-header.html Keep header
  @demo demo/hide-header.html Hide header
  @demo demo/toggle-fixed-header.html Toggle fixed header
  @demo demo/drawer-panel.html Combine with paper-drawer-panel
73bcce88   luigser   COMPONENTS
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
  @hero hero.svg
  -->
  
  <dom-module id="paper-scroll-header-panel">
  
    <style>
      :host {
        display: block;
        position: relative;
        overflow: hidden;
      }
  
      #mainContainer {
        position: absolute;
        top: 0;
        right: 0;
        bottom: 0;
        left: 0;
  
        -moz-box-sizing: border-box;
        box-sizing: border-box;
73bcce88   luigser   COMPONENTS
103
104
105
106
        -webkit-overflow-scrolling: touch;
  
        overflow-x: hidden;
        overflow-y: auto;
0a86ccc8   Luigi Serra   graph and datalet...
107
108
  
        @apply(--paper-scroll-header-container);
73bcce88   luigser   COMPONENTS
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
      }
  
      #headerContainer {
        position: absolute;
        top: 0;
        right: 0;
        left: 0;
      }
  
      .bg-container {
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        overflow: hidden;
      }
  
      #headerBg {
        @apply(--paper-scroll-header-panel-full-header);
      }
  
      #condensedHeaderBg {
        @apply(--paper-scroll-header-panel-condensed-header);
      }
  
      #headerBg, #condensedHeaderBg {
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        background-repeat: no-repeat;
        background-size: cover;
        background-position: center center;
      }
  
      #condensedHeaderBg {
        opacity: 0;
      }
    </style>
    <template>
      <div id="mainContainer">
        <content id="mainContent" select=":not(paper-toolbar):not(.paper-header)"></content>
      </div>
      <div id="headerContainer">
        <div class="bg-container">
          <div id="condensedHeaderBg"></div>
          <div id="headerBg"></div>
        </div>
        <content id="headerContent" select="paper-toolbar, .paper-header"></content>
      </div>
    </template>
  </dom-module>
  
  <script>
  (function() {
  
    'use strict';
  
0a86ccc8   Luigi Serra   graph and datalet...
169
    Polymer.PaperScrollHeaderPanel = Polymer({
73bcce88   luigser   COMPONENTS
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
  
      /**
       * Fired when the content has been scrolled.
       *
       * @event content-scroll
       */
  
      /**
       * Fired when the header is transformed.
       *
       * @event paper-header-transform
       */
  
      is: 'paper-scroll-header-panel',
  
      behaviors: [
        Polymer.IronResizableBehavior
      ],
  
      properties: {
  
        /**
         * If true, the header's height will condense to `condensedHeaderHeight`
         * as the user scrolls down from the top of the content area.
         */
        condenses: {
          type: Boolean,
          value: false
        },
  
        /**
         * If true, no cross-fade transition from one background to another.
         */
        noDissolve: {
          type: Boolean,
          value: false
        },
  
        /**
         * If true, the header doesn't slide back in when scrolling back up.
         */
        noReveal: {
          type: Boolean,
          value: false
        },
  
        /**
         * If true, the header is fixed to the top and never moves away.
         */
        fixed: {
          type: Boolean,
          value: false
        },
  
        /**
         * If true, the condensed header is always shown and does not move away.
         */
        keepCondensedHeader: {
          type: Boolean,
          value: false
        },
  
        /**
         * The height of the header when it is at its full size.
         *
         * By default, the height will be measured when it is ready.  If the height
         * changes later the user needs to either set this value to reflect the
         * new height or invoke `measureHeaderHeight()`.
         */
        headerHeight: {
          type: Number,
          value: 0
        },
  
        /**
         * The height of the header when it is condensed.
         *
         * By default, `condensedHeaderHeight` is 1/3 of `headerHeight` unless
         * this is specified.
         */
        condensedHeaderHeight: {
          type: Number,
          value: 0
        },
  
        /**
         * By default, the top part of the header stays when the header is being
         * condensed.  Set this to true if you want the top part of the header
         * to be scrolled away.
         */
        scrollAwayTopbar: {
          type: Boolean,
          value: false
        },
  
        /**
0a86ccc8   Luigi Serra   graph and datalet...
266
         * The state of the header. Depending on the configuration and the `scrollTop` value,
73bcce88   luigser   COMPONENTS
267
         * the header state could change to
0a86ccc8   Luigi Serra   graph and datalet...
268
269
270
271
         *      Polymer.PaperScrollHeaderPanel.HEADER_STATE_EXPANDED
         *      Polymer.PaperScrollHeaderPanel.HEADER_STATE_HIDDEN
         *      Polymer.PaperScrollHeaderPanel.HEADER_STATE_CONDENSED
         *      Polymer.PaperScrollHeaderPanel.HEADER_STATE_INTERPOLATED
73bcce88   luigser   COMPONENTS
272
273
274
275
         */
        headerState: {
          type: Number,
          readOnly: true,
0a86ccc8   Luigi Serra   graph and datalet...
276
          notify:true,
73bcce88   luigser   COMPONENTS
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
          value: 0
        },
  
        /** @type {number|null} */
        _defaultCondsensedHeaderHeight: {
          type: Number,
          value: 0
        }
      },
  
      observers: [
        '_setup(headerHeight, condensedHeaderHeight, fixed)',
        '_condensedHeaderHeightChanged(condensedHeaderHeight)',
        '_headerHeightChanged(headerHeight, condensedHeaderHeight)',
        '_condensesChanged(condenses)',
      ],
  
      listeners: {
        'iron-resize': 'measureHeaderHeight'
      },
  
      ready: function() {
73bcce88   luigser   COMPONENTS
299
300
301
302
        this._scrollHandler = this._scroll.bind(this);
        this.scroller.addEventListener('scroll', this._scrollHandler);
      },
  
0a86ccc8   Luigi Serra   graph and datalet...
303
304
305
      attached: function() {
        this.async(this.measureHeaderHeight, 1);
      },
73bcce88   luigser   COMPONENTS
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
  
      /**
       * Returns the header element.
       *
       * @property header
       * @type Object
       */
      get header() {
        return Polymer.dom(this.$.headerContent).getDistributedNodes()[0];
      },
  
      /**
       * Returns the content element.
       *
       * @property content
       * @type Object
       */
      get content() {
        return Polymer.dom(this.$.mainContent).getDistributedNodes()[0];
      },
  
      /**
       * Returns the scrollable element.
       *
       * @property scroller
       * @type Object
       */
      get scroller() {
        return this.$.mainContainer;
      },
  
      get _headerMaxDelta() {
        return this.keepCondensedHeader ? this._headerMargin : this.headerHeight;
      },
  
      get _headerMargin() {
        return this.headerHeight - this.condensedHeaderHeight;
      },
  
e619a3b0   Luigi Serra   Controllet cross ...
345
346
347
348
      _y: 0,
  
      _prevScrollTop: 0,
  
73bcce88   luigser   COMPONENTS
349
350
351
352
353
354
355
356
357
358
359
360
361
      /**
       * Invoke this to tell `paper-scroll-header-panel` to re-measure the header's
       * height.
       *
       * @method measureHeaderHeight
       */
      measureHeaderHeight: function() {
        var header = this.header;
        if (header && header.offsetHeight) {
          this.headerHeight = header.offsetHeight;
        }
      },
  
e619a3b0   Luigi Serra   Controllet cross ...
362
363
364
365
366
367
368
369
      /**
       * Scroll to a specific y coordinate.
       *
       * @method scroll
       * @param {number} top The coordinate to scroll to, along the y-axis.
       * @param {boolean} smooth true if the scroll position should be smoothly adjusted.
       */
      scroll: function(top, smooth) {
0a86ccc8   Luigi Serra   graph and datalet...
370
        // the scroll event will trigger _updateScrollState directly,
e619a3b0   Luigi Serra   Controllet cross ...
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
411
412
413
414
415
        // However, _updateScrollState relies on the previous `scrollTop` to update the states.
        // Calling _updateScrollState will ensure that the states are synced correctly.
  
        if (smooth) {
          // TODO(blasten): use CSS scroll-behavior once it ships in Chrome.
          var easingFn = function easeOutQuad(t, b, c, d) {
            t /= d;
            return -c * t*(t-2) + b;
          };
          var animationId = Math.random();
          var duration = 200;
          var startTime = Date.now();
          var currentScrollTop = this.scroller.scrollTop;
          var deltaScrollTop = top - currentScrollTop;
  
          this._currentAnimationId = animationId;
  
          (function updateFrame() {
            var now = Date.now();
            var elapsedTime = now - startTime;
  
            if (elapsedTime > duration) {
              this.scroller.scrollTop = top;
              this._updateScrollState(top);
  
            } else if (this._currentAnimationId === animationId) {
              this.scroller.scrollTop = easingFn(elapsedTime, currentScrollTop, deltaScrollTop, duration);
              requestAnimationFrame(updateFrame.bind(this));
            }
  
          }).call(this);
  
        } else {
          this.scroller.scrollTop = top;
          this._updateScrollState(top);
        }
      },
  
     /**
       * Condense the header.
       *
       * @method condense
       * @param {boolean} smooth true if the scroll position should be smoothly adjusted.
       */
      condense: function(smooth) {
0a86ccc8   Luigi Serra   graph and datalet...
416
        var ctx = Polymer.PaperScrollHeaderPanel;
e619a3b0   Luigi Serra   Controllet cross ...
417
418
        if (this.condenses && !this.fixed && !this.noReveal) {
          switch (this.headerState) {
0a86ccc8   Luigi Serra   graph and datalet...
419
            case ctx.HEADER_STATE_HIDDEN:
e619a3b0   Luigi Serra   Controllet cross ...
420
421
              this.scroll(this.scroller.scrollTop - (this._headerMaxDelta - this._headerMargin), smooth);
            break;
0a86ccc8   Luigi Serra   graph and datalet...
422
423
            case ctx.HEADER_STATE_EXPANDED:
            case ctx.HEADER_STATE_INTERPOLATED:
e619a3b0   Luigi Serra   Controllet cross ...
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
              this.scroll(this._headerMargin, smooth);
            break;
          }
        }
      },
  
      /**
       * Scroll to the top of the content.
       *
       * @method scrollToTop
       * @param {boolean} smooth true if the scroll position should be smoothly adjusted.
       */
      scrollToTop: function(smooth) {
        this.scroll(0, smooth);
      },
  
73bcce88   luigser   COMPONENTS
440
441
      _headerHeightChanged: function(headerHeight) {
        if (this._defaultCondsensedHeaderHeight !== null) {
e619a3b0   Luigi Serra   Controllet cross ...
442
          this._defaultCondsensedHeaderHeight = Math.round(headerHeight * 1/3);
73bcce88   luigser   COMPONENTS
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
          this.condensedHeaderHeight = this._defaultCondsensedHeaderHeight;
        }
      },
  
      _condensedHeaderHeightChanged: function(condensedHeaderHeight) {
        if (condensedHeaderHeight) {
          // a user custom value
          if (this._defaultCondsensedHeaderHeight != condensedHeaderHeight) {
            // disable the default value
            this._defaultCondsensedHeaderHeight = null;
          }
        }
      },
  
      _condensesChanged: function() {
e619a3b0   Luigi Serra   Controllet cross ...
458
459
        this._updateScrollState(this.scroller.scrollTop);
        this._condenseHeader(null);
73bcce88   luigser   COMPONENTS
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
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
      },
  
      _setup: function() {
        var s = this.scroller.style;
  
        s.paddingTop = this.fixed ? '' : this.headerHeight + 'px';
        s.top = this.fixed ? this.headerHeight + 'px' : '';
  
        if (this.fixed) {
          this._setHeaderState(this.HEADER_STATE_EXPANDED);
          this._transformHeader(null);
        } else {
          switch (this.headerState) {
            case this.HEADER_STATE_HIDDEN:
              this._transformHeader(this._headerMaxDelta);
              break;
            case this.HEADER_STATE_CONDENSED:
              this._transformHeader(this._headerMargin);
              break;
          }
        }
      },
  
      _transformHeader: function(y) {
        this._translateY(this.$.headerContainer, -y);
  
        if (this.condenses) {
          this._condenseHeader(y);
        }
  
        this.fire('paper-header-transform',
          { y: y,
            height: this.headerHeight,
            condensedHeight: this.condensedHeaderHeight
          }
        );
      },
  
      _condenseHeader: function(y) {
        var reset = (y === null);
  
        // adjust top bar in paper-header so the top bar stays at the top
        if (!this.scrollAwayTopbar && this.header && this.header.$ && this.header.$.topBar) {
          this._translateY(this.header.$.topBar,
              reset ? null : Math.min(y, this._headerMargin));
        }
        // transition header bg
        if (!this.noDissolve) {
          this.$.headerBg.style.opacity = reset ? '' :
              ( (this._headerMargin - y) / this._headerMargin);
        }
        // adjust header bg so it stays at the center
        this._translateY(this.$.headerBg, reset ? null : y / 2);
        // transition condensed header bg
        if (!this.noDissolve) {
          this.$.condensedHeaderBg.style.opacity = reset ? '' :
              (y / this._headerMargin);
  
          // adjust condensed header bg so it stays at the center
          this._translateY(this.$.condensedHeaderBg, reset ? null : y / 2);
        }
      },
  
      _translateY: function(node, y) {
        this.transform((y === null) ? '' : 'translate3d(0, ' + y + 'px, 0)', node);
      },
  
      /** @param {Event=} event */
      _scroll: function(event) {
e619a3b0   Luigi Serra   Controllet cross ...
529
530
531
532
533
534
535
536
537
        if (this.header) {
          this._updateScrollState(this.scroller.scrollTop);
  
          this.fire('content-scroll', {
            target: this.scroller
          },
          {
            cancelable: false
          });
73bcce88   luigser   COMPONENTS
538
        }
e619a3b0   Luigi Serra   Controllet cross ...
539
      },
73bcce88   luigser   COMPONENTS
540
  
e619a3b0   Luigi Serra   Controllet cross ...
541
      _updateScrollState: function(scrollTop) {
0a86ccc8   Luigi Serra   graph and datalet...
542
        var ctx = Polymer.PaperScrollHeaderPanel;
e619a3b0   Luigi Serra   Controllet cross ...
543
544
        var deltaScrollTop = scrollTop - this._prevScrollTop;
        var y = Math.max(0, (this.noReveal) ? scrollTop : this._y + deltaScrollTop);
73bcce88   luigser   COMPONENTS
545
546
547
  
        if (y > this._headerMaxDelta) {
          y = this._headerMaxDelta;
73bcce88   luigser   COMPONENTS
548
  
0a86ccc8   Luigi Serra   graph and datalet...
549
550
551
552
553
554
          if (this.keepCondensedHeader) {
            this._setHeaderState(ctx.HEADER_STATE_CONDENSED);
          } else {
            this._setHeaderState(ctx.HEADER_STATE_HIDDEN);
          }
        } else if (this.condenses && scrollTop >= this._headerMargin) {
73bcce88   luigser   COMPONENTS
555
          y = Math.max(y, this._headerMargin);
0a86ccc8   Luigi Serra   graph and datalet...
556
          this._setHeaderState(ctx.HEADER_STATE_CONDENSED);
73bcce88   luigser   COMPONENTS
557
558
  
        } else if (y === 0) {
0a86ccc8   Luigi Serra   graph and datalet...
559
          this._setHeaderState(ctx.HEADER_STATE_EXPANDED);
73bcce88   luigser   COMPONENTS
560
561
  
        } else {
0a86ccc8   Luigi Serra   graph and datalet...
562
          this._setHeaderState(ctx.HEADER_STATE_INTERPOLATED);
73bcce88   luigser   COMPONENTS
563
564
        }
  
e619a3b0   Luigi Serra   Controllet cross ...
565
        if (!this.fixed && y !== this._y) {
73bcce88   luigser   COMPONENTS
566
567
568
          this._transformHeader(y);
        }
  
e619a3b0   Luigi Serra   Controllet cross ...
569
        this._prevScrollTop = Math.max(scrollTop, 0);
73bcce88   luigser   COMPONENTS
570
        this._y = y;
73bcce88   luigser   COMPONENTS
571
      }
73bcce88   luigser   COMPONENTS
572
573
    });
  
0a86ccc8   Luigi Serra   graph and datalet...
574
575
576
577
578
    Polymer.PaperScrollHeaderPanel.HEADER_STATE_EXPANDED = 0;
    Polymer.PaperScrollHeaderPanel.HEADER_STATE_HIDDEN = 1;
    Polymer.PaperScrollHeaderPanel.HEADER_STATE_CONDENSED = 2;
    Polymer.PaperScrollHeaderPanel.HEADER_STATE_INTERPOLATED = 3;
  
73bcce88   luigser   COMPONENTS
579
580
581
  })();
  
  </script>