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 - Add i18n and dpi service.
21e484b35bSopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
22e484b35bSopenharmony_ci */
23e484b35bSopenharmony_ci
24e484b35bSopenharmony_ciimport {
25e484b35bSopenharmony_ci  isComponent,
26e484b35bSopenharmony_ci  isModule,
27e484b35bSopenharmony_ci  removePrefix,
28e484b35bSopenharmony_ci  Log
29e484b35bSopenharmony_ci} from '../../../utils/index';
30e484b35bSopenharmony_ciimport {
31e484b35bSopenharmony_ci  registerCustomComponent,
32e484b35bSopenharmony_ci  requireModule
33e484b35bSopenharmony_ci} from '../register';
34e484b35bSopenharmony_ciimport { pageMap } from '../../app/map';
35e484b35bSopenharmony_ciimport Vm from '../../model/index';
36e484b35bSopenharmony_ciimport Page from '../index';
37e484b35bSopenharmony_ciimport {updateDpi, updateLocale} from '../../app';
38e484b35bSopenharmony_ci
39e484b35bSopenharmony_ci/**
40e484b35bSopenharmony_ci * Parse page code.
41e484b35bSopenharmony_ci * @param {Page} page - Page.
42e484b35bSopenharmony_ci * @param {string} name - Name of page.
43e484b35bSopenharmony_ci * @param {*[]} args
44e484b35bSopenharmony_ci */
45e484b35bSopenharmony_ciexport const defineFn = function(page: Page, name?: string, ...args: any[] | null): void {
46e484b35bSopenharmony_ci  Log.debug(`Define a component ${name}.`);
47e484b35bSopenharmony_ci
48e484b35bSopenharmony_ci  const parseContent: Function = args[1];
49e484b35bSopenharmony_ci  let bundleContent: object = null;
50e484b35bSopenharmony_ci  const moduleContent = { exports: {} };
51e484b35bSopenharmony_ci
52e484b35bSopenharmony_ci  // Function to obtain bundle content.
53e484b35bSopenharmony_ci  if (parseContent) {
54e484b35bSopenharmony_ci    const pageRequire = (name: string) : any => {
55e484b35bSopenharmony_ci      if (isModule(name)) {
56e484b35bSopenharmony_ci        const packageName = page.packageName;
57e484b35bSopenharmony_ci        const appFunction = (): Page => {
58e484b35bSopenharmony_ci          if (page && page.doc) {
59e484b35bSopenharmony_ci            return page;
60e484b35bSopenharmony_ci          }
61e484b35bSopenharmony_ci          if (packageName === 'notset') {
62e484b35bSopenharmony_ci            return page;
63e484b35bSopenharmony_ci          }
64e484b35bSopenharmony_ci          const appPage: Page = pageMap.getTop(packageName);
65e484b35bSopenharmony_ci          return appPage || page;
66e484b35bSopenharmony_ci        };
67e484b35bSopenharmony_ci        const moduleName: string = removePrefix(name);
68e484b35bSopenharmony_ci        return requireModule(appFunction, moduleName);
69e484b35bSopenharmony_ci      }
70e484b35bSopenharmony_ci    };
71e484b35bSopenharmony_ci    parseContent(pageRequire, moduleContent.exports, moduleContent);
72e484b35bSopenharmony_ci    bundleContent = moduleContent.exports;
73e484b35bSopenharmony_ci  }
74e484b35bSopenharmony_ci  if (isComponent(name)) {
75e484b35bSopenharmony_ci    const componetName: string = removePrefix(name);
76e484b35bSopenharmony_ci    registerCustomComponent(page, componetName, bundleContent);
77e484b35bSopenharmony_ci  }
78e484b35bSopenharmony_ci};
79e484b35bSopenharmony_ci
80e484b35bSopenharmony_ci/**
81e484b35bSopenharmony_ci * Create i18n and dpi service, a new Vm.
82e484b35bSopenharmony_ci * @param {Page} page
83e484b35bSopenharmony_ci * @param {string} name - Name of page.
84e484b35bSopenharmony_ci * @param {*} config
85e484b35bSopenharmony_ci * @param {*} data
86e484b35bSopenharmony_ci * @return {*}
87e484b35bSopenharmony_ci */
88e484b35bSopenharmony_ciexport function bootstrap(page: Page, name: string, data: any): any {
89e484b35bSopenharmony_ci  Log.debug(`Bootstrap for ${name}.`);
90e484b35bSopenharmony_ci
91e484b35bSopenharmony_ci  // Check component name.
92e484b35bSopenharmony_ci  let componentName: string;
93e484b35bSopenharmony_ci  if (isComponent(name)) {
94e484b35bSopenharmony_ci    componentName = removePrefix(name);
95e484b35bSopenharmony_ci  } else {
96e484b35bSopenharmony_ci    return new Error(`Wrong component name: ${name}.`);
97e484b35bSopenharmony_ci  }
98e484b35bSopenharmony_ci
99e484b35bSopenharmony_ci  // Set i18n and dpi data.
100e484b35bSopenharmony_ci  if (global && global.aceapp && page.options && page.options.i18n) {
101e484b35bSopenharmony_ci    updateLocale(page.options.i18n);
102e484b35bSopenharmony_ci  }
103e484b35bSopenharmony_ci  if (global && global.aceapp && page.options && page.options.resourcesConfiguration) {
104e484b35bSopenharmony_ci    updateDpi(page.options.resourcesConfiguration);
105e484b35bSopenharmony_ci  }
106e484b35bSopenharmony_ci
107e484b35bSopenharmony_ci  // Start i18n service.
108e484b35bSopenharmony_ci  if (global && global.aceapp && global.aceapp._i18n_data_ && page.i18nService) {
109e484b35bSopenharmony_ci    const I18nService: any = page.i18nService;
110e484b35bSopenharmony_ci    global.aceapp.i18n = new I18nService(global.aceapp._i18n_data_);
111e484b35bSopenharmony_ci  }
112e484b35bSopenharmony_ci
113e484b35bSopenharmony_ci  // Start dpi service.
114e484b35bSopenharmony_ci  if (global && global.aceapp && global.aceapp._dpi_data_ && page.dpiService) {
115e484b35bSopenharmony_ci    const DpiService: any = page.dpiService;
116e484b35bSopenharmony_ci    global.aceapp.dpi = new DpiService(global.aceapp._dpi_data_);
117e484b35bSopenharmony_ci  }
118e484b35bSopenharmony_ci
119e484b35bSopenharmony_ci  // Create a new Vm and mark rootVm.
120e484b35bSopenharmony_ci  page.vm = new Vm(componentName, null, { __app: page, __rootVm: true }, null, data, null);
121e484b35bSopenharmony_ci}
122