# All files / scripts/CDP/UI/listview ScrollManager.ts

10.23% Statements 54/528
0.72% Branches 2/278
3.49% Functions 3/86
10.06% Lines 52/517

    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
    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
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    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
    337
    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
    411
    412
    413
    414
    415
    416
    417
    418
    419
    420
    421
    422
    423
    424
    425
    426
    427
    428
    429
    430
    431
    432
    433
    434
    435
    436
    437
    438
    439
    440
    441
    442
    443
    444
    445
    446
    447
    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
    509
    510
    511
    512
    513
    514
    515
    516
    517
    518
    519
    520
    521
    522
    523
    524
    525
    526
    527
    528
    529
    530
    531
    532
    533
    534
    535
    536
    537
    538
    539
    540
    541
    542
    543
    544
    545
    546
    547
    548
    549
    550
    551
    552
    553
    554
    555
    556
    557
    558
    559
    560
    561
    562
    563
    564
    565
    566
    567
    568
    569
    570
    571
    572
    573
    574
    575
    576
    577
    578
    579
    580
    581
    582
    583
    584
    585
    586
    587
    588
    589
    590
    591
    592
    593
    594
    595
    596
    597
    598
    599
    600
    601
    602
    603
    604
    605
    606
    607
    608
    609
    610
    611
    612
    613
    614
    615
    616
    617
    618
    619
    620
    621
    622
    623
    624
    625
    626
    627
    628
    629
    630
    631
    632
    633
    634
    635
    636
    637
    638
    639
    640
    641
    642
    643
    644
    645
    646
    647
    648
    649
    650
    651
    652
    653
    654
    655
    656
    657
    658
    659
    660
    661
    662
    663
    664
    665
    666
    667
    668
    669
    670
    671
    672
    673
    674
    675
    676
    677
    678
    679
    680
    681
    682
    683
    684
    685
    686
    687
    688
    689
    690
    691
    692
    693
    694
    695
    696
    697
    698
    699
    700
    701
    702
    703
    704
    705
    706
    707
    708
    709
    710
    711
    712
    713
    714
    715
    716
    717
    718
    719
    720
    721
    722
    723
    724
    725
    726
    727
    728
    729
    730
    731
    732
    733
    734
    735
    736
    737
    738
    739
    740
    741
    742
    743
    744
    745
    746
    747
    748
    749
    750
    751
    752
    753
    754
    755
    756
    757
    758
    759
    760
    761
    762
    763
    764
    765
    766
    767
    768
    769
    770
    771
    772
    773
    774
    775
    776
    777
    778
    779
    780
    781
    782
    783
    784
    785
    786
    787
    788
    789
    790
    791
    792
    793
    794
    795
    796
    797
    798
    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
    870
    871
    872
    873
    874
    875
    876
    877
    878
    879
    880
    881
    882
    883
    884
    885
    886
    887
    888
    889
    890
    891
    892
    893
    894
    895
    896
    897
    898
    899
    900
    901
    902
    903
    904
    905
    906
    907
    908
    909
    910
    911
    912
    913
    914
    915
    916
    917
    918
    919
    920
    921
    922
    923
    924
    925
    926
    927
    928
    929
    930
    931
    932
    933
    934
    935
    936
    937
    938
    939
    940
    941
    942
    943
    944
    945
    946
    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
    982
    983
    984
    985
    986
    987
    988
    989
    990
    991
    992
    993
    994
    995
    996
    997
    998
    999
    1000
    1001
    1002
    1003
    1004
    1005
    1006
    1007
    1008
    1009
    1010
    1011
    1012
    1013
    1014
    1015
    1016
    1017
    1018
    1019
    1020
    1021
    1022
    1023
    1024
    1025
    1026
    1027
    1028
    1029
    1030
    1031
    1032
    1033
    1034
    1035
    1036
    1037
    1038
    1039
    1040
    1041
    1042
    1043
    1044
    1045
    1046

    1x

    1x
    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x

     ﻿/* tslint:disable:no-bitwise no-unused-expression */
    /* jshint -W030 */  // for "Expected an assignment or function call and instead saw an expression"

    namespace CDP.UI {

        import _Config  = CDP.UI.ListViewGlobalConfig;
        import _Utils   = CDP.UI._ListViewUtils;

        const TAG = "[CDP.UI.ScrollManager] ";

        /**
         * @class ScrollManager
         * @brief メモリ管理を行うスクロール処理のコアロジック実装クラス
         *        本クラスは IListView インターフェイスを持ち DOM にアクセスするが、Backbone.View を継承しない
         */
        export class ScrollManager implements IListViewFramework, IScrollable, IBackupRestore {

            private _$root: JQuery = null;                                              //!< Scroll 対象のルートオブジェクト
            private _$map: JQuery = null;                                               //!< Scroll Map element を格納
            private _mapHeight: number = 0;                                             //!< Scroll Map の高さを格納 (_$map の状態に依存させない)
            private _scroller: IScroller = null;                                        //!< Scroll に使用する IScroller インスタンス
            private _settings: ListViewOptions = null;                                  //!< ScrollManager オプションを格納
            private _active = true;                                                     //!< UI 表示中は true に指定
            private _scrollEventHandler: (event: JQuery.Event) => void = null;          //!< Scroll Event Handler
            private _scrollStopEventHandler: (event: JQuery.Event) => void = null;      //!< Scroll Stop Event Handler
            private _baseHeight: number = 0;                                            //!< 高さの基準値
            private _lines: LineProfile[] = [];                                         //!< 管理下にある LineProfile 配列
            private _pages: PageProfile[] = [];                                         //!< 管理下にある PageProfile 配列
            //! 最新の表示領域情報を格納 (Scroll 中の更新処理に使用)
            private _lastActivePageContext = {
                index: 0,
                from: 0,
                to: 0,
                pos: 0,    // scroll position
            };
            protected _backup = {};    //!< データの backup 領域. key と _lines を格納。派生クラスで拡張可能。

            /**
             * constructor
             *
             * @param _$root  {JQuery} [in] 管理対象のルートエレメント
             * @param options {ListViewOptions} [in] オプション
             */
            constructor(options?: ListViewOptions) {
                // ListViewOptions 既定値
                const defOptions: ListViewOptions = {
                    scrollerFactory: ScrollerElement.getFactory(),
                    enableHiddenPage: false,
                    enableTransformOffset: false,
                    scrollMapRefreshInterval: 200,
                    scrollRefreshDistance: 200,
                    pagePrepareCount: 3,
                    pagePreloadCount: 1,
                    enableAnimation: true,
                    animationDuration: 0,
                    baseDepth: "auto",
                    itemTagName: "li",
                    removeItemWithTransition: true,
                    useDummyInactiveScrollMap: false,
                };

                // 設定格納
                this._settings = $.extend({}, defOptions, options);

                // スクロールイベント
                this._scrollEventHandler = (event: JQuery.Event): void => {
                    this.onScroll(this._scroller.getPos());
                };
                // スクロール停止イベント
                this._scrollStopEventHandler = (event: JQuery.Event): void => {
                    this.onScrollStop(this._scroller.getPos());
                };
            }

            ///////////////////////////////////////////////////////////////////////
            // public method

            //! 内部オブジェクトの初期化
            public initialize($root: JQuery, height: number): boolean {
                // 既に構築されていた場合は破棄
                if (this._$root) {
                    this.destroy();
                }

                this._$root = $root;
                this._$map = $root.hasClass(_Config.SCROLL_MAP_CLASS) ? $root : $root.find(_Config.SCROLL_MAP_SELECTOR);
                // _$map が無い場合は初期化しない
                if (this._$map.length <= 0) {
                    this._$root = null;
                    return false;
                }

                this._scroller = this.createScroller();
                this.setBaseHeight(height);
                this.setScrollerCondition();

                return true;
            }

            //! 内部オブジェクトの破棄
            public destroy(): void {
                if (this._scroller) {
                    this.resetScrollerCondition();
                    this._scroller.destroy();
                    this._scroller = null;
                }
                this.release();
                this._$map = null;
                this._$root = null;
            }

            //! ページの基準値を取得
            public setBaseHeight(height: number): void {
                this._baseHeight = height;
                if (this._baseHeight <= 0) {
                    console.warn(TAG + "invalid base height: " + this._baseHeight);
                }
                if (this._scroller) {
                    this._scroller.update();
                }
            }

            //! active 状態設定
            public setActiveState(active: boolean): void {
                this._active = active;
                this.treatScrollPosition();
            }

            //! active 状態判定
            public isActive(): boolean {
                return this._active;
            }

            //! scroller の種類を取得
            public getScrollerType(): string {
                return (<any>this._settings.scrollerFactory).type;
            }

            /**
             * 状態に応じたスクロール位置の保存/復元
             * cdp.ui.fs.js: PageTabListView が実験的に使用
             * TODO: ※iscroll は未対応
             */
            public treatScrollPosition(): void {
                let i: number;
                const transform = {};

                const updateOffset = ($target: JQuery): JQuery => {
                    const offset = (this._scroller.getPos() - this._lastActivePageContext.pos);
                    if (_Utils.getCssMatrixValue($target, "translateY") !== offset) {
                        for (i = 0; i < _Utils.cssPrefixes.length; i++) {
                            transform[_Utils.cssPrefixes[i] + "transform"] = "translate3d(0px," + offset + "px,0px)";
                        }
                        $target.css(transform);
                        return $target;
                    }
                };

                const clearOffset = ($target: JQuery): JQuery => {
                    for (i = 0; i < _Utils.cssPrefixes.length; i++) {
                        transform[_Utils.cssPrefixes[i] + "transform"] = "";
                    }
                    $target.css(transform);
                    return $target;
                };

                if (this._active) {
                    // 以下のスコープの処理に対して画面更新を1回にできないため、JB, ICS ではちらつきが発生する。Kitkat 以降は良好。
                    (() => {
                        if (this._scroller) {
                            this._scroller.scrollTo(this._lastActivePageContext.pos, false, 0);
                        }
                        clearOffset(this._$map).css("display", "block");
                    })();
                    if (this._settings.useDummyInactiveScrollMap) {
                        this.prepareInactiveMap().remove();
                    }
                } else if (this._scroller) {
                    if (this._settings.useDummyInactiveScrollMap) {
                        updateOffset(this.prepareInactiveMap());
                    } else {
                        updateOffset(this._$map);
                    }
                }
            }

            //! inactive 用 Map の生成
            private prepareInactiveMap(): JQuery {
                const $parent = this._$map.parent();
                let $inactiveMap = $parent.find(_Config.INACTIVE_CLASS_SELECTOR);
                if ($inactiveMap.length <= 0) {
                    const currentPageIndex = this.getPageIndex();
                    const $listItemViews = this._$map.clone().children().filter((index: number, element: HTMLElement) => {
                        const pageIndex = ~~$(element).attr(_Config.DATA_PAGE_INDEX);
                        if (currentPageIndex - 1 <= pageIndex || pageIndex <= currentPageIndex + 1) {
                            return true;
                        } else {
                            return false;
                        }
                    });
                    $inactiveMap = $("<section class='" + _Config.SCROLL_MAP_CLASS + " " + _Config.INACTIVE_CLASS + "'></section>")
                        .append($listItemViews)
                        .height(this._mapHeight);
                    $parent.append($inactiveMap);
                    this._$map.css("display", "none");
                }
                return $inactiveMap;
            }

            ///////////////////////////////////////////////////////////////////////
            // Implements: IListView プロファイル管理

            //! 初期化済みか判定
            isInitialized(): boolean {
                return !!this._$root;
            }

            //! プロパティを指定して、LineProfile を管理
            addItem(
                height: number,
                initializer: new (options?: any) => BaseListItemView,
                info: any,
                insertTo?: number
            ): void {
                this._addLine(new LineProfile(this, Math.floor(height), initializer, info), insertTo);
            }

            //! プロパティを指定して、LineProfile を管理. 登録 framework が使用する
            public _addLine(_line: any, insertTo?: number): void {
                const lines: LineProfile[] = (_line instanceof Array) ? <LineProfile[]>_line : [_line];
                let i: number, n: number;
                let deltaHeight = 0;
                let addTail = false;

                if (null == insertTo) {
                    insertTo = this._lines.length;
                }

                if (insertTo === this._lines.length) {
                    addTail = true;
                }

                // scroll map の更新
                for (i = 0, n = lines.length; i < n; i++) {
                    deltaHeight += lines[i].height;
                }
                this.updateScrollMapHeight(deltaHeight);

                // 挿入
                for (i = lines.length - 1; i >= 0; i--) {
                    this._lines.splice(insertTo, 0, lines[i]);
                }

                // page 設定の解除
                if (!addTail) {
                    if (0 === insertTo) {
                        this.clearPage();
                    } else if (null != this._lines[insertTo - 1].pageIndex) {
                        this.clearPage(this._lines[insertTo - 1].pageIndex);
                    }
                }

                // offset の再計算
                this.updateProfiles(insertTo);
            }

            //! 指定した Item を削除
            removeItem(index: number, size?: number, delay?: number): void;
            removeItem(index: number[], delay?: number): void;
            removeItem(index: any, arg2?: number, arg3?: number): void {
                if (index instanceof Array) {
                    this._removeLines(index, arg2);
                } else {
                    this._removeLine(index, arg2, arg3);
                }
            }

            //! 指定した LineProfile を削除: 連続 index 版
            public _removeLine(index: number, size?: number, delay?: number): void {
                if (null == size) {
                    size = 1;
                }
                if (index < 0 || this._lines.length < index + size) {
                    console.error(TAG + "logic error. removeItem(), invalid index: " + index);
                    return;
                }

                delay = (null != delay) ? delay : 0;

                const removed: LineProfile[] = [];
                let delta = 0;
                let mapTransition = false;

                // 削除候補と変化量の算出
                (() => {
                    let line: LineProfile;
                    for (let i = 0; i < size; i++) {
                        line = this._lines[index + i];
                        delta += line.height;
                        // 削除要素の z-index の初期化
                        line.resetDepth();
                        removed.push(line);
                    }
                    // 自動設定・削除遅延時間が設定されかつ、スクロールポジションに変更がある場合は transition 設定
                    if (this._settings.removeItemWithTransition && (0 < delay)) {
                        const current = this.getScrollPos();
                        const posMax = this.getScrollPosMax() - delta;
                        mapTransition = (posMax < current);
                    }
                })();

                // 更新
                (() => {
                    // transition 設定
                    if (mapTransition) {
                        this.setupScrollMapTransition(this._$map, delay);
                    }
                    // page 設定の解除
                    if (null != this._lines[index].pageIndex) {
                        this.clearPage(this._lines[index].pageIndex);
                    }
                    // スクロール領域の更新
                    this.updateScrollMapHeight(-delta);
                    // 配列から削除
                    this._lines.splice(index, size);
                    // offset の再計算
                    this.updateProfiles(index);
                    // 遅延削除
                    setTimeout(() => {
                        removed.forEach((line) => {
                            line.inactivate();
                        });
                    }, delay);
                })();
            }

            //! 指定した LineProfile を削除: random access 版
            public _removeLines(indexes: number[], delay?: number): void {
                delay = (null != delay) ? delay : 0;

                for (let i = 0, n = indexes.length; i < n; i++) {
                    if (i < 0 || this._lines.length < i) {
                        console.error(TAG + "logic error. removeItem(), invalid index: " + i);
                        return;
                    }
                }

                const removed: LineProfile[] = [];
                let delta = 0;
                let mapTransition = false;

                // 削除候補と変化量の算出
                (() => {
                    let line: LineProfile;
                    indexes.forEach((index: number) => {
                        line = this._lines[index];
                        delta += line.height;
                        // 削除要素の z-index の初期化
                        line.resetDepth();
                        removed.push(line);
                    });
                    // 自動設定・削除遅延時間が設定されかつ、スクロールポジションに変更がある場合は transition 設定
                    if (this._settings.removeItemWithTransition && (0 < delay)) {
                        const current = this.getScrollPos();
                        const posMax = this.getScrollPosMax() - delta;
                        mapTransition = (posMax < current);
                    }
                })();

                // 更新
                (() => {
                    // transition 設定
                    if (mapTransition) {
                        this.setupScrollMapTransition(this._$map, delay);
                    }
                    indexes.forEach((index: number) => {
                        // page 設定の解除
                        if (null != this._lines[index].pageIndex) {
                            this.clearPage(this._lines[index].pageIndex);
                        }
                        // 配列から削除
                        this._lines.splice(index, 1);
                        // offset の再計算
                        this.updateProfiles(index);
                    });
                    // スクロール領域の更新
                    this.updateScrollMapHeight(-delta);
                    // 遅延削除
                    setTimeout(() => {
                        removed.forEach((line) => {
                            line.inactivate();
                        });
                    }, delay);
                })();
            }

            //! scroll map のトランジション設定
            private setupScrollMapTransition($map: JQuery, delay: number): void {
                const transitionEndHandler = (event: JQuery.Event) => {
                    $map.off(_Utils.transitionEnd);
                    _Utils.clearTransitions($map);
                };
                this._$map.on(_Utils.transitionEnd, transitionEndHandler);
                _Utils.setTransformsTransitions($map, "height", delay, "ease");
            }

            //! 指定した Item に設定した情報を取得
            getItemInfo(target: number): any;
            getItemInfo(target: JQuery.Event): any;
            getItemInfo(target: any): any {
                let index: number;

                const parser = ($target: JQuery): number => {
                    if ($target.hasClass(_Config.LISTITEM_BASE_CLASS)) {
                        return ~~$target.attr(_Config.DATA_CONTAINER_INDEX);
                    } else if ($target.hasClass(_Config.SCROLL_MAP_CLASS) || $target.length <= 0) {
                        console.warn(TAG + "cannot ditect line from event object.");
                        return null;
                    } else {
                        return parser($target.parent());
                    }
                };

                if (target instanceof $.Event) {
                    index = parser($(target.currentTarget));
                } else if (typeof target === "number") {
                    index = target;
                }

                if (null == index) {
                    console.error(TAG + "logic error. unsupported arg type. type: " + typeof target);
                    return null;
                } else if (index < 0 || this._lines.length <= index) {
                    console.error(TAG + "logic error. invalid range. index: " + index);
                    return null;
                }

                return this._lines[index].info;
            }

            //! アクティブページを更新
            refresh(): void {
                const targets: any = {};
                const searchCount = this._settings.pagePrepareCount + this._settings.pagePreloadCount;
                const currentPageIndex = this.getPageIndex();
                const highPriorityIndex: number[] = [];

                const oldExsistPage = _.filter(this._pages, (page: PageProfile) => {
                    return "inactive" !== page.status;
                });

                const changeState = (index: number): void => {
                    if (index === currentPageIndex) {
                        targets[index] = "activate";
                        highPriorityIndex.push(index);
                    } else if (_Utils.abs(currentPageIndex - index) <= this._settings.pagePrepareCount) {
                        targets[index] = "activate";
                    } else {
                        if (this._settings.enableHiddenPage) {
                            targets[index] = "hide";
                        } else {
                            targets[index] = "activate";
                        }
                    }
                    // current page の 前後は high priority にする
                    if (currentPageIndex + 1 === index || currentPageIndex - 1 === index) {
                        highPriorityIndex.push(index);
                    }
                };

                // 対象無し
                if (this._lines.length <= 0) {
                    return;
                }

                (() => {
                    let i = 0;
                    let pageIndex = 0;
                    let overflowPrev = 0, overflowNext = 0;
                    const beginIndex = currentPageIndex - searchCount;
                    const endIndex = currentPageIndex + searchCount;
                    for (pageIndex = beginIndex; pageIndex <= endIndex; pageIndex++) {
                        if (pageIndex < 0) {
                            overflowPrev++;
                            continue;
                        }
                        if (this._pages.length <= pageIndex) {
                            overflowNext++;
                            continue;
                        }
                        changeState(pageIndex);
                    }

                    if (0 < overflowPrev) {
                        for (i = 0, pageIndex = currentPageIndex + searchCount + 1; i < overflowPrev; i++ , pageIndex++) {
                            if (this._pages.length <= pageIndex) {
                                break;
                            }
                            changeState(pageIndex);
                        }
                    }

                    if (0 < overflowNext) {
                        for (i = 0, pageIndex = currentPageIndex - searchCount - 1; i < overflowNext; i++ , pageIndex--) {
                            if (pageIndex < 0) {
                                break;
                            }
                            changeState(pageIndex);
                        }
                    }
                })();

                // 不要になった page の inactivate
                oldExsistPage.forEach((page: PageProfile) => {
                    const index = page.index;
                    if (null == targets[index]) {
                        page.inactivate();
                    }
                });

                // 優先 page の activate
                highPriorityIndex
                    .sort((lhs: number, rhs: number) => {
                        if (lhs < rhs) {
                            return -1;
                        } else if (lhs > rhs) {
                            return 1;
                        } else {
                            return 0;
                        }
                    })
                    .forEach((index: number) => {
                        setTimeout(() => {
                            if (this.isInitialized()) {
                                this._pages[index] && this._pages[index].activate();
                            }
                        }, 0);
                    });

                // そのほかの page の 状態変更
                _.each(targets, (action: string, key: string) => {
                    setTimeout(() => {
                        if (this.isInitialized()) {
                            const index = ~~key;
                            switch (action) {
                                case "activate":
                                    this._pages[index] && this._pages[index].activate();
                                    break;
                                case "hide":
                                    this._pages[index] && this._pages[index].hide();
                                    break;
                                case "inactivate":
                                    console.warn(TAG + "unexpected operation: inactivate");
                                    break;
                                default:
                                    console.warn(TAG + "unknown operation: " + targets[key]);
                                    break;
                            }
                        }
                    }, 0);
                });

                // 更新後に使用しなかった DOM を削除
                this.findRecycleElements().remove();

                this._lastActivePageContext.from = this._pages[currentPageIndex].getLineProfileFirst() ?
                    this._pages[currentPageIndex].getLineProfileFirst().index : 0;
                this._lastActivePageContext.to = this._pages[currentPageIndex].getLineProfileLast() ?
                    this._pages[currentPageIndex].getLineProfileLast().index : 0;
                this._lastActivePageContext.index = currentPageIndex;
            }

            //! 未アサインページを構築
            update(): void {
                const index = this._pages.length;
                this.assignPage(index);
                this.refresh();
            }

            //! ページアサインを再構成
            rebuild(): void {
                this.clearPage();
                this.assignPage();
                this.refresh();
            }

            //! 管轄データを破棄
            release(): void {
                this._lines.forEach((line: LineProfile) => {
                    line.inactivate();
                });
                this._pages = [];
                this._lines = [];
                if (this._$map) {
                    this._mapHeight = 0;
                    this._$map.height(0);
                }
            }

            ///////////////////////////////////////////////////////////////////////
            // Implements: IListView Backup / Restore

            //! 内部データをバックアップ
            backup(key: string): boolean {
                if (null == this._backup[key]) {
                    this._backup[key] = {
                        lines: this._lines,
                    };
                }
                return true;
            }

            //! 内部データをリストア
            restore(key: string, rebuild: boolean = true): boolean {
                if (null == this._backup[key]) {
                    return false;
                }
                if (0 < this._lines.length) {
                    this.release();
                }

                this._addLine(this._backup[key].lines);

                if (rebuild) {
                    this.rebuild();
                }

                return true;
            }

            //! バックアップデータの有無
            hasBackup(key: string): boolean {
                if (null != this._backup[key]) {
                    return true;
                } else {
                    return false;
                }
            }

            //! バックアップデータの破棄
            clearBackup(key?: string): boolean {
                if (null == key) {
                    this._backup = {};
                    return true;
                } else if (null != this._backup[key]) {
                    delete this._backup[key];
                    return true;
                } else {
                    return false;
                }
            }

            //! バックアップデータにアクセス
            get backupData(): any {
                return this._backup;
            }

            ///////////////////////////////////////////////////////////////////////
            // Implements: IListView Scroll

            //! スクロールイベントハンドラ設定/解除
            setScrollHandler(handler: (event: JQuery.Event) => void, on: boolean): void {
                if (this._scroller) {
                    if (on) {
                        this._scroller.on("scroll", handler);
                    } else {
                        this._scroller.off("scroll", handler);
                    }
                }
            }

            //! スクロール終了イベントハンドラ設定/解除
            setScrollStopHandler(handler: (event: JQuery.Event) => void, on: boolean): void {
                if (this._scroller) {
                    if (on) {
                        this._scroller.on("scrollstop", handler);
                    } else {
                        this._scroller.off("scrollstop", handler);
                    }
                }
            }

            //! スクロール位置を取得
            getScrollPos(): number {
                return this._scroller ? this._scroller.getPos() : 0;
            }

            //! スクロール位置の最大値を取得
            getScrollPosMax(): number {
                return this._scroller ? this._scroller.getPosMax() : 0;
            }

            //! スクロール位置を指定
            scrollTo(pos: number, animate?: boolean, time?: number): void {
                if (this._scroller) {
                    if (pos < 0) {
                        console.warn(TAG + "invalid position, too small. [pos: " + pos + "]");
                        pos = 0;
                    } else if (this._scroller.getPosMax() < pos) {
                        console.warn(TAG + "invalid position, too big. [pos: " + pos + "]");
                        pos = this._scroller.getPosMax();
                    }
                    // pos のみ先駆けて更新
                    this._lastActivePageContext.pos = pos;
                    if (pos !== this._scroller.getPos()) {
                        this._scroller.scrollTo(pos, animate, time);
                    }
                }
            }

            //! 指定された ListItemView の表示を保証
            ensureVisible(index: number, options?: EnsureVisibleOptions): void {
                if (index < 0 || this._lines.length <= index) {
                    console.warn(TAG + "ensureVisible(), invalid index, noop. [index: " + index + "]");
                    return;
                } else if (!this._scroller) {
                    console.warn(TAG + "scroller is not ready.");
                    return;
                }

                (() => {
                    const target = this._lines[index];

                    const defaultOptions: EnsureVisibleOptions = {
                        partialOK: true,
                        setTop: false,
                        animate: this._settings.enableAnimation,
                        time: this._settings.animationDuration,
                        callback: (): void => { /* noop */ },
                    };
                    const operation: EnsureVisibleOptions = $.extend({}, defaultOptions, options);

                    const currentScope = {
                        from: this._scroller.getPos(),
                        to: this._scroller.getPos() + this._baseHeight,
                    };
                    const targetScope = {
                        from: target.offset,
                        to: target.offset + target.height,
                    };

                    const isInScope = (): boolean => {
                        if (operation.partialOK) {
                            if (targetScope.from <= currentScope.from) {
                                if (currentScope.from <= targetScope.to) {
                                    return true;
                                } else {
                                    return false;
                                }
                            } else {
                                if (targetScope.from <= currentScope.to) {
                                    return true;
                                } else {
                                    return false;
                                }
                            }
                        } else {
                            if (currentScope.from <= targetScope.from && targetScope.to <= currentScope.to) {
                                return true;
                            } else {
                                return false;
                            }
                        }
                    };

                    const detectPosition = (): number => {
                        if (targetScope.from < currentScope.from) {
                            return targetScope.from;
                        } else if (currentScope.from < targetScope.from) {
                            return target.offset - target.height; // bottom 合わせは情報不足により不可
                        } else {
                            console.warn(TAG + "logic error.");
                            return 0;
                        }
                    };

                    let pos: number;

                    if (operation.setTop) {
                        pos = targetScope.from;
                    } else if (isInScope()) {
                        // noop.
                        operation.callback();
                        return;
                    } else {
                        pos = detectPosition();
                    }

                    // 補正
                    if (pos < 0) {
                        pos = 0;
                    } else if (this._scroller.getPosMax() < pos) {
                        pos = this._scroller.getPosMax();
                    }

                    setTimeout(operation.callback, operation.time);
                    this.scrollTo(pos, operation.animate, operation.time);
                })();
            }

            ///////////////////////////////////////////////////////////////////////
            // implements: IListViewFramework:

            //! Scroll Map の高さを取得
            getScrollMapHeight(): number {
                return this._$map ? this._mapHeight : 0;
            }

            //! Scroll Map の高さを更新. framework が使用する.
            updateScrollMapHeight(delta: number): void {
                if (this._$map) {
                    this._mapHeight += delta;
                    // for fail safe.
                    if (this._mapHeight < 0) {
                        this._mapHeight = 0;
                    }
                    this._$map.height(this._mapHeight);
                }
            }

            //! 内部 Profile の更新. framework が使用する.
            updateProfiles(from: number): void {
                let i: number, n: number;
                let last: LineProfile;
                for (i = from, n = this._lines.length; i < n; i++) {
                    if (0 < i) {
                        last = this._lines[i - 1];
                        this._lines[i].index = last.index + 1;
                        this._lines[i].offset = last.offset + last.height;
                    } else {
                        this._lines[i].index = 0;
                        this._lines[i].offset = 0;
                    }
                }
            }

            //! Scroll Map Element を取得. framework が使用する.
            getScrollMapElement(): JQuery {
                return this._$map || $("");
            }

            //! リサイクル可能な Element を取得. framework が使用する.
            findRecycleElements(): JQuery {
                return this._$map ? this._$map.find(_Config.RECYCLE_CLASS_SELECTOR) : $("");
            }

            //! ListViewOptions を取得. framework が使用する.
            getListViewOptions(): ListViewOptions {
                return this._settings;
            }

            ///////////////////////////////////////////////////////////////////////
            // private method:

            //! Scroller 用環境設定
            private setScrollerCondition(): void {
                this._scroller.on("scroll", this._scrollEventHandler);
                this._scroller.on("scrollstop", this._scrollStopEventHandler);
            }

            //! Scroller 用環境破棄
            private resetScrollerCondition(): void {
                this._scroller.off("scrollstop", this._scrollStopEventHandler);
                this._scroller.off("scroll", this._scrollEventHandler);
            }

            //! 既定の Scroller オブジェクトの作成
            private createScroller(): IScroller {
                return this._settings.scrollerFactory(this._$root[0], this._settings);
            }

            //! 現在の Page Index を取得
            private getPageIndex(): number {
                let i: number, n: number;
                let page: PageProfile;
                let candidate: number;

                const scrollPos = this._scroller ? this._scroller.getPos() : 0;
                const scrollPosMax = this._scroller ? this._scroller.getPosMax() : 0;
                const scrollMapSize = (() => {
                    const lastPage = this.getLastPage();
                    if (null != lastPage) {
                        return lastPage.offset + lastPage.height;
                    } else {
                        return this._baseHeight;
                    }
                })();

                const pos = (() => {
                    if (0 === scrollPosMax || scrollPosMax <= this._baseHeight) {
                        return 0;
                    } else {
                        return scrollPos * scrollMapSize / scrollPosMax;
                    }
                })();

                const validRange = (_page: PageProfile): boolean => {
                    if (null == _page) {
                        return false;
                    } else if (_page.offset <= pos && pos <= _page.offset + _page.height) {
                        return true;
                    } else {
                        return false;
                    }
                };

                if (this._baseHeight <= 0) {
                    console.error(TAG + "invalid base height: " + this._baseHeight);
                    return 0;
                }

                candidate = Math.floor(pos / this._baseHeight);
                if (this._pages.length <= candidate) {
                    candidate = this._pages.length - 1;
                }

                page = this._pages[candidate];
                if (validRange(page)) {
                    return page.index;
                } else if (pos < page.offset) {
                    for (i = candidate - 1; i >= 0; i--) {
                        page = this._pages[i];
                        if (validRange(page)) {
                            return page.index;
                        }
                    }
                    console.warn(TAG + "unknown page index.");
                    return 0;
                } else {
                    for (i = candidate + 1, n = this._pages.length; i < n; i++) {
                        page = this._pages[i];
                        if (validRange(page)) {
                            return page.index;
                        }
                    }
                    console.warn(TAG + "unknown page index.");
                    return this._pages.length - 1;
                }
            }

            /**
             * スクロールイベント
             *
             * @param pos {Number} [in] スクロールポジション
             */
            private onScroll(pos: number): void {
                if (this._active && 0 < this._pages.length) {
                    const currentPageIndex = this.getPageIndex();
                    // TODO: 調整
                    if (_Utils.abs(pos - this._lastActivePageContext.pos) < this._settings.scrollRefreshDistance) {
                        if (this._lastActivePageContext.index !== currentPageIndex) {
                            this.refresh();
                        }
                    }
                    this._lastActivePageContext.pos = pos;
                }
            }

            /**
             * スクロール停止イベント
             *
             * @param pos {Number} [in] スクロールポジション
             */
            private onScrollStop(pos: number): void {
                if (this._active && 0 < this._pages.length) {
                    const currentPageIndex = this.getPageIndex();
                    if (this._lastActivePageContext.index !== currentPageIndex) {
                        this.refresh();
                    }
                    this._lastActivePageContext.pos = pos;
                }
            }

            //! 最後のページを取得
            private getLastPage(): PageProfile {
                if (0 < this._pages.length) {
                    return this._pages[this._pages.length - 1];
                } else {
                    return null;
                }
            }

            /**
             * ページ区分のアサイン
             *
             * @param from {Number} [in] page index を指定
             */
            private assignPage(from?: number): void {
                let i: number, n: number;
                if (null == from) {
                    from = 0;
                } else {
                    this.clearPage(from);
                }

                (() => {
                    let lineIndex = 0;
                    let lastPage = this.getLastPage();
                    let lastLine: LineProfile;
                    let tempPage: PageProfile;
                    if (null == lastPage) {
                        lastPage = new PageProfile();
                        this._pages.push(lastPage);
                    } else {
                        lastLine = lastPage.getLineProfileLast();
                        if (null != lastLine) {
                            lineIndex = lastLine.index + 1;
                        }
                    }

                    const asignee = this._lines.slice(lineIndex);
                    for (i = 0, n = asignee.length; i < n; i++) {
                        if (this._baseHeight <= lastPage.height) {
                            lastPage.normalize();
                            tempPage = lastPage;
                            tempPage = new PageProfile();
                            tempPage.index = lastPage.index + 1;
                            tempPage.offset = lastPage.offset + lastPage.height;
                            lastPage = tempPage;
                            this._pages.push(lastPage);
                        }
                        asignee[i].pageIndex = lastPage.index;
                        lastPage.push(asignee[i]);
                    }
                    lastPage.normalize();
                })();

                if (this._scroller) {
                    this._scroller.update();
                }
            }

            /**
             * ページ区分の解除
             *
             * @param from {Number} [in] page index を指定
             */
            private clearPage(from?: number): void {
                if (null == from) {
                    from = 0;
                }
                this._pages = this._pages.slice(0, from);
            }
        }
    }