You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
99 lines
2.8 KiB
99 lines
2.8 KiB
/*
|
|
* @Author: aBin
|
|
* @email: binbin0314@126.com
|
|
* @Date: 2021-04-19 10:23:19
|
|
* @LastEditors: aBin
|
|
* @LastEditTime: 2021-06-09 09:24:35
|
|
*/
|
|
/**
|
|
* ccsens tall sdk.js
|
|
* v1.0.0
|
|
* 父组件调用 TallPlugin.init()即可
|
|
* 监听message消息, 如果是created消息 就把tall的参数传递过来
|
|
*/
|
|
|
|
(function(window) {
|
|
// 单例局部变量
|
|
var _instance = null;
|
|
// 对外暴露TallPlugin类
|
|
window.TallPlugin = function(config) {
|
|
this.config = config;
|
|
this.props = null;
|
|
this.parent = '*';
|
|
};
|
|
|
|
// 初始化并保证是单例
|
|
TallPlugin.init = function(callback, config) {
|
|
if (!_instance) {
|
|
_instance = new TallPlugin(config);
|
|
_instance.config = config;
|
|
_instance.parent = window.parent.origin;
|
|
// DOM加载完成
|
|
window.addEventListener('DOMContentLoaded', _instance.mounted, false);
|
|
// window onload
|
|
window.addEventListener('load', _instance.loaded, false);
|
|
// destroy
|
|
window.addEventListener('unload', _instance.destroy, false);
|
|
// error
|
|
window.addEventListener('error', _instance.error, false);
|
|
_instance.onMessage(callback);
|
|
}
|
|
return _instance;
|
|
};
|
|
|
|
TallPlugin.prototype.onMessage = function(callback) {
|
|
var _this = this;
|
|
window.addEventListener(
|
|
'message',
|
|
function(event) {
|
|
try {
|
|
if (_this.parent === event.origin) {
|
|
// 是父窗体传来的消息
|
|
console.log('event.data:', event.data);
|
|
var data = JSON.parse(event.data);
|
|
_this.props = data || null;
|
|
callback && typeof callback === 'function' && callback.call(_this, data);
|
|
} else {
|
|
_this.props = null;
|
|
}
|
|
} catch (e) {
|
|
// console.error(`TallPlugin warn: ${e}`)
|
|
_this.props = null;
|
|
}
|
|
},
|
|
false,
|
|
);
|
|
return this;
|
|
};
|
|
|
|
// DOMContentLoaded DOM加载完成触发
|
|
TallPlugin.prototype.mounted = function(callback) {
|
|
var _this = this;
|
|
window.postMessage('created', _this.parent);
|
|
callback && typeof callback === 'function' && callback.call(this);
|
|
return this;
|
|
};
|
|
// load window onload触发
|
|
TallPlugin.prototype.loaded = function(callback) {
|
|
var _this = this;
|
|
window.postMessage('loaded', _this.parent);
|
|
callback && typeof callback === 'function' && callback.call(this);
|
|
return this;
|
|
};
|
|
|
|
// 子窗体销毁触发
|
|
TallPlugin.prototype.destroy = function(callback) {
|
|
var _this = this;
|
|
window.postMessage('destroy', _this.parent);
|
|
callback && typeof callback === 'function' && callback.call(this);
|
|
return this;
|
|
};
|
|
|
|
// error触发
|
|
TallPlugin.prototype.error = function(callback) {
|
|
var _this = this;
|
|
window.postMessage('error', _this.parent);
|
|
callback && typeof callback === 'function' && callback.call(this);
|
|
return this;
|
|
};
|
|
})(window);
|
|
|