Skip to content

Commit 550db12

Browse files
authored
Merge pull request #150 from BuildFire/remove-static-key
fix(static-key): remove static google api key
2 parents 48733fa + 65abdb3 commit 550db12

File tree

10 files changed

+582
-472
lines changed

10 files changed

+582
-472
lines changed

control/content/app.js

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,56 @@
321321

322322
$httpProvider.interceptors.push(interceptor);
323323
}])
324-
.run(['Location', 'Messaging', 'EVENTS', 'PATHS', '$rootScope', 'Buildfire', function (Location, Messaging, EVENTS, PATHS, $rootScope, Buildfire) {
324+
.service('ScriptLoaderService', ['$q', function ($q) {
325+
this.loadScript = function () {
326+
const {apiKeys} = buildfire.getContext();
327+
const {googleMapKey} = apiKeys;
328+
329+
const url = `https://maps.googleapis.com/maps/api/js?libraries=places&sensor=true&key=${googleMapKey}`;
330+
331+
const deferred = $q.defer();
332+
333+
// Check if the script is already in the document
334+
const existingScript = document.getElementById('googleMapsScript');
335+
if (existingScript) {
336+
return deferred.resolve();
337+
}
338+
339+
const script = document.createElement('script');
340+
script.type = 'text/javascript';
341+
script.src = url;
342+
script.id = 'googleMapsScript';
343+
344+
script.onload = function () {
345+
console.info(`Successfully loaded script: ${url}`);
346+
deferred.resolve();
347+
};
348+
349+
script.onerror = function () {
350+
console.error(`Failed to load script: ${url}`);
351+
deferred.reject('Failed to load script.');
352+
};
353+
window.gm_authFailure = () => {
354+
buildfire.dialog.alert({
355+
title: 'Error',
356+
message: 'Failed to load Google Maps API.',
357+
});
358+
deferred.resolve('Failed to load script.');
359+
};
360+
361+
document.head.appendChild(script);
362+
return deferred.promise;
363+
};
364+
}])
365+
.run(['Location', 'Messaging', 'EVENTS', 'PATHS', '$rootScope', 'Buildfire','ScriptLoaderService', function (Location, Messaging, EVENTS, PATHS, $rootScope, Buildfire,ScriptLoaderService) {
366+
ScriptLoaderService.loadScript()
367+
.then(() => {
368+
console.info("Successfully loaded Google's Maps SDK.");
369+
})
370+
.catch((error) => {
371+
console.error("Failed to load Google Maps SDK.",error);
372+
});
373+
325374
// Handler to receive message from widget
326375
Messaging.onReceivedMessage = function (event) {
327376
console.log('Event rcv-----on Control Side------------------------?????????????????????????????????---------------********************* in Control Panal side----', event);
@@ -373,4 +422,4 @@
373422
};
374423
}]);
375424
})
376-
(window.angular, window.buildfire);
425+
(window.angular, window.buildfire);

control/content/enums.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,6 @@
3131
SECTION: "section",
3232
HOME: "HOME"
3333
})
34-
.constant('GOOGLE_KEYS', {
35-
API_KEY: 'AIzaSyCgcHZhcDGGxa60VwD5hLPExCTva0zQ3_A'
36-
})
3734
.constant('DEFAULT_DATA', {
3835
PLACE_INFO: {
3936
data: {

control/content/index.html

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@
3030
<script src="../../../../scripts/buildfire/components/actionItems/sortableList.js"></script>
3131
<script src="../../../../scripts/buildfire/components/images/thumbnail.js"></script>
3232

33-
<!--google map api-->
34-
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?libraries=places&sensor=true&key=AIzaSyCgcHZhcDGGxa60VwD5hLPExCTva0zQ3_A"></script>
3533

3634
<!--App js-->
3735
<script src="app.js"></script>

control/content/services.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@
269269
}
270270
};
271271
}])
272-
.factory("Utils", ['$http', 'GOOGLE_KEYS', '$q', function ($http, GOOGLE_KEYS, $q) {
272+
.factory("Utils", ['$http', '$q', function ($http, GOOGLE_KEYS, $q) {
273273
function inRange(min, number, max) {
274274
return ( !isNaN(number) && (number >= min) && (number <= max) );
275275
}
@@ -282,7 +282,9 @@
282282
, valid = (inRange(-90, latitude, 90) && inRange(-180, longitude, 180));
283283

284284
if (valid) {
285-
$http.get("https://maps.googleapis.com/maps/api/geocode/json?latlng=" + latitude + "," + longitude + "&key=" + GOOGLE_KEYS.API_KEY)
285+
const { apiKeys } = buildfire.getContext();
286+
const { googleMapKey } = apiKeys;
287+
$http.get("https://maps.googleapis.com/maps/api/geocode/json?latlng=" + latitude + "," + longitude + "&key=" + googleMapKey)
286288
.then(function (response) {
287289
// this callback will be called asynchronously
288290
// when the response is available
@@ -306,7 +308,9 @@
306308
var deferred = $q.defer();
307309

308310
if (address) {
309-
$http.get("https://maps.googleapis.com/maps/api/geocode/json?address=" + address + "&key=" + GOOGLE_KEYS.API_KEY)
311+
const { apiKeys } = buildfire.getContext();
312+
const { googleMapKey } = apiKeys;
313+
$http.get("https://maps.googleapis.com/maps/api/geocode/json?address=" + address + "&key=" + googleMapKey)
310314
.then(function (response) {
311315
// this callback will be called asynchronously
312316
// when the response is available

plugin.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
},
2020
"widget": {
2121
},
22+
"usesApiKeys": [
23+
{ "name": "googleMapKey" }
24+
],
2225
"features": [{"name": "Geo"}],
2326
"languages": [
2427
"en"

widget/app.js

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
(function (angular, buildfire) {
22
'use strict';
3+
window.authFailureFired = false;
4+
35
//created mediaCenterWidget module
46
angular
57
.module('placesWidget', [
@@ -58,7 +60,64 @@
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) {
@@ -89,4 +148,4 @@
89148
}
90149
});
91150
}]);
92-
})(window.angular, window.buildfire);
151+
})(window.angular, window.buildfire);

widget/controllers/widget.sections.controller.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@
7070
WidgetSections.noMoreItems = false;
7171

7272
WidgetSections.showDescription = function () {
73-
console.log('Description------------------------', WidgetSections.placesInfo, WidgetSections.placesInfo.data.content.descriptionHTML);
7473
if (WidgetSections.placesInfo && WidgetSections.placesInfo.data && WidgetSections.placesInfo.data.content && WidgetSections.placesInfo.data.content.descriptionHTML != '<p>&nbsp;<br></p>' && WidgetSections.placesInfo.data.content.descriptionHTML != '<p><br data-mce-bogus="1"></p>' && WidgetSections.placesInfo.data.content.descriptionHTML != "")
7574
return true;
7675
else
@@ -512,7 +511,7 @@
512511
if (_items && _items.length) {
513512
var distanceIn = (WidgetSections.placesInfo && WidgetSections.placesInfo.data && WidgetSections.placesInfo.data.settings && WidgetSections.placesInfo.data.settings.showDistanceIn) || 'mi';
514513

515-
var getDistance = function (items) {
514+
var getDistance = function (items) {
516515
var destinations = GeoDistance.getDistance(WidgetSections.locationData.currentCoordinates, items, distanceIn);
517516

518517
if (destinations && destinations.length > 0) {
@@ -596,7 +595,7 @@
596595
WidgetSections.selectedItem = WidgetSections.locationData.items[itemIndex];
597596
initCarousel(WidgetSections.placesInfo.data.settings.defaultView);
598597

599-
var distanceIn = (WidgetSections.placesInfo && WidgetSections.placesInfo.data && WidgetSections.placesInfo.data.settings && WidgetSections.placesInfo.data.settings.showDistanceIn) || 'mi';
598+
var distanceIn = (WidgetSections.placesInfo && WidgetSections.placesInfo.data && WidgetSections.placesInfo.data.settings && WidgetSections.placesInfo.data.settings.showDistanceIn) || 'mi';
600599
var distances =GeoDistance.getDistance(WidgetSections.locationData.currentCoordinates, [WidgetSections.selectedItem], distanceIn);
601600

602601
console.log('Distance---------------------', distances);

widget/controllers/widget.sectionsItems.controller.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,6 @@
133133
WidgetSections.noMoreItems = false;
134134

135135
WidgetSections.showDescription = function () {
136-
console.log('Description------------------------', WidgetSections.placesInfo, WidgetSections.placesInfo.data.content.descriptionHTML);
137136
if (WidgetSections.placesInfo && WidgetSections.placesInfo.data && WidgetSections.placesInfo.data.content && WidgetSections.placesInfo.data.content.descriptionHTML != '<p>&nbsp;<br></p>' && WidgetSections.placesInfo.data.content.descriptionHTML != '<p><br data-mce-bogus="1"></p>' && WidgetSections.placesInfo.data.content.descriptionHTML != "")
138137
return true;
139138
else
@@ -551,14 +550,14 @@
551550
return;
552551
}
553552
if (_items && _items.length) {
554-
var getDistance = function (items) {
553+
var getDistance = function (items) {
555554
var distances = GeoDistance.getDistance(WidgetSections.locationData.currentCoordinates, items, WidgetSections.placesInfo.data.settings.showDistanceIn)
556555
if (distances && distances.length > 0) {
557556

558557
console.log('distance result', distances);
559558
for (var _ind = 0; _ind < WidgetSections.locationData.items.length; _ind++) {
560559
if (items && items[_ind]) {
561-
560+
562561
items[_ind].data.distanceText = distances[_ind].distance?distances[_ind].distance.toFixed(2).replace(/\d(?=(\d{3})+\.)/g, '$&,')
563562
+" "+WidgetSections.placesInfo.data.settings.showDistanceIn:"NA";
564563
items[_ind].data.distance = distances[_ind].distance?distances[_ind].distance.toFixed(2).replace(/\d(?=(\d{3})+\.)/g, '$&,')
@@ -637,7 +636,7 @@
637636
var distances= GeoDistance.getDistance(WidgetSections.locationData.currentCoordinates, [WidgetSections.selectedItem], distanceIn);
638637
console.log('Distance---------------------', distances);
639638
WidgetSections.selectedItemDistance = distances.distance?distances.distance:"NA";
640-
639+
641640
initMapCarousel();
642641
};
643642

@@ -820,4 +819,4 @@
820819
}
821820
])
822821
;
823-
})(window.angular, window);
822+
})(window.angular, window);

0 commit comments

Comments
 (0)