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

20.93% Statements 18/86
6.67% Branches 2/30
17.65% Functions 3/17
19.28% Lines 16/83

    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

    1x

    1x
    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x

     ﻿// for non iscroll user. cdp.ui.listview.d.ts internal definition.
    interface IScrollOptions {
        [x: string]: any;
        probeType?: number; // [calm :1 < 2 < 3: aggressive]
    }

    namespace CDP.UI {

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

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

        interface IScrollEx extends IScroll {
            on: (type: string, fn: (event: any) => void) => void;
            off: (type: string, fn: (event: any) => void) => void;
            wrapper: HTMLElement;
            scroller: HTMLElement;
            scrollerWidth: number;
            scrollerHeight: number;
            maxScrollX: number;
            maxScrollY: number;
            getComputedPosition(): any;
        }

        /**
         * @class ScrollerIScroll
         * @brief iScroll を使用した Scroller クラス
         */
        export class ScrollerIScroll implements IScroller {
            private _$owner: JQuery = null;
            private _iscroll: IScrollEx = null;
            private _refreshTimerId: number = null;
            private _$wrapper: JQuery = null;
            private _$scroller: JQuery = null;
            private _listviewOptions: ListViewOptions = null;

            //! constructor
            constructor($owner: JQuery, element: string, iscrollOptions: IScrollOptions, listviewOptions: ListViewOptions);
            constructor($owner: JQuery, element: HTMLElement, iscrollOptions: IScrollOptions, listviewOptions: ListViewOptions);
            constructor($owner: JQuery, element: any, iscrollOptions: IScrollOptions, listviewOptions: ListViewOptions) {
                if (null != global.IScroll) {
                    this._$owner = $owner;
                    this._iscroll = <IScrollEx>new IScroll(element, iscrollOptions);
                    this._$wrapper = $(this._iscroll.wrapper);
                    this._$scroller = $(this._iscroll.scroller);
                    this._listviewOptions = listviewOptions;
                } else {
                    console.error(TAG + "iscroll module doesn't load.");
                }
            }

            //! Scroller の型を取得
            getType(): string {
                return ScrollerIScroll.TYPE;
            }

            //! position 取得
            getPos(): number {
                let pos = this._iscroll.getComputedPosition().y;
                if (_.isNaN(pos)) {
                    pos = 0;
                } else {
                    pos = -pos;
                }
                return pos;
            }

            //! position の最大値を取得
            getPosMax(): number {
                return _Utils.max(-this._iscroll.maxScrollY, 0);
            }

            //! イベント登録
            on(type: string, func: (event: JQuery.Event) => void): void {
                switch (type) {
                    case "scroll":
                        this._iscroll.on("scroll", func);
                        break;
                    case "scrollstop":
                        this._iscroll.on("scrollEnd", func);
                        break;
                    default:
                        console.warn(TAG + "unsupported type: " + type);
                        break;
                }
            }

            //! イベント登録解除
            off(type: string, func: (event: JQuery.Event) => void): void {
                switch (type) {
                    case "scroll":
                        this._iscroll.off("scroll", func);
                        break;
                    case "scrollstop":
                        this._iscroll.on("scrollEnd", func);
                        break;
                    default:
                        console.warn(TAG + "unsupported type: " + type);
                        break;
                }
            }

            //! スクロール位置を指定
            scrollTo(pos: number, animate?: boolean, time?: number): void {
                time = 0;
                if (this._listviewOptions.enableAnimation && animate) {
                    time = time || this._listviewOptions.animationDuration;
                }
                this._iscroll.scrollTo(0, -pos, time);
            }

            //! Scroller の状態更新
            update(): void {
                if (this._$owner) {
                    // update wrapper
                    (() => {
                        const ownerHeight = this._$owner.height();
                        if (ownerHeight !== this._$wrapper.height()) {
                            this._$wrapper.height(ownerHeight);
                        }
                    })();

                    if (null != this._refreshTimerId) {
                        clearTimeout(this._refreshTimerId);
                    }

                    const proc = () => {
                        if (this._$scroller && this._$scroller.height() !== this._iscroll.scrollerHeight) {
                            this._iscroll.refresh();
                            this._refreshTimerId = setTimeout(proc, this._listviewOptions.scrollMapRefreshInterval);
                        } else {
                            this._refreshTimerId = null;
                        }
                    };

                    this._iscroll.refresh();
                    this._refreshTimerId = setTimeout(proc, this._listviewOptions.scrollMapRefreshInterval);
                }
            }

            //! Scroller の破棄
            destroy(): void {
                this._$scroller = null;
                this._$wrapper = null;
                this._iscroll.destroy();
                this._$owner = null;
            }

            //! タイプ定義
            public static get TYPE(): string {
                return "iscroll";
            }

            //! factory 取得
            public static getFactory(options?: IScrollOptions): (element: any, options: ListViewOptions) => IScroller {
                const defaultOpt = {
                    scrollX: false,
                    bounce: false,
                    tap: true,
                    click: true,
                    mouseWheel: true,
                    scrollbars: true,
                    interactiveScrollbars: true,
                    shrinkScrollbars: "scale",
                    fadeScrollbars: true,
                    preventDefault: false,
                    disablePointer: true,
                    disableMouse: false,
                    disableTouch: false,
                    probeType: 2,
    //               eventPassthrough: true,
                };

                const iscrollOptions = $.extend({}, defaultOpt, options);

                const factory = (element: any, listviewOptions: ListViewOptions): IScroller => {
                    const $owner = $(element);
                    const $map = $owner.find(_Config.SCROLL_MAP_SELECTOR);
                    const $wrapper = $("<div class='" + _Config.WRAPPER_CLASS + "'></div>")
                        .css({
                            position: "absolute",
                            width: "100%",
                            height: "100%",
                            overflow: "hidden",
                        });
                    $map.wrap($wrapper);
                    return new ScrollerIScroll($owner, _Config.WRAPPER_SELECTOR, iscrollOptions, listviewOptions);
                };
                // set type signature.
                (<any>factory).type = ScrollerIScroll.TYPE;

                return factory;
            }
        }
    }