Skip to content

Commit 1e276b3

Browse files
committed
[REF] web_view_leaflet_map: full refactor.
- rename folder to follow odoo and OCA conventions, regarding views definition - isolate leaflet_map_view code - isolate leaflet_map_controller code - isolate leaflet_map_renderer code - split xml file into renderer and controller part - apply Odoo SA convention [FIX] remove useless logs, and put a single one in debug level - remove useless code - remove obsolete directive: control-panel-bottom-right [IMP] web_view_leaflet_map: introduce 'Create' Button
1 parent 144873e commit 1e276b3

10 files changed

Lines changed: 129 additions & 111 deletions

File tree

web_view_leaflet_map/README.rst

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
.. image:: https://odoo-community.org/readme-banner-image
2-
:target: https://odoo-community.org/get-involved?utm_source=readme
3-
:alt: Odoo Community Association
4-
51
================================
62
Leaflet Map View (OpenStreetMap)
73
================================
@@ -17,7 +13,7 @@ Leaflet Map View (OpenStreetMap)
1713
.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
1814
:target: https://odoo-community.org/page/development-status
1915
:alt: Beta
20-
.. |badge2| image:: https://img.shields.io/badge/license-AGPL--3-blue.png
16+
.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png
2117
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
2218
:alt: License: AGPL-3
2319
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fgeospatial-lightgray.png?logo=github

web_view_leaflet_map/__manifest__.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@
1717
],
1818
"assets": {
1919
"web.assets_backend": [
20-
"web_view_leaflet_map/static/src/components/map-component/map_view.esm.js",
21-
"web_view_leaflet_map/static/src/components/map-component/map_view.xml",
22-
"web_view_leaflet_map/static/src/components/map-component/web_view_leaflet_map.css",
20+
"web_view_leaflet_map/static/src/views/leaflet_map/leaflet_map_renderer.esm.js",
21+
"web_view_leaflet_map/static/src/views/leaflet_map/leaflet_map_renderer.xml",
22+
"web_view_leaflet_map/static/src/views/leaflet_map/leaflet_map_renderer.css",
23+
"web_view_leaflet_map/static/src/views/leaflet_map/leaflet_map_controller.esm.js",
24+
"web_view_leaflet_map/static/src/views/leaflet_map/leaflet_map_controller.xml",
25+
"web_view_leaflet_map/static/src/views/leaflet_map/leaflet_map_view.esm.js",
2326
],
2427
},
2528
"installable": True,

web_view_leaflet_map/static/description/index.html

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<head>
44
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
55
<meta name="generator" content="Docutils: https://docutils.sourceforge.io/" />
6-
<title>README.rst</title>
6+
<title>Leaflet Map View (OpenStreetMap)</title>
77
<style type="text/css">
88

