Blame view

bower_components/iron-scroll-target-behavior/iron-scroll-target-behavior.html 6.41 KB
a53fbbed   Renato De Donato   select-dataset ne...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
77
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
103
104
105
106
107
108
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
169
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
  <!--
  @license
  Copyright (c) 2016 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">
  
  <script>
  
    /**
     * `Polymer.IronScrollTargetBehavior` allows an element to respond to scroll events from a
     * designated scroll target.
     *
     * Elements that consume this behavior can override the `_scrollHandler`
     * method to add logic on the scroll event.
     *
     * @demo demo/scrolling-region.html Scrolling Region
     * @demo demo/document.html Document Element
     * @polymerBehavior
     */
    Polymer.IronScrollTargetBehavior = {
  
      properties: {
  
        /**
         * Specifies the element that will handle the scroll event
         * on the behalf of the current element. This is typically a reference to an element,
         * but there are a few more posibilities:
         *
         * ### Elements id
         *
         *```html
         * <div id="scrollable-element" style="overflow: auto;">
         *  <x-element scroll-target="scrollable-element">
         *    <!-- Content-->
         *  </x-element>
         * </div>
         *```
         * In this case, the `scrollTarget` will point to the outer div element. 
         *
         * ### Document scrolling
         *
         * For document scrolling, you can use the reserved word `document`:
         *
         *```html
         * <x-element scroll-target="document">
         *   <!-- Content -->
         * </x-element>
         *```
         *
         * ### Elements reference
         *
         *```js
         * appHeader.scrollTarget = document.querySelector('#scrollable-element');
         *```
         * 
         * @type {HTMLElement}
         */
        scrollTarget: {
          type: HTMLElement,
          value: function() {
            return this._defaultScrollTarget;
          }
        }
      },
  
      observers: [
        '_scrollTargetChanged(scrollTarget, isAttached)'
      ],
  
      _scrollTargetChanged: function(scrollTarget, isAttached) {
        var eventTarget;
  
        if (this._oldScrollTarget) {
          eventTarget = this._oldScrollTarget === this._doc ? window : this._oldScrollTarget;
          eventTarget.removeEventListener('scroll', this._boundScrollHandler);
          this._oldScrollTarget = null;
        }
  
        if (!isAttached) {
          return;
        }
        // Support element id references
        if (scrollTarget === 'document') {
  
          this.scrollTarget = this._doc;
  
        } else if (typeof scrollTarget === 'string') {
  
          this.scrollTarget = this.domHost ? this.domHost.$[scrollTarget] :
              Polymer.dom(this.ownerDocument).querySelector('#' + scrollTarget);
  
        } else if (this._isValidScrollTarget()) {
  
          eventTarget = scrollTarget === this._doc ? window : scrollTarget;
          this._boundScrollHandler = this._boundScrollHandler || this._scrollHandler.bind(this);
          this._oldScrollTarget = scrollTarget;
  
          eventTarget.addEventListener('scroll', this._boundScrollHandler);
        }
      },
  
      /**
       * Runs on every scroll event. Consumer of this behavior may override this method.
       *
       * @protected
       */
      _scrollHandler: function scrollHandler() {},
  
      /**
       * The default scroll target. Consumers of this behavior may want to customize
       * the default scroll target.
       *
       * @type {Element}
       */
      get _defaultScrollTarget() {
        return this._doc;
      },
  
      /**
       * Shortcut for the document element
       *
       * @type {Element}
       */
      get _doc() {
        return this.ownerDocument.documentElement;
      },
  
      /**
       * Gets the number of pixels that the content of an element is scrolled upward.
       *
       * @type {number}
       */
      get _scrollTop() {
        if (this._isValidScrollTarget()) {
          return this.scrollTarget === this._doc ? window.pageYOffset : this.scrollTarget.scrollTop;
        }
        return 0;
      },
  
      /**
       * Gets the number of pixels that the content of an element is scrolled to the left.
       *
       * @type {number}
       */
      get _scrollLeft() {
        if (this._isValidScrollTarget()) {
          return this.scrollTarget === this._doc ? window.pageXOffset : this.scrollTarget.scrollLeft;
        }
        return 0;
      },
  
      /**
       * Sets the number of pixels that the content of an element is scrolled upward.
       *
       * @type {number}
       */
      set _scrollTop(top) {
        if (this.scrollTarget === this._doc) {
          window.scrollTo(window.pageXOffset, top);
        } else if (this._isValidScrollTarget()) {
          this.scrollTarget.scrollTop = top;
        }
      },
  
      /**
       * Sets the number of pixels that the content of an element is scrolled to the left.
       *
       * @type {number}
       */
      set _scrollLeft(left) {
        if (this.scrollTarget === this._doc) {
          window.scrollTo(left, window.pageYOffset);
        } else if (this._isValidScrollTarget()) {
          this.scrollTarget.scrollLeft = left;
        }
      },
  
      /**
       * Scrolls the content to a particular place.
       *
       * @method scroll
       * @param {number} left The left position
       * @param {number} top The top position
       */
      scroll: function(left, top) {
         if (this.scrollTarget === this._doc) {
          window.scrollTo(left, top);
        } else if (this._isValidScrollTarget()) {
          this.scrollTarget.scrollLeft = left;
          this.scrollTarget.scrollTop = top;
        }
      },
  
      /**
       * Gets the width of the scroll target.
       *
       * @type {number}
       */
      get _scrollTargetWidth() {
        if (this._isValidScrollTarget()) {
          return this.scrollTarget === this._doc ? window.innerWidth : this.scrollTarget.offsetWidth;
        }
        return 0;
      },
  
      /**
       * Gets the height of the scroll target.
       *
       * @type {number}
       */
      get _scrollTargetHeight() {
        if (this._isValidScrollTarget()) {
          return this.scrollTarget === this._doc ? window.innerHeight : this.scrollTarget.offsetHeight;
        }
        return 0;
      },
  
      /**
       * Returns true if the scroll target is a valid HTMLElement.
       *
       * @return {boolean}
       */
      _isValidScrollTarget: function() {
        return this.scrollTarget instanceof HTMLElement;
      }
    };
  
  </script>