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

32.08% Statements 17/53
12.5% Branches 2/16
20% Functions 3/15
29.41% Lines 15/51

    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
    1101x

    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x

     ﻿namespace CDP.UI {

        import _Utils = CDP.UI._ListViewUtils;

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

        /**
         * @class ScrollerNative
         * @brief Browser Native の Scroller クラス
         */
        export class ScrollerNative implements IScroller {
            private _$body: JQuery = null;
            private _$target: JQuery = null;
            private _$scrollMap: JQuery = null;
            private _listviewOptions: ListViewOptions = null;

            //! constructor
            constructor(options: ListViewOptions) {
                this._$target = $(document);
                this._$body = $("body");
                this._listviewOptions = options;
            }

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

            //! position 取得
            getPos(): number {
                return this._$target.scrollTop();
            }

            //! position の最大値を取得
            getPosMax(): number {
                return _Utils.max(this._$target.height() - window.innerHeight, 0);
            }

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

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

            //! スクロール位置を指定
            scrollTo(pos: number, animate?: boolean, time?: number): void {
                if (!this._listviewOptions.enableAnimation || !animate) {
                    this._$body.scrollTop(pos);
                } else {
                    if (null == time) {
                        time = this._listviewOptions.animationDuration;
                    }
                    this._$body.animate({
                        scrollTop: pos
                    }, time);
                }
            }

            //! Scroller の状態更新
            update(): void {
                // noop.
            }

            //! Scroller の破棄
            destroy(): void {
                this._$scrollMap = null;
                this._$target = null;
            }

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

            //! factory 取得
            public static getFactory(): (element: any, options: ListViewOptions) => IScroller {
                const factory = (element: any, options: ListViewOptions): IScroller => {
                    return new ScrollerNative(options);
                };
                // set type signature.
                (<any>factory).type = ScrollerNative.TYPE;
                return factory;
            }
        }
    }