99
/*
@@ -360,21 +360,16 @@
360360
</style>
361361
</head>
362362
<body>
363-
<div class="document">
363+
<div class="document" id="leaflet-map-view-openstreetmap">
364+
<h1 class="title">Leaflet Map View (OpenStreetMap)</h1>
364365

365-
366-
<a class="reference external image-reference" href="https://odoo-community.org/get-involved?utm_source=readme">
367-
<img alt="Odoo Community Association" src="https://odoo-community.org/readme-banner-image" />
368-
</a>
369-
<div class="section" id="leaflet-map-view-openstreetmap">
370-
<h1>Leaflet Map View (OpenStreetMap)</h1>
371366
<!-- !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
372367
!! This file is generated by oca-gen-addon-readme !!
373368
!! changes will be overwritten. !!
374369
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
375370
!! source digest: sha256:4e68900a79d923acb16260b8dded77558e5fbbba071b0ec253adef930e58c15c
376371
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
377-
<p><a class="reference external image-reference" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external image-reference" href="http://www.gnu.org/licenses/agpl-3.0-standalone.html"><img alt="License: AGPL-3" src="https://img.shields.io/badge/license-AGPL--3-blue.png" /></a> <a class="reference external image-reference" href="https://github.com/OCA/geospatial/tree/18.0/web_view_leaflet_map"><img alt="OCA/geospatial" src="https://img.shields.io/badge/github-OCA%2Fgeospatial-lightgray.png?logo=github" /></a> <a class="reference external image-reference" href="https://translation.odoo-community.org/projects/geospatial-18-0/geospatial-18-0-web_view_leaflet_map"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external image-reference" href="https://runboat.odoo-community.org/builds?repo=OCA/geospatial&amp;target_branch=18.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
372+
<p><a class="reference external image-reference" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external image-reference" href="http://www.gnu.org/licenses/agpl-3.0-standalone.html"><img alt="License: AGPL-3" src="https://img.shields.io/badge/licence-AGPL--3-blue.png" /></a> <a class="reference external image-reference" href="https://github.com/OCA/geospatial/tree/18.0/web_view_leaflet_map"><img alt="OCA/geospatial" src="https://img.shields.io/badge/github-OCA%2Fgeospatial-lightgray.png?logo=github" /></a> <a class="reference external image-reference" href="https://translation.odoo-community.org/projects/geospatial-18-0/geospatial-18-0-web_view_leaflet_map"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external image-reference" href="https://runboat.odoo-community.org/builds?repo=OCA/geospatial&amp;target_branch=18.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
378373
<p>This module extends odoo views, to add a new kind of view, named
379374
<tt class="docutils literal">leaflet_map</tt> that is using the Leaflet javascript library to use
380375
maps. (<a class="reference external" href="https://leafletjs.com/">https://leafletjs.com/</a>) This library is for exemple, used in the
@@ -407,13 +402,13 @@ <h1>Leaflet Map View (OpenStreetMap)</h1>
407402
</ul>
408403
</div>
409404
<div class="section" id="configuration">
410-
<h2><a class="toc-backref" href="#toc-entry-1">Configuration</a></h2>
405+
<h1><a class="toc-backref" href="#toc-entry-1">Configuration</a></h1>
411406
<ul class="simple">
412407
<li>See configuration of the module <tt class="docutils literal">web_leaflet_lib</tt>.</li>
413408
</ul>
414409
</div>
415410
<div class="section" id="development">
416-
<h2><a class="toc-backref" href="#toc-entry-2">Development</a></h2>
411+
<h1><a class="toc-backref" href="#toc-entry-2">Development</a></h1>
417412
<p>Create a new view :</p>
418413
<pre class="code xml literal-block">
419414
<span class="nt">&lt;record</span><span class="w"> </span><span class="na">id=</span><span class="s">&quot;view_my_model_map&quot;</span><span class="w"> </span><span class="na">model=</span><span class="s">&quot;ir.ui.view&quot;</span><span class="nt">&gt;</span><span class="w">
@@ -467,7 +462,7 @@ <h2><a class="toc-backref" href="#toc-entry-2">Development</a></h2>
467462
globally, or per model.</p>
468463
</div>
469464
<div class="section" id="known-issues-roadmap">
470-
<h2><a class="toc-backref" href="#toc-entry-3">Known issues / Roadmap</a></h2>
465+
<h1><a class="toc-backref" href="#toc-entry-3">Known issues / Roadmap</a></h1>
471466
<ul class="simple">
472467
<li>For the time being, at the start of the map loading, the call of
473468
<tt class="docutils literal">invalidateSize()</tt> is required. We should investigate why and try to
@@ -480,29 +475,29 @@ <h2><a class="toc-backref" href="#toc-entry-3">Known issues / Roadmap</a></h2>
480475
</ul>
481476
</div>
482477
<div class="section" id="bug-tracker">
483-
<h2><a class="toc-backref" href="#toc-entry-4">Bug Tracker</a></h2>
478+
<h1><a class="toc-backref" href="#toc-entry-4">Bug Tracker</a></h1>
484479
<p>Bugs are tracked on <a class="reference external" href="https://github.com/OCA/geospatial/issues">GitHub Issues</a>.
485480
In case of trouble, please check there if your issue has already been reported.
486481
If you spotted it first, help us to smash it by providing a detailed and welcomed
487482
<a class="reference external" href="https://github.com/OCA/geospatial/issues/new?body=module:%20web_view_leaflet_map%0Aversion:%2018.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**">feedback</a>.</p>
488483
<p>Do not contact contributors directly about support or help with technical issues.</p>
489484
</div>
490485
<div class="section" id="credits">
491-
<h2><a class="toc-backref" href="#toc-entry-5">Credits</a></h2>
486+
<h1><a class="toc-backref" href="#toc-entry-5">Credits</a></h1>
492487
<div class="section" id="authors">
493-
<h3><a class="toc-backref" href="#toc-entry-6">Authors</a></h3>
488+
<h2><a class="toc-backref" href="#toc-entry-6">Authors</a></h2>
494489
<ul class="simple">
495490
<li>GRAP</li>
496491
</ul>
497492
</div>
498493
<div class="section" id="contributors">
499-
<h3><a class="toc-backref" href="#toc-entry-7">Contributors</a></h3>
494+
<h2><a class="toc-backref" href="#toc-entry-7">Contributors</a></h2>
500495
<ul class="simple">
501496
<li>Sylvain LE GAL (<a class="reference external" href="https://www.twitter.com/legalsylvain">https://www.twitter.com/legalsylvain</a>)</li>
502497
</ul>
503498
</div>
504499
<div class="section" id="maintainers">
505-
<h3><a class="toc-backref" href="#toc-entry-8">Maintainers</a></h3>
500+
<h2><a class="toc-backref" href="#toc-entry-8">Maintainers</a></h2>
506501
<p>This module is maintained by the OCA.</p>
507502
<a class="reference external image-reference" href="https://odoo-community.org">
508503
<img alt="Odoo Community Association" src="https://odoo-community.org/logo.png" />
@@ -517,6 +512,5 @@ <h3><a class="toc-backref" href="#toc-entry-8">Maintainers</a></h3>
517512
</div>
518513
</div>
519514
</div>
520-
</div>
521515
</body>
522516
</html>

web_view_leaflet_map/static/src/components/map-component/map_view.xml

Lines changed: 0 additions & 19 deletions
This file was deleted.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import {Component, useRef} from "@odoo/owl";
2+
import {Layout} from "@web/search/layout";
3+
import {MapRenderer} from "./leaflet_map_renderer.esm";
4+
import {executeButtonCallback} from "@web/views/view_button/view_button_hook";
5+
6+
/**
7+
* Controller class for the Map view, setting up the environment configuration.
8+
*/
9+
export class MapController extends Component {
10+
static template = "web_view_leaflet_map.MapView";
11+
static components = {Layout, MapRenderer};
12+
13+
setup() {
14+
this.rootRef = useRef("root");
15+
}
16+
17+
async onClickCreate() {
18+
return executeButtonCallback(this.rootRef.el, () => this.createRecord());
19+
}
20+
21+
async createRecord() {
22+
await this.props.createRecord();
23+
}
24+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<templates xml:space="preserve">
3+
4+
<t t-name="web_view_leaflet_map.MapView" owl="1">
5+
<div class="h-100 overflow-auto" t-ref="root">
6+
<Layout display="props.display" className="'h-100 overflow-auto'">
7+
<t t-set-slot="control-panel-create-button">
8+
<button
9+
type="button"
10+
class="btn btn-primary o_list_button_add"
11+
data-hotkey="c"
12+
t-on-click="onClickCreate"
13+
data-bounce-button=""
14+
>
15+
New
16+
</button>
17+
</t>
18+
19+
<MapRenderer t-props="props" />
20+
</Layout>
21+
</div>
22+
</t>
23+
24+
</templates>

web_view_leaflet_map/static/src/components/map-component/web_view_leaflet_map.css renamed to web_view_leaflet_map/static/src/views/leaflet_map/leaflet_map_renderer.css

File renamed without changes.

web_view_leaflet_map/static/src/components/map-component/map_view.esm.js renamed to web_view_leaflet_map/static/src/views/leaflet_map/leaflet_map_renderer.esm.js

Lines changed: 8 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
1-
import {registry} from "@web/core/registry";
2-
import {useService} from "@web/core/utils/hooks";
3-
import {Layout} from "@web/search/layout";
41
import {session} from "@web/session";
2+
import {useService} from "@web/core/utils/hooks";
53

6-
/* global L, console, document, DOMParser */
4+
/* global L, console, document */
75

8-
const {Component, useSubEnv, onWillStart, onMounted, onPatched, useRef} = owl;
6+
const {Component, onWillStart, onMounted, onPatched, useRef} = owl;
97

108
export class MapRenderer extends Component {
9+
static template = "web_view_leaflet_map.MapRenderer";
10+
static components = {};
11+
1112
/**
1213
* Initializes the MapRenderer component, setting up services, references, and configuration.
1314
*/
15+
// eslint-disable-next-line complexity
1416
setup() {
15-
console.log(this.props);
1617
this.orm = useService("orm");
1718
this.action = useService("action");
1819
this.mapRef = useRef("mapContainer");
@@ -53,7 +54,6 @@ export class MapRenderer extends Component {
5354
});
5455

5556
onPatched(() => {
56-
console.log("onPatched");
5757
if (this.leafletMap) {
5858
this.renderMarkers();
5959
}
@@ -179,7 +179,7 @@ export class MapRenderer extends Component {
179179
const lng = record[this.fieldLongitude];
180180
let marker = null;
181181
if (!lat || !lng) {
182-
console.log(`Record ${record.id} has no coordinates`);
182+
console.debug(`Record ${record.id} has no coordinates`);
183183
return;
184184
}
185185

@@ -273,61 +273,3 @@ export class MapRenderer extends Component {
273273
});
274274
}
275275
}
276-
277-
MapRenderer.template = "web_view_leaflet_map.MapRenderer";
278-
MapRenderer.components = {};
279-
280-
/**
281-
* Controller class for the Map view, setting up the environment configuration.
282-
*/
283-
export class MapController extends Component {
284-
setup() {
285-
useSubEnv({
286-
config: {
287-
...this.env.config,
288-
},
289-
});
290-
}
291-
}
292-
293-
MapController.template = "web_view_leaflet_map.MapView";
294-
MapController.components = {Layout, MapRenderer};
295-
296-
/**
297-
* Helper function that normalize the architecture input to ensure it is an HTMLElement.
298-
* @param arch
299-
* @returns {HTMLElement|*}
300-
*/
301-
function normalizeArch(arch) {
302-
if (arch && typeof arch !== "string") return arch;
303-
const xml = String(arch || "");
304-
const doc = new DOMParser().parseFromString(xml, "text/xml");
305-
return doc.documentElement;
306-
}
307-
308-
/**
309-
* Definition of the map view for Odoo, including its properties and components.
310-
* @type {{searchMenuTypes: string[], icon: string, Renderer: MapRenderer, multiRecord: boolean, type: string, display_name: string, Controller: MapController, props: (function(*, *): *&{archInfo: {arch: *}, Renderer: MapRenderer})}}
311-
*/
312-
export const mapView = {
313-
type: "leaflet_map",
314-
display_name: "Map",
315-
icon: "fa fa-map-o",
316-
multiRecord: true,
317-
Controller: MapController,
318-
Renderer: MapRenderer,
319-
searchMenuTypes: ["filter", "favorite"],
320-
321-
props: (genericProps) => {
322-
const archEl = normalizeArch(genericProps.arch);
323-
return {
324-
...genericProps,
325-
Renderer: MapRenderer,
326-
archInfo: {
327-
arch: archEl,
328-
},
329-
};
330-
},
331-
};
332-
333-
registry.category("views").add("leaflet_map", mapView);
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<templates xml:space="preserve">
3+
4+
<t t-name="web_view_leaflet_map.MapRenderer" owl="1">
5+
<div class="o_leaflet_main_container h-100" t-ref="root">
6+
<div t-ref="mapContainer" class="o_leaflet_map_container h-100" />
7+
</div>
8+
</t>
9+
10+
</templates>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import {MapController} from "./leaflet_map_controller.esm";
2+
import {MapRenderer} from "./leaflet_map_renderer.esm";
3+
import {registry} from "@web/core/registry";
4+
5+
/* global DOMParser */
6+
7+
/**
8+
* Helper function that normalize the architecture input to ensure it is an HTMLElement.
9+
* @param arch
10+
* @returns {HTMLElement|*}
11+
*/
12+
function normalizeArch(arch) {
13+
if (arch && typeof arch !== "string") return arch;
14+
const xml = String(arch || "");
15+
const doc = new DOMParser().parseFromString(xml, "text/xml");
16+
return doc.documentElement;
17+
}
18+
19+
/**
20+
* Definition of the map view for Odoo, including its properties and components.
21+
* @type {{searchMenuTypes: string[], icon: string, Renderer: MapRenderer, multiRecord: boolean, type: string, display_name: string, Controller: MapController, props: (function(*, *): *&{archInfo: {arch: *}, Renderer: MapRenderer})}}
22+
*/
23+
export const leafletMapView = {
24+
type: "leaflet_map",
25+
display_name: "Map",
26+
icon: "fa fa-map-o",
27+
multiRecord: true,
28+
Controller: MapController,
29+
Renderer: MapRenderer,
30+
searchMenuTypes: ["filter", "favorite"],
31+
32+
props: (genericProps) => {
33+
const archEl = normalizeArch(genericProps.arch);
34+
return {
35+
...genericProps,
36+
Renderer: MapRenderer,
37+
archInfo: {
38+
arch: archEl,
39+
},
40+
};
41+
},
42+
};
43+
44+
registry.category("views").add("leaflet_map", leafletMapView);

0 commit comments

Comments
 (0)