Skip to content

Commit

Permalink
without address, with readonly mode
Browse files Browse the repository at this point in the history
  • Loading branch information
irina235 authored and katyukha committed Nov 10, 2023
1 parent 830b014 commit 3b9c94b
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 60 deletions.
9 changes: 4 additions & 5 deletions crnd_web_widget_select_geolocation/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ CRND Web Widget Select Geolocation

|badge2| |badge5|

This addon adds a widget that allows you to select a geolocation on a map. Wdget has two reauired options:
'latitude_field' and 'longitude_field' and additional options 'address_field' and 'zoom'.
Fields defined in 'latitude_field', 'longitude_field', 'address_field' should be present in view definition
This addon adds a widget that allows you to select or view a geolocation on a map. Wdget has two reauired options:
'latitude_field' and 'longitude_field' and additional options 'readonly' and 'zoom'.
Fields defined in 'latitude_field', 'longitude_field', should be present in view definition

Usage
'''''
Expand All @@ -35,8 +35,7 @@ Example:
<field name="name"/>
<field name="latitude"/>
<field name="longitude"/>
<field name="address"/>
<field name="point" widget="select_geolocation" options="{'latitude_field': 'latitude', 'longitude_field': 'longitude', 'address_field': 'address', 'zoom': 12}"/>
<field name="point" widget="select_geolocation" options="{'latitude_field': 'latitude', 'longitude_field': 'longitude', 'readonly': '1', 'zoom': 12}"/>
</group>
<group></group>
Expand Down
2 changes: 1 addition & 1 deletion crnd_web_widget_select_geolocation/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
'support': '[email protected]',
'website': "https://crnd.pro",
'license': 'LGPL-3',
'version': '13.0.0.1.1',
'version': '13.0.0.1.2',

'depends': [
'base_geolocalize',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ odoo.define('generic_location_geolocalize.MapWidget', function (require) {
this._super.apply(this, arguments);
this.record = record;
this.parent = parent;
this.init_options = options;
this.options = this.nodeOptions;
this.data = this.recordData;
this.navigator_pos = false;
Expand All @@ -33,14 +32,16 @@ odoo.define('generic_location_geolocalize.MapWidget', function (require) {
this.mapId = new Date().getTime().toString();
let body = document.querySelector('body');
let popoverTemplate = document.createElement('template');
popoverTemplate.innerHTML = qweb.render('crnd_web_widget_select_geolocation.map_field_widget_popover', {'mapId': this.mapId});
popoverTemplate.innerHTML = qweb.render('crnd_web_widget_select_geolocation.map_field_widget_popover', {'mapId': this.mapId, 'readonly_mode': this.options.readonly});
body.appendChild(popoverTemplate.content);
this.mapPopover = document.querySelector('div.map_field_widget_wrapper');
this.mapContainer = document.querySelector('div.map_container');
this.closeBtn = document.querySelector('button.close_map_popover_btn');
this.saveBtn = document.querySelector('button.save_geolocation_btn')
this.closeBtn.addEventListener('click', this._closeMapPopover.bind(this));
this.saveBtn.addEventListener('click', this._saveGeolocation.bind(this));
if (this.saveBtn) {
this.saveBtn.addEventListener('click', this._saveGeolocation.bind(this));
}

var loader = this.get_loader();
this.api_key = await this.get_map_api_key();
Expand Down Expand Up @@ -71,29 +72,11 @@ odoo.define('generic_location_geolocalize.MapWidget', function (require) {
if (!this.newGeolocation) {
this.newGeolocation = await this._getGeolocation();
}
if (self.options.address_field) {
var url = "https://maps.googleapis.com/maps/api/geocode/json?latlng="+this.newGeolocation.lat+","+this.newGeolocation.lng+"&key="+this.api_key;
// todo
// When zoom is small geocode request returns several addresses. Now we select first. How to resolve this situation?
await $.ajax({
url: url,
success: function (result) {
console.log(result);
self.newGeolocation.address = result.results[0].formatted_address;
},
error: function (error) {
console.log(error)
self.get_error_warning('Get address error! Addres info is unavailable', false);
}
});
await self._saveData(self.options.latitude_field, self.newGeolocation.lat);
await self._saveData(self.options.longitude_field, self.newGeolocation.lng);
await self._saveData(self.options.address_field, self.newGeolocation.address);
}
else {
await self._saveData(self.options.latitude_field, self.newGeolocation.lat);
await self._saveData(self.options.longitude_field, self.newGeolocation.lng);
}


await self._saveData(self.options.latitude_field, self.newGeolocation.lat);
await self._saveData(self.options.longitude_field, self.newGeolocation.lng);

self._closeMapPopover ();
},

Expand All @@ -113,28 +96,27 @@ odoo.define('generic_location_geolocalize.MapWidget', function (require) {

async _createMarker () {
let self = this;
if (!this.options.readonly) {
this.marker = new google.maps.Marker({
position: await this._getGeolocation(),
map: this.map,
draggable: true,
});

this.marker = new google.maps.Marker({
position: await this._getGeolocation(),
map: this.map,
draggable: true,
});

google.maps.event.addListener(this.marker, 'dragend', function(event) {
if (!self.options.address_field) {
google.maps.event.addListener(this.marker, 'dragend', function (event) {
self.newGeolocation = {
lat: event.latLng.lat(),
lng: event.latLng.lng(),
};
}
else {
self.newGeolocation = {
lat: event.latLng.lat(),
lng: event.latLng.lng(),
address: '',
};
}
});
});
}
else {
this.marker = new google.maps.Marker({
position: await this._getGeolocation(),
map: this.map,
draggable: false,
});
}
},

get_default_geolocation: function () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@
<div class="map_field_widget_wrapper">
<div class="map_container">
<div class="map_header p-3 text-right">
<button type="button" class="btn btn-primary save_geolocation_btn mr8">Save &amp; Close</button>
<button type="button" class="btn btn-secondary close_map_popover_btn">Cancel</button>
<t t-if="!readonly_mode">
<button type="button" class="btn btn-primary save_geolocation_btn mr8">Save &amp; Close</button>
</t>

<button type="button" class="btn btn-secondary close_map_popover_btn">Close</button>
</div>
<div t-att-id="mapId" class="map"/>
</div>
Expand Down
2 changes: 1 addition & 1 deletion test_crnd_web_map_view/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
'author': "Center of Research and Development",
'website': "https://crnd.pro",
'license': 'LGPL-3',
'version': '13.0.0.3.0',
'version': '13.0.0.3.1',

'depends': [
'crnd_web_map_view',
Expand Down
4 changes: 2 additions & 2 deletions test_crnd_web_map_view/models/test_map_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ class TestMapView(models.Model):
name = fields.Char()
latitude = fields.Float(digits=(10, 7))
longitude = fields.Float(digits=(10, 7))
point = fields.Char()
address = fields.Char()
point_readonly = fields.Char()
point_draggable = fields.Char()
19 changes: 13 additions & 6 deletions test_crnd_web_map_view/views/test_map_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@
<field name="name"/>
<field name="latitude"/>
<field name="longitude"/>
<field name="address"/>
<field name="point" widget="select_geolocation"
options="{'latitude_field': 'latitude', 'longitude_field': 'longitude', 'zoom': 12}"/>
<field name="point_readonly"
widget="select_geolocation"
string="Point draggable"
options="{'latitude_field': 'latitude', 'longitude_field': 'longitude', 'readonly': 1, 'zoom': 12}"/>
</tree>
</field>
</record>
Expand All @@ -35,9 +36,15 @@
<field name="name"/>
<field name="latitude"/>
<field name="longitude"/>
<field name="address"/>
<field name="point" widget="select_geolocation"
options="{'latitude_field': 'latitude', 'longitude_field': 'longitude', 'zoom': 12}"/>

<field name="point_draggable"
widget="select_geolocation"
string="Point draggable"
options="{'latitude_field': 'latitude', 'longitude_field': 'longitude', 'zoom': 12}"/>
<field name="point_readonly"
widget="select_geolocation"
string="Point readonly"
options="{'latitude_field': 'latitude', 'longitude_field': 'longitude', 'readonly': 1, 'zoom': 12}"/>
</group>
<group></group>
</group>
Expand Down

0 comments on commit 3b9c94b

Please sign in to comment.