Skip to content

Commit f2a5093

Browse files
authored
Merge pull request #96 from BuildFire/remove-static-key
fix(static-key): remove static google api key
2 parents dacabd5 + 311e040 commit f2a5093

8 files changed

Lines changed: 394 additions & 343 deletions

File tree

control/content/app.js

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@
88
.when('/', {
99
templateUrl: 'templates/home.html',
1010
controllerAs: 'ContentHome',
11-
controller: 'ContentHomeCtrl'
11+
controller: 'ContentHomeCtrl',
12+
resolve: {
13+
ScriptLoaderService: function (ScriptLoaderService) {
14+
return ScriptLoaderService.loadScript();
15+
}
16+
}
1217
})
1318
.when('/item', {
1419
templateUrl: 'templates/item.html',
@@ -46,7 +51,44 @@
4651
});
4752
}
4853
}])
49-
.run(['$location', '$rootScope', function ($location, $rootScope) {
54+
.service('ScriptLoaderService', ['$q', function ($q) {
55+
this.loadScript = function () {
56+
const deferred = $q.defer();
57+
58+
const { apiKeys } = buildfire.getContext();
59+
const { googleMapKey } = apiKeys;
60+
const url = `https://maps.googleapis.com/maps/api/js?v=v=3.exp&libraries=places&key=${googleMapKey}`;
61+
62+
63+
const script = document.createElement('script');
64+
script.type = 'text/javascript';
65+
script.src = url;
66+
67+
if (document.querySelector(`script[src="${url}"]`)) {
68+
console.info(`Script: ${url} is already loaded`);
69+
deferred.resolve();
70+
return deferred.promise;
71+
}
72+
73+
script.onload = function () {
74+
console.info(`Successfully loaded script: ${url}`);
75+
deferred.resolve();
76+
};
77+
78+
script.onerror = function () {
79+
console.error(`Failed to load script: ${url}`);
80+
deferred.reject('Failed to load script.');
81+
};
82+
83+
window.gm_authFailure = () => {
84+
deferred.resolve();
85+
};
86+
87+
document.head.appendChild(script);
88+
return deferred.promise;
89+
};
90+
}])
91+
.run(['$location', '$rootScope','ScriptLoaderService', function ($location, $rootScope,ScriptLoaderService) {
5092
buildfire.messaging.onReceivedMessage = function (msg) {
5193
switch (msg.type) {
5294
case 'OpenItem':
@@ -60,4 +102,4 @@
60102
}
61103
};
62104
}]);
63-
})(window.angular);
105+
})(window.angular);

control/content/app.services.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,9 @@
185185
, valid = (inRange(-90, latitude, 90) && inRange(-180, longitude, 180));
186186

