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