# All files / scripts/CDP/Framework Patch.ts

50% Statements 32/64
32.61% Branches 15/46
52.94% Functions 9/17
48.39% Lines 30/62

    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

    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:typedef no-bitwise */

    namespace CDP.Framework {

        /**
         * vclick patch
         */
        const _vclickPatch = (): void => {
            const jquery_on = $.fn.on,
                jquery_off = $.fn.off;

            const custom_on = function (types, selector, data, fn, /*INTERNAL*/ one) {
                if (typeof types === "string") {
                    types = types.replace(/vclick/g, "click");
                }
                return (<any>_.bind(jquery_on, this))(types, selector, data, fn, one);
            };

            const custom_off = function (types, selector, fn) {
                if (typeof types === "string") {
                    types = types.replace(/vclick/g, "click");
                }
                return (<any>_.bind(jquery_off, this))(types, selector, fn);
            };

            // replace functions.
            (<any>$.fn).on = custom_on;
            (<any>$.fn).off = custom_off;
        };

        /**
         * $.mobile.widget.popup patch for "_createPrerequisites"
         *
         * patch code from jqm 1.5
         * https://github.com/jquery/jquery-mobile/commit/d34c86ae14d4ea603357be5e326de1fb8c31dbf9
         */
        const _apply_jqm_popup_createPrerequisites = (): void => {
            const _safeCreatePrerequisites = function (screenPrerequisite: any, containerPrerequisite: any, whenDone: any): void {
                let prerequisites;
                const self: any = this;

                prerequisites = {
                    screen: $.Deferred(),
                    container: $.Deferred()
                };

                prerequisites.screen.done(function () {
                    if (prerequisites === self._prerequisites) {
                        screenPrerequisite();
                    }
                });

                prerequisites.container.done(function () {
                    if (prerequisites === self._prerequisites) {
                        containerPrerequisite();
                    }
                });

                $.when(prerequisites.screen, prerequisites.container).done(function () {
                    if (prerequisites === self._prerequisites) {
                        self._prerequisites = null;
                        whenDone();
                    }
                });

                self._prerequisites = prerequisites;
            };

            const $mobile: any = $.mobile;
            Eif ($mobile.widgets && $mobile.widgets.popup) {
                $mobile.widgets.popup.prototype._createPrerequisites = _safeCreatePrerequisites;
            }
        };

        /**
         * $.mobile.widget.popup patch for "_handleWindowResize"
         *
         * patch code from stack overflow
         * https://stackoverflow.com/questions/27090445/jqmobile-re-positions-popup-window-every-time-when-android-opens-own-virtual-key
         */
        const _apply_jqm_popup_handleWindowResize = (): void => {
            const _safeHandleWindowResize = function (/* theEvent */): void {
                if (this._isOpen && this._ignoreResizeTo === 0) {
                    if ((this._expectResizeEvent() || this._orientationchangeInProgress) &&
                        !this._ui.container.hasClass("ui-popup-hidden")) {
                        // effectively rapid-close the popup while leaving the screen intact
                        //judge user is in input mode or not.
                        if (!($(":focus").is("input") || $(":focus").is("textarea"))) {
                            this._ui.container
                                .addClass("ui-popup-hidden ui-popup-truncate")
                                .removeAttr("style");
                        }
                    }
                }
            };

            const $mobile: any = $.mobile;
            Eif ($mobile.widgets && $mobile.widgets.popup) {
                $mobile.widgets.popup.prototype._handleWindowResize = _safeHandleWindowResize;
            }
        };

        /**
         * $.mobile.widget.popup patch
         */
        const _mobilePopupPatch = (): void => {
            const jqueryMajor = ~~$.fn.jquery.split(".")[0];
            const jqmVersion = $.mobile.version.split(".");
            const jqmMajor = ~~jqmVersion[0];
            const jqmMiner = ~~jqmVersion[1];

            Eif (3 <= jqueryMajor && jqmMajor < 2 && jqmMiner < 5) {
                _apply_jqm_popup_createPrerequisites();
                _apply_jqm_popup_handleWindowResize();
            }
        };

        //___________________________________________________________________________________________________________________//

        /**
         * @en patch class for jqm framework.
         * @ja jqm framework 用パッチ適用クラス
         *
         * @internal
         */
        export class Patch {

            static s_vclickEvent = "vclick";

            ///////////////////////////////////////////////////////////////////////
            // public static methods

            /**
             * @en Apply patch before initialize.
             * @ja 初期化前のパッチの適用
             */
            public static applyBeforeInit(): void {
                Iif (!Patch.isSupportedVclick()) {
                    _vclickPatch();
                    Patch.s_vclickEvent = "click";
                }
            }

            /**
             * @en Apply patch after initialize.
             * @ja 初期化後のパッチの適用
             */
            public static applyAfterInit(): void {
                _mobilePopupPatch();
            }

            /**
             * @en if "vclick" event is unsupported, returns false. ex: Android 4.4 (Kitkat)
             * @ja "vclick" event が非サポートである platform (KitKat) は false を返す。 <br>
             *      jQM の version up により、解決される場合は無効かする。
             */
            public static isSupportedVclick(): boolean {
                // for Android 4.4+ (Kitkat ～)
                Iif (Platform.Android && !Platform.ltAd4_4) {
                    return false;
                }
                return true;
            }
        }
    }