11( function ( angular , buildfire ) {
22 'use strict' ;
3+ window . authFailureFired = false ;
4+
35 //created mediaCenterWidget module
46 angular
57 . module ( 'placesWidget' , [
5860 $httpProvider . interceptors . push ( interceptor ) ;
5961
6062 } ] )
61- . run ( [ 'Location' , 'Messaging' , 'EVENTS' , 'PATHS' , '$location' , '$rootScope' , 'ViewStack' , function ( Location , Messaging , EVENTS , PATHS , $location , $rootScope , ViewStack ) {
63+ . service ( 'ScriptLoaderService' , [ '$q' , function ( $q ) {
64+ this . loadScript = function ( ) {
65+ const { apiKeys} = buildfire . getContext ( ) ;
66+ const { googleMapKey} = apiKeys ;
67+
68+ const url = `https://maps.googleapis.com/maps/api/js?libraries=places&sensor=true&key=${ googleMapKey } ` ;
69+
70+ const deferred = $q . defer ( ) ;
71+
72+ // Check if the script is already in the document
73+ const existingScript = document . getElementById ( 'googleMapsScript' ) ;
74+ if ( existingScript ) {
75+ return deferred . resolve ( ) ;
76+ }
77+
78+ const script = document . createElement ( 'script' ) ;
79+ script . type = 'text/javascript' ;
80+ script . src = url ;
81+ script . id = 'googleMapsScript' ;
82+
83+ script . onload = function ( ) {
84+ console . info ( `Successfully loaded script: ${ url } ` ) ;
85+ deferred . resolve ( ) ;
86+ } ;
87+
88+ script . onerror = function ( ) {
89+ console . error ( `Failed to load script: ${ url } ` ) ;
90+ deferred . reject ( 'Failed to load script.' ) ;
91+ } ;
92+ window . gm_authFailure = ( ) => {
93+ if ( window . authFailureFired ) return ;
94+ buildfire . dialog . alert ( {
95+ title : 'Error' ,
96+ message : 'Failed to load Google Maps API.' ,
97+ } ) ;
98+ window . authFailureFired = true ;
99+ deferred . reject ( 'Failed to load script.' ) ;
100+ } ;
101+
102+ document . head . appendChild ( script ) ;
103+ return deferred . promise ;
104+ } ;
105+ } ] )
106+ . run ( [ 'Location' , 'Messaging' , 'EVENTS' , 'PATHS' , '$location' , '$rootScope' , 'ViewStack' , 'ScriptLoaderService' , '$q' , function ( Location , Messaging , EVENTS , PATHS , $location , $rootScope , ViewStack , ScriptLoaderService , $q ) {
107+
108+ // Create a global promise for Google Maps loading
109+ angular . module ( 'placesWidget' ) . googleMapsReady = $q . defer ( ) ;
110+
111+ ScriptLoaderService . loadScript ( )
112+ . then ( function ( ) {
113+ // Resolve the global promise when the script is loaded
114+ angular . module ( 'placesWidget' ) . googleMapsReady . resolve ( ) ;
115+ } )
116+ . catch ( function ( err ) {
117+ console . error ( 'Google Maps failed to load:' , err ) ;
118+ angular . module ( 'placesWidget' ) . googleMapsReady . reject ( err ) ;
119+ } ) ;
120+
62121
63122 buildfire . deeplink . getData ( function ( data ) {
64123 if ( data ) {
89148 }
90149 } ) ;
91150 } ] ) ;
92- } ) ( window . angular , window . buildfire ) ;
151+ } ) ( window . angular , window . buildfire ) ;
0 commit comments