@@ -67,6 +67,7 @@ const ConfiguredVueRouter = config => {
6767 }
6868 )
6969 }
70+ console . debug ( 'setting routes:' , routes )
7071 return new VueRouter ( {
7172 mode : config . router_mode ,
7273 routes : routes
@@ -101,6 +102,18 @@ module.exports = function (public_key, config) {
101102 config : config ,
102103 } )
103104
105+ if ( ! config . console ) {
106+ config . console = console
107+ if ( ! window . localStorage . tcs_enable_debug ) {
108+ if ( ! window . tcs_debug_log ) {
109+ window . tcs_debug_log = [ ]
110+ }
111+ console . debug = function ( ) {
112+ window . tcs_debug_log . push ( Array . from ( arguments ) . join ( ) )
113+ }
114+ }
115+ }
116+
104117 let error = null
105118 if ( ! config . mode ) {
106119 config . mode = 'grid'
@@ -132,10 +145,6 @@ module.exports = function (public_key, config) {
132145 config . router_mode = 'hash'
133146 }
134147
135- if ( ! config . console ) {
136- config . console = console
137- }
138-
139148 if ( ! config . element ) {
140149 config . element = '#socket'
141150 }
@@ -163,7 +172,9 @@ module.exports = function (public_key, config) {
163172
164173 const ga_prefixes = init_ga ( router , config )
165174
166- return new Vue ( {
175+ console . debug ( 'using config:' , config )
176+
177+ const v = new Vue ( {
167178 el : config . element ,
168179 router : router ,
169180 render : h => h ( app ) ,
@@ -178,6 +189,7 @@ module.exports = function (public_key, config) {
178189 enquiry_form_info : { } ,
179190 enquiry_data : { } ,
180191 selected_subject_id : null ,
192+ grecaptcha_container_id : 'grecaptcha_' + Math . random ( ) . toString ( 36 ) . substring ( 2 , 10 ) ,
181193 } ,
182194 components : {
183195 app
@@ -190,7 +202,8 @@ module.exports = function (public_key, config) {
190202 }
191203 } ,
192204 watch : {
193- '$route' ( to ) {
205+ '$route' ( to , from ) {
206+ console . debug ( `route change ${ from . path } to ${ to . path } ` , from , to )
194207 if ( this . config . mode === 'grid' && to . name === 'index' ) {
195208 this . get_contractor_list ( )
196209 }
@@ -208,9 +221,11 @@ module.exports = function (public_key, config) {
208221 } ,
209222 request ( url , callback , expected_status , method , data ) {
210223 const xhr = new window . XMLHttpRequest ( )
211- xhr . open ( method || 'GET' , url )
224+ method = method || 'GET'
225+ xhr . open ( method , url )
212226 xhr . setRequestHeader ( 'Accept' , 'application/json' )
213227 xhr . onload = ( ) => {
228+ console . debug ( `request ${ method } ${ url } > ${ xhr . status } ` , data , xhr )
214229 try {
215230 if ( xhr . status !== ( expected_status || 200 ) ) {
216231 throw Error ( `bad response status ${ xhr . status } not 200` )
@@ -325,12 +340,41 @@ ${xhr.responseText}`)
325340 ga_event ( category , action , label ) {
326341 /* istanbul ignore next */
327342 for ( let prefix of ga_prefixes ) {
343+ console . debug ( 'ga sending event' , prefix , category , action , label )
328344 window . ga ( prefix + 'send' , 'event' , category , action , label )
329345 }
330346 } ,
347+ grecaptcha_callback ( response ) {
348+ Vue . set ( this . enquiry_data , 'grecaptcha_response' , response )
349+ } ,
350+ render_grecaptcha ( ) {
351+ const el = document . getElementById ( this . grecaptcha_container_id )
352+ if ( el && el . childElementCount === 0 ) {
353+ console . debug ( 'rendering grecaptcha' )
354+ window . grecaptcha . render ( this . grecaptcha_container_id , {
355+ sitekey : this . grecaptcha_key ,
356+ callback : this . grecaptcha_callback
357+ } )
358+ } else {
359+ console . debug ( 'not rendering grecaptcha' , el )
360+ }
361+ } ,
362+
331363 goto ( name , params ) {
332364 this . $router . push ( { 'name' : name , params : params } )
333365 }
334366 }
335367 } )
368+ if ( window . socket_view === undefined ) {
369+ window . socket_view = [ v ]
370+ } else {
371+ window . socket_view . push ( v )
372+ }
373+
374+ window . _tcs_grecaptcha_loaded = ( ) => {
375+ for ( let v of window . socket_view ) {
376+ v . render_grecaptcha ( )
377+ }
378+ }
379+ return v
336380}
0 commit comments