From df59b0b7d2146d5d8b2a88e19e79f1a24f07bfce Mon Sep 17 00:00:00 2001 From: Antony Lesuisse Date: Fri, 25 May 2012 21:11:27 +0200 Subject: [FIX] google map on event part1 bzr revid: al@openerp.com-20120525191127-v5o88etlbdq0jbzn --- addons/google_map/static/src/js/google_map.js | 72 +++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 addons/google_map/static/src/js/google_map.js (limited to 'addons/google_map/static/src') diff --git a/addons/google_map/static/src/js/google_map.js b/addons/google_map/static/src/js/google_map.js new file mode 100644 index 00000000000..cec51510fe2 --- /dev/null +++ b/addons/google_map/static/src/js/google_map.js @@ -0,0 +1,72 @@ +openerp.event = function(instance, mod) { + instance.web.form.widgets.add('many2one_address_google_map', 'instance.event.Many2OneAddress'); + + instance.google_map.GoogleMapConnector = instance.web.Class.extend({ + init: function(){ + this.googleMapsLoaded = $.Deferred(); + this.map_load(); + }, + map_load: function() { + var self = this; + if(this.googleMapsLoaded.state() != "pending"){return this.googleMapsLoaded.promise();} + googleMapsCallback = function () { + self.googleMapsLoaded.resolve(); + }; + $.ajax({ + url: "https://maps.googleapis.com/maps/api/js?v=3&callback=googleMapsCallback&sensor=false", + dataType: "script" + }).fail(self.googleMapsLoaded.reject); + return this.googleMapsLoaded.promise(); + }, + render_map: function(address,$element){ + var geocoder = new google.maps.Geocoder(); + geocoder.geocode( { 'address': address}, function(results, status){ + if (status == google.maps.GeocoderStatus.OK){ + var lat = results[0].geometry.location.lat(),lng =results[0].geometry.location.lng(); + var myOptions = { + zoom: 17, + center: new google.maps.LatLng(lat,lng), + mapTypeId: google.maps.MapTypeId.ROADMAP + } + return new google.maps.Marker({ + map : new google.maps.Map($element,myOptions), + position: new google.maps.LatLng(lat,lng) + }); + } + }); + }, + }); + instance.google_map.Many2OneAddress = instance.web.form.FieldMany2One.extend({ + init: function(field_manager, node){ + this._super(field_manager, node); + this.map = new instance.google_map.GoogleMapConnector(); + }, + get_address:function(value){ + var self = this; + if (!value || value.length == 0){ + return $.Deferred().reject(); + } + (value instanceof Array)?value = parseInt(value[0]):false; + var data = new instance.web.DataSet(this,this.field.relation, this.build_context()); + data.read_ids(value,["street","city","zip","country_id"]).done(function(value){ + var address; + if value['country_id'] { + address = _.str.sprintf('%(street)s, %(zip)s %(city)s, %(country_id[1])s', value); + } else { + address = _.str.sprintf('%(street)s, %(zip)s %(city)s', value); + } + self.map.googleMapsLoaded.done(function(){ + self.map.render_map(address,self.$(self.options.selector)[0]); + }) + }); + }, + set_value:function(value){ + this._super(value); + this.get_address(value); + }, + render_value:function(no_recurse){ + this.get_address(this.get("value")); + this._super(no_recurse); + } + }); +}; -- cgit v1.2.3 From 879b4a37c65975c5c615a1d52f2f39dc8923c66a Mon Sep 17 00:00:00 2001 From: Antony Lesuisse Date: Fri, 25 May 2012 22:03:26 +0200 Subject: [FIX] event and google map ok bzr revid: al@openerp.com-20120525200326-0cfysbpcnlk2agyn --- addons/google_map/static/src/js/google_map.js | 69 ++++++++++++++------------- 1 file changed, 36 insertions(+), 33 deletions(-) (limited to 'addons/google_map/static/src') diff --git a/addons/google_map/static/src/js/google_map.js b/addons/google_map/static/src/js/google_map.js index cec51510fe2..9aa693e53c6 100644 --- a/addons/google_map/static/src/js/google_map.js +++ b/addons/google_map/static/src/js/google_map.js @@ -1,45 +1,48 @@ openerp.event = function(instance, mod) { - instance.web.form.widgets.add('many2one_address_google_map', 'instance.event.Many2OneAddress'); + instance.web.form.widgets.add('many2one_address_google_map', 'instance.google_map.Many2OneAddress'); + + var googleMapsLoaded = null; instance.google_map.GoogleMapConnector = instance.web.Class.extend({ - init: function(){ - this.googleMapsLoaded = $.Deferred(); - this.map_load(); - }, map_load: function() { var self = this; - if(this.googleMapsLoaded.state() != "pending"){return this.googleMapsLoaded.promise();} - googleMapsCallback = function () { - self.googleMapsLoaded.resolve(); - }; - $.ajax({ - url: "https://maps.googleapis.com/maps/api/js?v=3&callback=googleMapsCallback&sensor=false", - dataType: "script" - }).fail(self.googleMapsLoaded.reject); - return this.googleMapsLoaded.promise(); + if(googleMapsLoaded === null) { + googleMapsLoaded = $.Deferred(); + // global + openerp_googleMapsCallback = googleMapsLoaded.resolve; + $.ajax({ + url: "https://maps.googleapis.com/maps/api/js?v=3&callback=openerp_googleMapsCallback&sensor=false", + dataType: "script" + }).fail(googleMapsLoaded.reject); + } + return googleMapsLoaded; }, - render_map: function(address,$element){ - var geocoder = new google.maps.Geocoder(); - geocoder.geocode( { 'address': address}, function(results, status){ - if (status == google.maps.GeocoderStatus.OK){ - var lat = results[0].geometry.location.lat(),lng =results[0].geometry.location.lng(); - var myOptions = { - zoom: 17, - center: new google.maps.LatLng(lat,lng), - mapTypeId: google.maps.MapTypeId.ROADMAP - } - return new google.maps.Marker({ - map : new google.maps.Map($element,myOptions), - position: new google.maps.LatLng(lat,lng) + render_map: function(address,element) { + this.map_load().then(function() { + var geocoder = new google.maps.Geocoder(); + geocoder.geocode({'address': address}, function(results, status) { + if (status == google.maps.GeocoderStatus.OK) { + var lat = results[0].geometry.location.lat(); + var lng = results[0].geometry.location.lng(); + var myOptions = { + zoom: 17, + center: new google.maps.LatLng(lat,lng), + mapTypeId: google.maps.MapTypeId.ROADMAP + }; + var map = new google.maps.Map(element,myOptions); + var position = new google.maps.LatLng(lat,lng); + var marker = new google.maps.Marker({ map: map, position: position }); + return marker; + } }); - } }); }, }); + instance.google_map.Many2OneAddress = instance.web.form.FieldMany2One.extend({ init: function(field_manager, node){ this._super(field_manager, node); - this.map = new instance.google_map.GoogleMapConnector(); + this.map = new instance.google_map.GoogleMapConnector(); }, get_address:function(value){ var self = this; @@ -50,14 +53,14 @@ openerp.event = function(instance, mod) { var data = new instance.web.DataSet(this,this.field.relation, this.build_context()); data.read_ids(value,["street","city","zip","country_id"]).done(function(value){ var address; - if value['country_id'] { + if (value['country_id']) { address = _.str.sprintf('%(street)s, %(zip)s %(city)s, %(country_id[1])s', value); } else { address = _.str.sprintf('%(street)s, %(zip)s %(city)s', value); } - self.map.googleMapsLoaded.done(function(){ - self.map.render_map(address,self.$(self.options.selector)[0]); - }) + // TODO repalce by widget_option selector self.options.selector + var el = self.view.$element.find(".oe_form_google_map")[0]; + self.map.render_map(address,el); }); }, set_value:function(value){ -- cgit v1.2.3