• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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']}
106];
107
108type components<T> = {
109  'methods': T[],
110  'type': T
111}
112
113const ComponentsInfo: components<string>[] = [
114  {'methods': ['focus', 'animate', 'getBoundingClientRect', 'scrollBy', 'getScrollOffset', 'scrollTo', 'createIntersectionObserver'], 'type': 'clock'},
115  {'methods': ['focus', 'animate', 'getBoundingClientRect', 'scrollBy', 'getScrollOffset', 'scrollTo', 'createIntersectionObserver'], 'type': 'image'},
116  {'methods': ['focus', 'animate', 'getBoundingClientRect', 'scrollBy', 'getScrollOffset', 'scrollTo', 'createIntersectionObserver'], 'type': 'label'},
117  {'methods': ['focus', 'animate', 'getBoundingClientRect', 'scrollBy', 'getScrollOffset', 'scrollTo', 'createIntersectionObserver'], 'type': 'list-item'},
118  {'methods': ['focus', 'animate', 'getBoundingClientRect', 'scrollBy', 'getScrollOffset', 'scrollTo', 'createIntersectionObserver'], 'type': 'list-item-group'},
119  {'methods': ['focus', 'animate', 'getBoundingClientRect', 'scrollBy', 'getScrollOffset', 'scrollTo', 'createIntersectionObserver'], 'type': 'progress'},
120  {'methods': ['focus', 'animate', 'getBoundingClientRect', 'scrollBy', 'getScrollOffset', 'scrollTo', 'createIntersectionObserver'], 'type': 'rating'},
121  {'methods': ['focus', 'animate', 'getBoundingClientRect', 'scrollBy', 'getScrollOffset', 'scrollTo', 'createIntersectionObserver'], 'type': 'select'},
122  {'methods': ['focus', 'animate', 'getBoundingClientRect', 'scrollBy', 'getScrollOffset', 'scrollTo', 'createIntersectionObserver'], 'type': 'switch'},
123  {'methods': ['focus', 'animate', 'getBoundingClientRect', 'scrollBy', 'getScrollOffset', 'scrollTo', 'createIntersectionObserver'], 'type': 'tabs'},
124  {'methods': ['focus', 'animate', 'getBoundingClientRect', 'scrollBy', 'getScrollOffset', 'scrollTo', 'createIntersectionObserver'], 'type': 'tab-bar'},
125  {'methods': ['focus', 'animate', 'getBoundingClientRect', 'scrollBy', 'getScrollOffset', 'scrollTo', 'createIntersectionObserver'], 'type': 'tab-content'},
126  {'methods': ['focus', 'animate', 'getBoundingClientRect', 'scrollBy', 'getScrollOffset', 'scrollTo', 'createIntersectionObserver'], 'type': 'text'},
127  {'methods': ['focus', 'animate', 'getBoundingClientRect', 'scrollBy', 'getScrollOffset', 'scrollTo', 'createIntersectionObserver'], 'type': 'div'},
128  {
129    'methods': ['setProgress', 'focus', 'animate', 'getBoundingClientRect', 'scrollBy', 'getScrollOffset', 'scrollTo', 'createIntersectionObserver'],
130    'type': 'button'
131  },
132  {'methods': ['append', 'focus', 'getBoundingClientRect', 'scrollBy', 'getScrollOffset', 'scrollTo', 'createIntersectionObserver'], 'type': 'chart'},
133  {'methods': ['goto', 'focus', 'getBoundingClientRect', 'scrollBy', 'getScrollOffset', 'scrollTo', 'createIntersectionObserver'], 'type': 'calendar'},
134  {
135    'methods': ['getContext', 'toDataURL', 'focus', 'animate', 'getBoundingClientRect', 'scrollBy', 'getScrollOffset', 'scrollTo', 'createIntersectionObserver'],
136    'type': 'canvas'
137  },
138  {
139    'methods': ['getXComponentContext', 'createIntersectionObserver'],
140    'type': 'xcomponent'
141  },
142  {'methods': ['show', 'close', 'getBoundingClientRect', 'scrollBy', 'getScrollOffset', 'scrollTo', 'createIntersectionObserver'], 'type': 'dialog'},
143  {'methods': ['animate', 'getBoundingClientRect', 'scrollBy', 'getScrollOffset', 'scrollTo', 'createIntersectionObserver'], 'type': 'divider'},
144  {
145    'methods': ['getColumns', 'getColumnWidth', 'getGutterWidth', 'getSizeType', 'getBoundingClientRect', 'scrollBy', 'getScrollOffset', 'scrollTo', 'createIntersectionObserver'],
146    'type': 'grid-container'
147  },
148  {
149    'methods': ['start', 'stop', 'pause', 'resume', 'getState', 'focus', 'animate', 'getBoundingClientRect', 'scrollBy', 'getScrollOffset', 'scrollTo', 'createIntersectionObserver'],
150    'type': 'image-animator'
151  },
152  {
153    'methods': ['showError', 'delete', 'focus', 'animate', 'getBoundingClientRect', 'scrollBy', 'getScrollOffset', 'scrollTo', 'createIntersectionObserver'],
154    'type': 'input'
155  },
156  {
157    'methods': ['scrollTo', 'scrollBy', 'focus', 'scrollArrow', 'scrollTop', 'scrollBottom', 'scrollPage', 'collapseGroup', 'expandGroup', 'currentOffset', 'rotation', 'animate', 'chainanimation', 'getBoundingClientRect', 'getScrollOffset', 'createIntersectionObserver'],
158    'type': 'list'
159  },
160  {
161    'methods': ['start', 'stop', 'focus', 'animate', 'getBoundingClientRect', 'scrollBy', 'getScrollOffset', 'scrollTo', 'createIntersectionObserver'],
162    'type': 'marquee'
163  },
164  {'methods': ['show', 'getBoundingClientRect', 'scrollBy', 'getScrollOffset', 'scrollTo', 'createIntersectionObserver'], 'type': 'menu'},
165  {'methods': ['focus', 'getBoundingClientRect', 'scrollBy', 'getScrollOffset', 'scrollTo', 'createIntersectionObserver'], 'type': 'option'},
166  {'methods': ['show', 'close', 'getBoundingClientRect', 'scrollBy', 'getScrollOffset', 'scrollTo', 'createIntersectionObserver'], 'type': 'panel'},
167  {'methods': ['show', 'animate', 'focus', 'getBoundingClientRect', 'scrollBy', 'getScrollOffset', 'scrollTo', 'createIntersectionObserver'], 'type': 'picker'},
168  {
169    'methods': ['rotation', 'animate', 'focus', 'getBoundingClientRect', 'scrollBy', 'getScrollOffset', 'scrollTo', 'createIntersectionObserver'],
170    'type': 'picker-view'
171  },
172  {'methods': ['focus', 'getBoundingClientRect', 'scrollBy', 'getScrollOffset', 'scrollTo', 'createIntersectionObserver'], 'type': 'piece'},
173  {'methods': ['focus', 'show', 'hide', 'getBoundingClientRect', 'scrollBy', 'getScrollOffset', 'scrollTo', 'createIntersectionObserver'], 'type': 'popup'},
174  {'methods': ['animate', 'focus', 'delete', 'getBoundingClientRect', 'scrollBy', 'getScrollOffset', 'scrollTo', 'createIntersectionObserver'], 'type': 'search'},
175  {
176    'methods': ['rotation', 'focus', 'animate', 'getBoundingClientRect', 'scrollBy', 'getScrollOffset', 'scrollTo', 'createIntersectionObserver'],
177    'type': 'slider'
178  },
179  {'methods': ['focus', 'animate', 'getScrollOffset', 'scrollBy', 'getBoundingClientRect', 'scrollTo', 'createIntersectionObserver'], 'type': 'stack'},
180  {
181    'methods': ['swipeTo', 'focus', 'showPrevious', 'showNext', 'rotation', 'animate', 'getBoundingClientRect', 'scrollBy', 'getScrollOffset', 'scrollTo', 'createIntersectionObserver'],
182    'type': 'swiper'
183  },
184  {
185    'methods': ['start', 'pause', 'stop', 'setCurrentTime', 'requestFullscreen', 'exitFullscreen', 'focus', 'animate', 'getBoundingClientRect', 'scrollBy', 'getScrollOffset', 'scrollTo', 'createIntersectionObserver'],
186    'type': 'video'
187  },
188  {
189    'methods': ['setNextButtonStatus', 'focus', 'getBoundingClientRect', 'scrollBy', 'getScrollOffset', 'scrollTo', 'createIntersectionObserver'],
190    'type': 'stepper'
191  },
192  {
193    'methods': ['focus', 'animate', 'delete', 'getBoundingClientRect', 'scrollBy', 'getScrollOffset', 'scrollTo', 'createIntersectionObserver'],
194    'type': 'textarea'
195  },
196  {'methods': ['reload', 'createIntersectionObserver'], 'type': 'web'},
197  {
198    'methods': ['takePhoto', 'startRecorder', 'closeRecorder', 'scrollTo', 'createIntersectionObserver'],
199    'type': 'camera'
200  }
201];
202