-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
17.0: Widget map and new map field #2953
Open
drkpkg
wants to merge
17
commits into
OCA:17.0
Choose a base branch
from
drkpkg:17.0
base: 17.0
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
4a7f686
First version of map widget and field
drkpkg 2e6ee84
Cleanup and name change.
drkpkg 9f817de
Import order fix
drkpkg 97ca3fa
Asset import to *
drkpkg 77d8266
widget name changed for assets.backend
drkpkg e9eeaed
Tests added
drkpkg dcee877
Add tests for Map field widget
drkpkg 2e3166b
Add test cases
drkpkg 66c4adf
Add tests using x_location
drkpkg 955340c
Refactor: typo on convert_to_record
drkpkg 7dfefbd
Refactor: Map field methods dropped. No need because it will act as a…
drkpkg 72cac8f
Drop translation files. No need to add additional data.
drkpkg 29d6574
Linter fixes
drkpkg 4dfa039
Change Leaflet old version to current (1.9.4)
drkpkg d23eecd
Update dependencies to include web_view_leaflet_map
drkpkg 971e1bb
Update dependencies to include web_leaflet_lib
drkpkg 65f514f
Merge branch 'OCA:17.0' into 17.0
drkpkg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
# Map field and widget | ||
|
||
This new widget is a combination of the `map` field and the `map` widget. It allows you | ||
to display a map in the form and to select a location by dragging a marker on the map. | ||
|
||
## Configuration | ||
|
||
## Field instance | ||
|
||
The python instance of the field is `fields.Map`. | ||
|
||
```python | ||
|
||
from odoo import models, fields, api, _ | ||
|
||
_logger = logging.getLogger(__name__) | ||
|
||
class MyModel(models.Model): | ||
_name = 'my.model' | ||
|
||
name = fields.Char(string='Name') | ||
location = fields.Map(string=_('Location')) | ||
|
||
@api.onchange('location') | ||
def _onchange_location(self): | ||
""" | ||
This will be called when the location is changed. | ||
""" | ||
if self.location: | ||
_logger.info('Location: %s', self.location) | ||
``` | ||
|
||
Now lets implement the widget. | ||
|
||
```xml | ||
<odoo> | ||
<data> | ||
<record id="view_my_model_form" model="ir.ui.view"> | ||
<field name="name">my.model.form</field> | ||
<field name="model">my.model</field> | ||
<field name="arch" type="xml"> | ||
<form string="My Model"> | ||
<sheet> | ||
<group> | ||
<field name="name" /> | ||
<field name="location" widget="map" /> | ||
</group> | ||
</sheet> | ||
</form> | ||
</field> | ||
</record> | ||
</data> | ||
</odoo> | ||
``` | ||
|
||
After that you will get this result: | ||
|
||
![Map widget](static/img/map.png) | ||
|
||
## Thanks | ||
|
||
I wanna thank Cybrosys Technologies for the documentation they maintain. I have used | ||
their documentation as a reference to create this widget. | ||
|
||
https://www.cybrosys.com/blog/how-to-create-a-widget-in-odoo-17 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from . import fields |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"name": "Map field widget", | ||
"summary": "A map field widget for Odoo.", | ||
"author": "Odoo Community Association (OCA)", | ||
"website": "https://github.com/OCA/web", | ||
"category": "Customizations", | ||
"version": "17.0.1.0.0", | ||
"depends": ["web", "web_leaflet_lib"], | ||
"assets": { | ||
"web.assets_backend": [ | ||
"web_widget_map/static/src/components/*", | ||
], | ||
}, | ||
"license": "AGPL-3", | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from . import map |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from odoo import fields | ||
|
||
|
||
class MapField(fields.Field): | ||
""" | ||
Custom field to store the location of a record. | ||
|
||
The field stores the location as a string in the format 'lat,lng'. | ||
""" | ||
|
||
type = "char" | ||
column_type = ("varchar", "varchar") | ||
|
||
|
||
# Monkey patch the fields module to add the Map field. | ||
fields.Map = MapField |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# Translation of Odoo Server. | ||
# This file contains the translation of the following modules: | ||
# * map_field | ||
# | ||
msgid "" | ||
msgstr "" | ||
"Project-Id-Version: Odoo Server 17.0+e\n" | ||
"Report-Msgid-Bugs-To: \n" | ||
"POT-Creation-Date: 2024-09-18 18:51+0000\n" | ||
"PO-Revision-Date: 2024-09-18 18:51+0000\n" | ||
"Last-Translator: \n" | ||
"Language-Team: \n" | ||
"MIME-Version: 1.0\n" | ||
"Content-Type: text/plain; charset=UTF-8\n" | ||
"Content-Transfer-Encoding: \n" | ||
"Plural-Forms: \n" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[build-system] | ||
requires = ["whool"] | ||
build-backend = "whool.buildapi" |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
/** @odoo-module */ | ||
|
||
import {loadCSS, loadJS} from "@web/core/assets"; | ||
import {registry} from "@web/core/registry"; | ||
import {useService} from "@web/core/utils/hooks"; | ||
import {standardFieldProps} from "@web/views/fields/standard_field_props"; | ||
import {useInputField} from "@web/views/fields/input_field_hook"; | ||
import { | ||
Component, | ||
onMounted, | ||
onPatched, | ||
onWillPatch, | ||
onWillStart, | ||
onWillUpdateProps, | ||
useRef, | ||
useState, | ||
} from "@odoo/owl"; | ||
|
||
export class MapField extends Component { | ||
static template = "map_field.MapField"; | ||
static props = { | ||
...standardFieldProps, | ||
}; | ||
|
||
setup() { | ||
this.rpc = useService("rpc"); | ||
this.orm = useRef("orm"); | ||
this.marker = null; | ||
this.mapElementList = null; | ||
this.state = useState({ | ||
location: { | ||
lat: this.props.record.data[this.props.name] | ||
? parseFloat(this.props.record.data[this.props.name].split(",")[0]) | ||
: 0.0, | ||
lng: this.props.record.data[this.props.name] | ||
? parseFloat(this.props.record.data[this.props.name].split(",")[1]) | ||
: 0.0, | ||
}, | ||
fields: this.props.field_list | ||
? JSON.parse(this.props.field_list.split(",")) | ||
: [], | ||
}); | ||
useInputField({ | ||
getValue: () => this.state.location, | ||
parse: (value) => this.parseLatLng(value), | ||
setValue: (value) => { | ||
this.state.location = value; | ||
this.placeMarker(value); | ||
}, | ||
}); | ||
|
||
onWillStart(() => | ||
Promise.all([ | ||
loadJS("/web_leaflet_lib/static/lib/leaflet/leaflet.js"), | ||
loadCSS("/web_leaflet_lib/static/lib/leaflet/leaflet.css"), | ||
]) | ||
); | ||
|
||
onMounted(() => { | ||
this.initializeMap(); | ||
}); | ||
|
||
onWillUpdateProps(() => { | ||
this.placeMarker(this.parseLatLng(this.props.record.data[this.props.name])); | ||
this.state.location = this.parseLatLng( | ||
this.props.record.data[this.props.name] | ||
); | ||
}); | ||
|
||
onPatched(() => { | ||
this.placeMarker(this.parseLatLng(this.props.record.data[this.props.name])); | ||
this.resetZoom(this.parseLatLng(this.props.record.data[this.props.name])); | ||
}); | ||
|
||
onWillPatch(() => { | ||
this.placeMarker(this.parseLatLng(this.props.record.data[this.props.name])); | ||
this.resetZoom(this.parseLatLng(this.props.record.data[this.props.name])); | ||
}); | ||
} | ||
|
||
initializeMap() { | ||
const mapOptions = { | ||
center: {lat: this.state.location.lat, lng: this.state.location.lng}, | ||
zoom: 13, | ||
}; | ||
// eslint-disable-next-line no-undef | ||
this.map = L.tileLayer("https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png", { | ||
maxZoom: 18, | ||
}); | ||
this.mapElementList = document.getElementsByClassName("o_map_field"); | ||
this.mapDiv = this.mapElementList[0]; | ||
// eslint-disable-next-line no-undef | ||
this.mapDiv.el = L.map(this.mapDiv, mapOptions); | ||
this.mapDiv.el.addLayer(this.map); | ||
this.placeMarker(this.state.location); | ||
} | ||
|
||
placeMarker(latLng) { | ||
if (this.marker) { | ||
this.marker.remove(); | ||
} | ||
// eslint-disable-next-line no-undef | ||
this.marker = new L.Marker([latLng.lat, latLng.lng], {draggable: true}); | ||
this.marker.addTo(this.mapDiv.el); | ||
this.marker.on("dragend", (e) => this.updateLocation(e.target.getLatLng())); | ||
} | ||
|
||
updateLocation(latLng) { | ||
this.state.location = { | ||
lat: latLng.lat, | ||
lng: latLng.lng, | ||
}; | ||
this.props.record.update({[this.props.name]: `${latLng.lat}, ${latLng.lng}`}); | ||
} | ||
|
||
resetZoom(latLng) { | ||
this.mapDiv.el.setView([latLng.lat, latLng.lng], 13); | ||
} | ||
|
||
parseLatLng(value) { | ||
if (typeof value === "string" && value.includes(",")) { | ||
return { | ||
lat: parseFloat(value.split(",")[0]), | ||
lng: parseFloat(value.split(",")[1]), | ||
}; | ||
} | ||
return {lat: 0, lng: 0}; | ||
} | ||
} | ||
|
||
export const mapField = { | ||
component: MapField, | ||
displayName: "Map", | ||
supportedTypes: ["point"], | ||
}; | ||
|
||
registry.category("fields").add("map", mapField); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<templates> | ||
<t t-name="map_field.MapField"> | ||
<div class="o_map_location_wrapper"> | ||
<div | ||
class="o_map_field" | ||
style="height: 400px; width: 100%; margin-top: 15px;" | ||
> | ||
<div t-att-ref="map" style="height: 100%;" /> | ||
</div> | ||
</div> | ||
</t> | ||
</templates> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
.o_map_field { | ||
width: 100%; | ||
height: 100%; | ||
position: relative; | ||
} | ||
|
||
.o_map_location_wrapper { | ||
display: flex; | ||
flex-direction: column; | ||
gap: 10px; | ||
width: 100%; | ||
padding: 10px 0; | ||
} | ||
|
||
.o_map_info { | ||
display: flex; | ||
flex-direction: row; | ||
justify-content: space-evenly; | ||
align-items: center; | ||
width: 100%; | ||
gap: 15px; | ||
} | ||
|
||
.o_map_label { | ||
font-size: 0.85rem; | ||
font-weight: bold; | ||
width: 100px; | ||
text-align: right; | ||
margin-right: 10px; | ||
} | ||
|
||
.o_map_data { | ||
font-size: 0.85rem; | ||
} | ||
|
||
.o_map_button { | ||
display: inline-flex; | ||
align-items: center; | ||
justify-content: center; | ||
border: none; | ||
background-color: #007bff; // Color azul para el botón | ||
color: white; | ||
border-radius: 50%; | ||
width: 25px; | ||
height: 25px; | ||
cursor: pointer; | ||
transition: background-color 0.3s ease; | ||
|
||
i { | ||
font-size: 1.2rem; | ||
} | ||
|
||
&:hover { | ||
background-color: #0056b3; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from . import test_main |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi could you reuse settings introduced by web_leaflet_lib module ?
See :
https://github.com/OCA/geospatial/blob/16.0/web_view_leaflet_map/static/src/js/view/map/map_renderer.js#L15-L16
https://github.com/OCA/geospatial/blob/16.0/web_view_leaflet_map/static/src/js/view/map/map_renderer.js#L169-L172
values are set here : https://github.com/OCA/geospatial/pull/383/files#diff-5be4d3d480aee272fac8a7719d7470286c43ba57485ca6fc98da0cc374ee8910R11
Thanks !