Blame view

bower_components/iron-list/iron-list.html 39.6 KB
73bcce88   luigser   COMPONENTS
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
  <!--

  @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">

  

  <!--

  

  `iron-list` displays a virtual, 'infinite' list. The template inside

  the iron-list element represents the DOM to create for each list item.

  The `items` property specifies an array of list item data.

  

  For performance reasons, not every item in the list is rendered at once;

  instead a small subset of actual template elements *(enough to fill the viewport)*

  are rendered and reused as the user scrolls. As such, it is important that all

  state of the list template be bound to the model driving it, since the view may

  be reused with a new model at any time. Particularly, any state that may change

  as the result of a user interaction with the list item must be bound to the model

  to avoid view state inconsistency.

  

a1a3bc73   Luigi Serra   graphs updates
28
  __Important:__ `iron-list` must either be explicitly sized, or delegate scrolling to an

73bcce88   luigser   COMPONENTS
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
  explicitly sized parent. By "explicitly sized", we mean it either has an explicit

  CSS `height` property set via a class or inline style, or else is sized by other

  layout means (e.g. the `flex` or `fit` classes).

  

  ### Template model

  

  List item templates should bind to template models of the following structure:

  

      {

        index: 0,     // data index for this item

        item: {       // user data corresponding to items[index]

          /* user item data  */

        }

      }

  

  Alternatively, you can change the property name used as data index by changing the

  `indexAs` property. The `as` property defines the name of the variable to add to the binding

  scope for the array.

  

  For example, given the following `data` array:

  

  ##### data.json

  

a1a3bc73   Luigi Serra   graphs updates
52
53
54
55
56
57
58
  ```js

  [

    {"name": "Bob"},

    {"name": "Tim"},

    {"name": "Mike"}

  ]

  ```

73bcce88   luigser   COMPONENTS
59
60
61
62
  

  The following code would render the list (note the name and checked properties are

  bound from the model object provided to the template scope):

  

a1a3bc73   Luigi Serra   graphs updates
63
64
65
66
67
68
69
70
  ```html

  <template is="dom-bind">

    <iron-ajax url="data.json" last-response="{{data}}" auto></iron-ajax>

    <iron-list items="[[data]]" as="item">

      <template>

        <div>

          Name: <span>[[item.name]]</span>

        </div>

73bcce88   luigser   COMPONENTS
71
      </template>

a1a3bc73   Luigi Serra   graphs updates
72
73
74
    </iron-list>

  </template>

  ```

73bcce88   luigser   COMPONENTS
75
  

eb240478   Luigi Serra   public room cards...
76
77
78
79
  ### Styling

  

  Use the `--iron-list-items-container` mixin to style the container of items, e.g.

  

a1a3bc73   Luigi Serra   graphs updates
80
81
82
83
84
85
86
  ```css

  iron-list {

   --iron-list-items-container: {

      margin: auto;

    };

  }

  ```

eb240478   Luigi Serra   public room cards...
87
  

e619a3b0   Luigi Serra   Controllet cross ...
88
89
  ### Resizing

  

a1a3bc73   Luigi Serra   graphs updates
90
  `iron-list` lays out the items when it receives a notification via the `iron-resize` event.

e619a3b0   Luigi Serra   Controllet cross ...
91
92
93
94
95
96
97
  This event is fired by any element that implements `IronResizableBehavior`.

  

  By default, elements such as `iron-pages`, `paper-tabs` or `paper-dialog` will trigger

  this event automatically. If you hide the list manually (e.g. you use `display: none`)

  you might want to implement `IronResizableBehavior` or fire this event manually right

  after the list became visible again. e.g.

  

a1a3bc73   Luigi Serra   graphs updates
98
99
100
101
102
103
104
105
106
107
  ```js

  document.querySelector('iron-list').fire('iron-resize');

  ```

  

  ### When should `<iron-list>` be used?

  

  `iron-list` should be used when a page has significantly more DOM nodes than the ones

  visible on the screen. e.g. the page has 500 nodes, but only 20 are visible at the time.

  This is why we refer to it as a `virtual` list. In this case, a `dom-repeat` will still

  create 500 nodes which could slow down the web app, but `iron-list` will only create 20.

e619a3b0   Luigi Serra   Controllet cross ...
108
  

a1a3bc73   Luigi Serra   graphs updates
109
110
111
112
  However, having an `iron-list` does not mean that you can load all the data at once. 

  Say, you have a million records in the database, you want to split the data into pages

  so you can bring a page at the time. The page could contain 500 items, and iron-list

  will only render 20.

73bcce88   luigser   COMPONENTS
113
114
115
  

  @group Iron Element

  @element iron-list

eb240478   Luigi Serra   public room cards...
116
117
118
  @demo demo/index.html Simple list

  @demo demo/selection.html Selection of items

  @demo demo/collapse.html Collapsable items

73bcce88   luigser   COMPONENTS
119
120
121
  -->

  

  <dom-module id="iron-list">

eb240478   Luigi Serra   public room cards...
122
123
124
125
126
    <template>

      <style>

        :host {

          display: block;

        }

73bcce88   luigser   COMPONENTS
127
  

eb240478   Luigi Serra   public room cards...
128
129
130
        :host(.has-scroller) {

          overflow: auto;

        }

e619a3b0   Luigi Serra   Controllet cross ...
131
  

eb240478   Luigi Serra   public room cards...
132
133
134
        :host(:not(.has-scroller)) {

          position: relative;

        }

73bcce88   luigser   COMPONENTS
135
  

eb240478   Luigi Serra   public room cards...
136
137
138
139
        #items {

          @apply(--iron-list-items-container);

          position: relative;

        }

73bcce88   luigser   COMPONENTS
140
  

eb240478   Luigi Serra   public room cards...
141
142
143
144
145
146
147
148
        #items > ::content > * {

          width: 100%;

          box-sizing: border-box;

          position: absolute;

          top: 0;

          will-change: transform;

        }

      </style>

e619a3b0   Luigi Serra   Controllet cross ...
149
150
  

      <array-selector id="selector" items="{{items}}"

eb240478   Luigi Serra   public room cards...
151
        selected="{{selectedItems}}" selected-item="{{selectedItem}}">

e619a3b0   Luigi Serra   Controllet cross ...
152
153
      </array-selector>

  

73bcce88   luigser   COMPONENTS
154
155
156
      <div id="items">

        <content></content>

      </div>

e619a3b0   Luigi Serra   Controllet cross ...
157
  