187187
if (valid) {
188-
$http.get("https://maps.googleapis.com/maps/api/geocode/json?latlng=" + latitude + "," + longitude + "&key=" + GOOGLE_KEYS.API_KEY)
188+
const { apiKeys } = buildfire.getContext();
189+
const { googleMapKey } = apiKeys;
190+
$http.get("https://maps.googleapis.com/maps/api/geocode/json?latlng=" + latitude + "," + longitude + "&key=" + googleMapKey)
189191
.then(function (response) {
190192
// this callback will be called asynchronously
191193
// when the response is available
@@ -209,7 +211,9 @@
209211
var deferred = $q.defer();
210212

211213
if (address) {
212-
$http.get("https://maps.googleapis.com/maps/api/geocode/json?address=" + address + "&key=" + GOOGLE_KEYS.API_KEY)
214+
const { apiKeys } = buildfire.getContext();
215+
const { googleMapKey } = apiKeys;
216+
$http.get("https://maps.googleapis.com/maps/api/geocode/json?address=" + address + "&key=" + googleMapKey)
213217
.then(function (response) {
214218
// this callback will be called asynchronously
215219
// when the response is available

control/content/controllers/content.home.controller.js

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
ContentHome.filter = null;
1717
ContentHome.isBusy = true;
1818
var _data = defaultInfo;
19-
19+
2020
// Show the top plugin info part when on home view
2121
Buildfire.appearance.setHeaderVisibility(true);
2222

@@ -472,12 +472,12 @@
472472
}
473473
});
474474
}
475-
475+
476476
Deeplink.deleteById(ContentHome.items[index].id);
477477
Buildfire.datastore.delete(ContentHome.items[index].id, TAG_NAMES.COUPON_ITEMS, function (err, result) {
478478
if (err)
479479
return;
480-
480+
481481
PluginEvents.unregister(ContentHome.items[index].id);
482482
//ContentHome.items.splice(_index, 1);
483483
ContentHome.items.splice(index, 1);
@@ -524,11 +524,11 @@
524524
} else {
525525
ContentHome.data.content.selectedStatus = 'All Statuses';
526526
ContentHome.data.content.selectedFilter = {title: undefined, id: "All Categories"};
527-
527+
528528
ContentHome.searchOptionsForItems.filter = { "$json.title": { "$regex": '/*' } };
529529
ContentHome.items = [];
530530
ContentHome.searchOptionsForItems.skip = 0;
531-
531+
532532
ContentHome.loadMoreItems('items');
533533
}
534534
}
@@ -825,7 +825,7 @@
825825
}
826826
ContentHome.busy = true;
827827
ContentHome.isBusy = true;
828-
828+
829829
if (str !== 'filter')
830830
Buildfire.datastore.search(ContentHome.searchOptionsForItems, TAG_NAMES.COUPON_ITEMS, function (err, result) {
831831
if (err) {
@@ -843,7 +843,7 @@
843843
var tmpArray = [];
844844
var lastIndex = result.length;
845845
result.forEach(function (res, index) {
846-
846+
847847
tmpArray.push({
848848
'title': res.data.title,
849849
rank: index + 1,
@@ -970,7 +970,7 @@
970970
}
971971
$csv.import(headerRow).then(function (rows) {
972972
rows = rows.filter(function (row) { return row.title; });
973-
973+
974974
if (rows && rows.length > 1) {
975975
var categoriesList = [], columns = rows.shift();
976976

@@ -979,23 +979,23 @@
979979
ContentHome.csvDataInvalid = true;
980980
return;
981981
}
982-
982+
983983
rows.map(el => categoriesList = categoriesList.concat(el.Categories.split(",")));
984984
categoriesList = [... new Set(categoriesList)];
985-
985+
986986
var sortedCategories = [];
987987
categoriesList.map(category => {
988-
var exists = rows.filter(row =>
988+
var exists = rows.filter(row =>
989989
row.SelectedCategories.toLowerCase().includes(category.toLowerCase()));
990990

991-
exists ? sortedCategories.push({ title: category, number: exists.length})
991+
exists ? sortedCategories.push({ title: category, number: exists.length})
992992
: null;
993993
});
994-
994+
995995
buildfire.messaging.sendMessageToWidget({ type: "ImportCSV", importing: true });
996996
buildfire.messaging.sendMessageToWidget({ importCSV: 'started' });
997997
ContentHome.importingCSV = true;
998-
998+
999999
const insertCategories = (callback) => {
10001000
buildfire.datastore.search({ recordCount: true }, TAG_NAMES.COUPON_CATEGORIES, (err, categories) => {
10011001
if (err) return console.error(err);
@@ -1013,7 +1013,7 @@
10131013
let toUpdate = ContentHome.filters.find(el => el.id === categoryExists.id);
10141014
if(toUpdate) {
10151015
let toUpdateIndex = ContentHome.filters.indexOf(toUpdate);
1016-
ContentHome.filters[toUpdateIndex].data.noOfItems = categoryExists.data.noOfItems;
1016+
ContentHome.filters[toUpdateIndex].data.noOfItems = categoryExists.data.noOfItems;
10171017
}
10181018

10191019
updatedAll--;
@@ -1038,7 +1038,7 @@
10381038
const saveRow = (row) => {
10391039
buildfire.datastore.insert(row, TAG_NAMES.COUPON_ITEMS, (err, result) => {
10401040
if (err) console.error("Failed saving row data", row);
1041-
if (result && result.id) {
1041+
if (result && result.id) {
10421042
PluginEvents.register({ key: result.id, title: result.data.title }, true);
10431043
if (!row.deepLinkId) {
10441044
new Deeplink({
@@ -1108,7 +1108,7 @@
11081108
});
11091109
}
11101110

1111-
1111+
11121112
var rank = ContentHome.data.content.rankOfLastItem || 0;
11131113
RankOfLastItem.setRank(rank);
11141114

0 commit comments

Comments
 (0)