File : dialogs/osmSearchDialog/OsmSearchLimits.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 ObjId from '../../data/ObjId.js';
26
import theConfig from '../../data/Config.js';
27
import theEventDispatcher from '../../core/lib/EventDispatcher.js';
28
import theTravelNotesData from '../../data/TravelNotesData.js';
29
import theOsmSearchEngine from '../../core/osmSearch/OsmSearchEngine.js';
30
import { INVALID_OBJ_ID } from '../../main/Constants.js';
31
32
/* ------------------------------------------------------------------------------------------------------------------------- */
33
/**
34
This class manages the search limits on the map
35
*/
36
/* ------------------------------------------------------------------------------------------------------------------------- */
37
38
class OsmSearchLimits {
39
40
    /**
41
    ObjId for the previous search limits
42
    @type {Number}
43
    */
44
45
    #previousSearchLimitObjId;
46
47
    /**
48
    ObjId for the  search limits
49
    @type {Number}
50
    */
51
52
    #searchLimitObjId;
53
54
    /**
55
    Draw the search limit on the map.
56
    Also used as event listener for pan and zoom operations on the map.
57
    */
58
59
    #drawSearchLimit ( ) {
60
        if ( INVALID_OBJ_ID === this.#searchLimitObjId ) {
61
            this.#searchLimitObjId = ObjId.nextObjId;
62
        }
63
        else {
64
            theEventDispatcher.dispatch ( 'removeobject', { objId : this.#searchLimitObjId } );
65
        }
66
67
        theEventDispatcher.dispatch (
68
            'addrectangle',
69
            {
70
                objId : this.#searchLimitObjId,
71
                bounds : theOsmSearchEngine.searchBounds,
72
                properties : theConfig.osmSearch.nextSearchLimit
73
            }
74
        );
75
    }
76
77
    /**
78
    Draw the previous search limit on the map
79
    */
80
81
    #drawPreviousSearchlimit ( ) {
82
        const previousSearchBounds = theOsmSearchEngine.previousSearchBounds;
83
        if ( ! previousSearchBounds ) {
84
            return;
85
        }
86
        if ( INVALID_OBJ_ID === this.#previousSearchLimitObjId ) {
87
            this.#previousSearchLimitObjId = ObjId.nextObjId;
88
        }
89
        else {
90
            theEventDispatcher.dispatch ( 'removeobject', { objId : this.#previousSearchLimitObjId } );
91
        }
92
        theEventDispatcher.dispatch (
93
            'addrectangle',
94
            {
95
                objId : this.#previousSearchLimitObjId,
96
                bounds : [
97
                    [ previousSearchBounds.getSouthWest ( ).lat, previousSearchBounds.getSouthWest ( ).lng ],
98
                    [ previousSearchBounds.getNorthEast ( ).lat, previousSearchBounds.getNorthEast ( ).lng ]
99
                ],
100
                properties : theConfig.osmSearch.previousSearchLimit
101
            }
102
        );
103
    }
104
105
    /**
106
    The constructor
107
    */
108
109
    constructor ( ) {
110
        Object.freeze ( this );
111
        this.#previousSearchLimitObjId = INVALID_OBJ_ID;
112
        this.#searchLimitObjId = INVALID_OBJ_ID;
113
    }
114
115
    /**
116
    Add maps event listeners and search limits on the map
117
    */
118
119
    show ( ) {
120
        theTravelNotesData.map.on ( 'zoom', this.#drawSearchLimit, this );
121
        theTravelNotesData.map.on ( 'move', this.#drawSearchLimit, this );
122
        this.#drawSearchLimit ( );
123
        this.#drawPreviousSearchlimit ( );
124
    }
125
126
    /**
127
    Remove maps event listeners and search limits on the map
128
    */
129
130
    hide ( ) {
131
        theTravelNotesData.map.off ( 'zoom', this.#drawSearchLimit, this );
132
        theTravelNotesData.map.off ( 'move', this.#drawSearchLimit, this );
133
        if ( INVALID_OBJ_ID !== this.#searchLimitObjId ) {
134
            theEventDispatcher.dispatch ( 'removeobject', { objId : this.#searchLimitObjId } );
135
            this.#searchLimitObjId = INVALID_OBJ_ID;
136
        }
137
        if ( INVALID_OBJ_ID !== this.#previousSearchLimitObjId ) {
138
            theEventDispatcher.dispatch ( 'removeobject', { objId : this.#previousSearchLimitObjId } );
139
            this.#previousSearchLimitObjId = INVALID_OBJ_ID;
140
        }
141
    }
142
}
143
144
export default OsmSearchLimits;
145
146
/* --- End of file --------------------------------------------------------------------------------------------------------- */
147