1/* 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, 13 * software distributed under the License is distributed on an 14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 * KIND, either express or implied. See the License for the 16 * specific language governing permissions and limitations 17 * under the License. 18 */ 19/* 20 * 2021.01.08 - Rewrite the function 'initFramework' and make it simpler. 21 * Copyright (c) 2021 Huawei Device Co., Ltd. 22 */ 23 24import service from './service'; 25import i18n from '../main/extend/i18n/index'; 26import dpi from '../main/extend/dpi/index'; 27import { Log } from '../utils/utils'; 28import { Options } from '../main/app'; 29import globalApi from './methods'; 30import NativeElementClassFactory from '../vdom/NativeElementClassFactory'; 31 32export interface GlobalInterface { 33 createInstance: (id: string, code: string, config: Options, data: object) => any | Error; 34 registerModules: (modules: object) => void; 35 appDestroy: (packageName: string) => void; 36 appShow: (packageName: string) => void; 37 appHide: (packageName: string) => void; 38 appError: (packageName: string, errors: any) => void; 39 destroyInstance: (pageId: string) => any | Error; 40 getRoot: (...args: any[]) => any | Error; 41 callJS: (pageId: string, tasks: any[]) => any | Error; 42} 43 44/** 45 * Setup framework: register services and initialize the global methods. 46 */ 47export function initFramework(): void { 48 for (const serviceName in i18n) { 49 service.register(serviceName, i18n[serviceName]); 50 } 51 for (const serviceName in dpi) { 52 service.register(serviceName, dpi[serviceName]); 53 } 54 55 const globalMethods: GlobalInterface = { 56 'createInstance': globalApi.createInstance, 57 'registerModules': globalApi.registerModules, 58 'appDestroy': globalApi.appDestroy, 59 'appError': globalApi.appError, 60 'appShow': globalApi.appShow, 61 'appHide': globalApi.appHide, 62 'destroyInstance': globalApi.destroyInstance, 63 'getRoot': globalApi.getRoot, 64 'callJS': globalApi.callJS 65 }; 66 67 // registerModules and registerComponents 68 ModulesInfo.forEach(modules => { 69 globalMethods['registerModules'](modules); 70 }); 71 72 ComponentsInfo.forEach((name) => { 73 if (name && name.type && name.methods) { 74 NativeElementClassFactory.createNativeElementClass( 75 name.type, 76 name.methods 77 ); 78 } 79 }); 80 81 for (const methodName in globalMethods) { 82 global[methodName] = (...args: any) => { 83 const res: any = globalMethods[methodName](...args); 84 if (res instanceof Error) { 85 Log.error(res.toString()); 86 } 87 return res; 88 }; 89 } 90} 91 92const ModulesInfo: Record<string, string[]>[] = [ 93 {'system.router': ['push', 'replace', 'back', 'clear', 'getLength', 'getState', 'enableAlertBeforeBackPage', 'disableAlertBeforeBackPage', 'getParams']}, 94 {'system.app': ['getInfo', 'getPackageInfo', 'terminate', 'requestFullWindow', 'screenOnVisible', 'setSwipeToDismiss']}, 95 {'system.prompt': ['showToast', 'showDialog', 'showActionMenu']}, 96 {'system.configuration': ['getLocale']}, 97 {'timer': ['setTimeout', 'clearTimeout', 'setInterval', 'clearInterval']}, 98 {'system.image': ['getImage']}, 99 {'system.offscreenCanvas': ['create']}, 100 {'system.device': ['getInfo']}, 101 {'system.grid': ['getSystemLayoutInfo']}, 102 {'system.mediaquery': ['addListener', 'getDeviceType']}, 103 {'animation': ['requestAnimationFrame', 'cancelAnimationFrame']}, 104 {'system.resource': ['readText']}, 105 {'ohos.animator': ['createAnimator', 'create']} 106]; 107 108type components<T> = { 109 'methods': T[], 110 'type': T 111} 112 113const CommanMethods: Array<string> = ['focus', 'addChild', 'animate', 'getBoundingClientRect', 'getInspector', 'scrollBy', 'getScrollOffset', 'scrollTo', 'createIntersectionObserver']; 114 115const ComponentsInfo: components<string>[] = [ 116 { 'methods': CommanMethods, 'type': 'clock' }, 117 { 'methods': ['show'], 'type': 'colorpicker' }, 118 { 'methods': CommanMethods, 'type': 'image' }, 119 { 'methods': CommanMethods, 'type': 'label' }, 120 { 'methods': CommanMethods, 'type': 'list-item' }, 121 { 'methods': CommanMethods, 'type': 'list-item-group' }, 122 { 'methods': CommanMethods, 'type': 'progress' }, 123 { 'methods': CommanMethods, 'type': 'rating' }, 124 { 'methods': CommanMethods, 'type': 'select' }, 125 { 'methods': CommanMethods, 'type': 'switch' }, 126 { 'methods': CommanMethods, 'type': 'tabs' }, 127 { 'methods': CommanMethods, 'type': 'tab-bar' }, 128 { 'methods': CommanMethods, 'type': 'tab-content' }, 129 { 'methods': CommanMethods, 'type': 'text' }, 130 { 'methods': CommanMethods, 'type': 'div' }, 131 { 'methods': CommanMethods, 'type': 'badge' }, 132 { 133 'methods': ['setProgress', 'focus', 'addChild', 'animate', 'getBoundingClientRect', 'getInspector', 'scrollBy', 'getScrollOffset', 'scrollTo', 'createIntersectionObserver'], 134 'type': 'button' 135 }, 136 { 'methods': ['append', 'focus', 'addChild', 'getBoundingClientRect', 'getInspector', 'scrollBy', 'getScrollOffset', 'scrollTo', 'createIntersectionObserver'], 'type': 'chart' }, 137 { 'methods': ['goto', 'focus', 'addChild', 'getBoundingClientRect', 'getInspector', 'scrollBy', 'getScrollOffset', 'scrollTo', 'createIntersectionObserver'], 'type': 'calendar' }, 138 { 139 'methods': ['getContext', 'toDataURL', 'focus', 'addChild', 'animate', 'getBoundingClientRect', 'getInspector', 'scrollBy', 'getScrollOffset', 'scrollTo', 'createIntersectionObserver'], 140 'type': 'canvas' 141 }, 142 { 143 'methods': ['getXComponentContext', 'createIntersectionObserver', 'addChild', 'getXComponentSurfaceId', 'setXComponentSurfaceSize'], 144 'type': 'xcomponent' 145 }, 146 { 'methods': ['show', 'close', 'getBoundingClientRect', 'getInspector', 'scrollBy', 'getScrollOffset', 'scrollTo', 'createIntersectionObserver', 'addChild'], 'type': 'dialog' }, 147 { 'methods': ['animate', 'getBoundingClientRect', 'getInspector', 'scrollBy', 'getScrollOffset', 'scrollTo', 'createIntersectionObserver', 'addChild'], 'type': 'divider' }, 148 { 149 'methods': ['getColumns', 'getColumnWidth', 'getGutterWidth', 'getSizeType', 'getBoundingClientRect', 'getInspector', 'scrollBy', 'getScrollOffset', 'scrollTo', 'createIntersectionObserver', 'addChild'], 150 'type': 'grid-container' 151 }, 152 { 153 'methods': ['start', 'stop', 'pause', 'resume', 'getState', 'focus', 'addChild', 'animate', 'getBoundingClientRect', 'getInspector', 'scrollBy', 'getScrollOffset', 'scrollTo', 'createIntersectionObserver'], 154 'type': 'image-animator' 155 }, 156 { 157 'methods': ['showError', 'insert', 'delete', 'focus', 'addChild', 'animate', 'getBoundingClientRect', 'getInspector', 'scrollBy', 'getScrollOffset', 'scrollTo', 'createIntersectionObserver'], 158 'type': 'input' 159 }, 160 { 161 'methods': ['scrollTo', 'scrollBy', 'focus', 'addChild', 'scrollArrow', 'scrollTop', 'scrollBottom', 'scrollPage', 'collapseGroup', 'expandGroup', 'currentOffset', 'rotation', 'animate', 'chainanimation', 'getBoundingClientRect', 'getInspector', 'getScrollOffset', 'createIntersectionObserver'], 162 'type': 'list' 163 }, 164 { 165 'methods': ['start', 'stop', 'focus', 'addChild', 'animate', 'getBoundingClientRect', 'getInspector', 'scrollBy', 'getScrollOffset', 'scrollTo', 'createIntersectionObserver'], 166 'type': 'marquee' 167 }, 168 { 'methods': ['show', 'getBoundingClientRect', 'getInspector', 'scrollBy', 'getScrollOffset', 'scrollTo', 'createIntersectionObserver', 'addChild'], 'type': 'menu' }, 169 { 'methods': CommanMethods, 'type': 'option' }, 170 { 'methods': ['show', 'close', 'getBoundingClientRect', 'getInspector', 'scrollBy', 'getScrollOffset', 'scrollTo', 'createIntersectionObserver', 'addChild'], 'type': 'panel' }, 171 { 'methods': ['show', 'animate', 'focus', 'addChild', 'getBoundingClientRect', 'getInspector', 'scrollBy', 'getScrollOffset', 'scrollTo', 'createIntersectionObserver'], 'type': 'picker' }, 172 { 173 'methods': ['rotation', 'animate', 'focus', 'addChild', 'getBoundingClientRect', 'getInspector', 'scrollBy', 'getScrollOffset', 'scrollTo', 'createIntersectionObserver'], 174 'type': 'picker-view' 175 }, 176 { 'methods': CommanMethods, 'type': 'piece' }, 177 { 'methods': ['focus', 'addChild', 'show', 'hide', 'getBoundingClientRect', 'getInspector', 'scrollBy', 'getScrollOffset', 'scrollTo', 'createIntersectionObserver'], 'type': 'popup' }, 178 { 'methods': ['animate', 'focus', 'addChild', 'delete', 'getBoundingClientRect', 'getInspector', 'scrollBy', 'getScrollOffset', 'scrollTo', 'createIntersectionObserver'], 'type': 'search' }, 179 { 180 'methods': ['rotation', 'focus', 'addChild', 'animate', 'getBoundingClientRect', 'getInspector', 'scrollBy', 'getScrollOffset', 'scrollTo', 'createIntersectionObserver'], 181 'type': 'slider' 182 }, 183 { 'methods': ['focus', 'addChild', 'animate', 'getScrollOffset', 'scrollBy', 'getBoundingClientRect', 'getInspector', 'scrollTo', 'createIntersectionObserver'], 'type': 'stack' }, 184 { 185 'methods': ['swipeTo', 'focus', 'addChild', 'showPrevious', 'showNext', 'rotation', 'animate', 'getBoundingClientRect', 'getInspector', 'scrollBy', 'getScrollOffset', 'scrollTo', 'createIntersectionObserver'], 186 'type': 'swiper' 187 }, 188 { 189 'methods': ['start', 'pause', 'stop', 'setCurrentTime', 'requestFullscreen', 'exitFullscreen', 'focus', 'addChild', 'animate', 'getBoundingClientRect', 'getInspector', 'scrollBy', 'getScrollOffset', 'scrollTo', 'createIntersectionObserver'], 190 'type': 'video' 191 }, 192 { 193 'methods': ['setNextButtonStatus', 'focus', 'addChild', 'getBoundingClientRect', 'getInspector', 'scrollBy', 'getScrollOffset', 'scrollTo', 'createIntersectionObserver'], 194 'type': 'stepper' 195 }, 196 { 197 'methods': ['focus', 'addChild', 'animate', 'delete', 'getBoundingClientRect', 'getInspector', 'scrollBy', 'getScrollOffset', 'scrollTo', 'createIntersectionObserver'], 198 'type': 'textarea' 199 }, 200 { 'methods': ['reload', 'createIntersectionObserver', 'addChild'], 'type': 'web' }, 201 { 202 'methods': ['takePhoto', 'startRecorder', 'closeRecorder', 'scrollTo', 'createIntersectionObserver', 'addChild'], 203 'type': 'camera' 204 }, 205 { 'methods': ['getInspector'], 'type': 'toolbar' }, 206 { 'methods': ['getInspector'], 'type': 'toolbar-item' } 207]; 208