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/** 21 * @fileOverview 22 * page controls from native 23 * 24 * - fire event 25 * - callback 26 * - refresh 27 * - destroy 28 * 29 * corresponded with the API of page manager (framework.js) 30 */ 31 32import { 33 typof, 34 Log 35} from '../../../utils/index'; 36import Page from '../index'; 37import Element from '../../../vdom/Element'; 38 39/** 40 * Destroy a page. 41 * @param {Page} page 42 */ 43export function destroy(page: Page): void { 44 Log.debug(`Destroy a page(${page.id}).`); 45 46 if (page.vm) { 47 page.vm.destroy(); 48 } 49 50 page.id = null; 51 page.options = null; 52 page.vm = null; 53 page.doc.taskCenter.destroyCallback(); 54 page.doc.destroy(); 55 page.doc = null; 56 page.customComponentMap = null; 57 page.commonModules = null; 58} 59 60/** 61 * Fire an event to update domChanges of a page. 62 * @param {Page} page 63 * @param {*} ref 64 * @param {*} type 65 * @param {Object} e 66 * @param {Object} domChanges 67 * @param {*[] | null} args 68 * @return any 69 */ 70export function fireEvent(page: Page, ref?: any, type?: any, e?: object, domChanges?: boolean, ...args: any[] | null): any { 71 Log.debug(`Fire a '${type}' event on an element(${ref}) in page(${page.id}), args = ${JSON.stringify(args)}.`); 72 if (Array.isArray(ref)) { 73 ref.some((ref) => { 74 return fireEvent(page, ref, type, e) !== false; 75 }); 76 return; 77 } 78 const el: Element = page.doc.getRef(ref); 79 if (el) { 80 const options: {params?: any} = {}; 81 if (args) { 82 options.params = [...args]; 83 } 84 const result: any = page.doc.fireEvent(el, type, e, domChanges, options); 85 page.differ.flush(); 86 page.doc.taskCenter.send('dom', { action: 'updateFinish' }, []); 87 return result; 88 } 89 return new Error(`Invalid element reference '${ref}'.`); 90} 91 92/** 93 * Update page content on a callback. 94 * @param {Page} page 95 * @param {number} callbackId 96 * @param {Object} data 97 * @param {boolean} ifKeepAlive 98 * @return {*} 99 */ 100export function callback(page: Page, callbackId?: number, data?: object, ifKeepAlive?: boolean): any { 101 Log.debug(`runtime/main/page/api/misc.js: Invoke a callback(${callbackId}) with ` 102 + `${JSON.stringify(data)} in page(${page.id}) ifKeepAlive = ${ifKeepAlive}.`); 103 const result: any = page.doc.taskCenter.consumeCallback(callbackId, data, ifKeepAlive); 104 updateActions(page); 105 page.doc.taskCenter.send('dom', { action: 'updateFinish' }, []); 106 return result; 107} 108 109/** 110 * Fire an event to update domChanges of a page. 111 * @param {Page} page - page. 112 * @param {*} - correspond to fireEvent args. 113 * @return {*} 114 */ 115export function fireEventSync(page: Page, ...args: any[]): any { 116 const result: any = fireEvent(page, ...args); 117 if (page && page.callTasks) { 118 let callbackId = ''; 119 for (let i = 0; i < args.length; i++) { 120 if (args[i]._callbackId !== undefined) { 121 callbackId = args[i]._callbackId; 122 break; 123 } 124 } 125 let resultStr = result || {}; 126 if (Array.isArray(resultStr)) { 127 resultStr = result[0] || {}; 128 } 129 if (resultStr.constructor !== String) { 130 resultStr = JSON.stringify(resultStr); 131 } 132 return page.callTasks([{ 133 module: 'internal.jsResult', 134 method: 'callbackNative', 135 args: [callbackId, resultStr] 136 }]); 137 } 138} 139 140/** 141 * Collect all virtual-DOM mutations together and send them to renderer. 142 * @param {Page} page 143 */ 144export function updateActions(page: Page): void { 145 page.differ.flush(); 146} 147 148/** 149 * Call all tasks from a page to renderer (native). 150 * @param {object} page 151 * @param {*} tasks 152 * @return {*} 153 */ 154export function callTasks(page: Page, tasks: any): any { 155 let result: any; 156 if (typof(tasks) !== 'array') { 157 tasks = [tasks]; 158 } 159 tasks.forEach(task => { 160 result = page.doc.taskCenter.send( 161 'module', 162 { 163 module: task.module, 164 method: task.method 165 }, 166 task.args 167 ); 168 }); 169 return result; 170} 171