-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
webView-component.ts
269 lines (236 loc) · 8.18 KB
/
webView-component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
http://www.cocos.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated engine source code (the "Software"), a limited,
worldwide, royalty-free, non-assignable, revocable and non-exclusive license
to use Cocos Creator solely to develop games on your target platforms. You shall
not use Cocos Creator software for developing other software or tools that's
used for developing games. You are not granted to publish, distribute,
sublicense, and/or sell copies of Cocos Creator.
The software or tools in this License Agreement are licensed, not sold.
Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
/**
* @category ui
*/
import { EventHandler } from '../../../core/components/component-event-handler';
import { ccclass, executionOrder, menu, property } from '../../../core/data/class-decorator';
import { UIComponent } from '../../../core/components/ui-base/ui-component';
import { WebViewEventType, WebViewImpl } from './webview-impl';
/**
* @en WebView event type
* @zh 网页视图事件类型。
* @enum WebView.EventType
*/
const EventType = WebViewEventType;
/**
* @en Web page Load completed.
* @zh 网页加载完成。
* @property {String} LOADED
*/
/**
* @en Web page is loading.
* @zh 网页加载中。
* @property {String} LOADING
*/
/**
* @en Web page error occurs when loading.
* @zh 网页加载出错。
* @property {String} ERROR
*/
//
function emptyCallback () { }
/**
* @en
* cc.WebView is a component for display web pages in the game.
*
* @zh
* WebView 组件,用于在游戏中显示网页。
*/
@ccclass('cc.WebviewComponent')
@menu('UI/WebView')
@executionOrder(100)
// @executeInEditMode
export class WebviewComponent extends UIComponent {
/**
* @en
* A given URL to be loaded by the WebView, it should have a http or https prefix.
*
* @zh
* 指定 WebView 加载的网址,它应该是一个 http 或者 https 开头的字符串。
*/
@property({
tooltip:'指定 WebView 加载的网址,它应该是一个 http 或者 https 开头的字符串',
})
get url () {
return this._url;
}
set url (url: string) {
this._url = url;
const impl = this._impl;
if (impl) {
impl.loadURL(url);
}
}
public static EventType = EventType;
/**
* @en
* The webview's event callback , it will be triggered when certain webview event occurs.
*
* @zh
* WebView 的回调事件,当网页加载过程中,加载完成后或者加载出错时都会回调此函数。
*/
@property({
type: EventHandler,
tooltip:'WebView 的回调事件,当网页加载过程中,加载完成后或者加载出错时都会回调此函数',
})
public webviewEvents: EventHandler[] = [];
@property
private _url = '';
private _impl: WebViewImpl | null = null;
constructor (){
super();
// TODO: finished in CC_EDITOR
if (!CC_EDITOR){
this._impl = new WebViewImpl();
}
}
public onRestore () {
if (!this._impl) {
this._impl = new WebViewImpl();
}
}
public onEnable () {
if (!this._impl){
return;
}
const impl = this._impl;
impl.createDomElementIfNeeded(this.node.width, this.node.height);
impl.loadURL(this._url);
impl.setVisible(true);
// if (!CC_EDITOR) {
impl.setEventListener(EventType.LOADED, this._onWebViewLoaded.bind(this));
impl.setEventListener(EventType.LOADING, this._onWebViewLoading.bind(this));
impl.setEventListener(EventType.ERROR, this._onWebViewLoadError.bind(this));
// }
}
public onDisable () {
if (!this._impl) {
return;
}
const impl = this._impl;
impl.setVisible(false);
// if (!CC_EDITOR) {
impl.setEventListener(EventType.LOADED, emptyCallback);
impl.setEventListener(EventType.LOADING, emptyCallback);
impl.setEventListener(EventType.ERROR, emptyCallback);
// }
}
public onDestroy () {
if (this._impl) {
this._impl.destroy();
this._impl = null;
}
}
public update (dt) {
if (CC_EDITOR){
return;
}
if (this._impl) {
this._impl.updateMatrix(this.node);
}
}
/**
* @en
* Set javascript interface scheme (see also setOnJSCallback). <br/>
* Note: Supports only on the Android and iOS. For HTML5, please refer to the official documentation.<br/>
* Please refer to the official documentation for more details.
*
* @zh
* 设置 JavaScript 接口方案(与 'setOnJSCallback' 配套使用)。<br/>
* 注意:只支持 Android 和 iOS ,Web 端用法请前往官方文档查看。<br/>
* 详情请参阅官方文档
* @param scheme - 接口方案。
*/
public setJavascriptInterfaceScheme (scheme: string) {
if (this._impl) {
this._impl.setJavascriptInterfaceScheme(scheme);
}
}
/**
* @en
* This callback called when load URL that start with javascript
* interface scheme (see also setJavascriptInterfaceScheme). <br/>
* Note: Supports only on the Android and iOS. For HTML5, please refer to the official documentation.<br/>
* Please refer to the official documentation for more details.
*
* @zh
* 当加载 URL 以 JavaScript 接口方案开始时调用这个回调函数。<br/>
* 注意:只支持 Android 和 iOS,Web 端用法请前往官方文档查看。
* 详情请参阅官方文档
*
* @param callback
*/
public setOnJSCallback (callback: Function) {
if (this._impl) {
this._impl.setOnJSCallback(callback);
}
}
/**
* @en
* Evaluates JavaScript in the context of the currently displayed page. <br/>
* Please refer to the official document for more details <br/>
* Note: Cross domain issues need to be resolved by yourself <br/>
*
* @zh
* 执行 WebView 内部页面脚本(详情请参阅官方文档)。 <br/>
* 注意:需要自行解决跨域问题
*
* @param str
*/
public evaluateJS (str: string) {
if (this._impl) {
this._impl.evaluateJS(str);
}
}
private _onWebViewLoaded () {
EventHandler.emitEvents(this.webviewEvents, this, EventType.LOADED);
this.node.emit('loaded', this);
}
private _onWebViewLoading () {
EventHandler.emitEvents(this.webviewEvents, this, EventType.LOADING);
this.node.emit('loading', this);
return true;
}
private _onWebViewLoadError () {
EventHandler.emitEvents(this.webviewEvents, this, EventType.ERROR);
this.node.emit('error', this);
}
}
cc.WebviewComponent = WebviewComponent;
/**
* @en if you don't need the WebView and it isn't in any running Scene, you should
* call the destroy method on this component or the associated node explicitly.
* Otherwise, the created DOM element won't be removed from web page.
*
* @zh
* 如果你不再使用 WebView,并且组件未添加到场景中,那么你必须手动对组件或所在节点调用 destroy。
* 这样才能移除网页上的 DOM 节点,避免 Web 平台内存泄露。
* @example
* ```
* webview.node.parent = null; // or webview.node.removeFromParent(false);
* // when you don't need webview anymore
* webview.node.destroy();
* ```
* @method destroy
* @return {Boolean} whether it is the first time the destroy being called
*/