File : controls/addressControl/GeoCoderHelper.js

1
/*
2
Copyright - 2017 2023 - wwwouaiebe - Contact: https://www.ouaie.be/
3
4
This  program is free software;
5
you can redistribute it and/or modify it under the terms of the
6
GNU General Public License as published by the Free Software Foundation;
7
either version 3 of the License, or any later version.
8
9
This program is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
GNU General Public License for more details.
13
14
You should have received a copy of the GNU General Public License
15
along with this program; if not, write to the Free Software
16
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
*/
18
/*
19
Changes:
20
    - v4.0.0:
21
        - created from v3.6.0
22
Doc reviewed 202208
23
 */
24
25
import theTranslator from '../../core/uiLib/Translator.js';
26
import GeoCoder from '../../core/lib/GeoCoder.js';
27
28
/* ------------------------------------------------------------------------------------------------------------------------- */
29
/**
30
Helper class for the address button
31
*/
32
/* ------------------------------------------------------------------------------------------------------------------------- */
33
34
class GeoCoderHelper {
35
36
    /**
37
    A reference to the dialog object
38
    @type {ModalBaseDialog}
39
    */
40
41
    #dialog;
42
43
    /**
44
    Success handler for the geoCoder.getAddressWithPromise ( ) method
45
    @param {GeoCoderAddress} address The address found by the geocoder
46
    */
47
48
    #onAddressUpdatedByGeoCoder ( address ) {
49
        this.#dialog.hideWait ( );
50
        let addressString = address.street;
51
        if ( '' !== address.city ) {
52
            addressString += ' ' + address.city;
53
        }
54
55
        this.#dialog.setControlsValues (
56
            {
57
                address : addressString,
58
                name : address.name
59
            }
60
        );
61
        if ( this.#dialog.updatePreview ) {
62
            this.#dialog.updatePreview ( { address : addressString } );
63
        }
64
    }
65
66
    /**
67
    The constructor
68
    @param {NoteDialog} noteDialog A reference to the Notedialog object
69
    */
70
71
    constructor ( noteDialog ) {
72
        Object.freeze ( this );
73
        this.#dialog = noteDialog;
74
    }
75
76
    /**
77
    Set the address in the dialog, using GeoCoder
78
    @param {Array.<Number>} latLng The lat and lng of the address to find
79
    */
80
81
    setAddressWithGeoCoder ( latLng ) {
82
        this.#dialog.showWait ( );
83
        this.#dialog.hideError ( );
84
        new GeoCoder ( ).getAddressWithPromise ( latLng )
85
            .then ( address => { this.#onAddressUpdatedByGeoCoder ( address ); } )
86
            .catch (
87
                err => {
88
                    if ( err ) {
89
                        console.error ( err );
90
                    }
91
                    this.#dialog.hideWait ( );
92
                    this.#dialog.showError (
93
                        theTranslator.getText ( 'NoteDialogGeoCoderHelper - an error occurs when searching the adress' )
94
                    );
95
                }
96
            );
97
    }
98
}
99
100
export default GeoCoderHelper;
101
102
/* --- End of file --------------------------------------------------------------------------------------------------------- */
103