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