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

30.91% Statements 17/55
10% Branches 2/20
20% Functions 3/15
28.3% Lines 15/53

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

    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.ScrollerElement] ";

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

            //! constructor
            constructor(element: string, options: ListViewOptions);
            constructor(element: HTMLElement, options: ListViewOptions);
            constructor(element: any, options: ListViewOptions) {
                this._$target = $(element);
                this._listviewOptions = options;
            }

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

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

            //! position の最大値を取得
            getPosMax(): number {
                if (null == this._$scrollMap) {
                    this._$scrollMap = this._$target.children().first();
                }
                return _Utils.max(this._$scrollMap.height() - this._$target.height(), 0);
            }

            //! イベント登録
            on(type: string, func: (event: JQuery.Event) => void): void {
                switch (type) {
                    case "scroll":
                        this._$target.on("scroll", func);
                        break;
                    case "scrollstop":
                        this._$target.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":
                        this._$target.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._$target.scrollTop(pos);
                } else {
                    if (null == time) {
                        time = this._listviewOptions.animationDuration;
                    }
                    this._$target.animate({
                        scrollTop: pos
                    }, time);
                }
            }

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

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

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

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