@@ -4,126 +4,108 @@ import {createBox} from "ol/interaction/Draw";
4
4
import { transformExtent } from "ol/proj" ;
5
5
6
6
/**
7
- * Class representing a button to draw an extent on the map.
8
- * @extends Control
7
+ * @augments Control
8
+ * @typedef {object } HTMLButtonElement
9
+ * @typedef {object } Vector
10
+ * @typedef {object } QueueExtent
11
+ * @property {Draw } _draw Draw.
12
+ * @property {boolean } _isActive Is active.
13
+ * @property {HTMLButtonElement } _button Button.
14
+ * @property {Vector } _source Source.
15
+ * @property {QueueExtent } _history History of extent.
16
+ * @property {Map } _map Map.
9
17
*/
10
18
export class SelectExtentControl extends Control {
11
- /**
12
- * @type {Draw }
13
- */
14
- #draw;
15
- /**
16
- * @type {boolean }
17
- */
18
- #isActive;
19
- /**
20
- * @type {HTMLButtonElement }
21
- */
22
- #button;
23
- /**
24
- * @type {VectorSource }
25
- */
26
- #source;
27
- /**
28
- * @type {QueueExtent }
29
- */
30
- #history;
31
- /**
32
- * @type {Map }
33
- */
34
- #map;
35
-
36
- /**
37
- * @typedef {Object } Options
38
- * @property {VectorSource } [source] Source.
39
- * @property {QueueExtent } [history] History of extent.
40
- */
41
- constructor ( opt_options ) {
42
-
43
- let options = opt_options || { } ;
44
-
45
- let button = document . createElement ( 'button' ) ;
46
- button . className = 'fas fa-pen-square' ;
47
- button . title = 'Once clicked, select the extent you wish on the map' ;
48
-
49
- let element = document . createElement ( 'div' ) ;
50
- element . className = 'ol-select-extent ol-unselectable ol-control' ;
51
- element . appendChild ( button ) ;
52
-
53
- super ( {
54
- element : element ,
55
- target : options . target ,
56
- } ) ;
57
-
58
- this . #button = button ;
59
-
60
- this . #isActive = false ;
61
-
62
- this . #source = options . source ;
63
-
64
- this . #draw = new Draw ( {
65
- source : this . #source,
66
- type : 'Circle' ,
67
- geometryFunction : createBox ( )
68
- } ) ;
69
-
70
- this . #history = options . history ;
71
-
72
- //Clear the source when the user starts to draw a new extent
73
- this . #draw. on ( 'drawstart' , ( ) => {
74
- this . #source. clear ( ) ;
75
-
76
- //Disable buttons to prevent bugs from user
77
- this . #button. disabled = true ;
78
- this . #button. id = "control-button-disabled" ;
79
- let undoElement = document . querySelector ( ".ol-do-control-undo" )
80
- undoElement . id = "control-button-disabled" ;
81
- undoElement . disabled = true ;
82
- let redoElement = document . querySelector ( ".ol-do-control-redo" )
83
- redoElement . id = "control-button-disabled" ;
84
- redoElement . disabled = true ;
85
- } ) ;
86
-
87
- //Set the extent in the input field when the user finishes to draw an extent
88
- this . #draw. on ( 'drawend' , ( e ) => {
89
- let tmpExtent = e . feature . getGeometry ( ) . getExtent ( ) ;
90
- $ ( '#jforms_mapBuilderAdmin_config_extent' ) . val ( transformExtent ( tmpExtent , 'EPSG:3857' , 'EPSG:4326' ) ) ;
91
-
92
- //Enable button
93
- this . #button. disabled = false ;
94
- this . #button. id = "" ;
95
-
96
- this . #history. deleteAllAfter ( this . #history. getIndex ( ) ) ;
97
-
98
- this . #history. increaseIndex ( ) ;
99
- this . #history. addElement ( tmpExtent ) ;
100
-
101
- let undoElement = document . querySelector ( ".ol-do-control-undo" )
102
- undoElement . id = "" ;
103
- undoElement . disabled = false ;
104
- } ) ;
105
-
106
- this . #button. addEventListener ( 'click' , this . handleSelectExtent . bind ( this ) , false ) ;
107
- }
108
-
109
- //Handle the click event on the button
110
- handleSelectExtent ( ) {
111
- if ( this . #isActive) {
112
- this . #map. removeInteraction ( this . #draw) ;
113
- this . #isActive = false ;
114
- document . querySelector ( ".fas.fa-pen-square" ) . style . backgroundColor = '' ;
115
- } else {
116
- this . #map. addInteraction ( this . #draw) ;
117
- this . #isActive = true ;
118
- document . querySelector ( ".fas.fa-pen-square" ) . style . backgroundColor = '#FFCDCD' ;
19
+ /**
20
+ * Class representing a button to draw an extent on the map.
21
+ * @param {object } opt_options Control options.
22
+ */
23
+ constructor ( opt_options ) {
24
+
25
+ let options = opt_options || { } ;
26
+
27
+ let button = document . createElement ( 'button' ) ;
28
+ button . className = 'fas fa-pen-square' ;
29
+ button . title = 'Once clicked, select the extent you wish on the map' ;
30
+
31
+ let element = document . createElement ( 'div' ) ;
32
+ element . className = 'ol-select-extent ol-unselectable ol-control' ;
33
+ element . appendChild ( button ) ;
34
+
35
+ super ( {
36
+ element : element ,
37
+ target : options . target ,
38
+ } ) ;
39
+
40
+ this . _button = button ;
41
+
42
+ this . _isActive = false ;
43
+
44
+ this . _source = options . source ;
45
+
46
+ this . _draw = new Draw ( {
47
+ source : this . _source ,
48
+ type : 'Circle' ,
49
+ geometryFunction : createBox ( )
50
+ } ) ;
51
+
52
+ this . _history = options . history ;
53
+
54
+ //Clear the source when the user starts to draw a new extent
55
+ this . _draw . on ( 'drawstart' , ( ) => {
56
+ this . _source . clear ( ) ;
57
+
58
+ //Disable buttons to prevent bugs from user
59
+ this . _button . disabled = true ;
60
+ this . _button . id = "control-button-disabled" ;
61
+ let undoElement = document . querySelector ( ".ol-do-control-undo" )
62
+ undoElement . id = "control-button-disabled" ;
63
+ undoElement . disabled = true ;
64
+ let redoElement = document . querySelector ( ".ol-do-control-redo" )
65
+ redoElement . id = "control-button-disabled" ;
66
+ redoElement . disabled = true ;
67
+ } ) ;
68
+
69
+ //Set the extent in the input field when the user finishes to draw an extent
70
+ this . _draw . on ( 'drawend' , ( e ) => {
71
+ let tmpExtent = e . feature . getGeometry ( ) . getExtent ( ) ;
72
+ $ ( '#jforms_mapBuilderAdmin_config_extent' ) . val ( transformExtent ( tmpExtent , 'EPSG:3857' , 'EPSG:4326' ) ) ;
73
+
74
+ //Enable button
75
+ this . _button . disabled = false ;
76
+ this . _button . id = "" ;
77
+
78
+ this . _history . deleteAllAfter ( this . _history . getIndex ( ) ) ;
79
+
80
+ this . _history . increaseIndex ( ) ;
81
+ this . _history . addElement ( tmpExtent ) ;
82
+
83
+ let undoElement = document . querySelector ( ".ol-do-control-undo" )
84
+ undoElement . id = "" ;
85
+ undoElement . disabled = false ;
86
+ } ) ;
87
+
88
+ this . _button . addEventListener ( 'click' , this . handleSelectExtent . bind ( this ) , false ) ;
89
+ }
90
+
91
+ //Handle the click event on the button
92
+ handleSelectExtent ( ) {
93
+ if ( this . _isActive ) {
94
+ this . _map . removeInteraction ( this . _draw ) ;
95
+ this . _isActive = false ;
96
+ document . querySelector ( ".fas.fa-pen-square" ) . style . backgroundColor = '' ;
97
+ } else {
98
+ this . _map . addInteraction ( this . _draw ) ;
99
+ this . _isActive = true ;
100
+ document . querySelector ( ".fas.fa-pen-square" ) . style . backgroundColor = '#FFCDCD' ;
101
+ }
102
+ }
103
+
104
+ /**
105
+ * Set the map value.
106
+ * @param {Map } value Map.
107
+ */
108
+ set map ( value ) {
109
+ this . _map = value ;
119
110
}
120
- }
121
-
122
- /**
123
- * Set the map value.
124
- * @param {Map } value Map.
125
- */
126
- set map ( value ) {
127
- this . #map = value ;
128
- }
129
111
}
0 commit comments