Skip to content

Commit c3c8f9b

Browse files
authored
Merge pull request #2 from vanus-labs/develop
feat: init code
2 parents 6642f3b + a9e1fcd commit c3c8f9b

File tree

2 files changed

+226
-0
lines changed

2 files changed

+226
-0
lines changed

.gitignore

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# System
2+
.DS_Store
3+
4+
# IDE or eidtor
5+
.idea/
6+
.code/
7+
8+
# Dependency directories
9+
node_modules/
10+
11+
# Logs
12+
logs
13+
*.log
14+
npm-debug.log*
15+
yarn-debug.log*
16+
yarn-error.log*
17+
18+
# Runtime data
19+
pids
20+
*.pid
21+
*.seed
22+
*.pid.lock
23+
24+
# Package tool
25+
.happypack
26+
27+
# Directory for instrumented libs generated by jscoverage/JSCover
28+
lib-cov
29+
30+
# Coverage directory used by tools like istanbul
31+
coverage
32+
33+
# nyc test coverage
34+
.nyc_output
35+
36+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
37+
.grunt
38+
39+
# node-waf configuration
40+
.lock-wscript
41+
42+
# Optional npm cache directory
43+
.npm
44+
45+
# Optional eslint cache
46+
.eslintcache
47+
48+
# Optional REPL history
49+
.node_repl_history
50+
51+
# Output of 'npm pack'
52+
*.tgz
53+
54+
# Yarn Integrity file
55+
.yarn-integrity
56+
57+
# dotenv environment variables file
58+
.env
59+
60+
package-lock.json

index.js

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
(function () {
2+
window.addEventListener('DOMContentLoaded', init);
3+
})();
4+
5+
function init() {
6+
window.vanusConfig = {
7+
id: null,
8+
lang: 'en',
9+
10+
buttonAttr: {
11+
innerHTML: '?',
12+
},
13+
14+
iframeStyles: {
15+
padding: '8px',
16+
borderRadius: '12px',
17+
backgroundColor: '#f9f9f9',
18+
width: '360px',
19+
height: '640px',
20+
position: 'fixed',
21+
right: '20px',
22+
bottom: '80px',
23+
zIndex: 500,
24+
display: 'none',
25+
border: '0px none',
26+
},
27+
28+
buttonStyles: {
29+
position: 'fixed',
30+
right: '20px',
31+
bottom: '20px',
32+
zIndex: 500,
33+
width: '50px',
34+
height: '50px',
35+
borderRadius: '25px',
36+
border: '0px none',
37+
color: '#fff',
38+
fontSize: '20px',
39+
background: 'linear-gradient(25deg, #086BFF, #A839FF)',
40+
},
41+
};
42+
43+
const flag = checkVanusConfig();
44+
if (!flag) {
45+
console.error('Must set correct config');
46+
return;
47+
}
48+
49+
const config = getVanusConfig();
50+
51+
const iframe = initFloatIframe(config);
52+
const button = initFloatButton(config, iframe);
53+
54+
document.body.appendChild(button);
55+
document.body.appendChild(iframe);
56+
}
57+
58+
function initFloatButton(config, iframe) {
59+
let display = false;
60+
let button = document.createElement("button");
61+
button.innerHTML = config.buttonAttr.innerHTML;
62+
deepAssign(button.style, config.buttonStyles);
63+
button.onclick = function () {
64+
if (display === false) {
65+
iframe.style.display = 'block';
66+
display = true;
67+
} else {
68+
iframe.style.display = 'none';
69+
display = false;
70+
}
71+
};
72+
return button;
73+
}
74+
75+
function initFloatIframe(config) {
76+
let iframe = document.createElement('iframe');
77+
deepAssign(iframe.style, config.iframeStyles);
78+
iframe.setAttribute('src', generateUrl());
79+
return iframe;
80+
}
81+
82+
function setStylesToIframe(styles = {}) {
83+
let config = getVanusConfig();
84+
let iframeStyles = config.iframeStyles;
85+
let newIframeStyles = {
86+
...iframeStyles,
87+
...styles,
88+
};
89+
config.iframeStyles = newIframeStyles;
90+
setVanusConfig(config);
91+
}
92+
93+
function setStylesToButton(styles = {}) {
94+
let config = getVanusConfig();
95+
let buttonStyles = config.buttonStyles;
96+
let newButtonStyles = {
97+
...buttonStyles,
98+
...styles,
99+
};
100+
config.buttonStyles = newButtonStyles;
101+
setVanusConfig(config);
102+
}
103+
104+
function setVanusConfig(config) {
105+
const oldConfig = getVanusConfig();
106+
const newConfig = deepAssign({}, oldConfig, config);
107+
window.vanusConfig = newConfig;
108+
}
109+
110+
function getVanusConfig() {
111+
return window.vanusConfig;
112+
}
113+
114+
function checkVanusConfig() {
115+
let res = true;
116+
const config = getVanusConfig();
117+
if (!config.id || config.id.length === 0 || config.id.toString() !== config.id) {
118+
res = false;
119+
}
120+
if (!config.lang || config.lang.length === 0 || config.lang.toString() !== config.lang || ['en', 'cn'].indexOf(config.lang) === -1) {
121+
res = false;
122+
}
123+
return res;
124+
}
125+
126+
function generateUrl() {
127+
const config = getVanusConfig();
128+
const lang = config.lang;
129+
const id = config.id;
130+
if (lang === 'cn') {
131+
return 'https://ai.vanus.cn/app/embed?id=' + id;
132+
} else {
133+
return 'https://ai.vanus.ai/app/embed?id=' + id;
134+
}
135+
}
136+
137+
function deepAssign() {
138+
let name = null;
139+
let options = null;
140+
let src = null;
141+
let copy = null;
142+
143+
let length = arguments.length;
144+
let i = 1;
145+
let target = arguments[0] || {}
146+
if (typeof target !== 'object') {
147+
target = {};
148+
}
149+
for (; i < length; i++) {
150+
options = arguments[i];
151+
if (options != null) {
152+
for (name in options) {
153+
src = target[name];
154+
copy = options[name];
155+
156+
if (copy && typeof copy == 'object') {
157+
target[name] = deepAssign(src, copy);
158+
} else if (copy !== undefined) {
159+
target[name] = copy;
160+
}
161+
}
162+
}
163+
}
164+
165+
return target
166+
}

0 commit comments

Comments
 (0)