Skip to content

Commit 08452ec

Browse files
author
Adi Shavit
committed
[AI] Perf - map loading.
1 parent 437130a commit 08452ec

2 files changed

Lines changed: 276 additions & 235 deletions

File tree

_layouts/default.html

Lines changed: 83 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -45,35 +45,93 @@
4545
}
4646
</script>
4747
<script src="{{ "/js/default.js" | prepend: site.baseurl }}"></script>
48-
{% if page.permalink == '/' or page.permalink == '/hackathon/' %}
48+
49+
<!-- Maps API loading script with callback -->
4950
<script>
51+
// Define global callback function that will be called when Maps API is loaded
52+
window.initGoogleMaps = function() {
53+
// This flag indicates Maps API is fully loaded and ready
54+
window.googleMapsLoaded = true;
55+
56+
// Trigger an event to notify other scripts
57+
if (document.createEvent) {
58+
var event = document.createEvent('Event');
59+
event.initEvent('google-maps-loaded', true, true);
60+
window.dispatchEvent(event);
61+
}
62+
};
63+
64+
// Pre-define variables that will be needed globally
65+
{% if page.permalink == '/' %}
66+
if ($(window).width() > 767) {
67+
window.googleMaps = 'index';
68+
} else {
69+
window.staticGoogleMaps = true;
70+
window.eventPlaceCoordinates = '{{ site.eventPlaceCoordinates | replace:' ','' }}';
71+
window.centerMapCoordinates = '{{ site.mapCenterCoordinates | replace:' ','' }}';
72+
window.mobileCenterMapCoordinates = '{{ site.mapMobileCenterCoordinates | replace:' ','' }}';
73+
window.icon = '{{ site.baseurl | prepend: site.url }}/img/other/map-marker.png';
74+
}
75+
window.twitterFeedUrl = '{{ site.twitterFeed }}';
76+
{% elsif page.permalink == '/logistics/' %}
77+
window.googleMaps = 'logistics';
78+
window.autoDirectionEnabled = {% if site.logisticsMapAutoDirections %} true {% else %} false {% endif %};
79+
{% elsif page.permalink == '/hackathon/' %}
80+
if ($(window).width() > 767) {
81+
window.googleMaps = 'hackathon';
82+
} else {
83+
window.staticGoogleMaps = true;
84+
window.eventPlaceCoordinates = '{{ site.hackathonPlaceCoordinates | replace:' ','' }}';
85+
window.centerMapCoordinates = '{{ site.hackathonMapCenterCoordinates | replace:' ','' }}';
86+
window.mobileCenterMapCoordinates = '{{ site.hackathonMapMobileCenterCoordinates | replace:' ','' }}';
87+
window.icon = '{{ site.baseurl | prepend: site.url }}/img/other/map-marker.png';
88+
}
89+
{% endif %}
90+
91+
// Conditionally load appropriate Maps API based on page
92+
{% if page.permalink == '/' or page.permalink == '/hackathon/' %}
5093
if ($(window).width() > 767) {
51-
document.write('<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&key=AIzaSyAQr5Br-dyp10Z_dV0Q0aOIBKSbnRjjz3k"><\/script>')
52-
}
94+
var script = document.createElement('script');
95+
script.src = "https://maps.googleapis.com/maps/api/js?v=3.exp&key=AIzaSyAQr5Br-dyp10Z_dV0Q0aOIBKSbnRjjz3k&callback=initGoogleMaps";
96+
script.async = true;
97+
document.head.appendChild(script);
98+
}
99+
{% elsif page.permalink == '/logistics/' %}
100+
var script = document.createElement('script');
101+
script.src = "https://maps.googleapis.com/maps/api/js?v=3.exp&libraries=places,geometry&callback=initGoogleMaps";
102+
script.async = true;
103+
document.head.appendChild(script);
104+
{% endif %}
53105
</script>
54-
{% elsif page.permalink == '/logistics/' %}
55-
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&libraries=places,geometry"></script>
56-
<script type="text/javascript">
57-
var autoDirectionEnabled = {% if site.logisticsMapAutoDirections %} true {% else %} false {% endif %};
58-
</script>
59-
{% endif %}
106+
60107
<script>
61108
Waves.displayEffect();
109+
110+
// Setup map coordinates once API is loaded
111+
document.addEventListener('google-maps-loaded', function() {
112+
{% if page.permalink == '/' %}
113+
if ($(window).width() > 767) {
114+
window.eventPlace = new google.maps.LatLng({{ site.eventPlaceCoordinates | replace:' ','' }});
115+
window.centerMap = new google.maps.LatLng({{ site.mapCenterCoordinates | replace:' ','' }});
116+
window.mobileCenterMap = new google.maps.LatLng({{ site.mapMobileCenterCoordinates | replace:' ','' }});
117+
window.icon = '{{ site.baseurl }}/img/other/map-marker.svg';
118+
}
119+
{% elsif page.permalink == '/logistics/' %}
120+
window.eventPlace = new google.maps.LatLng({{ site.eventPlaceCoordinates }});
121+
window.centerMap = new google.maps.LatLng({{ site.logisticsMapCenterCoordinates }});
122+
window.mobileCenterMap = new google.maps.LatLng({{ site.logisticsMapMobileCenterCoordinates }});
123+
window.icon = '{{ site.baseurl }}/img/other/map-marker.svg';
124+
{% elsif page.permalink == '/hackathon/' %}
125+
if ($(window).width() > 767) {
126+
window.eventPlace = new google.maps.LatLng({{ site.hackathonPlaceCoordinates }});
127+
window.centerMap = new google.maps.LatLng({{ site.hackathonMapCenterCoordinates }});
128+
window.mobileCenterMap = new google.maps.LatLng({{ site.hackathonMapMobileCenterCoordinates }});
129+
window.icon = '{{ site.baseurl }}/img/other/map-marker.svg';
130+
}
131+
{% endif %}
132+
});
133+
62134
{% if page.permalink == '/' %}
63-
if ($(window).width() > 767) {
64-
var googleMaps = 'index',
65-
eventPlace = new google.maps.LatLng({{ site.eventPlaceCoordinates | replace:' ','' }}),
66-
centerMap = new google.maps.LatLng({{ site.mapCenterCoordinates | replace:' ','' }}),
67-
mobileCenterMap = new google.maps.LatLng({{ site.mapMobileCenterCoordinates | replace:' ','' }}),
68-
icon = '{{ site.baseurl }}/img/other/map-marker.svg';
69-
} else {
70-
var staticGoogleMaps = true,
71-
eventPlaceCoordinates = '{{ site.eventPlaceCoordinates | replace:' ','' }}',
72-
centerMapCoordinates = '{{ site.mapCenterCoordinates | replace:' ','' }}',
73-
mobileCenterMapCoordinates = '{{ site.mapMobileCenterCoordinates | replace:' ','' }}',
74-
icon = '{{ site.baseurl | prepend: site.url }}/img/other/map-marker.png';
75-
}
76-
var twitterFeedUrl = '{{ site.twitterFeed }}';
77135
$(document).ready(function () {
78136
$(function () {
79137
if ($(window).width() > 767) {
@@ -107,30 +165,9 @@
107165
}
108166
}
109167
});
110-
{% elsif page.permalink == '/logistics/' %}
111-
var googleMaps = 'logistics',
112-
eventPlace = new google.maps.LatLng({{ site.eventPlaceCoordinates }}),
113-
centerMap = new google.maps.LatLng({{ site.logisticsMapCenterCoordinates }}),
114-
mobileCenterMap = new google.maps.LatLng({{ site.logisticsMapMobileCenterCoordinates }}),
115-
icon = '{{ site.baseurl }}/img/other/map-marker.svg';
116-
{% elsif page.permalink == '/hackathon/' %}
117-
if ($(window).width() > 767) {
118-
var googleMaps = 'hackathon',
119-
eventPlace = new google.maps.LatLng({{ site.hackathonPlaceCoordinates }}),
120-
centerMap = new google.maps.LatLng({{ site.hackathonMapCenterCoordinates }}),
121-
mobileCenterMap = new google.maps.LatLng({{ site.hackathonMapMobileCenterCoordinates }}),
122-
icon = '{{ site.baseurl }}/img/other/map-marker.svg';
123-
} else {
124-
var staticGoogleMaps = true,
125-
eventPlaceCoordinates = '{{ site.hackathonPlaceCoordinates | replace:' ','' }}',
126-
centerMapCoordinates = '{{ site.hackathonMapCenterCoordinates | replace:' ','' }}',
127-
mobileCenterMapCoordinates = '{{ site.hackathonMapMobileCenterCoordinates | replace:' ','' }}',
128-
icon = '{{ site.baseurl | prepend: site.url }}/img/other/map-marker.png';
129-
}
130-
131-
{% endif %}
168+
{% endif %}
132169
</script>
133-
<script src="{{ "/js/scripts.min.js" | prepend: site.baseurl }}"></script>
170+
<script src="{{ "/js/scripts.js" | prepend: site.baseurl }}"></script>
134171
{% if page.permalink == '/schedule/' %}
135172
<script type="text/javascript" defer>
136173
$(document).ready(function () {

0 commit comments

Comments
 (0)