-
Notifications
You must be signed in to change notification settings - Fork 107
/
Copy pathAction.js
314 lines (287 loc) · 8.54 KB
/
Action.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/*
* Copyright (c) 2008-present The Open Source Geospatial Foundation
*
* Published under the BSD license.
* See https://github.com/geoext/geoext2/blob/master/license.txt for the full
* text of the license.
*/
/*
* @requires GeoExt/Version.js
*/
/**
* Action class to create GeoExt.Action
*
* Sample code to create a toolbar with an OpenLayers control into it.
*
* Example:
*
* var action = Ext.create('GeoExt.Action', {
* text: "max extent",
* control: new OpenLayers.Control.ZoomToMaxExtent(),
* map: map
* });
* var toolbar = Ext.create('Ext.toolbar.Toolbar', action);
*
* @class GeoExt.Action
*/
Ext.define('GeoExt.Action', {
extend: 'Ext.Action',
alias : 'widget.gx_action',
requires: [
'GeoExt.Version'
],
/**
* The OpenLayers control wrapped in this action.
*
* @cfg {OpenLayers.Control}
*/
control: null,
/**
* Activate the action's control when the action is enabled.
*
* @property {Boolean} activateOnEnable
*/
/**
* Activate the action's control when the action is enabled.
*
* @cfg {Boolean} activateOnEnable
*/
activateOnEnable: false,
/**
* Deactivate the action's control when the action is disabled.
*
* @property {Boolean} deactivateOnDisable
*/
/**
* Deactivate the action's control when the action is disabled.
*
* @cfg {Boolean} deactivateOnDisable
*/
deactivateOnDisable: false,
/**
* The OpenLayers map that the control should be added to. For controls that
* don't need to be added to a map or have already been added to one, this
* config property may be omitted.
*
* @cfg {OpenLayers.Map}
*/
map: null,
/**
* The user-provided scope, used when calling uHandler, uToggleHandler,
* and uCheckHandler.
*
* @property {Object}
* @private
*/
uScope: null,
/**
* References the function the user passes through the "handler" property.
*
* @property {Function}
* @private
*/
uHandler: null,
/**
* References the function the user passes through the "toggleHandler"
* property.
*
* @property {Function}
* @private
*/
uToggleHandler: null,
/**
* References the function the user passes through the "checkHandler"
* property.
*
* @property {Function}
* @private
*/
uCheckHandler: null,
/**
* Create a GeoExt.Action instance. A GeoExt.Action is created to insert
* an OpenLayers control in a toolbar as a button or in a menu as a menu
* item. A GeoExt.Action instance can be used like a regular Ext.Action,
* look at the Ext.Action API doc for more detail.
*
* @param {Object} config (optional) Config object.
* @private
*/
constructor: function(config){
// store the user scope and handlers
this.uScope = config.scope;
this.uHandler = config.handler;
this.uToggleHandler = config.toggleHandler;
this.uCheckHandler = config.checkHandler;
config.scope = this;
config.handler = this.pHandler;
config.toggleHandler = this.pToggleHandler;
config.checkHandler = this.pCheckHandler;
// set control in the instance, the Ext.Action
// constructor won't do it for us
this.control = config.control;
var ctrl = this.control;
delete config.control;
this.activateOnEnable = !!config.activateOnEnable;
delete config.activateOnEnable;
this.deactivateOnDisable = !!config.deactivateOnDisable;
delete config.deactivateOnDisable;
// register "activate" and "deactivate" listeners
// on the control
if (ctrl) {
// If map is provided in config, add control to map.
if (config.map) {
config.map.addControl(ctrl);
delete config.map;
}
if((config.pressed || config.checked) && ctrl.map) {
ctrl.activate();
}
if (ctrl.active) {
config.pressed = true;
config.checked = true;
}
ctrl.events.on({
activate: this.onCtrlActivate,
deactivate: this.onCtrlDeactivate,
scope: this
});
}
this.callParent(arguments);
},
/**
* The private handler.
*
* @param {Ext.Component} The component that triggers the handler.
* @private
*/
pHandler: function(cmp){
var ctrl = this.control;
if (ctrl &&
ctrl.type == OpenLayers.Control.TYPE_BUTTON) {
ctrl.trigger();
}
if (this.uHandler) {
this.uHandler.apply(this.uScope, arguments);
}
},
/**
* The private toggle handler.
*
* @param {Ext.Component} cmp The component that triggers the toggle
* handler.
* @param {Boolean} state The state of the toggle.
* @private
*/
pToggleHandler: function(cmp, state){
this.changeControlState(state);
if (this.uToggleHandler) {
this.uToggleHandler.apply(this.uScope, arguments);
}
},
/**
* The private check handler.
*
* @param {Ext.Component} cmp The component that triggers the check handler.
* @param {Boolean} state The state of the toggle.
* @private
*/
pCheckHandler: function(cmp, state){
this.changeControlState(state);
if (this.uCheckHandler) {
this.uCheckHandler.apply(this.uScope, arguments);
}
},
/**
* Change the control state depending on the state boolean.
*
* @param {Boolean} state The state of the toggle.
* @private
*/
changeControlState: function(state){
if(state) {
if(!this._activating) {
this._activating = true;
this.control.activate();
// update initialConfig for next component created from this action
this.initialConfig.pressed = true;
this.initialConfig.checked = true;
this._activating = false;
}
} else {
if(!this._deactivating) {
this._deactivating = true;
this.control.deactivate();
// update initialConfig for next component created from this action
this.initialConfig.pressed = false;
this.initialConfig.checked = false;
this._deactivating = false;
}
}
},
/**
* Called when this action's control is activated.
*
* @private
*/
onCtrlActivate: function(){
var ctrl = this.control;
if(ctrl.type == OpenLayers.Control.TYPE_BUTTON) {
this.enable();
} else {
// deal with buttons
this.safeCallEach("toggle", [true]);
// deal with check items
this.safeCallEach("setChecked", [true]);
}
},
/**
* Called when this action's control is deactivated.
*
* @private
*/
onCtrlDeactivate: function(){
var ctrl = this.control;
if(ctrl.type == OpenLayers.Control.TYPE_BUTTON) {
this.disable();
} else {
// deal with buttons
this.safeCallEach("toggle", [false]);
// deal with check items
this.safeCallEach("setChecked", [false]);
}
},
/**
* Called when the control which should get toggled
* is not of type OpenLayers.Control.TYPE_BUTTON
*
* @private
*/
safeCallEach: function(fnName, args) {
var cs = this.items;
for(var i = 0, len = cs.length; i < len; i++){
if(cs[i][fnName]) {
cs[i].rendered ?
cs[i][fnName].apply(cs[i], args) :
cs[i].on({
"render": Ext.Function.bind(cs[i][fnName], cs[i], args),
single: true
});
}
}
},
/**
* Override method on super to optionally deactivate controls on disable.
*
* @param {Boolean} v Disable the action's components.
* @private
*/
setDisabled : function(v) {
if (!v && this.activateOnEnable && this.control && !this.control.active) {
this.control.activate();
}
if (v && this.deactivateOnDisable && this.control && this.control.active) {
this.control.deactivate();
}
return GeoExt.Action.superclass.setDisabled.apply(this, arguments);
}
});