-
Notifications
You must be signed in to change notification settings - Fork 1
/
Widget.js
289 lines (249 loc) · 10.7 KB
/
Widget.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
///////////////////////////////////////////////////////////////////////////
// Copyright © 2015 MAGIS. All Rights Reserved.
//
// Licensed under the GPL licence 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
//
// https://github.com/magis-nc/esri-webappbuilder-widget-url/blob/master/LICENSE
//
// 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.
///////////////////////////////////////////////////////////////////////////
/**
* This widget for ESRI WebApp Builder deals with...
*/
define([
'dojo/_base/declare',
'jimu/BaseWidget',
'dojo/on',
'dojo/_base/lang',
'dojo/aspect',
'dojo/query',
'esri/urlUtils',
'dojo/has',
'dojo/_base/sniff'
],
function (
declare,
BaseWidget,
on,
lang,
aspect,
query,
urlUtils,
has
) {
var clazz = declare([BaseWidget], {
name: 'ePopup',
baseClass: 'jimu-widget-epopup',
/**
* On startup
*/
startup: function () {
this.afterGetAttachments = lang.hitch(this, this.afterGetAttachments);
this.onAttachementsResult = lang.hitch(this, this.onAttachementsResult);
this.map.infoWindow.on("set-features", lang.hitch(this, this.onSetFeatures));
this.layers = {};
this.theme = (this.config.attachments && this.config.attachments.theme) ? this.config.attachments.theme : "default";
this.maxImgSize = (this.config.attachments && this.config.attachments.maxImgSize) ? this.config.attachments.maxImgSize : 1024*1024 ;
},
_getPopupRenderers:function(){
var popups = dojo.query(".esriViewPopup");
var renderers = [];
for(var i=0,nb=popups.length;i<nb;i++){
var popup = popups[i];
var id = popup.id || popup.widgetid;
var popupRenderer = dijit.byId(id);
if(!popupRenderer){
continue;
}
renderers.push(popupRenderer);
if(!this._nls){
this._nls = popupRenderer._nls;
console.log("popupRenderer NLS", this._nls);
}
}
return renderers;
},
onAttachementsResult:function(attachments){
//get attachments zones (can be multiple because of mobile/non-mobile view)
var popupRenderers = this._getPopupRenderers();
if(!popupRenderers)
return;
//Prepare attachments content
if(attachments.length > 0){
var html = '';
for(var i=0,nb=attachments.length;i<nb;i++){
html += this._getLi(attachments[i]);
}
}
else{
html = '<li>'+ this._nls.NLS_noAttach +'</li>';
}
//Setup attachments content
for(var i=0,nb=popupRenderers.length;i<nb;i++){
var attach_ul = popupRenderers[i]._attachmentsList;
attach_ul.innerHTML = html;
attach_ul.className = 'widget-epopup-' + this.theme;
if(this.config.attachments.hide.sectionTitle){
attach_ul.parentNode.childNodes[0].style.display = 'none';
}
if(this.config.attachments.hide.noAttachments && !attachments.length){
attach_ul.parentNode.style.display = 'none';
}
else{
attach_ul.parentNode.className = attach_ul.parentNode.className.replace(" hidden", "");
}
}
},
_getHumanSize:function(size){
var unity = "o";
var unities = ["ko", "mo", "go", "to"];
for(var i = 0, nb=unities.length;i<nb;i++){
if(size < 1024)
break;
unity = unities[i];
size = size / 1024;
}
return (Math.round(size*100)/100) + ' ' + unity;
},
_getIcon:function(attachment, noImage){
var ext = attachment.name.split(".").slice(-1)[0].toLowerCase();
var contentType = attachment.contentType.split("/");
if(!noImage && contentType[0] == "image" && attachment.size <= this.maxImgSize){
return '<img class="thumbnail" src="'+attachment.url+'" />';
}
if(this.config.icons && this.config.icons.indexOf(ext) > -1){
return '<img src="'+this.folderUrl +'/images/extensions/'+ext+'.png" />';
}
return '<img src="'+this.folderUrl +'/images/extensions/_blank.png" />';
},
_getLinkAttributes:function(attachment){
var title = attachment.name + ' ('+this._getHumanSize(attachment.size)+')';
var proxyRule = urlUtils.getProxyRule(attachment.url);
if(proxyRule && proxyRule.proxyUrl)
attachment.url = proxyRule.proxyUrl + "?" + attachment.url;
var download_name = attachment.name.replace('"', " ");
var attrs = ' title="'+title.replace('"', ' ')+'"'
+ ' type="'+attachment.contentType+'"'
+ ' target="_blank"'
+ ' href="'+attachment.url+'"';
if(!has("ff"))
attrs += ' download="'+download_name+'"';
return attrs;
},
_getLi:function(attachment){
var title = attachment.name + ' ('+this._getHumanSize(attachment.size)+')';
var link_attributes = this._getLinkAttributes(attachment);
switch(this.theme){
case "icon":
var img = this._getIcon(attachment);
return '<li title="'+title+'">'
+ '<a '+link_attributes+'>'
+ img + '<span>' + title + '</span>'
+'</a></li>';
case "thumb":
var img = this._getIcon(attachment);
return '<li title="'+title+'">'
+ '<a '+link_attributes+'>'
+ img
+'</a></li>';
case "medium":
var img = this._getIcon(attachment);
return '<li title="'+title+'">'
+ '<a '+link_attributes+'>'
+ img
+'</a></li>';
case "large":
var img = this._getIcon(attachment);
return '<li title="'+title+'">'
+ '<a '+link_attributes+'>'
+ img
+'</a></li>';
default:
return '<li title="'+title+'"><a '+link_attributes+'>'+title+'</a></li>';
}
},
afterGetAttachments:function(defered){
var popupRenderers = this._getPopupRenderers();
for(var i=0,nb=popupRenderers.length;i<nb;i++){
var attach_ul = popupRenderers[i]._attachmentsList;
attach_ul.parentNode.childNodes[0].style.display = 'block';
attach_ul.parentNode.style.display = 'block';
attach_ul.innerHTML = this._nls.NLS_searching;
}
defered.then(this.onAttachementsResult);
},
onSetFeatures: function (evt) {
//Get layers and track infoTemplate getAttachments
for(var i=0, nb=evt.target.features.length;i<nb;i++){
var layer = evt.target.features[i].getLayer();
if(!this.layers[layer.id] && layer.hasAttachments){
this.layers[layer.id] = layer;
if(layer.infoTemplate.getAttachments && layer.infoTemplate.info.showAttachments){
aspect.after(layer.infoTemplate, "getAttachments", this.afterGetAttachments);
}
}
}
},
setSizeAndPlacement:function(){
if(!this.config.size)
return;
var width = this.config.size.width;
var height = this.config.size.height;
//Control if proportions are respected
if(width > (this.map.width * this.config.size.max_with_proportion))
width = this.map.width * this.config.size.max_with_proportion;
if(height > (this.map.height * this.config.size.max_height_proportion))
height = this.map.height * this.config.size.max_height_proportion;
//Get spaces for popup
var popup_anchor = this.map.toScreen(this.map.infoWindow.location);
var anchor_distances = {
"top":popup_anchor.y,
"bottom":this.map.height - popup_anchor.y,
"left":popup_anchor.x,
"right":this.map.width - popup_anchor.x
};
//Ajust with deltas optionnaly configured
if(this.config.placement_deltas){
for(var anchor in anchor_distances){
if(this.config.placement_deltas[anchor])
anchor_distances[anchor] -= this.config.placement_deltas[anchor];
}
}
//Determine best placement (top/bottom/left/right)
var best_anchor = null;
var max = 0;
for(var anchor in anchor_distances){
if(anchor_distances[anchor]>max){
max = anchor_distances[anchor];
best_anchor = anchor;
}
}
//Ajust the placement if needed
if(best_anchor == "top" || best_anchor == "bottom"){
if(anchor_distances["left"] < (heigth / 2)){
best_anchor += "-right";
}
else if(anchor_distances["right"] < (heigth / 2)){
best_anchor += "-left";
}
}
else{
if(anchor_distances["top"] < (heigth / 2)){
best_anchor = "bottom-" + best_anchor;
}
else if(anchor_distances["bottom"] < (heigth / 2)){
best_anchor = "top-" + best_anchor;
}
}
this.map.infoWindow.resize(width, height);
this.map.infoWindow.set("anchor", best_anchor);
}
});
return clazz;
});