/
_WidgetsMixin.js
276 lines (245 loc) · 10.3 KB
/
_WidgetsMixin.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
define([
'dojo/_base/declare',
'dojo/_base/array',
'dojo/_base/lang',
'put-selector',
'dijit/Menu',
'dijit/layout/ContentPane',
'gis/dijit/FloatingTitlePane',
'gis/dijit/FloatingWidgetDialog'
], function (
declare,
array,
lang,
put,
Menu,
ContentPane,
FloatingTitlePane,
FloatingWidgetDialog
) {
return declare(null, {
legendLayerInfos: [],
editorLayerInfos: [],
identifyLayerInfos: [],
layerControlLayerInfos: [],
widgets: {},
widgetTypes: ['titlePane', 'contentPane', 'floating', 'domNode', 'invisible', 'map', 'layer', 'layout', 'loading'],
createWidgets: function (widgetTypes) {
var widgets = [],
paneWidgets;
widgetTypes = widgetTypes || this.widgetTypes;
for (var key in this.config.widgets) {
if (this.config.widgets.hasOwnProperty(key)) {
var widget = lang.clone(this.config.widgets[key]);
widget.widgetKey = widget.widgetKey || widget.id || key;
if (widget.include && (!this.widgets[widget.widgetKey]) && (array.indexOf(widgetTypes, widget.type) >= 0)) {
widget.position = (typeof (widget.position) !== 'undefined') ? widget.position : 10000;
if ((widget.type === 'titlePane' || widget.type === 'contentPane') && !widget.placeAt) {
widget.placeAt = 'left';
}
widgets.push(widget);
this.widgets[key] = true; // will be replaced by actual widget once created
}
}
}
function getPaneWidgets (pane) {
paneWidgets = array.filter(widgets, function (paneWidget) {
return (paneWidget.placeAt && paneWidget.placeAt === pane);
});
return paneWidgets;
}
for (var pane in this.panes) {
if (this.panes.hasOwnProperty(pane) && pane !== 'outer' && pane !== 'center') {
paneWidgets = getPaneWidgets(pane);
paneWidgets.sort(function (a, b) {
return a.position - b.position;
});
if (paneWidgets.length > 0 && paneWidgets[0].position !== 0) {
paneWidgets[0].position = 0;
}
array.forEach(paneWidgets, function (paneWidget, i) {
this.widgetLoader(paneWidget, i);
}, this);
}
}
paneWidgets = array.filter(widgets, function (paneWidget) {
return !paneWidget.placeAt;
});
paneWidgets.sort(function (a, b) {
return a.position - b.position;
});
array.forEach(paneWidgets, function (paneWidget, i) {
this.widgetLoader(paneWidget, i);
}, this);
},
widgetLoader: function (widgetConfig, position) {
var parentId, pnl;
var widgetTypes = this.widgetTypes;
// add any user-defined widget types
widgetTypes = widgetTypes.concat(this.config.widgetTypes || []);
// only proceed for valid widget types
if (array.indexOf(widgetTypes, widgetConfig.type) < 0) {
this.handleError({
source: 'Controller',
error: 'Widget type "' + widgetConfig.type + '" (' + widgetConfig.title + ') at position ' + position + ' is not supported.'
});
return;
}
if (position) {
widgetConfig.position = position;
}
// build a titlePane or floating widget as the parent
if ((widgetConfig.type === 'titlePane' || widgetConfig.type === 'contentPane' || widgetConfig.type === 'floating')) {
parentId = widgetConfig.widgetKey + '_parent';
if (widgetConfig.type === 'titlePane') {
pnl = this._createTitlePaneWidget(parentId, widgetConfig);
} else if (widgetConfig.type === 'contentPane') {
pnl = this._createContentPaneWidget(parentId, widgetConfig);
} else if (widgetConfig.type === 'floating') {
pnl = this._createFloatingWidget(parentId, widgetConfig);
}
widgetConfig.parentWidget = pnl;
this._showWidgetLoader(pnl);
}
// 2 ways to use require to accommodate widgets that may have an optional separate configuration file
if (typeof (widgetConfig.options) === 'string') {
require([widgetConfig.options, widgetConfig.path], lang.hitch(this, 'createWidget', widgetConfig));
} else {
require([widgetConfig.path], lang.hitch(this, 'createWidget', widgetConfig, widgetConfig.options));
}
},
createWidget: function (widgetConfig, options, WidgetClass) {
var key = widgetConfig.widgetKey;
if (!key) {
return;
}
// set any additional options
options = this._setWidgetOptions(widgetConfig, options);
// create the widget
var pnl = options.parentWidget;
var widgets = this.widgets;
if ((widgetConfig.type === 'titlePane' || widgetConfig.type === 'contentPane' || widgetConfig.type === 'floating')) {
widgets[key] = new WidgetClass(options, put('div')).placeAt(pnl.containerNode);
} else if (widgetConfig.type === 'domNode') {
widgets[key] = new WidgetClass(options, widgetConfig.srcNodeRef);
} else {
widgets[key] = new WidgetClass(options);
}
// start up the widget
if (widgets[key] && widgets[key].startup && !widgets[key]._started) {
widgets[key].startup();
}
this._hideWidgetLoader(pnl);
},
_setWidgetOptions: function (widgetConfig, options) {
if (widgetConfig.id) {
options.id = widgetConfig.id + '_widget';
}
options.parentWidget = widgetConfig.parentWidget;
//replace config map, layerInfos arrays, etc
if (options.map) {
options.map = this.map;
}
if (options.mapRightClickMenu) {
// create right-click menu
if (!this.mapRightClickMenu) {
this.mapRightClickMenu = new Menu({
targetNodeIds: [this.map.root],
selector: '.esriMapLayers' // restrict to map only
});
this.mapRightClickMenu.startup();
}
options.mapRightClickMenu = this.mapRightClickMenu;
}
if (options.mapClickMode) {
options.mapClickMode = this.mapClickMode.current;
}
if (options.legendLayerInfos) {
options.layerInfos = this.legendLayerInfos;
}
if (options.layerControlLayerInfos) {
options.layerInfos = this.layerControlLayerInfos;
}
if (options.editorLayerInfos) {
options.layerInfos = this.editorLayerInfos;
}
if (options.identifyLayerInfos) {
options.layerInfos = this.identifyLayerInfos;
}
return options;
},
_showWidgetLoader: function (pnl) {
if (pnl && pnl.containerNode) {
pnl.loadingNode = put(pnl.containerNode, 'div.widgetLoader i.fa.fa-spinner.fa-pulse.fa-fw').parentNode;
}
},
_hideWidgetLoader: function (pnl) {
if (pnl && pnl.loadingNode) {
require(['dojo/domReady!'], function () {
put(pnl.loadingNode, '!');
});
}
},
_createTitlePaneWidget: function (parentId, widgetConfig) {
var tp,
options = lang.mixin({
title: widgetConfig.title || 'Widget',
iconClass: widgetConfig.iconClass,
open: widgetConfig.open || false,
canFloat: widgetConfig.canFloat || false,
resizable: widgetConfig.resizable || false
}, widgetConfig.paneOptions || {});
if (parentId) {
options.id = parentId;
}
var placeAt = widgetConfig.placeAt;
if (typeof (placeAt) === 'string') {
placeAt = this.panes[placeAt];
}
if (!placeAt) {
placeAt = this.panes.left;
}
if (placeAt) {
options.sidebar = placeAt;
tp = new FloatingTitlePane(options).placeAt(placeAt, widgetConfig.position);
tp.startup();
}
return tp;
},
_createFloatingWidget: function (parentId, widgetConfig) {
var options = lang.mixin({
title: widgetConfig.title
}, widgetConfig.paneOptions || {});
if (parentId) {
options.id = parentId;
}
var fw = new FloatingWidgetDialog(options);
fw.startup();
return fw;
},
_createContentPaneWidget: function (parentId, widgetConfig) {
var cp,
options = lang.mixin({
title: widgetConfig.title,
region: widgetConfig.region || 'center'
}, widgetConfig.paneOptions || {});
if (widgetConfig.className) {
options.className = widgetConfig.className;
}
if (parentId) {
options.id = parentId;
}
var placeAt = widgetConfig.placeAt;
if (!placeAt) {
placeAt = this.panes.left;
} else if (typeof (placeAt) === 'string') {
placeAt = this.panes[placeAt];
}
if (placeAt) {
cp = new ContentPane(options).placeAt(placeAt);
cp.startup();
}
return cp;
}
});
});