73bcce88   luigser   COMPONENTS
158
159
160
161
162
163
164
165
166
    </template>

  </dom-module>

  

  <script>

  

  (function() {

  

    var IOS = navigator.userAgent.match(/iP(?:hone|ad;(?: U;)? CPU) OS (\d+)/);

    var IOS_TOUCH_SCROLLING = IOS && IOS[1] >= 8;

a1a3bc73   Luigi Serra   graphs updates
167
    var DEFAULT_PHYSICAL_COUNT = 3;

e619a3b0   Luigi Serra   Controllet cross ...
168
    var MAX_PHYSICAL_COUNT = 500;

73bcce88   luigser   COMPONENTS
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
  

    Polymer({

  

      is: 'iron-list',

  

      properties: {

  

        /**

         * An array containing items determining how many instances of the template

         * to stamp and that that each template instance should bind to.

         */

        items: {

          type: Array

        },

  

        /**

         * The name of the variable to add to the binding scope for the array

         * element associated with a given template instance.

         */

        as: {

          type: String,

          value: 'item'

        },

  

        /**

         * The name of the variable to add to the binding scope with the index

eb240478   Luigi Serra   public room cards...
195
         * for the row.

73bcce88   luigser   COMPONENTS
196
197
198
199
         */

        indexAs: {

          type: String,

          value: 'index'

e619a3b0   Luigi Serra   Controllet cross ...
200
        },

73bcce88   luigser   COMPONENTS
201
  

e619a3b0   Luigi Serra   Controllet cross ...
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
        /**

         * The name of the variable to add to the binding scope to indicate

         * if the row is selected.

         */

        selectedAs: {

          type: String,

          value: 'selected'

        },

  

        /**

         * When true, tapping a row will select the item, placing its data model

         * in the set of selected items retrievable via the selection property.

         *

         * Note that tapping focusable elements within the list item will not

         * result in selection, since they are presumed to have their * own action.

         */

        selectionEnabled: {

          type: Boolean,

          value: false

        },

  

        /**

         * When `multiSelection` is false, this is the currently selected item, or `null`

         * if no item is selected.

         */

        selectedItem: {

          type: Object,

          notify: true

        },

  

        /**

         * When `multiSelection` is true, this is an array that contains the selected items.

         */

        selectedItems: {

          type: Object,

          notify: true

        },

  

        /**

         * When `true`, multiple items may be selected at once (in this case,

         * `selected` is an array of currently selected items).  When `false`,

         * only one item may be selected at a time.

         */

        multiSelection: {

          type: Boolean,

          value: false

        }

73bcce88   luigser   COMPONENTS
249
250
251
      },

  

      observers: [

e619a3b0   Luigi Serra   Controllet cross ...
252
253
254
        '_itemsChanged(items.*)',

        '_selectionEnabledChanged(selectionEnabled)',

        '_multiSelectionChanged(multiSelection)'

73bcce88   luigser   COMPONENTS
255
256
257
258
259
260
261
262
263
264
265
266
267
      ],

  

      behaviors: [

        Polymer.Templatizer,

        Polymer.IronResizableBehavior

      ],

  

      listeners: {

        'iron-resize': '_resizeHandler'

      },

  

      /**

       * The ratio of hidden tiles that should remain in the scroll direction.

e619a3b0   Luigi Serra   Controllet cross ...
268
       * Recommended value ~0.5, so it will distribute tiles evely in both directions.

73bcce88   luigser   COMPONENTS
269
270
271
272
273
       */

      _ratio: 0.5,

  

      /**

       * The element that controls the scroll

e619a3b0   Luigi Serra   Controllet cross ...
274
       * @type {?Element}

73bcce88   luigser   COMPONENTS
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
       */

      _scroller: null,

  

      /**

       * The padding-top value of the `scroller` element

       */

      _scrollerPaddingTop: 0,

  

      /**

       * This value is the same as `scrollTop`.

       */

      _scrollPosition: 0,

  

      /**

       * The number of tiles in the DOM.

       */

e619a3b0   Luigi Serra   Controllet cross ...
291
      _physicalCount: 0,

73bcce88   luigser   COMPONENTS
292
293
294
295
296
297
298
299
300
301
302
303
304
305
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
  

      /**

       * The k-th tile that is at the top of the scrolling list.

       */

      _physicalStart: 0,

  

      /**

       * The k-th tile that is at the bottom of the scrolling list.

       */

      _physicalEnd: 0,

  

      /**

       * The sum of the heights of all the tiles in the DOM.

       */

      _physicalSize: 0,

  

      /**

       * The average `offsetHeight` of the tiles observed till now.

       */

      _physicalAverage: 0,

  

      /**

       * The number of tiles which `offsetHeight` > 0 observed until now.

       */

      _physicalAverageCount: 0,

  

      /**

       * The Y position of the item rendered in the `_physicalStart`

       * tile relative to the scrolling list.

       */

      _physicalTop: 0,

  

      /**

       * The number of items in the list.

       */

      _virtualCount: 0,

  

      /**

       * The n-th item rendered in the `_physicalStart` tile.

       */

      _virtualStartVal: 0,

  

      /**

       * A map between an item key and its physical item index

       */

e619a3b0   Luigi Serra   Controllet cross ...
337
338
339
340
341
342
      _physicalIndexForKey: null,

  

      /**

       * The estimated scroll height based on `_physicalAverage`

       */

      _estScrollHeight: 0,

73bcce88   luigser   COMPONENTS
343
344
  

      /**

e619a3b0   Luigi Serra   Controllet cross ...
345
       * The scroll height of the dom node

73bcce88   luigser   COMPONENTS
346
       */

e619a3b0   Luigi Serra   Controllet cross ...
347
      _scrollHeight: 0,

73bcce88   luigser   COMPONENTS
348
349
  

      /**

a1a3bc73   Luigi Serra   graphs updates
350
       * The height of the list. This is referred as the viewport in the context of list.

73bcce88   luigser   COMPONENTS
351
352
353
354
355
       */

      _viewportSize: 0,

  

      /**

       * An array of DOM nodes that are currently in the tree

e619a3b0   Luigi Serra   Controllet cross ...
356
       * @type {?Array<!TemplatizerNode>}

73bcce88   luigser   COMPONENTS
357
358
359
360
361
       */

      _physicalItems: null,

  

      /**

       * An array of heights for each item in `_physicalItems`

e619a3b0   Luigi Serra   Controllet cross ...
362
       * @type {?Array<number>}

73bcce88   luigser   COMPONENTS
363
364
365
366
367
368
       */

      _physicalSizes: null,

  

      /**

       * A cached value for the visible index.

       * See `firstVisibleIndex`

e619a3b0   Luigi Serra   Controllet cross ...
369
       * @type {?number}

73bcce88   luigser   COMPONENTS
370
371
372
373
374
       */

      _firstVisibleIndexVal: null,

  

      /**

       * A Polymer collection for the items.

e619a3b0   Luigi Serra   Controllet cross ...
375
       * @type {?Polymer.Collection}

73bcce88   luigser   COMPONENTS
376
377
378
       */

      _collection: null,

  

e619a3b0   Luigi Serra   Controllet cross ...
379
380
381
382
383
384
385
      /**

       * True if the current item list was rendered for the first time

       * after attached.

       */

      _itemsRendered: false,

  

      /**

a1a3bc73   Luigi Serra   graphs updates
386
387
388
389
390
391
392
393
394
395
       * The page that is currently rendered.

       */

      _lastPage: null,

  

      /**

       * The max number of pages to render. One page is equivalent to the height of the list.

       */

      _maxPages: 3,

  

      /**

e619a3b0   Luigi Serra   Controllet cross ...
396
397
       * The bottom of the physical content.

       */

73bcce88   luigser   COMPONENTS
398
399
400
401
      get _physicalBottom() {

        return this._physicalTop + this._physicalSize;

      },

  

e619a3b0   Luigi Serra   Controllet cross ...
402
      /**

f748e9cf   Luigi Serra   new controllet an...
403
404
405
406
407
408
409
       * The bottom of the scroll.

       */

      get _scrollBottom() {

        return this._scrollPosition + this._viewportSize;

      },

  

      /**

e619a3b0   Luigi Serra   Controllet cross ...
410
411
       * The n-th item rendered in the last physical item.

       */

73bcce88   luigser   COMPONENTS
412
413
414
415
      get _virtualEnd() {

        return this._virtualStartVal + this._physicalCount - 1;

      },

  

e619a3b0   Luigi Serra   Controllet cross ...
416
417
418
419
420
421
422
423
      /**

       * The lowest n-th value for an item such that it can be rendered in `_physicalStart`.

       */

      _minVirtualStart: 0,

  

      /**

       * The largest n-th value for an item such that it can be rendered in `_physicalStart`.

       */

73bcce88   luigser   COMPONENTS
424
      get _maxVirtualStart() {

eb240478   Luigi Serra   public room cards...
425
        return Math.max(0, this._virtualCount - this._physicalCount);

73bcce88   luigser   COMPONENTS
426
427
      },

  

e619a3b0   Luigi Serra   Controllet cross ...
428
429
430
      /**

       * The height of the physical content that isn't on the screen.

       */

73bcce88   luigser   COMPONENTS
431
432
433
434
      get _hiddenContentSize() {

        return this._physicalSize - this._viewportSize;

      },

  

e619a3b0   Luigi Serra   Controllet cross ...
435
436
437
      /**

       * The maximum scroll top value.

       */

73bcce88   luigser   COMPONENTS
438
      get _maxScrollTop() {

e619a3b0   Luigi Serra   Controllet cross ...
439
        return this._estScrollHeight - this._viewportSize;

73bcce88   luigser   COMPONENTS
440
441
      },

  

e619a3b0   Luigi Serra   Controllet cross ...
442
443
444
      /**

       * Sets the n-th item rendered in `_physicalStart`

       */

73bcce88   luigser   COMPONENTS
445
      set _virtualStart(val) {

e619a3b0   Luigi Serra   Controllet cross ...
446
        // clamp the value so that _minVirtualStart <= val <= _maxVirtualStart

73bcce88   luigser   COMPONENTS
447
448
449
450
451
        this._virtualStartVal = Math.min(this._maxVirtualStart, Math.max(this._minVirtualStart, val));

        this._physicalStart = this._virtualStartVal % this._physicalCount;

        this._physicalEnd = (this._physicalStart + this._physicalCount - 1) % this._physicalCount;

      },

  

e619a3b0   Luigi Serra   Controllet cross ...
452
453
454
      /**

       * Gets the n-th item rendered in `_physicalStart`

       */

73bcce88   luigser   COMPONENTS
455
456
457
458
459
      get _virtualStart() {

        return this._virtualStartVal;

      },

  

      /**

e619a3b0   Luigi Serra   Controllet cross ...
460
461
462
463
464
465
466
       * An optimal physical size such that we will have enough physical items

       * to fill up the viewport and recycle when the user scrolls.

       *

       * This default value assumes that we will at least have the equivalent

       * to a viewport of physical items above and below the user's viewport.

       */

      get _optPhysicalSize() {

a1a3bc73   Luigi Serra   graphs updates
467
        return this._viewportSize * this._maxPages;

e619a3b0   Luigi Serra   Controllet cross ...
468
469
470
471
472
473
474
475
476
477
      },

  

     /**

      * True if the current list is visible.

      */

      get _isVisible() {

        return this._scroller && Boolean(this._scroller.offsetWidth || this._scroller.offsetHeight);

      },

  

      /**

eb240478   Luigi Serra   public room cards...
478
       * Gets the index of the first visible item in the viewport.

73bcce88   luigser   COMPONENTS
479
       *

eb240478   Luigi Serra   public room cards...
480
       * @type {number}

73bcce88   luigser   COMPONENTS
481
482
483
484
       */

      get firstVisibleIndex() {

        var physicalOffset;

  

e619a3b0   Luigi Serra   Controllet cross ...
485
        if (this._firstVisibleIndexVal === null) {

73bcce88   luigser   COMPONENTS
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
          physicalOffset = this._physicalTop;

  

          this._firstVisibleIndexVal = this._iterateItems(

            function(pidx, vidx) {

              physicalOffset += this._physicalSizes[pidx];

  

              if (physicalOffset > this._scrollPosition) {

                return vidx;

              }

            }) || 0;

        }

  

        return this._firstVisibleIndexVal;

      },

  

e619a3b0   Luigi Serra   Controllet cross ...
501
502
503
504
505
506
507
508
509
510
511
512
513
      ready: function() {

        if (IOS_TOUCH_SCROLLING) {

          this._scrollListener = function() {

            requestAnimationFrame(this._scrollHandler.bind(this));

          }.bind(this);

        } else {

          this._scrollListener = this._scrollHandler.bind(this);

        }

      },

  

      /**

       * When the element has been attached to the DOM tree.

       */

73bcce88   luigser   COMPONENTS
514
515
516
517
518
      attached: function() {

        // delegate to the parent's scroller

        // e.g. paper-scroll-header-panel

        var el = Polymer.dom(this);

  

e619a3b0   Luigi Serra   Controllet cross ...
519
520
521
        var parentNode = /** @type {?{scroller: ?Element}} */ (el.parentNode);

        if (parentNode && parentNode.scroller) {

          this._scroller = parentNode.scroller;

73bcce88   luigser   COMPONENTS
522
523
524
525
526
527
528
        } else {

          this._scroller = this;

          this.classList.add('has-scroller');

        }

  

        if (IOS_TOUCH_SCROLLING) {

          this._scroller.style.webkitOverflowScrolling = 'touch';

e619a3b0   Luigi Serra   Controllet cross ...
529
        }

73bcce88   luigser   COMPONENTS
530
  

e619a3b0   Luigi Serra   Controllet cross ...
531
532
533
534
535
536
537
538
539
540
541
542
543
        this._scroller.addEventListener('scroll', this._scrollListener);

  

        this.updateViewportBoundaries();

        this._render();

      },

  

      /**

       * When the element has been removed from the DOM tree.

       */

      detached: function() {

        this._itemsRendered = false;

        if (this._scroller) {

          this._scroller.removeEventListener('scroll', this._scrollListener);

73bcce88   luigser   COMPONENTS
544
545
546
547
548
549
550
551
552
553
554
        }

      },

  

      /**

       * Invoke this method if you dynamically update the viewport's

       * size or CSS padding.

       *

       * @method updateViewportBoundaries

       */

      updateViewportBoundaries: function() {

        var scrollerStyle = window.getComputedStyle(this._scroller);

e619a3b0   Luigi Serra   Controllet cross ...
555
        this._scrollerPaddingTop = parseInt(scrollerStyle['padding-top'], 10);

73bcce88   luigser   COMPONENTS
556
557
558
559
560
561
562
563
        this._viewportSize = this._scroller.offsetHeight;

      },

  

      /**

       * Update the models, the position of the

       * items in the viewport and recycle tiles as needed.

       */

      _refresh: function() {

73bcce88   luigser   COMPONENTS
564
565
566
567
        // clamp the `scrollTop` value

        // IE 10|11 scrollTop may go above `_maxScrollTop`

        // iOS `scrollTop` may go below 0 and above `_maxScrollTop`

        var scrollTop = Math.max(0, Math.min(this._maxScrollTop, this._scroller.scrollTop));

a1a3bc73   Luigi Serra   graphs updates
568
        var tileHeight, tileTop, kth, recycledTileSet, scrollBottom, physicalBottom;

73bcce88   luigser   COMPONENTS
569
570
        var ratio = this._ratio;

        var delta = scrollTop - this._scrollPosition;

73bcce88   luigser   COMPONENTS
571
572
573
574
575
576
577
578
579
580
581
        var recycledTiles = 0;

        var hiddenContentSize = this._hiddenContentSize;

        var currentRatio = ratio;

        var movingUp = [];

  

        // track the last `scrollTop`

        this._scrollPosition = scrollTop;

  

        // clear cached visible index

        this._firstVisibleIndexVal = null;

  

f748e9cf   Luigi Serra   new controllet an...
582
        scrollBottom = this._scrollBottom;

a1a3bc73   Luigi Serra   graphs updates
583
        physicalBottom = this._physicalBottom;

f748e9cf   Luigi Serra   new controllet an...
584
  

73bcce88   luigser   COMPONENTS
585
        // random access

e619a3b0   Luigi Serra   Controllet cross ...
586
        if (Math.abs(delta) > this._physicalSize) {

73bcce88   luigser   COMPONENTS
587
          this._physicalTop += delta;

73bcce88   luigser   COMPONENTS
588
589
590
591
592
593
          recycledTiles =  Math.round(delta / this._physicalAverage);

        }

        // scroll up

        else if (delta < 0) {

          var topSpace = scrollTop - this._physicalTop;

          var virtualStart = this._virtualStart;

73bcce88   luigser   COMPONENTS
594
  

73bcce88   luigser   COMPONENTS
595
596
597
598
599
600
601
602
603
604
605
606
          recycledTileSet = [];

  

          kth = this._physicalEnd;

          currentRatio = topSpace / hiddenContentSize;

  

          // move tiles from bottom to top

          while (

              // approximate `currentRatio` to `ratio`

              currentRatio < ratio &&

              // recycle less physical items than the total

              recycledTiles < this._physicalCount &&

              // ensure that these recycled tiles are needed

f748e9cf   Luigi Serra   new controllet an...
607
608
609
              virtualStart - recycledTiles > 0 &&

              // ensure that the tile is not visible

              physicalBottom - this._physicalSizes[kth] > scrollBottom

73bcce88   luigser   COMPONENTS
610
611
          ) {

  

f748e9cf   Luigi Serra   new controllet an...
612
            tileHeight = this._physicalSizes[kth];

73bcce88   luigser   COMPONENTS
613
            currentRatio += tileHeight / hiddenContentSize;

f748e9cf   Luigi Serra   new controllet an...
614
            physicalBottom -= tileHeight;

73bcce88   luigser   COMPONENTS
615
616
617
618
619
620
621
            recycledTileSet.push(kth);

            recycledTiles++;

            kth = (kth === 0) ? this._physicalCount - 1 : kth - 1;

          }

  

          movingUp = recycledTileSet;

          recycledTiles = -recycledTiles;

73bcce88   luigser   COMPONENTS
622
623
624
        }

        // scroll down

        else if (delta > 0) {

a1a3bc73   Luigi Serra   graphs updates
625
          var bottomSpace = physicalBottom - scrollBottom;

73bcce88   luigser   COMPONENTS
626
627
628
          var virtualEnd = this._virtualEnd;

          var lastVirtualItemIndex = this._virtualCount-1;

  

73bcce88   luigser   COMPONENTS
629
630
631
632
633
634
635
636
637
638
639
640
          recycledTileSet = [];

  

          kth = this._physicalStart;

          currentRatio = bottomSpace / hiddenContentSize;

  

          // move tiles from top to bottom

          while (

              // approximate `currentRatio` to `ratio`

              currentRatio < ratio &&

              // recycle less physical items than the total

              recycledTiles < this._physicalCount &&

              // ensure that these recycled tiles are needed

f748e9cf   Luigi Serra   new controllet an...
641
642
643
              virtualEnd + recycledTiles < lastVirtualItemIndex &&

              // ensure that the tile is not visible

              this._physicalTop + this._physicalSizes[kth] < scrollTop

73bcce88   luigser   COMPONENTS
644
645
            ) {

  

f748e9cf   Luigi Serra   new controllet an...
646
            tileHeight = this._physicalSizes[kth];

73bcce88   luigser   COMPONENTS
647
648
649
650
651
652
653
654
655
            currentRatio += tileHeight / hiddenContentSize;

  

            this._physicalTop += tileHeight;

            recycledTileSet.push(kth);

            recycledTiles++;

            kth = (kth + 1) % this._physicalCount;

          }

        }

  

f748e9cf   Luigi Serra   new controllet an...
656
657
658
659
        if (recycledTiles === 0) {

          // If the list ever reach this case, the physical average is not significant enough

          // to create all the items needed to cover the entire viewport.

          // e.g. A few items have a height that differs from the average by serveral order of magnitude.

a1a3bc73   Luigi Serra   graphs updates
660
661
          if (physicalBottom < scrollBottom || this._physicalTop > scrollTop) {

            this.async(this._increasePool.bind(this, 1));

f748e9cf   Luigi Serra   new controllet an...
662
663
          }

        } else {

73bcce88   luigser   COMPONENTS
664
665
666
667
668
          this._virtualStart = this._virtualStart + recycledTiles;

          this._update(recycledTileSet, movingUp);

        }

      },

  

e619a3b0   Luigi Serra   Controllet cross ...
669
670
671
672
673
674
      /**

       * Update the list of items, starting from the `_virtualStartVal` item.

       * @param {!Array<number>=} itemSet

       * @param {!Array<number>=} movingUp

       */

      _update: function(itemSet, movingUp) {

73bcce88   luigser   COMPONENTS
675
        // update models

e619a3b0   Luigi Serra   Controllet cross ...
676
        this._assignModels(itemSet);

73bcce88   luigser   COMPONENTS
677
678
  

        // measure heights

eb240478   Luigi Serra   public room cards...
679
        this._updateMetrics(itemSet);

73bcce88   luigser   COMPONENTS
680
681
682
683
684
685
686
  

        // adjust offset after measuring

        if (movingUp) {

          while (movingUp.length) {

            this._physicalTop -= this._physicalSizes[movingUp.pop()];

          }

        }

e619a3b0   Luigi Serra   Controllet cross ...
687
        // update the position of the items

73bcce88   luigser   COMPONENTS
688
689
690
691
        this._positionItems();

  

        // set the scroller size

        this._updateScrollerSize();

e619a3b0   Luigi Serra   Controllet cross ...
692
  

a1a3bc73   Luigi Serra   graphs updates
693
694
        // increase the pool of physical items

        this._increasePoolIfNeeded();

73bcce88   luigser   COMPONENTS
695
696
      },

  

e619a3b0   Luigi Serra   Controllet cross ...
697
698
699
700
701
702
703
704
705
706
      /**

       * Creates a pool of DOM elements and attaches them to the local dom.

       */

      _createPool: function(size) {

        var physicalItems = new Array(size);

  

        this._ensureTemplatized();

  

        for (var i = 0; i < size; i++) {

          var inst = this.stamp(null);

e619a3b0   Luigi Serra   Controllet cross ...
707
708
709
710
711
712
713
714
715
716
          // First element child is item; Safari doesn't support children[0]

          // on a doc fragment

          physicalItems[i] = inst.root.querySelector('*');

          Polymer.dom(this).appendChild(inst.root);

        }

  

        return physicalItems;

      },

  

      /**

f748e9cf   Luigi Serra   new controllet an...
717
       * Increases the pool of physical items only if needed.

e619a3b0   Luigi Serra   Controllet cross ...
718
       * This function will allocate additional physical items

a1a3bc73   Luigi Serra   graphs updates
719
       * if the physical size is shorter than `_optPhysicalSize`

e619a3b0   Luigi Serra   Controllet cross ...
720
721
       */

      _increasePoolIfNeeded: function() {

a1a3bc73   Luigi Serra   graphs updates
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
        if (this._viewportSize !== 0 && this._physicalSize < this._optPhysicalSize) {

          // 0 <= `currentPage` <= `_maxPages`

          var currentPage = Math.floor(this._physicalSize / this._viewportSize);

  

          if (currentPage === 0) {

            // fill the first page

            this.async(this._increasePool.bind(this, Math.round(this._physicalCount * 0.5)));

          } else if (this._lastPage !== currentPage) {

            // once a page is filled up, paint it and defer the next increase

            requestAnimationFrame(this._increasePool.bind(this, 1));

          } else {

            // fill the rest of the pages

            this.async(this._increasePool.bind(this, 1));

          }

          this._lastPage = currentPage;

          return true;

f748e9cf   Luigi Serra   new controllet an...
738
739
740
        }

        return false;

      },

e619a3b0   Luigi Serra   Controllet cross ...
741
  

f748e9cf   Luigi Serra   new controllet an...
742
743
744
745
      /**

       * Increases the pool size.

       */

      _increasePool: function(missingItems) {

e619a3b0   Luigi Serra   Controllet cross ...
746
747
748
749
750
751
        // limit the size

        var nextPhysicalCount = Math.min(

            this._physicalCount + missingItems,

            this._virtualCount,

            MAX_PHYSICAL_COUNT

          );

e619a3b0   Luigi Serra   Controllet cross ...
752
753
754
        var prevPhysicalCount = this._physicalCount;

        var delta = nextPhysicalCount - prevPhysicalCount;

  

a1a3bc73   Luigi Serra   graphs updates
755
756
757
        if (delta > 0) {

          [].push.apply(this._physicalItems, this._createPool(delta));

          [].push.apply(this._physicalSizes, new Array(delta));

eb240478   Luigi Serra   public room cards...
758
  

a1a3bc73   Luigi Serra   graphs updates
759
760
761
762
          this._physicalCount = prevPhysicalCount + delta;

          // tail call

          return this._update();

        }

73bcce88   luigser   COMPONENTS
763
764
      },

  

e619a3b0   Luigi Serra   Controllet cross ...
765
766
767
768
769
770
771
      /**

       * Render a new list of items. This method does exactly the same as `update`,

       * but it also ensures that only one `update` cycle is created.

       */

      _render: function() {

        var requiresUpdate = this._virtualCount > 0 || this._physicalCount > 0;

  

a1a3bc73   Luigi Serra   graphs updates
772
773
        if (this.isAttached && !this._itemsRendered && this._isVisible && requiresUpdate) {

          this._lastPage = 0;

e619a3b0   Luigi Serra   Controllet cross ...
774
775
776
777
778
779
780
781
          this._update();

          this._itemsRendered = true;

        }

      },

  

      /**

       * Templetizes the user template.

       */

73bcce88   luigser   COMPONENTS
782
783
784
      _ensureTemplatized: function() {

        if (!this.ctor) {

          // Template instance props that should be excluded from forwarding

e619a3b0   Luigi Serra   Controllet cross ...
785
786
787
788
789
790
791
792
          var props = {};

  

          props.__key__ = true;

          props[this.as] = true;

          props[this.indexAs] = true;

          props[this.selectedAs] = true;

  

          this._instanceProps = props;

73bcce88   luigser   COMPONENTS
793
          this._userTemplate = Polymer.dom(this).querySelector('template');

e619a3b0   Luigi Serra   Controllet cross ...
794
  

73bcce88   luigser   COMPONENTS
795
796
797
          if (this._userTemplate) {

            this.templatize(this._userTemplate);

          } else {

e619a3b0   Luigi Serra   Controllet cross ...
798
            console.warn('iron-list requires a template to be provided in light-dom');

73bcce88   luigser   COMPONENTS
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
          }

        }

      },

  

      /**

       * Implements extension point from Templatizer mixin.

       */

      _getStampedChildren: function() {

        return this._physicalItems;

      },

  

      /**

       * Implements extension point from Templatizer

       * Called as a side effect of a template instance path change, responsible

       * for notifying items.<key-for-instance>.<path> change up to host.

       */

      _forwardInstancePath: function(inst, path, value) {

        if (path.indexOf(this.as + '.') === 0) {

          this.notifyPath('items.' + inst.__key__ + '.' +

            path.slice(this.as.length + 1), value);

        }

      },

  

      /**

       * Implements extension point from Templatizer mixin

       * Called as side-effect of a host property change, responsible for

       * notifying parent path change on each row.

       */

      _forwardParentProp: function(prop, value) {

        if (this._physicalItems) {

          this._physicalItems.forEach(function(item) {

            item._templateInstance[prop] = value;

          }, this);

        }

      },

  

      /**

       * Implements extension point from Templatizer

       * Called as side-effect of a host path change, responsible for

       * notifying parent.<path> path change on each row.

       */

      _forwardParentPath: function(path, value) {

        if (this._physicalItems) {

          this._physicalItems.forEach(function(item) {

            item._templateInstance.notifyPath(path, value, true);

          }, this);

        }

      },

  

      /**

       * Called as a side effect of a host items.<key>.<path> path change,

       * responsible for notifying item.<path> changes to row for key.

       */

      _forwardItemPath: function(path, value) {

        if (this._physicalIndexForKey) {

          var dot = path.indexOf('.');

          var key = path.substring(0, dot < 0 ? path.length : dot);

          var idx = this._physicalIndexForKey[key];

          var row = this._physicalItems[idx];

          if (row) {

            var inst = row._templateInstance;

            if (dot >= 0) {

              path = this.as + '.' + path.substring(dot+1);

              inst.notifyPath(path, value, true);

            } else {

              inst[this.as] = value;

            }

          }

        }

      },

  

e619a3b0   Luigi Serra   Controllet cross ...
870
871
872
873
      /**

       * Called when the items have changed. That is, ressignments

       * to `items`, splices or updates to a single item.

       */

73bcce88   luigser   COMPONENTS
874
875
      _itemsChanged: function(change) {

        if (change.path === 'items') {

e619a3b0   Luigi Serra   Controllet cross ...
876
877
878
          // render the new set

          this._itemsRendered = false;

  

73bcce88   luigser   COMPONENTS
879
880
881
882
          // update the whole set

          this._virtualStartVal = 0;

          this._physicalTop = 0;

          this._virtualCount = this.items ? this.items.length : 0;

e619a3b0   Luigi Serra   Controllet cross ...
883
884
          this._collection = this.items ? Polymer.Collection.get(this.items) : null;

          this._physicalIndexForKey = {};

73bcce88   luigser   COMPONENTS
885
  

e619a3b0   Luigi Serra   Controllet cross ...
886
887
          // scroll to the top

          this._resetScrollPosition(0);

73bcce88   luigser   COMPONENTS
888
  

e619a3b0   Luigi Serra   Controllet cross ...
889
890
891
892
893
          // create the initial physical items

          if (!this._physicalItems) {

            this._physicalCount = Math.max(1, Math.min(DEFAULT_PHYSICAL_COUNT, this._virtualCount));

            this._physicalItems = this._createPool(this._physicalCount);

            this._physicalSizes = new Array(this._physicalCount);

73bcce88   luigser   COMPONENTS
894
895
          }

  

e619a3b0   Luigi Serra   Controllet cross ...
896
          this.debounce('refresh', this._render);

73bcce88   luigser   COMPONENTS
897
898
  

        } else if (change.path === 'items.splices') {

e619a3b0   Luigi Serra   Controllet cross ...
899
900
          // render the new set

          this._itemsRendered = false;

73bcce88   luigser   COMPONENTS
901
902
903
904
  

          this._adjustVirtualIndex(change.value.indexSplices);

          this._virtualCount = this.items ? this.items.length : 0;

  

e619a3b0   Luigi Serra   Controllet cross ...
905
          this.debounce('refresh', this._render);

73bcce88   luigser   COMPONENTS
906
907
908
909
910
911
912
  

        } else {

          // update a single item

          this._forwardItemPath(change.path.split('.').slice(1).join('.'), change.value);

        }

      },

  

e619a3b0   Luigi Serra   Controllet cross ...
913
914
915
      /**

       * @param {!Array<!PolymerSplice>} splices

       */

73bcce88   luigser   COMPONENTS
916
      _adjustVirtualIndex: function(splices) {

e619a3b0   Luigi Serra   Controllet cross ...
917
918
919
920
921
922
923
924
925
        var i, splice, idx;

  

        for (i = 0; i < splices.length; i++) {

          splice = splices[i];

  

          // deselect removed items

          splice.removed.forEach(this.$.selector.deselect, this.$.selector);

  

          idx = splice.index;

73bcce88   luigser   COMPONENTS
926
927
928
929
930
931
932
933
934
935
936
937
938
939
          // We only need to care about changes happening above the current position

          if (idx >= this._virtualStartVal) {

            break;

          }

  

          this._virtualStart = this._virtualStart +

              Math.max(splice.addedCount - splice.removed.length, idx - this._virtualStartVal);

        }

      },

  

      _scrollHandler: function() {

        this._refresh();

      },

  

e619a3b0   Luigi Serra   Controllet cross ...
940
941
942
      /**

       * Executes a provided function per every physical index in `itemSet`

       * `itemSet` default value is equivalent to the entire set of physical indexes.

eb240478   Luigi Serra   public room cards...
943
       *

e619a3b0   Luigi Serra   Controllet cross ...
944
945
946
       * @param {!function(number, number)} fn

       * @param {!Array<number>=} itemSet

       */

73bcce88   luigser   COMPONENTS
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
      _iterateItems: function(fn, itemSet) {

        var pidx, vidx, rtn, i;

  

        if (arguments.length === 2 && itemSet) {

          for (i = 0; i < itemSet.length; i++) {

            pidx = itemSet[i];

            if (pidx >= this._physicalStart) {

              vidx = this._virtualStartVal + (pidx - this._physicalStart);

            } else {

              vidx = this._virtualStartVal + (this._physicalCount - this._physicalStart) + pidx;

            }

            if ((rtn = fn.call(this, pidx, vidx)) != null) {

              return rtn;

            }

          }

        } else {

          pidx = this._physicalStart;

          vidx = this._virtualStartVal;

  

          for (; pidx < this._physicalCount; pidx++, vidx++) {

            if ((rtn = fn.call(this, pidx, vidx)) != null) {

              return rtn;

            }

          }

  

          pidx = 0;

  

          for (; pidx < this._physicalStart; pidx++, vidx++) {

            if ((rtn = fn.call(this, pidx, vidx)) != null) {

              return rtn;

            }

          }

        }

      },

  

e619a3b0   Luigi Serra   Controllet cross ...
982
983
984
985
      /**

       * Assigns the data models to a given set of items.

       * @param {!Array<number>=} itemSet

       */

73bcce88   luigser   COMPONENTS
986
987
988
989
990
991
992
993
994
      _assignModels: function(itemSet) {

        this._iterateItems(function(pidx, vidx) {

          var el = this._physicalItems[pidx];

          var inst = el._templateInstance;

          var item = this.items && this.items[vidx];

  

          if (item) {

            inst[this.as] = item;

            inst.__key__ = this._collection.getKey(item);

e619a3b0   Luigi Serra   Controllet cross ...
995
996
            inst[this.selectedAs] =

              /** @type {!ArraySelectorElement} */ (this.$.selector).isSelected(item);

73bcce88   luigser   COMPONENTS
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
            inst[this.indexAs] = vidx;

            el.removeAttribute('hidden');

            this._physicalIndexForKey[inst.__key__] = pidx;

          } else {

            inst.__key__ = null;

            el.setAttribute('hidden', '');

          }

  

        }, itemSet);

      },

  

e619a3b0   Luigi Serra   Controllet cross ...
1008
1009
      /**

       * Updates the height for a given set of items.

eb240478   Luigi Serra   public room cards...
1010
1011
       *

       * @param {!Array<number>=} itemSet

e619a3b0   Luigi Serra   Controllet cross ...
1012
       */

eb240478   Luigi Serra   public room cards...
1013
1014
1015
       _updateMetrics: function(itemSet) {

        var newPhysicalSize = 0;

        var oldPhysicalSize = 0;

73bcce88   luigser   COMPONENTS
1016
1017
        var prevAvgCount = this._physicalAverageCount;

        var prevPhysicalAvg = this._physicalAverage;

e619a3b0   Luigi Serra   Controllet cross ...
1018
1019
1020
1021
        // Make sure we distributed all the physical items

        // so we can measure them

        Polymer.dom.flush();

  

eb240478   Luigi Serra   public room cards...
1022
1023
1024
1025
1026
1027
        this._iterateItems(function(pidx, vidx) {

          oldPhysicalSize += this._physicalSizes[pidx] || 0;

          this._physicalSizes[pidx] = this._physicalItems[pidx].offsetHeight;

          newPhysicalSize += this._physicalSizes[pidx];

          this._physicalAverageCount += this._physicalSizes[pidx] ? 1 : 0;

        }, itemSet);

73bcce88   luigser   COMPONENTS
1028
  

eb240478   Luigi Serra   public room cards...
1029
        this._physicalSize = this._physicalSize + newPhysicalSize - oldPhysicalSize;

73bcce88   luigser   COMPONENTS
1030
1031
        this._viewportSize = this._scroller.offsetHeight;

  

eb240478   Luigi Serra   public room cards...
1032
        // update the average if we measured something

73bcce88   luigser   COMPONENTS
1033
1034
        if (this._physicalAverageCount !== prevAvgCount) {

          this._physicalAverage = Math.round(

eb240478   Luigi Serra   public room cards...
1035
              ((prevPhysicalAvg * prevAvgCount) + newPhysicalSize) /

73bcce88   luigser   COMPONENTS
1036
1037
1038
1039
              this._physicalAverageCount);

        }

      },

  

e619a3b0   Luigi Serra   Controllet cross ...
1040
1041
1042
1043
      /**

       * Updates the position of the physical items.

       */

      _positionItems: function() {

73bcce88   luigser   COMPONENTS
1044
1045
1046
1047
1048
1049
1050
1051
1052
        this._adjustScrollPosition();

  

        var y = this._physicalTop;

  

        this._iterateItems(function(pidx) {

  

          this.transform('translate3d(0, ' + y + 'px, 0)', this._physicalItems[pidx]);

          y += this._physicalSizes[pidx];

  

e619a3b0   Luigi Serra   Controllet cross ...
1053
        });

73bcce88   luigser   COMPONENTS
1054
1055
      },

  

e619a3b0   Luigi Serra   Controllet cross ...
1056
1057
1058
      /**

       * Adjusts the scroll position when it was overestimated.

       */

73bcce88   luigser   COMPONENTS
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
      _adjustScrollPosition: function() {

        var deltaHeight = this._virtualStartVal === 0 ? this._physicalTop :

            Math.min(this._scrollPosition + this._physicalTop, 0);

  

        if (deltaHeight) {

          this._physicalTop = this._physicalTop - deltaHeight;

  

          // juking scroll position during interial scrolling on iOS is no bueno

          if (!IOS_TOUCH_SCROLLING) {

            this._resetScrollPosition(this._scroller.scrollTop - deltaHeight);

          }

        }

      },

  

e619a3b0   Luigi Serra   Controllet cross ...
1073
1074
1075
      /**

       * Sets the position of the scroll.

       */

73bcce88   luigser   COMPONENTS
1076
      _resetScrollPosition: function(pos) {

e619a3b0   Luigi Serra   Controllet cross ...
1077
1078
1079
1080
        if (this._scroller) {

          this._scroller.scrollTop = pos;

          this._scrollPosition = this._scroller.scrollTop;

        }

73bcce88   luigser   COMPONENTS
1081
1082
      },

  

e619a3b0   Luigi Serra   Controllet cross ...
1083
1084
      /**

       * Sets the scroll height, that's the height of the content,

eb240478   Luigi Serra   public room cards...
1085
1086
       *

       * @param {boolean=} forceUpdate If true, updates the height no matter what.

e619a3b0   Luigi Serra   Controllet cross ...
1087
1088
1089
       */

      _updateScrollerSize: function(forceUpdate) {

        this._estScrollHeight = (this._physicalBottom +

73bcce88   luigser   COMPONENTS
1090
1091
            Math.max(this._virtualCount - this._physicalCount - this._virtualStartVal, 0) * this._physicalAverage);

  

e619a3b0   Luigi Serra   Controllet cross ...
1092
1093
1094
1095
1096
1097
1098
1099
        forceUpdate = forceUpdate || this._scrollHeight === 0;

        forceUpdate = forceUpdate || this._scrollPosition >= this._estScrollHeight - this._physicalSize;

  

        // amortize height adjustment, so it won't trigger repaints very often

        if (forceUpdate || Math.abs(this._estScrollHeight - this._scrollHeight) >= this._optPhysicalSize) {

          this.$.items.style.height = this._estScrollHeight + 'px';

          this._scrollHeight = this._estScrollHeight;

        }

73bcce88   luigser   COMPONENTS
1100
1101
1102
1103
1104
1105
1106
      },

  

      /**

       * Scroll to a specific item in the virtual list regardless

       * of the physical items in the DOM tree.

       *

       * @method scrollToIndex

e619a3b0   Luigi Serra   Controllet cross ...
1107
       * @param {number} idx The index of the item

73bcce88   luigser   COMPONENTS
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
       */

      scrollToIndex: function(idx) {

        if (typeof idx !== 'number') {

          return;

        }

  

        var firstVisible = this.firstVisibleIndex;

  

        idx = Math.min(Math.max(idx, 0), this._virtualCount-1);

  

        // start at the previous virtual item

        // so we have a item above the first visible item

        this._virtualStart = idx - 1;

  

        // assign new models

        this._assignModels();

  

        // measure the new sizes

        this._updateMetrics();

  

        // estimate new physical offset

        this._physicalTop = this._virtualStart * this._physicalAverage;

  

        var currentTopItem = this._physicalStart;

        var currentVirtualItem = this._virtualStart;

        var targetOffsetTop = 0;

        var hiddenContentSize = this._hiddenContentSize;

  

        // scroll to the item as much as we can

a1a3bc73   Luigi Serra   graphs updates
1137
        while (currentVirtualItem < idx && targetOffsetTop < hiddenContentSize) {

73bcce88   luigser   COMPONENTS
1138
1139
1140
1141
1142
1143
          targetOffsetTop = targetOffsetTop + this._physicalSizes[currentTopItem];

          currentTopItem = (currentTopItem + 1) % this._physicalCount;

          currentVirtualItem++;

        }

  

        // update the scroller size

e619a3b0   Luigi Serra   Controllet cross ...
1144
        this._updateScrollerSize(true);

73bcce88   luigser   COMPONENTS
1145
1146
1147
1148
1149
1150
1151
  

        // update the position of the items

        this._positionItems();

  

        // set the new scroll position

        this._resetScrollPosition(this._physicalTop + targetOffsetTop + 1);

  

e619a3b0   Luigi Serra   Controllet cross ...
1152
        // increase the pool of physical items if needed

a1a3bc73   Luigi Serra   graphs updates
1153
1154
        this._increasePoolIfNeeded();

  

73bcce88   luigser   COMPONENTS
1155
1156
1157
1158
        // clear cached visible index

        this._firstVisibleIndexVal = null;

      },

  

e619a3b0   Luigi Serra   Controllet cross ...
1159
1160
1161
      /**

       * Reset the physical average and the average count.

       */

73bcce88   luigser   COMPONENTS
1162
1163
1164
1165
1166
      _resetAverage: function() {

        this._physicalAverage = 0;

        this._physicalAverageCount = 0;

      },

  

e619a3b0   Luigi Serra   Controllet cross ...
1167
      /**

eb240478   Luigi Serra   public room cards...
1168
       * A handler for the `iron-resize` event triggered by `IronResizableBehavior`

e619a3b0   Luigi Serra   Controllet cross ...
1169
1170
       * when the element is resized.

       */

73bcce88   luigser   COMPONENTS
1171
      _resizeHandler: function() {

e619a3b0   Luigi Serra   Controllet cross ...
1172
1173
1174
        this.debounce('resize', function() {

          this._render();

          if (this._itemsRendered && this._physicalItems && this._isVisible) {

73bcce88   luigser   COMPONENTS
1175
1176
1177
            this._resetAverage();

            this.updateViewportBoundaries();

            this.scrollToIndex(this.firstVisibleIndex);

e619a3b0   Luigi Serra   Controllet cross ...
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
          }

        });

      },

  

      _getModelFromItem: function(item) {

        var key = this._collection.getKey(item);

        var pidx = this._physicalIndexForKey[key];

  

        if (pidx !== undefined) {

          return this._physicalItems[pidx]._templateInstance;

73bcce88   luigser   COMPONENTS
1188
        }

e619a3b0   Luigi Serra   Controllet cross ...
1189
1190
1191
1192
        return null;

      },

  

      /**

eb240478   Luigi Serra   public room cards...
1193
       * Gets a valid item instance from its index or the object value.

e619a3b0   Luigi Serra   Controllet cross ...
1194
       *

eb240478   Luigi Serra   public room cards...
1195
       * @param {(Object|number)} item The item object or its index

e619a3b0   Luigi Serra   Controllet cross ...
1196
       */

eb240478   Luigi Serra   public room cards...
1197
      _getNormalizedItem: function(item) {

e619a3b0   Luigi Serra   Controllet cross ...
1198
1199
1200
1201
1202
        if (typeof item === 'number') {

          item = this.items[item];

          if (!item) {

            throw new RangeError('<item> not found');

          }

eb240478   Luigi Serra   public room cards...
1203
1204
        } else if (this._collection.getKey(item) === undefined) {

          throw new TypeError('<item> should be a valid item');

e619a3b0   Luigi Serra   Controllet cross ...
1205
        }

eb240478   Luigi Serra   public room cards...
1206
1207
        return item;

      },

e619a3b0   Luigi Serra   Controllet cross ...
1208
  

eb240478   Luigi Serra   public room cards...
1209
1210
1211
1212
1213
1214
1215
1216
      /**

       * Select the list item at the given index.

       *

       * @method selectItem

       * @param {(Object|number)} item The item object or its index

       */

      selectItem: function(item) {

        item = this._getNormalizedItem(item);

e619a3b0   Luigi Serra   Controllet cross ...
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
        var model = this._getModelFromItem(item);

  

        if (!this.multiSelection && this.selectedItem) {

          this.deselectItem(this.selectedItem);

        }

        if (model) {

          model[this.selectedAs] = true;

        }

        this.$.selector.select(item);

      },

  

      /**

       * Deselects the given item list if it is already selected.

       *

eb240478   Luigi Serra   public room cards...
1231
  

e619a3b0   Luigi Serra   Controllet cross ...
1232
       * @method deselect

eb240478   Luigi Serra   public room cards...
1233
       * @param {(Object|number)} item The item object or its index

e619a3b0   Luigi Serra   Controllet cross ...
1234
1235
       */

      deselectItem: function(item) {

eb240478   Luigi Serra   public room cards...
1236
        item = this._getNormalizedItem(item);

e619a3b0   Luigi Serra   Controllet cross ...
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
        var model = this._getModelFromItem(item);

  

        if (model) {

          model[this.selectedAs] = false;

        }

        this.$.selector.deselect(item);

      },

  

      /**

       * Select or deselect a given item depending on whether the item

       * has already been selected.

       *

       * @method toggleSelectionForItem

eb240478   Luigi Serra   public room cards...
1250
       * @param {(Object|number)} item The item object or its index

e619a3b0   Luigi Serra   Controllet cross ...
1251
1252
       */

      toggleSelectionForItem: function(item) {

eb240478   Luigi Serra   public room cards...
1253
        item = this._getNormalizedItem(item);

e619a3b0   Luigi Serra   Controllet cross ...
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
        if (/** @type {!ArraySelectorElement} */ (this.$.selector).isSelected(item)) {

          this.deselectItem(item);

        } else {

          this.selectItem(item);

        }

      },

  

      /**

       * Clears the current selection state of the list.

       *

       * @method clearSelection

       */

      clearSelection: function() {

        function unselect(item) {

          var model = this._getModelFromItem(item);

          if (model) {

            model[this.selectedAs] = false;

          }

        }

  

        if (Array.isArray(this.selectedItems)) {

          this.selectedItems.forEach(unselect, this);

        } else if (this.selectedItem) {

          unselect.call(this, this.selectedItem);

        }

  

        /** @type {!ArraySelectorElement} */ (this.$.selector).clearSelection();

      },

  

      /**

       * Add an event listener to `tap` if `selectionEnabled` is true,

       * it will remove the listener otherwise.

       */

      _selectionEnabledChanged: function(selectionEnabled) {

        if (selectionEnabled) {

          this.listen(this, 'tap', '_selectionHandler');

          this.listen(this, 'keypress', '_selectionHandler');

        } else {

          this.unlisten(this, 'tap', '_selectionHandler');

          this.unlisten(this, 'keypress', '_selectionHandler');

        }

      },

  

      /**

       * Select an item from an event object.

       */

      _selectionHandler: function(e) {

eb240478   Luigi Serra   public room cards...
1301
1302
        if (e.type !== 'keypress' || e.keyCode === 13) {

          var model = this.modelForElement(e.target);

e619a3b0   Luigi Serra   Controllet cross ...
1303
1304
1305
1306
1307
1308
1309
1310
1311
          if (model) {

            this.toggleSelectionForItem(model[this.as]);

          }

        }

      },

  

      _multiSelectionChanged: function(multiSelection) {

        this.clearSelection();

        this.$.selector.multi = multiSelection;

eb240478   Luigi Serra   public room cards...
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
      },

  

      /**

       * Updates the size of an item.

       *

       * @method updateSizeForItem

       * @param {(Object|number)} item The item object or its index

       */

      updateSizeForItem: function(item) {

        item = this._getNormalizedItem(item);

        var key = this._collection.getKey(item);

        var pidx = this._physicalIndexForKey[key];

  

        if (pidx !== undefined) {

          this._updateMetrics([pidx]);

          this._positionItems();

        }

73bcce88   luigser   COMPONENTS
1329
1330
1331
1332
1333
1334
      }

    });

  

  })();

  

  </script>