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

64.75% Statements 79/122
32.5% Branches 13/40
51.22% Functions 21/41
64.75% Lines 79/122

    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

    1x

    1x

    1x
    1x
    1x
    1x
    1x

    1x

    1x
    3x
    3x
    3x

    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
    2x
    1x

    1x

    1x
    1x
    1x
    6x
    1x

    1x

    1x

    1x
    3x

    5x

    1x

    1x

     ﻿/// <reference path="../../@types/jquery.mobile.d.ts" />
    /* tslint:disable:max-line-length forin no-bitwise */

    namespace CDP {

        import Promise = CDP.Promise;

        /**
         * @en The function returned Promise waits until `cordova` is ready. <br>
         *     [Note] emulate when PC enviroment.
         *
         * @ja `cordova` が有効になるまで待機 <br>
         *     PC 環境ではエミュレートされる。
         */
        export function waitForDeviceReady(): Promise<void> {
            return new Promise<void>((resolve) => {
                Eif (!CDP.Framework.Platform.Mobile) {
                    setTimeout(() => {
                        resolve();
                    }, 100);
                } else {
                    if (null == global.cordova || null == global.cordova.exec) {
                        $(document).one("deviceready", () => {
                            resolve();
                        });
                    } else {
                        resolve();
                    }
                }
            });
        }

        let _defaultBackButtonHandler: (event?: JQuery.Event) => void = null;

        /**
         * @en Setup H/W Back key handler.
         * @ja H/W Back key ハンドラの設定関数
         *
         * @param
         * - `en` handler function.
         * - `ja` handler 指定.
         * @returns
         * - `en` old handler function.
         * - `ja` 以前の handler.
         */
        export function setBackButtonHandler(handler: (event?: JQuery.Event) => void): (event?: JQuery.Event) => void {
            const oldHandler = _defaultBackButtonHandler;
            _defaultBackButtonHandler = handler;
            return oldHandler;
        }

        // back key handler implement.
        (() => {
            waitForDeviceReady()
                .then(() => {
                    $(document).on("backbutton", (event: JQuery.Event) => {
                        if (_defaultBackButtonHandler) {
                            _defaultBackButtonHandler(event);
                        }
                    });
                });
        })();

        //___________________________________________________________________________________________________________________//

        export namespace Framework {

            /**
             * @en Any parameter types interface
             * @ja 任意のパラメータ型定義
             */
            export interface PlainSettings<T = any> {
                [key: string]: T;
            }

            /**
             * @ja Setting parameters for jQuery
             * @ja jQuery に設定可能なオプション
             */
            export interface JQuerySettings extends PlainSettings {
                /**
                 * @en default $.ajax settings
                 * @ja $.ajax の既定設定
                 */
                ajaxSetup?: JQuery.AjaxSettings;
            }

            /**
             * @en Applicable options for [[CDP.Framework]]
             * @ja [[CDP.Framework]] に設定可能なオプション
             */
            export interface FrameworkOptions {
                jquery?: JQuerySettings;
                jquerymobile?: PlainSettings;
                i18n?: CDP.I18NSettings;
                applyJQueryConfig?: () => void;
                applyJQueryMobileConfig?: () => void;
                applyPatch?: boolean;
                anchorVclick?: boolean;
                firstPageTransition?: boolean;
            }

            interface OrientationListenerHolder {
                [key: string]: IOrientationChangedListener;
            }

            const TAG = "[CDP.Framework] ";

            let _dfInitialize: JQueryDeferred<void> = $.Deferred<void>();

            /**
             * @en Initialization function of Framework.
             * @ja Framework の初期化関数
             */
            export function initialize(options?: FrameworkOptions): IPromiseBase<void> {
                Iif ("pending" !== _dfInitialize.state()) {
                    return <any>_dfInitialize.promise();
                }

                // CDP 環境の初期化
                // 現状は、console オブジェクトの保証と jQuery の WinRT 対応。
                CDP.initialize({
                    success: () => {
                        const config = getConfig(options);

                        // Framework 用の Patch 適用 (Framework 初期化前)
                        Eif (config.applyPatch) {
                            Framework.Patch.applyBeforeInit();
                        }

                        // config の反映: jquery
                        config.applyJQueryConfig();

                        // jQuery Mobile の初期化
                        $(document).on("mobileinit", (): void => {
                            // config の反映: jquery mobile
                            config.applyJQueryMobileConfig();

                            // cdp.i18n の初期化
                            CDP.initializeI18N(config.i18n)
                                .always((info) => {
                                    // i18next の初期化時のエラーは無視する. info が array の場合、エラー情報が格納されている.
                                    $(document)
                                        .one("pagebeforechange", (event: JQuery.Event, data: any) => {
                                            (<ChangePageOptions>data.options).showLoadMsg = false;
                                        })
                                        .on("pagebeforecreate", (event: JQuery.Event) => {
                                            // i18nextライブラリによるhtml fragmentの翻訳処理
                                            (<any>$(event.target)).localize();
                                        })
                                    ;

                                    // Router の初期化
                                    Eif (Router._initialize({
                                        anchorVclick: config.anchorVclick,
                                        firstPageTransition: config.firstPageTransition,
                                    })) {
                                        // Framework 用の Patch 適用 (Framework 初期化後)
                                        Eif (config.applyPatch) {
                                            Framework.Patch.applyAfterInit();
                                        }
                                        _dfInitialize.resolve();
                                    } else {
                                        _dfInitialize.reject(makeErrorInfo(
                                            RESULT_CODE.ERROR_CDP_FRAMEWORK_ROUTER_INITIALIZE_FAILED,
                                            TAG,
                                            "error. CDP.Framework.Router.initialize() failed."
                                        ));
                                        _dfInitialize = $.Deferred<void>();
                                    }
                                });
                        });

                        { // jQuery Mobile のロード
                            // jquery 3+
                            const jqueryMajor = ~~$.fn.jquery.split(".")[0];
                            Eif (3 <= jqueryMajor) {
                                Iif (4 <= jqueryMajor) {
                                    console.warn(TAG + "jquery-migrate for 3.0.0+ in use.");
                                }
                                require(["jquery-migrate"], () => {
                                    require(["jquery.mobile"]);
                                });
                            } else {
                                require(["jquery.mobile"]);
                            }
                        }
                    },
                    fail: (error: ErrorInfo) => {
                        _dfInitialize.reject(makeErrorInfo(
                            RESULT_CODE.ERROR_CDP_FRAMEWORK_INITIALIZE_FAILED,
                            TAG,
                            "error. CDP.initialize() failed.",
                            error
                        ));
                        _dfInitialize = $.Deferred<void>();
                    },
                });

                return <any>_dfInitialize.promise();
            }

            /**
             * @en Check for initialization status.
             * @ja 初期化済みか判定
             *
             * @returns
             * - `en` true: initialized / false: uninitialized
             * - `ja` true: 初期化済み / false: 未初期化
             */
            export function isInitialized(): boolean {
                return "resolved" === _dfInitialize.state();
            }

            /**
             * @en Wait for initialization status.
             * @ja 初期化待ち
             */
            export function waitForInitialize(): IPromiseBase<void> {
                return <any>_dfInitialize.promise();
            }

            const _orientationListenerHolder: OrientationListenerHolder = {};

            /**
             * @en Register IOrientationChangedListener to framework.
             * @ja IOrientationChangedListener を Framework に登録
             *
             * @param key
             *  - `en` ID key
             *  - `ja` クライアントで管理可能な ID
             * @param listener
             *  - `en` IOrientationChangedListener instance
             *  - `ja` IOrientationChangedListener インスタンス
             */
            export function registerOrientationChangedListener(key: string, listener: IOrientationChangedListener): void {
                _orientationListenerHolder[key] = listener;
            }

            /**
             * @en Unregister IOrientationChangedListener from framework.
             * @ja IOrientationChangedListener を Framework から登録解除
             *
             * @param key
             *  - `en` ID key by using register
             *  - `ja` 登録時に使用した ID
             */
            export function unregisterOrientationChangedListener(key: string): void {
                delete _orientationListenerHolder[key];
            }

            let _activePage: IPage = null;

            /**
             * @en Setup event handlers when after router initialized.
             * @ja イベントハンドラの設定. Router 初期化後に Framework がコールする.
             *
             * @internal
             */
            export function _setupEventHandlers(): void {
                (() => {
                    const oldBackButtonHandler = CDP.setBackButtonHandler(null);
                    const baseBackButtonHandler = (event?: JQuery.Event): void => {
                        if (_activePage && _activePage.onHardwareBackButton(event)) {
                            // クライアント側でハンドリング済みと指定された場合、既定の処理を行わない
                            return;
                        } else {
                            oldBackButtonHandler(event);
                        }
                    };
                    CDP.setBackButtonHandler(baseBackButtonHandler);
                })();
                (() => {
                    const oldRouteChangeHandler = CDP.Framework.setBeforeRouteChangeHandler(null);
                    const baseRouteChangeHandler = (): IPromiseBase<any> => {
                        if (_activePage) {
                            return _activePage.onBeforeRouteChange();
                        } else {
                            return oldRouteChangeHandler();
                        }
                    };
                    CDP.Framework.setBeforeRouteChangeHandler(baseRouteChangeHandler);
                })();
            }

            let _lastOrientation: Orientation = null;

            /**
             * @en Setup active IPage instance.
             * @ja active Page の設定. Framework がコールする.
             *
             * @internal
             */
            export function _setActivePage(page: IPage): void {
                _activePage = page;
                if (_activePage) {
                    _lastOrientation = getOrientation();
                }
            }

            /**
             * @en Reterns framework default click event string.
             * @ja Framework が既定に使用するクリックイベント文字列を取得
             *
             * @returns "vclick" / "click"
             */
            export function getDefaultClickEvent(): string {
                return Patch.s_vclickEvent;
            }

            let _pageConstructors: (new () => IPage)[] = [];
            let _pageInstances: IPage[] = [];

            /**
             * @en Register Concrete IPage class. <br>
             *     If constructor needs arguments, use bind() function.
             * @ja Page の登録 <br>
             *     constructor を指定する. 引数がある場合は、bind を行うこと
             *
             * @param ctor
             *  - `en` Concrete IPage class constructor
             *  - `ja` コンストラクタを指定
             */
            export function registerPage(ctor: new () => IPage): void {
                _pageConstructors.push(ctor);
            }

            /**
             * @en Page instances are generated from the reserved constructor.
             * @ja 予約されたコンストラクタからPage インスタンスを生成
             */
            export function constructPages(): void {
                _pageConstructors.forEach((ctor: any) => {
                    _pageInstances.push(new ctor());
                });
                _pageConstructors = [];
            }

            /**
             * @en Release Page instance reference
             * @ja Page インスタンスの参照を破棄
             */
            export function disposePages(): void {
                _pageInstances = [];
            }

            /**
             * @en Get Config object.
             * @ja Config object の取得
             *
             * @internal
             */
            function getConfig(options: FrameworkOptions): FrameworkOptions {
                const defConfig = {
                    // for fail safe, default settings.
                    jquery: {
                        ajaxSetup: { cache: false },
                        migrateMute: true,
                    },

                    jquerymobile: {
                        allowCrossDomainPages: true,
                        defaultPageTransition: "platform-default",
                        defaultDialogTransition: "platform-default",
                        hashListeningEnabled: false,
                        pushStateEnabled: false,
                        // @see jquery.mobile.js LINE 6394
                        transitionHandler: {
                            defaultHandler: "simultaneous",
                            sequential: ["fade", "flip", "flow"],
                            simultaneous: [],
                        },
                    },

                    i18n: {
                        fallbackResources: {},
                        options: {},
                    },

                    applyPatch: true,
                    anchorVclick: true,
                    firstPageTransition: false,

                    applyJQueryConfig: function () {
                        Object.keys(this.jquery).forEach((key) => {
                            if ("ajaxSetup" === key) {
                                $.ajaxSetup(this.jquery.ajaxSetup);
                            } else {
                                $[key] = this.jquery[key];
                            }
                        });
                    },

                    applyJQueryMobileConfig: function () {
                        $.mobile.loader.prototype.options.text = undefined;
                        Object.keys(this.jquerymobile).forEach((key) => {
                            if ("transitionHandler" === key) {
                                const $mobile: any = $.mobile;
                                // default handler
                                $mobile.defaultTransitionHandler
                                    = $mobile.transitionHandlers[this.jquerymobile.transitionHandler.defaultHandler];
                                // simultaneous handler
                                this.jquerymobile.transitionHandler.simultaneous.forEach((transition) => {
                                    $mobile.transitionHandlers[transition] = $mobile.transitionHandlers.simultaneous;
                                });
                                // sequential handler
                                this.jquerymobile.transitionHandler.sequential.forEach((transition) => {
                                    $mobile.transitionHandlers[transition] = $mobile.transitionHandlers.sequential;
                                });
                            } else {
                                $.mobile[key] = this.jquerymobile[key];
                            }
                        });
                    },
                };

                return $.extend(true, {}, defConfig, CDP.Config, options);
            }

            ///////////////////////////////////////////////////////////////////////
            // closure methods

            // resize handler
            $(window).on("resize", (event: JQuery.Event): void => {
                const newOrientation = getOrientation();
                if (_lastOrientation !== newOrientation) {
                    for (const key in _orientationListenerHolder) {
                        _orientationListenerHolder[key].onOrientationChanged(newOrientation);
                    }
                    if (_activePage) {
                        _activePage.onOrientationChanged(newOrientation);
                    }
                    _lastOrientation = newOrientation;
                }
            });
        }
    }