mirror of
https://github.com/cocos/cocos4.git
synced 2026-12-01 15:52:40 +08:00
transplant webview in runtime (#4131)
This commit is contained in:
@@ -1,419 +0,0 @@
|
||||
/****************************************************************************
|
||||
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.
|
||||
****************************************************************************/
|
||||
|
||||
const utils = require('../core/platform/utils');
|
||||
const sys = require('../core/platform/CCSys');
|
||||
const renderEngine = require('../core/renderer/render-engine');
|
||||
const math = renderEngine.math;
|
||||
|
||||
let _mat4_temp = math.mat4.create();
|
||||
|
||||
let WebViewImpl = cc.Class({
|
||||
name: "WebViewImpl",
|
||||
|
||||
ctor () {
|
||||
// this.setContentSize(cc.size(300, 200));
|
||||
this._EventList = {};
|
||||
|
||||
this._visible = false;
|
||||
this._parent = null;
|
||||
this._div = null;
|
||||
this._iframe = null;
|
||||
this._listener = null;
|
||||
|
||||
// update matrix cache
|
||||
this._forceUpdate = true;
|
||||
this._m00 = 0;
|
||||
this._m01 = 0;
|
||||
this._m04 = 0;
|
||||
this._m05 = 0;
|
||||
this._m12 = 0;
|
||||
this._m13 = 0;
|
||||
this._w = 0;
|
||||
this._h = 0;
|
||||
//
|
||||
this.__eventListeners = {};
|
||||
},
|
||||
|
||||
_updateVisibility () {
|
||||
if (!this._div) return;
|
||||
let div = this._div;
|
||||
if (this._visible) {
|
||||
div.style.visibility = 'visible';
|
||||
}
|
||||
else {
|
||||
div.style.visibility = 'hidden';
|
||||
}
|
||||
this._forceUpdate = true;
|
||||
},
|
||||
|
||||
_updateSize (w, h) {
|
||||
let div = this._div;
|
||||
if (div) {
|
||||
div.style.width = w + "px";
|
||||
div.style.height = h + "px";
|
||||
}
|
||||
},
|
||||
|
||||
_initEvent () {
|
||||
let iframe = this._iframe;
|
||||
if (iframe) {
|
||||
let cbs = this.__eventListeners, self = this;
|
||||
cbs.load = function () {
|
||||
self._dispatchEvent(WebViewImpl.EventType.LOADED);
|
||||
};
|
||||
cbs.error = function () {
|
||||
self._dispatchEvent(WebViewImpl.EventType.ERROR);
|
||||
};
|
||||
iframe.addEventListener("load", cbs.load);
|
||||
iframe.addEventListener("error", cbs.error);
|
||||
}
|
||||
},
|
||||
|
||||
_initStyle () {
|
||||
if (!this._div) return;
|
||||
let div = this._div;
|
||||
div.style.position = "absolute";
|
||||
div.style.bottom = "0px";
|
||||
div.style.left = "0px";
|
||||
},
|
||||
|
||||
_setOpacity (opacity) {
|
||||
let iframe = this._iframe;
|
||||
if (iframe && iframe.style) {
|
||||
iframe.style.opacity = opacity / 255;
|
||||
}
|
||||
},
|
||||
|
||||
_createDom (w, h) {
|
||||
if (WebViewImpl._polyfill.enableDiv) {
|
||||
this._div = document.createElement("div");
|
||||
this._div.style["-webkit-overflow"] = "auto";
|
||||
this._div.style["-webkit-overflow-scrolling"] = "touch";
|
||||
this._iframe = document.createElement("iframe");
|
||||
this._div.appendChild(this._iframe);
|
||||
this._iframe.style.width = "100%";
|
||||
this._iframe.style.height = "100%";
|
||||
}
|
||||
else {
|
||||
this._div = this._iframe = document.createElement("iframe");
|
||||
}
|
||||
|
||||
if (WebViewImpl._polyfill.enableBG)
|
||||
this._div.style["background"] = "#FFF";
|
||||
|
||||
this._div.style["background"] = "#FFF";
|
||||
this._div.style.height = h + "px";
|
||||
this._div.style.width = w + "px";
|
||||
this._div.style.overflow = "scroll";
|
||||
this._iframe.style.border = "none";
|
||||
|
||||
cc.game.container.appendChild(this._div);
|
||||
this._updateVisibility();
|
||||
},
|
||||
|
||||
_createNativeControl (w, h) {
|
||||
this._createDom(w, h);
|
||||
this._initStyle();
|
||||
this._initEvent();
|
||||
},
|
||||
|
||||
createDomElementIfNeeded: CC_EDITOR ? function (w, h) {
|
||||
this._div = document.createElement('div');
|
||||
this._div.style.background = 'rgba(255, 255, 255, 0.8)';
|
||||
this._div.style.color = 'rgb(51, 51, 51)';
|
||||
this._div.style.height = w + 'px';
|
||||
this._div.style.width = h + 'px';
|
||||
this._div.style.position = 'absolute';
|
||||
this._div.style.bottom = '0px';
|
||||
this._div.style.left = '0px';
|
||||
this._div.style['word-wrap'] = 'break-word';
|
||||
cc.game.container.appendChild(this._div);
|
||||
} : function (w, h) {
|
||||
if (!this._div) {
|
||||
this._createNativeControl(w, h);
|
||||
}
|
||||
else {
|
||||
this._updateSize(w, h);
|
||||
}
|
||||
},
|
||||
|
||||
removeDom () {
|
||||
let div = this._div;
|
||||
if (div) {
|
||||
let hasChild = utils.contains(cc.game.container, div);
|
||||
if (hasChild)
|
||||
cc.game.container.removeChild(div);
|
||||
|
||||
this._div = null;
|
||||
}
|
||||
let iframe = this._iframe;
|
||||
if (iframe) {
|
||||
let cbs = this.__eventListeners;
|
||||
iframe.removeEventListener("load", cbs.load);
|
||||
iframe.removeEventListener("error", cbs.error);
|
||||
cbs.load = null;
|
||||
cbs.error = null;
|
||||
this._iframe = null;
|
||||
}
|
||||
},
|
||||
|
||||
setOnJSCallback (callback) {},
|
||||
setJavascriptInterfaceScheme (scheme) {},
|
||||
loadData (data, MIMEType, encoding, baseURL) {},
|
||||
loadHTMLString (string, baseURL) {},
|
||||
|
||||
/**
|
||||
* Load an URL
|
||||
* @param {String} url
|
||||
*/
|
||||
loadURL: CC_EDITOR ? function (url) {
|
||||
this._div.innerText = url;
|
||||
} : function (url) {
|
||||
let iframe = this._iframe;
|
||||
if (iframe) {
|
||||
iframe.src = url;
|
||||
let self = this;
|
||||
let cb = function () {
|
||||
self._loaded = true;
|
||||
self._updateVisibility();
|
||||
iframe.removeEventListener("load", cb);
|
||||
};
|
||||
iframe.addEventListener("load", cb);
|
||||
this._dispatchEvent(WebViewImpl.EventType.LOADING);
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Stop loading
|
||||
*/
|
||||
stopLoading () {
|
||||
cc.logID(7800);
|
||||
},
|
||||
|
||||
/**
|
||||
* Reload the WebView
|
||||
*/
|
||||
reload () {
|
||||
let iframe = this._iframe;
|
||||
if (iframe) {
|
||||
let win = iframe.contentWindow;
|
||||
if (win && win.location)
|
||||
win.location.reload();
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Determine whether to go back
|
||||
*/
|
||||
canGoBack () {
|
||||
cc.logID(7801);
|
||||
return true;
|
||||
},
|
||||
|
||||
/**
|
||||
* Determine whether to go forward
|
||||
*/
|
||||
canGoForward () {
|
||||
cc.logID(7802);
|
||||
return true;
|
||||
},
|
||||
|
||||
/**
|
||||
* go back
|
||||
*/
|
||||
goBack () {
|
||||
try {
|
||||
if (WebViewImpl._polyfill.closeHistory)
|
||||
return cc.logID(7803);
|
||||
let iframe = this._iframe;
|
||||
if (iframe) {
|
||||
let win = iframe.contentWindow;
|
||||
if (win && win.location)
|
||||
win.history.back.call(win);
|
||||
}
|
||||
} catch (err) {
|
||||
cc.log(err);
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* go forward
|
||||
*/
|
||||
goForward () {
|
||||
try {
|
||||
if (WebViewImpl._polyfill.closeHistory)
|
||||
return cc.logID(7804);
|
||||
let iframe = this._iframe;
|
||||
if (iframe) {
|
||||
let win = iframe.contentWindow;
|
||||
if (win && win.location)
|
||||
win.history.forward.call(win);
|
||||
}
|
||||
} catch (err) {
|
||||
cc.log(err);
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* In the webview execution within a period of js string
|
||||
* @param {String} str
|
||||
*/
|
||||
evaluateJS (str) {
|
||||
let iframe = this._iframe;
|
||||
if (iframe) {
|
||||
let win = iframe.contentWindow;
|
||||
try {
|
||||
win.eval(str);
|
||||
this._dispatchEvent(WebViewImpl.EventType.JS_EVALUATED);
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Limited scale
|
||||
*/
|
||||
setScalesPageToFit () {
|
||||
cc.logID(7805);
|
||||
},
|
||||
|
||||
/**
|
||||
* The binding event
|
||||
* @param {WebViewImpl.EventType} event
|
||||
* @param {Function} callback
|
||||
*/
|
||||
setEventListener (event, callback) {
|
||||
this._EventList[event] = callback;
|
||||
},
|
||||
|
||||
/**
|
||||
* Delete events
|
||||
* @param {WebViewImpl.EventType} event
|
||||
*/
|
||||
removeEventListener (event) {
|
||||
this._EventList[event] = null;
|
||||
},
|
||||
|
||||
_dispatchEvent (event) {
|
||||
let callback = this._EventList[event];
|
||||
if (callback)
|
||||
callback.call(this, this, this._iframe.src);
|
||||
},
|
||||
|
||||
_createRenderCmd () {
|
||||
return new WebViewImpl.RenderCmd(this);
|
||||
},
|
||||
|
||||
destroy () {
|
||||
this.removeDom();
|
||||
},
|
||||
|
||||
setVisible (visible) {
|
||||
if (this._visible !== visible) {
|
||||
this._visible = !!visible;
|
||||
this._updateVisibility();
|
||||
}
|
||||
},
|
||||
|
||||
updateMatrix (node) {
|
||||
if (!this._div || !this._visible) return;
|
||||
|
||||
node.getWorldMatrix(_mat4_temp);
|
||||
if (!this._forceUpdate &&
|
||||
this._m00 === _mat4_temp.m00 && this._m01 === _mat4_temp.m01 &&
|
||||
this._m04 === _mat4_temp.m04 && this._m05 === _mat4_temp.m05 &&
|
||||
this._m12 === _mat4_temp.m12 && this._m13 === _mat4_temp.m13 &&
|
||||
this._w === node._contentSize.width && this._h === node._contentSize.height) {
|
||||
return;
|
||||
}
|
||||
|
||||
// update matrix cache
|
||||
this._m00 = _mat4_temp.m00;
|
||||
this._m01 = _mat4_temp.m01;
|
||||
this._m04 = _mat4_temp.m04;
|
||||
this._m05 = _mat4_temp.m05;
|
||||
this._m12 = _mat4_temp.m12;
|
||||
this._m13 = _mat4_temp.m13;
|
||||
this._w = node._contentSize.width;
|
||||
this._h = node._contentSize.height;
|
||||
|
||||
let scaleX = cc.view._scaleX, scaleY = cc.view._scaleY;
|
||||
let dpr = cc.view._devicePixelRatio;
|
||||
|
||||
scaleX /= dpr;
|
||||
scaleY /= dpr;
|
||||
|
||||
let container = cc.game.container;
|
||||
let a = _mat4_temp.m00 * scaleX, b = _mat4_temp.m01, c = _mat4_temp.m04, d = _mat4_temp.m05 * scaleY;
|
||||
|
||||
let offsetX = container && container.style.paddingLeft ? parseInt(container.style.paddingLeft) : 0;
|
||||
let offsetY = container && container.style.paddingBottom ? parseInt(container.style.paddingBottom) : 0;
|
||||
this._updateSize(this._w, this._h);
|
||||
let w = this._div.clientWidth * scaleX;
|
||||
let h = this._div.clientHeight * scaleY;
|
||||
let appx = (w * _mat4_temp.m00) * node._anchorPoint.x;
|
||||
let appy = (h * _mat4_temp.m05) * node._anchorPoint.y;
|
||||
let tx = _mat4_temp.m12 * scaleX - appx + offsetX, ty = _mat4_temp.m13 * scaleY - appy + offsetY;
|
||||
|
||||
let matrix = "matrix(" + a + "," + -b + "," + -c + "," + d + "," + tx + "," + -ty + ")";
|
||||
this._div.style['transform'] = matrix;
|
||||
this._div.style['-webkit-transform'] = matrix;
|
||||
this._div.style['transform-origin'] = '0px 100% 0px';
|
||||
this._div.style['-webkit-transform-origin'] = '0px 100% 0px';
|
||||
|
||||
// chagned iframe opacity
|
||||
this._setOpacity(node.opacity);
|
||||
}
|
||||
});
|
||||
|
||||
WebViewImpl.EventType = {
|
||||
LOADING: 0,
|
||||
LOADED: 1,
|
||||
ERROR: 2,
|
||||
JS_EVALUATED: 3
|
||||
};
|
||||
|
||||
let polyfill = WebViewImpl._polyfill = {
|
||||
devicePixelRatio: false,
|
||||
enableDiv: false
|
||||
};
|
||||
|
||||
if (sys.os === sys.OS_IOS)
|
||||
polyfill.enableDiv = true;
|
||||
|
||||
if (sys.isMobile) {
|
||||
if (sys.browserType === sys.BROWSER_TYPE_FIREFOX) {
|
||||
polyfill.enableBG = true;
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (sys.browserType === sys.BROWSER_TYPE_IE) {
|
||||
polyfill.closeHistory = true;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = WebViewImpl;
|
||||
+103
-96
@@ -24,15 +24,17 @@
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
const WebViewImpl = require('./webview-impl');
|
||||
import { EventHandler } from '../../../../components/component-event-handler';
|
||||
import { ccclass, executeInEditMode, executionOrder, menu, property } from '../../../../core/data/class-decorator';
|
||||
import { UIComponent } from '../ui-component';
|
||||
import { WebViewEventType, WebViewImpl } from './webview-impl';
|
||||
|
||||
/**
|
||||
* !#en WebView event type
|
||||
* !#zh 网页视图事件类型
|
||||
* @enum WebView.EventType
|
||||
*/
|
||||
const EventType = WebViewImpl.EventType;
|
||||
|
||||
const EventType = WebViewEventType;
|
||||
|
||||
/**
|
||||
* !#en Web page Load completed.
|
||||
@@ -61,115 +63,105 @@ function emptyCallback () { }
|
||||
* @class WebView
|
||||
* @extends Component
|
||||
*/
|
||||
let WebView = cc.Class({
|
||||
name: 'cc.WebView',
|
||||
extends: cc.Component,
|
||||
|
||||
editor: CC_EDITOR && {
|
||||
menu: 'i18n:MAIN_MENU.component.ui/WebView',
|
||||
executeInEditMode: true
|
||||
},
|
||||
@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 {String} url
|
||||
*/
|
||||
@property
|
||||
get url () {
|
||||
return this._url;
|
||||
}
|
||||
|
||||
properties: {
|
||||
_useOriginalSize: true,
|
||||
set url (url: string) {
|
||||
this._url = url;
|
||||
const impl = this._impl;
|
||||
if (impl) {
|
||||
impl.loadURL(url);
|
||||
}
|
||||
}
|
||||
public static EventType = EventType;
|
||||
|
||||
_url: '',
|
||||
/**
|
||||
* !#en A given URL to be loaded by the WebView, it should have a http or https prefix.
|
||||
* !#zh 指定 WebView 加载的网址,它应该是一个 http 或者 https 开头的字符串
|
||||
* @property {String} url
|
||||
*/
|
||||
url: {
|
||||
type: String,
|
||||
tooltip: CC_DEV && 'i18n:COMPONENT.webview.url',
|
||||
get: function () {
|
||||
return this._url;
|
||||
},
|
||||
set: function (url) {
|
||||
this._url = url;
|
||||
let impl = this._impl;
|
||||
if (impl) {
|
||||
impl.loadURL(url);
|
||||
}
|
||||
}
|
||||
},
|
||||
/**
|
||||
* !#en The webview's event callback , it will be triggered when certain webview event occurs.
|
||||
* !#zh WebView 的回调事件,当网页加载过程中,加载完成后或者加载出错时都会回调此函数
|
||||
* @property {Component.EventHandler[]} webviewLoadedEvents
|
||||
*/
|
||||
@property({
|
||||
type: EventHandler,
|
||||
})
|
||||
public webviewEvents: EventHandler[] = [];
|
||||
|
||||
/**
|
||||
* !#en The webview's event callback , it will be triggered when certain webview event occurs.
|
||||
* !#zh WebView 的回调事件,当网页加载过程中,加载完成后或者加载出错时都会回调此函数
|
||||
* @property {Component.EventHandler[]} webviewLoadedEvents
|
||||
*/
|
||||
webviewEvents: {
|
||||
default: [],
|
||||
type: cc.Component.EventHandler,
|
||||
},
|
||||
},
|
||||
@property
|
||||
private _url = '';
|
||||
private _impl: WebViewImpl | null = null;
|
||||
|
||||
statics: {
|
||||
EventType: EventType,
|
||||
},
|
||||
constructor (){
|
||||
super();
|
||||
// TODO: finished in CC_EDITOR
|
||||
if (!CC_EDITOR){
|
||||
this._impl = new WebViewImpl();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
ctor () {
|
||||
this._impl = new WebViewImpl();
|
||||
},
|
||||
|
||||
onRestore () {
|
||||
public onRestore () {
|
||||
if (!this._impl) {
|
||||
this._impl = new WebViewImpl();
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
onEnable () {
|
||||
let impl = this._impl;
|
||||
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));
|
||||
}
|
||||
},
|
||||
// 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));
|
||||
// }
|
||||
}
|
||||
|
||||
onDisable () {
|
||||
let impl = this._impl;
|
||||
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);
|
||||
}
|
||||
},
|
||||
// if (!CC_EDITOR) {
|
||||
impl.setEventListener(EventType.LOADED, emptyCallback);
|
||||
impl.setEventListener(EventType.LOADING, emptyCallback);
|
||||
impl.setEventListener(EventType.ERROR, emptyCallback);
|
||||
// }
|
||||
}
|
||||
|
||||
onDestroy () {
|
||||
public onDestroy () {
|
||||
if (this._impl) {
|
||||
this._impl.destroy();
|
||||
this._impl = null;
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
public update (dt) {
|
||||
if (CC_EDITOR){
|
||||
return;
|
||||
}
|
||||
|
||||
update (dt) {
|
||||
if (this._impl) {
|
||||
this._impl.updateMatrix(this.node);
|
||||
}
|
||||
},
|
||||
|
||||
_onWebViewLoaded () {
|
||||
cc.Component.EventHandler.emitEvents(this.webviewEvents, this, EventType.LOADED);
|
||||
this.node.emit('loaded', this);
|
||||
},
|
||||
|
||||
_onWebViewLoading () {
|
||||
cc.Component.EventHandler.emitEvents(this.webviewEvents, this, EventType.LOADING);
|
||||
this.node.emit('loading', this);
|
||||
return true;
|
||||
},
|
||||
|
||||
_onWebViewLoadError () {
|
||||
cc.Component.EventHandler.emitEvents(this.webviewEvents, this, EventType.ERROR);
|
||||
this.node.emit('error', this);
|
||||
},
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en
|
||||
@@ -183,11 +175,11 @@ let WebView = cc.Class({
|
||||
* @method setJavascriptInterfaceScheme
|
||||
* @param {String} scheme
|
||||
*/
|
||||
setJavascriptInterfaceScheme (scheme) {
|
||||
public setJavascriptInterfaceScheme (scheme: string) {
|
||||
if (this._impl) {
|
||||
this._impl.setJavascriptInterfaceScheme(scheme);
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en
|
||||
@@ -202,11 +194,11 @@ let WebView = cc.Class({
|
||||
* @method setOnJSCallback
|
||||
* @param {Function} callback
|
||||
*/
|
||||
setOnJSCallback (callback) {
|
||||
public setOnJSCallback (callback: Function) {
|
||||
if (this._impl) {
|
||||
this._impl.setOnJSCallback(callback);
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en
|
||||
@@ -219,15 +211,30 @@ let WebView = cc.Class({
|
||||
* @method evaluateJS
|
||||
* @param {String} str
|
||||
*/
|
||||
evaluateJS (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);
|
||||
}
|
||||
|
||||
cc.WebView = module.exports = WebView;
|
||||
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
|
||||
* Note: This event is emitted from the node to which the component belongs.
|
||||
@@ -0,0 +1,459 @@
|
||||
/****************************************************************************
|
||||
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.
|
||||
****************************************************************************/
|
||||
|
||||
import { ccclass} from '../../../../core/data/class-decorator';
|
||||
import sys from '../../../../core/platform/CCSys';
|
||||
import * as utils from '../../../../core/utils/misc';
|
||||
import * as vmath from '../../../../core/vmath';
|
||||
import { Node } from '../../../../scene-graph';
|
||||
import { UIRenderComponent } from '../ui-render-component';
|
||||
|
||||
const _mat4_temp = vmath.mat4.create();
|
||||
|
||||
export enum WebViewEventType {
|
||||
LOADING = 0,
|
||||
LOADED = 1,
|
||||
ERROR = 2,
|
||||
JS_EVALUATED = 3,
|
||||
}
|
||||
|
||||
interface IFrameEventListener{
|
||||
load: EventListenerOrEventListenerObject | null;
|
||||
error: EventListenerOrEventListenerObject | null;
|
||||
}
|
||||
|
||||
interface IPolyfill{
|
||||
devicePixelRatio: boolean;
|
||||
enableDiv: boolean;
|
||||
enableBG?: boolean;
|
||||
closeHistory?: boolean;
|
||||
}
|
||||
|
||||
const polyfill: IPolyfill = {
|
||||
devicePixelRatio: false,
|
||||
enableDiv: false,
|
||||
};
|
||||
|
||||
if (sys.os === sys.OS_IOS) {
|
||||
polyfill.enableDiv = true;
|
||||
}
|
||||
|
||||
if (sys.isMobile) {
|
||||
if (sys.browserType === sys.BROWSER_TYPE_FIREFOX) {
|
||||
polyfill.enableBG = true;
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (sys.browserType === sys.BROWSER_TYPE_IE) {
|
||||
polyfill.closeHistory = true;
|
||||
}
|
||||
}
|
||||
|
||||
@ccclass('cc.WebviewImpl')
|
||||
export class WebViewImpl{
|
||||
public static Polyfill = polyfill;
|
||||
public static EventType = WebViewEventType;
|
||||
private _EventList = new Map<WebViewEventType, Function | null>();
|
||||
|
||||
private _visible = false;
|
||||
private _div: HTMLElement | null = null;
|
||||
private _iframe: HTMLIFrameElement | null = null;
|
||||
|
||||
// update matrix cache
|
||||
private _forceUpdate = true;
|
||||
private _m00 = 0;
|
||||
private _m01 = 0;
|
||||
private _m04 = 0;
|
||||
private _m05 = 0;
|
||||
private _m12 = 0;
|
||||
private _m13 = 0;
|
||||
private _w = 0;
|
||||
private _h = 0;
|
||||
private _eventListeners: IFrameEventListener = { load: () => {}, error: () => {} };
|
||||
|
||||
public createDomElementIfNeeded (w: number, h: number){
|
||||
// if (CC_EDITOR) {
|
||||
// this._div = document.createElement('div');
|
||||
// this._div.style.background = 'rgba(255, 255, 255, 0.8)';
|
||||
// this._div.style.color = 'rgb(51, 51, 51)';
|
||||
// this._div.style.height = w + 'px';
|
||||
// this._div.style.width = h + 'px';
|
||||
// this._div.style.position = 'absolute';
|
||||
// this._div.style.bottom = '0px';
|
||||
// this._div.style.left = '0px';
|
||||
// this._div.style['word-wrap'] = 'break-word';
|
||||
// cc.game.container.appendChild(this._div);
|
||||
// } else {
|
||||
if (!this._div) {
|
||||
this._createNativeControl(w, h);
|
||||
}
|
||||
else {
|
||||
this._updateSize(w, h);
|
||||
}
|
||||
// }
|
||||
}
|
||||
|
||||
public removeDom () {
|
||||
const div = this._div;
|
||||
if (div) {
|
||||
const hasChild = utils.contains(cc.game.container, div);
|
||||
if (hasChild) {
|
||||
cc.game.container.removeChild(div);
|
||||
}
|
||||
|
||||
this._div = null;
|
||||
}
|
||||
const iframe = this._iframe;
|
||||
if (iframe) {
|
||||
const cbs = this._eventListeners;
|
||||
iframe.removeEventListener('load', cbs.load!);
|
||||
iframe.removeEventListener('error', cbs.error!);
|
||||
cbs.load = null;
|
||||
cbs.error = null;
|
||||
this._iframe = null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Load an URL
|
||||
* @param {String} url
|
||||
*/
|
||||
public loadURL (url: string) {
|
||||
if (CC_EDITOR) {
|
||||
if (this._div){
|
||||
this._div.innerText = url;
|
||||
}
|
||||
} else {
|
||||
const iframe = this._iframe;
|
||||
if (iframe) {
|
||||
iframe.src = url;
|
||||
const self = this;
|
||||
const cb = () => {
|
||||
// self._loaded = true;
|
||||
self._updateVisibility();
|
||||
iframe.removeEventListener('load', cb);
|
||||
};
|
||||
iframe.addEventListener('load', cb);
|
||||
this._dispatchEvent(WebViewImpl.EventType.LOADING);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Stop loading
|
||||
*/
|
||||
public stopLoading () {
|
||||
cc.logID(7800);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reload the WebView
|
||||
*/
|
||||
public reload () {
|
||||
const iframe = this._iframe;
|
||||
if (iframe) {
|
||||
const win = iframe.contentWindow;
|
||||
if (win && win.location) {
|
||||
win.location.reload();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether to go back
|
||||
*/
|
||||
public canGoBack () {
|
||||
cc.logID(7801);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether to go forward
|
||||
*/
|
||||
public canGoForward () {
|
||||
cc.logID(7802);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* go back
|
||||
*/
|
||||
public goBack () {
|
||||
try {
|
||||
if (WebViewImpl.Polyfill.closeHistory) {
|
||||
return cc.logID(7803);
|
||||
}
|
||||
const iframe = this._iframe;
|
||||
if (iframe) {
|
||||
const win = iframe.contentWindow;
|
||||
if (win && win.location) {
|
||||
win.history.back.call(win);
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
cc.log(err);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* go forward
|
||||
*/
|
||||
public goForward () {
|
||||
try {
|
||||
if (WebViewImpl.Polyfill.closeHistory) {
|
||||
return cc.logID(7804);
|
||||
}
|
||||
const iframe = this._iframe;
|
||||
if (iframe) {
|
||||
const win = iframe.contentWindow;
|
||||
if (win && win.location) {
|
||||
win.history.forward.call(win);
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
cc.log(err);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* In the webview execution within a period of js string
|
||||
* @param {String} str
|
||||
*/
|
||||
public evaluateJS (str: string) {
|
||||
const iframe = this._iframe;
|
||||
if (iframe) {
|
||||
const win = iframe.contentWindow;
|
||||
if (win){
|
||||
try {
|
||||
// eval(str);
|
||||
// this._dispatchEvent(WebViewImpl.EventType.JS_EVALUATED);
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Limited scale
|
||||
*/
|
||||
public setScalesPageToFit () {
|
||||
cc.logID(7805);
|
||||
}
|
||||
|
||||
/**
|
||||
* The binding event
|
||||
* @param {WebViewImpl.EventType} event
|
||||
* @param {Function} callback
|
||||
*/
|
||||
public setEventListener (event: WebViewEventType, callback: Function) {
|
||||
this._EventList[event] = callback;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete events
|
||||
* @param {WebViewImpl.EventType} event
|
||||
*/
|
||||
public removeEventListener (event: WebViewEventType) {
|
||||
this._EventList[event] = null;
|
||||
}
|
||||
|
||||
public _dispatchEvent (event: WebViewEventType) {
|
||||
const callback = this._EventList[event];
|
||||
if (callback && this._iframe) {
|
||||
callback.call(this, this, this._iframe.src);
|
||||
}
|
||||
}
|
||||
|
||||
// public _createRenderCmd () {
|
||||
// return new WebViewImpl.RenderCmd(this);
|
||||
// }
|
||||
|
||||
public destroy () {
|
||||
this.removeDom();
|
||||
}
|
||||
|
||||
public setVisible (visible: boolean) {
|
||||
if (this._visible !== visible) {
|
||||
this._visible = !!visible;
|
||||
this._updateVisibility();
|
||||
}
|
||||
}
|
||||
|
||||
public updateMatrix (node: Node) {
|
||||
if (!this._div || !this._visible) {
|
||||
return;
|
||||
}
|
||||
|
||||
node.getWorldMatrix(_mat4_temp);
|
||||
const contentSize = node.getContentSize();
|
||||
if (!this._forceUpdate &&
|
||||
this._m00 === _mat4_temp.m00 && this._m01 === _mat4_temp.m01 &&
|
||||
this._m04 === _mat4_temp.m04 && this._m05 === _mat4_temp.m05 &&
|
||||
this._m12 === _mat4_temp.m12 && this._m13 === _mat4_temp.m13 &&
|
||||
this._w === contentSize.width && this._h === contentSize.height) {
|
||||
return;
|
||||
}
|
||||
|
||||
// update matrix cache
|
||||
this._m00 = _mat4_temp.m00;
|
||||
this._m01 = _mat4_temp.m01;
|
||||
this._m04 = _mat4_temp.m04;
|
||||
this._m05 = _mat4_temp.m05;
|
||||
this._m12 = _mat4_temp.m12;
|
||||
this._m13 = _mat4_temp.m13;
|
||||
this._w = contentSize.width;
|
||||
this._h = contentSize.height;
|
||||
|
||||
let scaleX = cc.view._scaleX;
|
||||
let scaleY = cc.view._scaleY;
|
||||
const dpr = cc.view._devicePixelRatio;
|
||||
|
||||
scaleX /= dpr;
|
||||
scaleY /= dpr;
|
||||
|
||||
const container = cc.game.container;
|
||||
const a = _mat4_temp.m00 * scaleX;
|
||||
const b = _mat4_temp.m01;
|
||||
const c = _mat4_temp.m04;
|
||||
const d = _mat4_temp.m05 * scaleY;
|
||||
|
||||
const offsetX = container && container.style.paddingLeft ? parseInt(container.style.paddingLeft) : 0;
|
||||
const offsetY = container && container.style.paddingBottom ? parseInt(container.style.paddingBottom) : 0;
|
||||
this._updateSize(this._w, this._h);
|
||||
const w = this._div.clientWidth * scaleX;
|
||||
const h = this._div.clientHeight * scaleY;
|
||||
const ap = node.getAnchorPoint();
|
||||
const appx = (w * _mat4_temp.m00) * ap.x;
|
||||
const appy = (h * _mat4_temp.m05) * ap.y;
|
||||
const tx = _mat4_temp.m12 * scaleX - appx + offsetX;
|
||||
const ty = _mat4_temp.m13 * scaleY - appy + offsetY;
|
||||
|
||||
const matrix = 'matrix(' + a + ',' + -b + ',' + -c + ',' + d + ',' + tx + ',' + -ty + ')';
|
||||
this._div.style.transform = matrix;
|
||||
this._div.style['-webkit-transform'] = matrix;
|
||||
this._div.style['transform-origin'] = '0px 100% 0px';
|
||||
this._div.style['-webkit-transform-origin'] = '0px 100% 0px';
|
||||
|
||||
// chagned iframe opacity
|
||||
const renderComp = node.getComponent(UIRenderComponent);
|
||||
if (renderComp){
|
||||
this._setOpacity(renderComp.color.a);
|
||||
}
|
||||
}
|
||||
|
||||
public setOnJSCallback (callback) { }
|
||||
public setJavascriptInterfaceScheme (scheme) { }
|
||||
public loadData (data, MIMEType, encoding, baseURL) { }
|
||||
public loadHTMLString (string, baseURL) { }
|
||||
|
||||
private _updateVisibility () {
|
||||
if (!this._div) { return; }
|
||||
const div = this._div;
|
||||
if (this._visible) {
|
||||
div.style.visibility = 'visible';
|
||||
}
|
||||
else {
|
||||
div.style.visibility = 'hidden';
|
||||
}
|
||||
this._forceUpdate = true;
|
||||
}
|
||||
|
||||
private _updateSize (w: number, h: number) {
|
||||
const div = this._div;
|
||||
if (div) {
|
||||
div.style.width = w + 'px';
|
||||
div.style.height = h + 'px';
|
||||
}
|
||||
}
|
||||
|
||||
private _initEvent () {
|
||||
const iframe = this._iframe;
|
||||
if (iframe) {
|
||||
const cbs = this._eventListeners;
|
||||
const self = this;
|
||||
cbs.load = () => {
|
||||
self._dispatchEvent(WebViewImpl.EventType.LOADED);
|
||||
};
|
||||
cbs.error = () => {
|
||||
self._dispatchEvent(WebViewImpl.EventType.ERROR);
|
||||
};
|
||||
iframe.addEventListener('load', cbs.load);
|
||||
iframe.addEventListener('error', cbs.error);
|
||||
}
|
||||
}
|
||||
|
||||
private _initStyle () {
|
||||
if (!this._div) {
|
||||
return;
|
||||
}
|
||||
|
||||
const div = this._div;
|
||||
div.style.position = 'absolute';
|
||||
div.style.bottom = '0px';
|
||||
div.style.left = '0px';
|
||||
}
|
||||
|
||||
private _setOpacity (opacity: number) {
|
||||
const iframe = this._iframe;
|
||||
if (iframe && iframe.style) {
|
||||
iframe.style.opacity = (opacity / 255).toString();
|
||||
}
|
||||
}
|
||||
|
||||
private _createDom (w: number, h: number) {
|
||||
if (WebViewImpl.Polyfill.enableDiv) {
|
||||
this._div = document.createElement('div');
|
||||
this._div.style['-webkit-overflow'] = 'auto';
|
||||
this._div.style['-webkit-overflow-scrolling'] = 'touch';
|
||||
this._iframe = document.createElement('iframe');
|
||||
this._div.appendChild(this._iframe);
|
||||
this._iframe.style.width = '100%';
|
||||
this._iframe.style.height = '100%';
|
||||
}
|
||||
else {
|
||||
this._div = this._iframe = document.createElement('iframe');
|
||||
}
|
||||
|
||||
if (WebViewImpl.Polyfill.enableBG) {
|
||||
this._div.style.background = '#FFF';
|
||||
}
|
||||
|
||||
this._div.style.background = '#FFF';
|
||||
this._div.style.height = h + 'px';
|
||||
this._div.style.width = w + 'px';
|
||||
this._div.style.overflow = 'scroll';
|
||||
this._iframe.style.border = 'none';
|
||||
|
||||
cc.game.container.appendChild(this._div);
|
||||
this._updateVisibility();
|
||||
}
|
||||
|
||||
private _createNativeControl (w: number, h: number) {
|
||||
this._createDom(w, h);
|
||||
this._initStyle();
|
||||
this._initEvent();
|
||||
}
|
||||
}
|
||||
@@ -18,4 +18,5 @@ import './components/ui-render-component';
|
||||
import './components/ui-transfrom-component';
|
||||
import './components/ui-transfrom-component';
|
||||
import './components/view-group-component';
|
||||
import './components/webview/webView-component';
|
||||
import './components/widget-component';
|
||||
|
||||
Reference in New Issue
Block a user