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