From e25043765a64130a721025490f42247b5759d75b Mon Sep 17 00:00:00 2001 From: rolux Date: Wed, 22 Dec 2010 17:58:39 +0000 Subject: [PATCH] lazyload google maps --- build/js/ox.ui.js | 151 +++++++++++++++++++++++++--------------------- 1 file changed, 82 insertions(+), 69 deletions(-) diff --git a/build/js/ox.ui.js b/build/js/ox.ui.js index b6498b6..f5f4f52 100644 --- a/build/js/ox.ui.js +++ b/build/js/ox.ui.js @@ -218,40 +218,37 @@ requires var config = data; Ox.print('config', config); document.title = config.site.name; - window.google = function() {}; - $.getScript('http://maps.google.com/maps/api/js?callback=google&sensor=false', function() { - loadImages(function() { - that.api.api(function(result) { - $.each(result.data.actions, function(i, action) { - that.api[action] = function(data, callback) { - if (arguments.length == 1) { - callback = data; - data = {}; - } - return Ox.Request.send({ - url: self.options.apiURL, - data: { - action: action, - data: JSON.stringify(data) - }, - callback: callback - }); - }; - }); - that.api[self.options.init](getUserData(), function(data) { - var user = data.data.user; - $(function() { - var $div = $body.find('div'); - $body.find('img').remove(); - $div.animate({ - opacity: 0 - }, 1000, function() { - $div.remove(); - }); - callback({ - config: config, - user: user - }); + loadImages(function() { + that.api.api(function(result) { + $.each(result.data.actions, function(i, action) { + that.api[action] = function(data, callback) { + if (arguments.length == 1) { + callback = data; + data = {}; + } + return Ox.Request.send({ + url: self.options.apiURL, + data: { + action: action, + data: JSON.stringify(data) + }, + callback: callback + }); + }; + }); + that.api[self.options.init](getUserData(), function(data) { + var user = data.data.user; + $(function() { + var $div = $body.find('div'); + $body.find('img').remove(); + $div.animate({ + opacity: 0 + }, 1000, function() { + $div.remove(); + }); + callback({ + config: config, + user: user }); }); }); @@ -7270,40 +7267,56 @@ requires key_escape: deselectPlace }); - $.extend(self, { - geocoder: new google.maps.Geocoder(), - selected: -1 - }); - if (Ox.isObject(self.options.places[0])) { - $.each(self.options.places, function(i, place) { - place.bounds = getBounds(), - place.center = place.bounds.getCenter(); - $.extend(place, { - marker: new Marker(place), - polygon: new Polygon(place) - }); - self.bounds = i == 0 ? getBounds() : self.bounds.union(place.bounds); - self.options.places[i] = place; - function getBounds() { - return new google.maps.LatLngBounds( - new google.maps.LatLng(place.points.southwest[0], place.points.southwest[1]), - new google.maps.LatLng(place.points.northeast[0], place.points.northeast[1]) - ); - } - }); - loadMap(); - } else { - self.counter = 0; - $.each(self.options.places, function(i, place) { - getLocationByName(place, function(place) { - Ox.print(self.counter, i, place); - self.options.places[i] = place; - if (self.counter++ == self.options.places.length - 1) { - loadMap(); - } - }); - }); - } + if (Ox.isUndefined(window.google)) { + window.google = function() {}; + $.getScript('http://maps.google.com/maps/api/js?callback=google&sensor=false', function() { + var interval = setInterval(function() { + if (!Ox.isUndefined(google.maps)) { + clearInterval(interval); + initMap(); + } + }, 100); + }); + } else { + initMap(); + } + + function initMap() { + $.extend(self, { + geocoder: new google.maps.Geocoder(), + selected: -1 + }); + if (Ox.isObject(self.options.places[0])) { + $.each(self.options.places, function(i, place) { + place.bounds = getBounds(), + place.center = place.bounds.getCenter(); + $.extend(place, { + marker: new Marker(place), + polygon: new Polygon(place) + }); + self.bounds = i == 0 ? getBounds() : self.bounds.union(place.bounds); + self.options.places[i] = place; + function getBounds() { + return new google.maps.LatLngBounds( + new google.maps.LatLng(place.points.southwest[0], place.points.southwest[1]), + new google.maps.LatLng(place.points.northeast[0], place.points.northeast[1]) + ); + } + }); + loadMap(); + } else { + self.counter = 0; + $.each(self.options.places, function(i, place) { + getLocationByName(place, function(place) { + Ox.print(self.counter, i, place); + self.options.places[i] = place; + if (self.counter++ == self.options.places.length - 1) { + loadMap(); + } + }); + }); + } + } function loadMap() { self.center = self.bounds ? self.bounds.getCenter() : new google.maps.LatLng(0, 0); @@ -7328,9 +7341,9 @@ requires }); resize(); that.gainFocus(); + that.triggerEvent('loaded'); } - function canContain(outerBounds, innerBounds) { var outerSpan = outerBounds.toSpan(), innerSpan = innerBounds.toSpan();