-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
166 lines (146 loc) · 4.11 KB
/
index.js
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
(function () {
window.vanusConfig = {
id: null,
lang: 'en',
buttonAttr: {
innerHTML: '?',
},
iframeStyles: {
padding: '8px',
borderRadius: '12px',
backgroundColor: '#f9f9f9',
width: '360px',
height: '640px',
position: 'fixed',
right: '20px',
bottom: '80px',
zIndex: 500,
display: 'none',
border: '0px none',
},
buttonStyles: {
position: 'fixed',
right: '20px',
bottom: '20px',
zIndex: 500,
width: '50px',
height: '50px',
borderRadius: '25px',
border: '0px none',
color: '#fff',
fontSize: '20px',
background: 'linear-gradient(25deg, #086BFF, #A839FF)',
},
};
window.addEventListener('DOMContentLoaded', init);
})();
function init() {
const flag = checkVanusConfig();
if (!flag) {
console.error('Must set correct config');
return;
}
const config = getVanusConfig();
const iframe = initFloatIframe(config);
const button = initFloatButton(config, iframe);
document.body.appendChild(button);
document.body.appendChild(iframe);
}
function initFloatButton(config, iframe) {
let display = false;
let button = document.createElement("button");
button.innerHTML = config.buttonAttr.innerHTML;
deepAssign(button.style, config.buttonStyles);
button.onclick = function () {
if (display === false) {
iframe.style.display = 'block';
display = true;
} else {
iframe.style.display = 'none';
display = false;
}
};
return button;
}
function initFloatIframe(config) {
let iframe = document.createElement('iframe');
deepAssign(iframe.style, config.iframeStyles);
iframe.setAttribute('src', generateUrl());
return iframe;
}
function setStylesToIframe(styles = {}) {
let config = getVanusConfig();
let iframeStyles = config.iframeStyles;
let newIframeStyles = {
...iframeStyles,
...styles,
};
config.iframeStyles = newIframeStyles;
setVanusConfig(config);
}
function setStylesToButton(styles = {}) {
let config = getVanusConfig();
let buttonStyles = config.buttonStyles;
let newButtonStyles = {
...buttonStyles,
...styles,
};
config.buttonStyles = newButtonStyles;
setVanusConfig(config);
}
function setVanusConfig(config) {
const oldConfig = getVanusConfig();
const newConfig = deepAssign({}, oldConfig, config);
window.vanusConfig = newConfig;
}
function getVanusConfig() {
return window.vanusConfig;
}
function checkVanusConfig() {
let res = true;
const config = getVanusConfig();
if (!config.id || config.id.length === 0 || config.id.toString() !== config.id) {
res = false;
}
if (!config.lang || config.lang.length === 0 || config.lang.toString() !== config.lang || ['en', 'cn'].indexOf(config.lang) === -1) {
res = false;
}
return res;
}
function generateUrl() {
const config = getVanusConfig();
const lang = config.lang;
const id = config.id;
if (lang === 'cn') {
return 'https://ai.vanus.cn/app/embed?id=' + id;
} else {
return 'https://ai.vanus.ai/app/embed?id=' + id;
}
}
function deepAssign() {
let name = null;
let options = null;
let src = null;
let copy = null;
let length = arguments.length;
let i = 1;
let target = arguments[0] || {}
if (typeof target !== 'object') {
target = {};
}
for (; i < length; i++) {
options = arguments[i];
if (options != null) {
for (name in options) {
src = target[name];
copy = options[name];
if (copy && typeof copy == 'object') {
target[name] = deepAssign(src, copy);
} else if (copy !== undefined) {
target[name] = copy;
}
}
}
}
return target
}