hotent.helper.js
3.29 KB
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
/**
* !Copyright (c) 2021 Hotent(http://www.hotent.com)
*
* Version: 1.0.0
*/
; (function () {
var hotent = {
alreadyGetHeight: false,
}
window.hotent = hotent
setTimeout(function () {
// 如果url表单未传递高度消息时,会默认在300ms后传递
if (!hotent.alreadyGetHeight) {
hotent.getHeight()
}
}, 300)
// 是否现代浏览器
hotent.isModern = function () {
return window.addEventListener ? true : false
}
// 获取url参数
hotent.getUrlParam = function (key) {
var query = window.location.search.substring(1)
var vars = query.split('&')
for (var i = 0; i < vars.length; i++) {
var pair = vars[i].split('=')
if (pair[0] == key) {
return pair[1]
}
}
return false
}
// 添加事件监听
hotent.addListener = function (name, fn) {
if (!name || name.constructor != String) {
throw 'name could not be empty and must be String.'
}
if (!fn || fn.constructor != Function) {
throw 'fn could not be empty and must be Function.'
}
var eventMethod = hotent.isModern ? 'addEventListener' : 'attachEvent',
eventer = window[eventMethod]
var match = name.match(/^on(\w+)$/)
if (match && match.length == 2) {
name = match[1]
}
eventer(hotent.isModern ? name : 'on' + name, fn)
}
// 发送消息给父页面
hotent.sendMessage = function (params) {
window.parent && window.parent.postMessage(params, '*')
}
// 监听父页面发送过来的message
hotent.addListener('message', function (e) {
var type = e.data || 0
// window.addEventListener('message', (res) => {
//
// })
if (typeof type != 'string' && type.data) {
type = type.data.type ? type.data.type : type
}
if (!type || type.type != 'roger') {
// 子页面先回复父页面:收到消息
hotent.sendMessage({ type: 'roger' })
}
var alias = ''
// frm.contentWindow.postMessage({ messageName: 'saveData', alias }, '*')
if (type.messageName) {
alias = type.alias
type = type.messageName
}
switch (type) {
case 'getHeight' /*获取页面高度*/:
hotent.getHeight()
break
case 'saveData': /*保存页面数据*/
case 'modifyForm': /*打开新页面编辑数据*/
case 'printForm': /*打印*/
case 'validForm': /*验证数据*/
case 'getSubPageData' /*获取子页面数据的data 回执数据格式为{type:"getSubPageData",data:{表单数据}}*/:
hotent.invoke(type, alias)
break
case 'getParentPageData':
hotent.invoke(type, type.data)
break
}
})
// 调用页面定义的方法
hotent.invoke = function (methodName, params) {
var r = window[methodName]
if (!r || r.constructor != Function) {
throw '页面未提供方法:' + methodName
} else {
r.apply(null, [params])
}
}
// 获取页面高度
hotent.getHeight = function () {
var height = document.getElementsByTagName('body')[0].scrollHeight,
params = { type: 'height', height: height }
hotent.sendMessage(params)
hotent.alreadyGetHeight = true
}
// 监听鼠标滚动事件
hotent.addListener('wheel', function (e) {
var params = { type: 'wheel', wheelDeltaY: -e.deltaY }
hotent.sendMessage(params)
})
})()