This repository has been archived by the owner on Apr 5, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 75
/
DrawText.js
228 lines (199 loc) · 6.67 KB
/
DrawText.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/**
* @copyright 2011 geOps
* @author Just van den Broecke
* @license https://github.com/geops/ole/blob/master/license.txt
* @link https://github.com/geops/ole
*/
/**
* Class: OpenLayers.Editor.Control.DrawText
*
* Draw a text label at selected point. Save label text as feature
* attribute.
*
* Inherits from:
* - <OpenLayers.Editor.Control.DrawPoint>
*/
OpenLayers.Editor.Control.DrawText = OpenLayers.Class(OpenLayers.Editor.Control.DrawPoint, {
title: null,
featureType: 'text',
defaultStyle: 'defaultLabel',
selectStyle: 'selectLabel',
modal: true,
/**
* Constructor: OpenLayers.Editor.Control.DrawPath
* Create a new control for drawing points.
*
* Parameters:
* @param {OpenLayers.Layer.Vector} layer - Points will be added to this layer.
* @param options - {Object} An optional object whose properties will be used
* to extend the control.
*/
initialize: function (layer, options) {
OpenLayers.Editor.Control.DrawPoint.prototype.initialize.apply(this,
[layer, OpenLayers.Handler.Point, options]);
this.title = OpenLayers.i18n('oleDrawText');
// Capture events to do text-label specific processing
layer.events.on({
'beforefeatureadded': this.onBeforeFeatureAdded,
'featureselected': this.onFeatureSelected,
'featureunselected': this.onFeatureUnselected,
scope: this
});
},
deactivate: function () {
var deactivated = OpenLayers.Control.Button.prototype.deactivate.call(this);
if (this.popup && this.popup.feature) {
var feature = this.popup.feature;
// remove and destroy Popup
if (feature.layer) {
feature.layer.map.removePopup(feature.popup);
feature.layer.removeFeatures([feature]);
}
feature.popup.destroy();
feature.popup = null;
this.popup = null;
}
},
/**
* Assign text to feature and redraw.
*
* Parameters:
* feature - {<OpenLayers.Feature.Vector>} the Vector feature.
* text - {String} the label's text string.
*/
setLabelText: function (feature, text) {
// Assign Popup-text to feature attribute 'label'
feature.attributes.label = text;
// Redraw single feature
feature.layer.drawFeature(feature, this.defaultStyle);
},
/**
* Remove and destroy Popup for this feature.
*
* Parameters:
* feature - {<OpenLayers.Feature.Vector>} the Vector feature.
*/
removePopup: function (feature) {
this.popup = null;
if (!feature.popup) {
return;
}
// remove and destroy Popup
feature.layer.map.removePopup(feature.popup);
feature.popup.destroy();
feature.popup = null;
// End modality
var editControl = feature.editControl;
if (editControl) {
editControl.activate();
}
},
/**
* Callback when Popup closed.
*
* Parameters:
* evt - {Object} event object (unused)
*/
onPopupClose: function (evt) {
this.popup = null;
// 'this' is the Popup.
var feature = this.feature;
if (!feature) {
return false;
}
// 'self' is the DrawText Control.
var self = feature.editControl;
if (!self) {
return false;
}
// Input element should contain label text
var labelInput = document.getElementById('olLabelInput');
// Assign Popup-text to feature attribute 'label' and close Popup
self.setLabelText(feature, labelInput.value);
self.removePopup(feature);
// Feature does not exist without text: delete in that case
if (!labelInput.value) {
feature.layer.removeFeatures([feature]);
}
},
/**
* Is this a Text Feature?.
*
* Parameters:
* feature - {<OpenLayers.Feature.Vector>} the Vector feature.
*/
isTextFeature: function (feature) {
return feature ? feature.featureType === 'text' : false;
},
/**
* Callback when Feature selected.
*
* Parameters:
* evt - {Object} event object with Feature
*/
onFeatureSelected: function (evt) {
var feature = evt.feature;
if (this.isTextFeature(feature)) {
feature.layer.drawFeature(feature, this.selectStyle);
}
},
/**
* Callback when Feature deselected.
*
* Parameters:
* evt - {Object} event object with Feature
*/
onFeatureUnselected: function (evt) {
var feature = evt.feature;
if (this.isTextFeature(feature)) {
feature.layer.drawFeature(feature, this.defaultStyle);
}
},
/**
* Callback when Feature added.
*
* Parameters:
* evt - {Object} event object with Feature
*/
onBeforeFeatureAdded: function (evt) {
var feature = evt.feature;
if (!this.isTextFeature(feature)) {
// Feature may be added from file upload, the featureType wil be Point
// Promote to text-feature
if (feature.attributes.label) {
feature.editControl = this;
feature.featureType = 'text';
feature.layer.drawFeature(feature, this.defaultStyle);
}
return true;
}
// Create standard OL Popup to add/edit text
var popup = new OpenLayers.Popup.FramedCloud("featurePopup",
feature.geometry.getBounds().getCenterLonLat(),
new OpenLayers.Size(100, 100),
'<div class="oleDrawTextPopup"><input type="text" size="32" id="olLabelInput"/>' +
'<p>' + OpenLayers.i18n('oleDrawTextEdit') + '</p>' +
'</div>',
null, true, this.onPopupClose);
feature.popup = popup;
feature.editControl = this;
popup.feature = feature;
// Show popup
this.layer.map.addPopup(popup, true);
// Modality: do not allow other labels to be drawn
this.deactivate();
// Close and assign on pressing return-key
var labelInput = document.getElementById('olLabelInput');
var self = this;
labelInput.onkeypress = function (event) {
var keyCode = window.event ? window.event.keyCode : event.keyCode;
if (keyCode == 13) {
self.setLabelText(feature, labelInput.value);
self.removePopup(feature);
}
};
labelInput.focus();
this.popup = popup;
},
CLASS_NAME: 'OpenLayers.Editor.Control.DrawText'
});