-
Notifications
You must be signed in to change notification settings - Fork 0
/
BaseWidget1.as
633 lines (540 loc) · 17.8 KB
/
BaseWidget1.as
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
///////////////////////////////////////////////////////////////////////////
// Copyright (c) 2010-2011 Esri. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
///////////////////////////////////////////////////////////////////////////
package com.esri.viewer
{
import com.esri.ags.Map;
import com.esri.ags.symbols.Symbol;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.events.SecurityErrorEvent;
import mx.collections.ArrayCollection;
import mx.events.FlexEvent;
import mx.modules.Module;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import mx.rpc.http.HTTPService;
import mx.utils.StringUtil;
[Event(name="widgetConfigLoaded", type="flash.events.Event")]
[Event(name="onChainRequest", type="com.esri.viewer.AppEvent")]
[ResourceBundle("ViewerStrings")]
/**
* BaseWidget is the foundation of all widgets. All widgets need to be derived from this BaseWidget class.
*
* <p><b>NOTE</b>: Once a new widget class is created by extending this BaseWidget class,
* the developer is responsible for adding the new widget class to the Flash Builder project properties's module table.
* This allows the new widget to be compiled into a separate SWF file.</p>
*/
public class BaseWidget1 extends Module implements IBaseWidget
{
//--------------------------------------------------------------------------
//
// Constants
//
//--------------------------------------------------------------------------
private static const VIEWER_STRINGS:String = "ViewerStrings";
private static const WIDGET_CONFIG_LOADED:String = "widgetConfigLoaded";
//--------------------------------------------------------------------------
//
// Constructor
//
//--------------------------------------------------------------------------
public function BaseWidget()
{
this.autoLayout = true;
addEventListener(FlexEvent.CREATION_COMPLETE, creationCompleteHandler);
AppEvent.addListener(AppEvent.WIDGET_CHANGE_STATE, widgetChangeStateHandler);
}
//--------------------------------------------------------------------------
//
// Variables
//
//--------------------------------------------------------------------------
/**
* The data structure that holds the configuration information parsed by the ConfigManager from config.xml.
* A widget can access top level configuration information through this property.
* The WidgetManager will set it when the widget is initialized.
*
* @see configData
* @see ConfigManager
*/
private var _configData:ConfigData;
/**
* Current active map that the container shows.
* The WidgetManager will set its value when a widget is initialized.
*/
private var _map:Map;
/**
* The default widget icon.
*/
private var _widgetIcon:String = "assets/images/i_widget.png";
private var _widgetTitle:String = "";
[Bindable]
private var _widgetId:Number;
private var _configURL:String;
private var _widgetPreload:String;
private var _widgetState:String;
private var _widgetTemplate:IWidgetTemplate;
private var _isDraggable:Boolean = true;
private var _isResizeable:Boolean = true;
private var _initialWidth:Number = 0;
private var _initialHeight:Number = 0;
private var _waitForCreationComplete:Boolean = true;
//--------------------------------------------------------------------------
//
// Properties
//
//--------------------------------------------------------------------------
//----------------------------------
// configXML
//----------------------------------
private var _configXML:XML;
/**
* The XML type of configuration data.
* @see configData
*/
public function get configXML():XML
{
return _configXML;
}
/**
* @private
*/
public function set configXML(value:XML):void
{
_configXML = value;
}
//----------------------------------
// config
//----------------------------------
public function get config():String
{
return _configURL;
}
/**
* Set configuration file URL. A widget can have its own configuration file.
* The URL of the widget configuration file is specified in the main config.xml.
* The WidgetManager will pass the URL for the configuration file to the actual widget.
*
* @param value the configuration file URL.
*/
public function set config(value:String):void
{
_configURL = value;
}
//----------------------------------
// config data
//----------------------------------
public function get configData():ConfigData
{
return _configData;
}
/**
* Pass in application level configuration data parsed from config.xml.
*
* @param value the configuration data structure object.
* @see ConfigData
*/
public function set configData(value:ConfigData):void
{
_configData = value;
}
//----------------------------------
// proxy url
//----------------------------------
public function get proxyUrl():String
{
return _configData ? _configData.proxyUrl : null;
}
//----------------------------------
// Map
//----------------------------------
[Bindable]
/**
* Set a map object reference. Used by WidgetManager to pass in the current map.
*
* @param value the map reference object.
*/
public function get map():Map
{
return _map;
}
public function set map(value:Map):void
{
_map = value;
}
//----------------------------------
// isDraggable
//----------------------------------
public function get isDraggable():Boolean
{
return _isDraggable;
}
public function set isDraggable(value:Boolean):void
{
_isDraggable = value;
setWidgetTemplateControl();
}
//----------------------------------
// isResizable
//----------------------------------
public function get isResizeable():Boolean
{
return _isResizeable;
}
public function set isResizeable(value:Boolean):void
{
_isResizeable = value;
setWidgetTemplateControl();
}
//----------------------------------
// widget id
//----------------------------------
/**
* Get the widget ID. A widget ID is an internally generated number.
*/
public function get widgetId():Number
{
return _widgetId;
}
/**
* Set the widget ID. A widget ID is an internally generated number.
*
* @param value the Number id.
*/
public function set widgetId(value:Number):void
{
_widgetId = value;
}
//----------------------------------
// widget title
//----------------------------------
public function get widgetTitle():String
{
return _widgetTitle;
}
/**
* Set the widget title. A widget title can be configured in the config.xml.
*
* @param value the title text.
*/
public function set widgetTitle(value:String):void
{
_widgetTitle = value;
}
//----------------------------------
// widget icon
//----------------------------------
public function get widgetIcon():String
{
return _widgetIcon;
}
/**
* Set widget icon.
* A widget icon is usually a JPG or PNG file in 40x40 size and set in the config.xml.
*
* @param value the icon URL.
*/
public function set widgetIcon(value:String):void
{
_widgetIcon = value;
}
//--------------------------------------------------------------------------
//
// Methods
//
//--------------------------------------------------------------------------
private function creationCompleteHandler(event:Event):void
{
var children:Array = this.getChildren();
for each (var child:Object in children)
{
if (child is IWidgetTemplate)
{
_widgetTemplate = child as IWidgetTemplate;
_widgetTemplate.baseWidget = this;
if (_widgetState)
{
_widgetTemplate.widgetState = _widgetState;
}
if (_widgetPreload == WidgetStates.WIDGET_MINIMIZED)
{
_widgetTemplate.widgetState = WidgetStates.WIDGET_MINIMIZED;
}
}
}
if (_waitForCreationComplete) // by default wait for creationComplete before loading the config
{
loadConfigXML();
}
}
private function setWidgetTemplateControl():void
{
var children:Array = this.getChildren();
for each (var child:Object in children)
{
if (child is IWidgetTemplate)
{
_widgetTemplate = child as IWidgetTemplate;
_widgetTemplate.resizable = isResizeable;
_widgetTemplate.draggable = isDraggable;
}
}
}
private function widgetChangeStateHandler(event:AppEvent):void
{
var data:Object = event.data;
var reqId:Number = data.id as Number;
var reqState:String = data.state as String;
if (reqId == this.widgetId)
{
this.setState(reqState);
}
}
/**
* Set configuration file URL(infoconfig) for the info widget on operational layer.
*
* @param value the configuration file URL.
* @param waitForCreationComplete specifies whether to wait for creationComplete before loading the config.
*/
public function setInfoConfig(value:String, waitForCreationComplete:Boolean = true):void
{
this.config = value;
_waitForCreationComplete = waitForCreationComplete;
if (!_waitForCreationComplete)
{
loadConfigXML(); //false for info widget, do not wait for creation complete to load the config
}
}
public function setPreload(value:String):void
{
_widgetPreload = value;
}
/**
* Get the widget state.
*/
public function getState():String
{
return _widgetState;
}
/**
* Set the widget state.
* @param value the state string defined in BaseWidget.
*/
public function setState(value:String):void
{
_widgetState = value;
if (_widgetTemplate)
{
_widgetTemplate.widgetState = value;
}
notifyStateChanged(value);
}
private function notifyStateChanged(widgetState:String):void
{
var data:Object =
{
id: this._widgetId,
state: widgetState
};
AppEvent.dispatch(AppEvent.WIDGET_STATE_CHANGED, data);
}
/**
* Add information from a widget to the DataManager so that it can be shared between widgets.
*
* @param key the widget name
* @param arrayCollection the list of object in infoData structure.
*/
public function addSharedData(key:String, arrayCollection:ArrayCollection):void
{
var data:Object =
{
key: key,
collection: arrayCollection
};
AppEvent.dispatch(AppEvent.DATA_PUBLISH, data);
}
public function fetchSharedData():void
{
AppEvent.dispatch(AppEvent.DATA_FETCH_ALL);
}
public function showInfoWindow(infoData:Object):void
{
AppEvent.dispatch(AppEvent.SHOW_INFOWINDOW, infoData);
}
public function setMapAction(action:String, status:String, symbol:Symbol, callback:Function, showDrawTips:Boolean = true, enableGraphicsLayerMouseEvents:Boolean = false):void
{
var data:Object =
{
tool: action,
status: status,
symbol: symbol,
handler: callback,
showDrawTips: showDrawTips,
enableGraphicsLayerMouseEvents: enableGraphicsLayerMouseEvents
};
AppEvent.dispatch(AppEvent.SET_MAP_ACTION, data);
}
/**
* Set map navigation mode, such a pan, zoomin, etc.
* <p>The navigation methods supported are:</p>
* <listing>
* pan (Navigation.PAN)
* zoomin (Navigation.ZOOM_IN)
* zoomout (Navigation.ZOOM_OUT)
* zoomfull (ViewerContainer.NAVIGATION_ZOOM_FULL)
* zoomprevious (ViewerContainer.NAVIGATION_ZOOM_PREVIOUS)
* zoomnext (ViewerContainer.NAVIGATION_ZOOM_NEXT)
* </listing>
*/
public function setMapNavigation(navMethod:String, status:String):void
{
var data:Object =
{
tool: navMethod,
status: status
};
AppEvent.dispatch(AppEvent.SET_MAP_NAVIGATION, data);
}
public function showError(message:String, title:String = null):void
{
var errorTitle:String = title ? title : widgetTitle;
var errorMessage:ErrorMessage = new ErrorMessage(message, errorTitle);
AppEvent.dispatch(AppEvent.APP_ERROR, errorMessage);
}
private function loadConfigXML():void
{
if (_configXML)
{
dispatchEvent(new Event(WIDGET_CONFIG_LOADED));
}
else if (_configURL)
{
var configService:HTTPService = new HTTPService();
configService.url = _configURL;
configService.resultFormat = HTTPService.RESULT_FORMAT_TEXT;
configService.addEventListener(ResultEvent.RESULT, configResultHandler);
configService.addEventListener(FaultEvent.FAULT, configFaultHandler);
configService.send();
}
}
private function configFaultHandler(event:mx.rpc.events.FaultEvent):void
{
// happens if for example a widget file is missing or have crossdomain problem
var sInfo:String = "";
// Missing file
if (event.fault.rootCause is IOErrorEvent)
{
var ioe:IOErrorEvent = event.fault.rootCause as IOErrorEvent;
if (ioe.text.indexOf("2032: Stream Error. URL:") > -1)
{
sInfo += StringUtil.substitute(getDefaultString('missingConfigFileText'), ioe.text.substring(32)) + "\n\n";
}
else
{
// some other IOError
sInfo += event.fault.rootCause + "\n\n";
}
}
// config file with crossdomain issue
if (event.fault.rootCause is SecurityErrorEvent)
{
var sec:SecurityErrorEvent = event.fault.rootCause as SecurityErrorEvent;
if (sec.text.indexOf("Error #2048: ") > -1) // debug player
{
sInfo += StringUtil.substitute(getDefaultString('configFileCrossDomain'), "\n", sec.text) + "\n\n";
}
else if (sec.text.indexOf("Error #2048") > -1) // non-debug player
{
sInfo += StringUtil.substitute(getDefaultString('configFileCrossDomain'), "\n", sec.toString()) + "\n\n";
}
else
{
// some other Security error
sInfo += event.fault.rootCause + "\n\n";
}
}
if (event.statusCode) // e.g. 404 - Not Found - http://en.wikipedia.org/wiki/List_of_HTTP_status_codes
{
sInfo += StringUtil.substitute(getDefaultString('httpResponseStatus'), event.statusCode) + "\n\n";
}
sInfo += StringUtil.substitute(getDefaultString('faultCode'), event.fault.faultCode) + "\n\n";
sInfo += StringUtil.substitute(getDefaultString('faultInfo'), event.fault.faultString) + "\n\n";
sInfo += StringUtil.substitute(getDefaultString('faultDetail'), event.fault.faultDetail);
showError(sInfo);
}
private function configResultHandler(event:ResultEvent):void
{
try
{
configXML = XML(event.result);
dispatchEvent(new Event(WIDGET_CONFIG_LOADED));
}
catch (error:Error)
{
showError(StringUtil.substitute(getDefaultString("parseConfigErrorText"), _configURL + "\n" + error.message));
}
}
public function get initialWidth():Number
{
return _initialWidth;
}
public function set initialWidth(value:Number):void
{
_initialWidth = value;
}
public function get initialHeight():Number
{
return _initialHeight;
}
public function set initialHeight(value:Number):void
{
_initialHeight = value;
}
public function setXYPosition(x:Number, y:Number):void
{
this.setLayoutBoundsPosition(x, y);
}
public function setRelativePosition(left:String, right:String, top:String, bottom:String):void
{
if (left)
{
this.left = Number(left);
}
if (right)
{
this.right = Number(right);
}
if (top)
{
this.top = Number(top);
}
if (bottom)
{
this.bottom = Number(bottom);
}
}
public function run():void
{
//TODO: need a better way of doing it
//This is to disable a widget's move and resize when it's loaded as a controller
isDraggable = true;
}
public function getDefaultString(resourceName:String):String
{
return resourceManager.getString(VIEWER_STRINGS, resourceName);
}
}
}