Mercurial > hg > extraction-interface
comparison geotemco/js/Dataloader/DataloaderWidget.js @ 0:b12c99b7c3f0
commit for previous development
author | Zoe Hong <zhong@mpiwg-berlin.mpg.de> |
---|---|
date | Mon, 19 Jan 2015 17:13:49 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:b12c99b7c3f0 |
---|---|
1 /* | |
2 * DataloaderWidget.js | |
3 * | |
4 * Copyright (c) 2013, Sebastian Kruse. All rights reserved. | |
5 * | |
6 * This library is free software; you can redistribute it and/or | |
7 * modify it under the terms of the GNU Lesser General Public | |
8 * License as published by the Free Software Foundation; either | |
9 * version 3 of the License, or (at your option) any later version. | |
10 * | |
11 * This library is distributed in the hope that it will be useful, | |
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 * Lesser General Public License for more details. | |
15 * | |
16 * You should have received a copy of the GNU Lesser General Public | |
17 * License along with this library; if not, write to the Free Software | |
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, | |
19 * MA 02110-1301 USA | |
20 */ | |
21 | |
22 /** | |
23 * @class DataloaderWidget | |
24 * DataloaderWidget Implementation | |
25 * @author Sebastian Kruse (skruse@mpiwg-berlin.mpg.de) | |
26 * | |
27 * @param {WidgetWrapper} core wrapper for interaction to other widgets | |
28 * @param {HTML object} div parent div to append the Dataloader widget div | |
29 * @param {JSON} options user specified configuration that overwrites options in DataloaderConfig.js | |
30 */ | |
31 DataloaderWidget = function(core, div, options) { | |
32 | |
33 this.core = core; | |
34 this.core.setWidget(this); | |
35 | |
36 this.options = (new DataloaderConfig(options)).options; | |
37 this.gui = new DataloaderGui(this, div, this.options); | |
38 | |
39 this.dataLoader = new Dataloader(this); | |
40 } | |
41 | |
42 DataloaderWidget.prototype = { | |
43 | |
44 initWidget : function() { | |
45 | |
46 var dataloaderWidget = this; | |
47 }, | |
48 | |
49 highlightChanged : function(objects) { | |
50 if( !GeoTemConfig.highlightEvents ){ | |
51 return; | |
52 } | |
53 }, | |
54 | |
55 selectionChanged : function(selection) { | |
56 if( !GeoTemConfig.selectionEvents ){ | |
57 return; | |
58 } | |
59 }, | |
60 | |
61 triggerHighlight : function(item) { | |
62 }, | |
63 | |
64 tableSelection : function() { | |
65 }, | |
66 | |
67 deselection : function() { | |
68 }, | |
69 | |
70 filtering : function() { | |
71 }, | |
72 | |
73 inverseFiltering : function() { | |
74 }, | |
75 | |
76 triggerRefining : function() { | |
77 }, | |
78 | |
79 reset : function() { | |
80 }, | |
81 | |
82 loadFromURL : function() { | |
83 var dataLoaderWidget = this; | |
84 //using jQuery-URL-Parser (https://github.com/skruse/jQuery-URL-Parser) | |
85 var datasets = []; | |
86 $.each($.url().param(),function(paramName, paramValue){ | |
87 //startsWith and endsWith defined in SIMILE Ajax (string.js) | |
88 var fileName = dataLoaderWidget.dataLoader.getFileName(paramValue); | |
89 var origURL = paramValue; | |
90 if (typeof GeoTemConfig.proxy != 'undefined') | |
91 paramValue = GeoTemConfig.proxy + paramValue; | |
92 if (paramName.toLowerCase().startsWith("kml")){ | |
93 var kmlDoc = GeoTemConfig.getKml(paramValue); | |
94 var dataSet = new Dataset(GeoTemConfig.loadKml(kmlDoc), fileName, origURL); | |
95 if (dataSet != null){ | |
96 var datasetID = parseInt(paramName.substr(3)); | |
97 if (!isNaN(datasetID)){ | |
98 datasets[datasetID] = dataSet; | |
99 } else { | |
100 datasets.push(dataSet); | |
101 } | |
102 } | |
103 } | |
104 else if (paramName.toLowerCase().startsWith("csv")){ | |
105 var json = GeoTemConfig.getCsv(paramValue); | |
106 var dataSet = new Dataset(GeoTemConfig.loadJson(json), fileName, origURL); | |
107 if (dataSet != null){ | |
108 var datasetID = parseInt(paramName.substr(3)); | |
109 if (!isNaN(datasetID)){ | |
110 datasets[datasetID] = dataSet; | |
111 } else { | |
112 datasets.push(dataSet); | |
113 } | |
114 } | |
115 } | |
116 else if (paramName.toLowerCase().startsWith("json")){ | |
117 var json = GeoTemConfig.getJson(paramValue); | |
118 var dataSet = new Dataset(GeoTemConfig.loadJson(json), fileName, origURL); | |
119 if (dataSet != null){ | |
120 var datasetID = parseInt(paramName.substr(4)); | |
121 if (!isNaN(datasetID)){ | |
122 datasets[datasetID] = dataSet; | |
123 } else { | |
124 datasets.push(dataSet); | |
125 } | |
126 } | |
127 } | |
128 else if (paramName.toLowerCase().startsWith("local")){ | |
129 var csv = $.remember({name:encodeURIComponent(origURL)}); | |
130 //TODO: this is a bad idea and will be changed upon having a better | |
131 //usage model for local stored data | |
132 var fileName = origURL.substring("GeoBrowser_dataset_".length); | |
133 var json = GeoTemConfig.convertCsv(csv); | |
134 var dataSet = new Dataset(GeoTemConfig.loadJson(json), fileName, origURL, "local"); | |
135 if (dataSet != null){ | |
136 var datasetID = parseInt(paramName.substr(5)); | |
137 if (!isNaN(datasetID)){ | |
138 datasets[datasetID] = dataSet; | |
139 } else { | |
140 datasets.push(dataSet); | |
141 } | |
142 } | |
143 } | |
144 }); | |
145 //load (optional!) attribute renames | |
146 //each rename param is {latitude:..,longitude:..,place:..,date:..,timeSpanBegin:..,timeSpanEnd:..} | |
147 //examples: | |
148 // &rename1={"latitude":"lat1","longitude":"lon1"} | |
149 // &rename2=[{"latitude":"lat1","longitude":"lon1"},{"latitude":"lat2","longitude":"lon2"}] | |
150 $.each($.url().param(),function(paramName, paramValue){ | |
151 if (paramName.toLowerCase().startsWith("rename")){ | |
152 var datasetID = parseInt(paramName.substr(6)); | |
153 var dataset; | |
154 if (isNaN(datasetID)){ | |
155 var dataset; | |
156 for (datasetID in datasets){ | |
157 break; | |
158 } | |
159 } | |
160 dataset = datasets[datasetID]; | |
161 | |
162 if (typeof dataset === "undefined") | |
163 return; | |
164 | |
165 var renameFunc = function(index,latAttr,lonAttr,placeAttr,dateAttr,timespanBeginAttr, | |
166 timespanEndAttr,indexAttr){ | |
167 var renameArray = []; | |
168 | |
169 if (typeof index === "undefined"){ | |
170 index = 0; | |
171 } | |
172 | |
173 if ((typeof latAttr !== "undefined") && (typeof lonAttr !== "undefined")){ | |
174 renameArray.push({ | |
175 oldColumn:latAttr, | |
176 newColumn:"locations["+index+"].latitude" | |
177 }); | |
178 renameArray.push({ | |
179 oldColumn:lonAttr, | |
180 newColumn:"locations["+index+"].longitude" | |
181 }); | |
182 } | |
183 | |
184 if (typeof placeAttr !== "undefined"){ | |
185 renameArray.push({ | |
186 oldColumn:placeAttr, | |
187 newColumn:"locations["+index+"].place" | |
188 }); | |
189 } | |
190 | |
191 if (typeof dateAttr !== "undefined"){ | |
192 renameArray.push({ | |
193 oldColumn:dateAttr, | |
194 newColumn:"dates["+index+"]" | |
195 }); | |
196 } | |
197 | |
198 if ((typeof timespanBeginAttr !== "undefined") && | |
199 (typeof timespanEndAttr !== "undefined")){ | |
200 renameArray.push({ | |
201 oldColumn:timespanBeginAttr, | |
202 newColumn:"tableContent[TimeSpan:begin]" | |
203 }); | |
204 renameArray.push({ | |
205 oldColumn:timespanEndAttr, | |
206 newColumn:"tableContent[TimeSpan:end]" | |
207 }); | |
208 } | |
209 | |
210 if (typeof indexAttr !== "undefined"){ | |
211 renameArray.push({ | |
212 oldColumn:indexAttr, | |
213 newColumn:"index" | |
214 }); | |
215 } | |
216 | |
217 GeoTemConfig.renameColumns(dataset,renameArray); | |
218 }; | |
219 | |
220 var renames = JSON.parse(paramValue); | |
221 | |
222 if (renames instanceof Array){ | |
223 for (var i=0; i < renames.length; i++){ | |
224 renameFunc(i,renames[i].latitude,renames[i].longitude,renames[i].place,renames[i].date, | |
225 renames[i].timeSpanBegin,renames[i].timeSpanEnd,renames[i].index); | |
226 } | |
227 } else { | |
228 renameFunc(0,renames.latitude,renames.longitude,renames.place,renames.date, | |
229 renames.timeSpanBegin,renames.timeSpanEnd,renames.index); | |
230 } | |
231 } | |
232 }); | |
233 //load (optional!) filters | |
234 //those will create a new(!) dataset, that only contains the filtered IDs | |
235 $.each($.url().param(),function(paramName, paramValue){ | |
236 //startsWith and endsWith defined in SIMILE Ajax (string.js) | |
237 if (paramName.toLowerCase().startsWith("filter")){ | |
238 var datasetID = parseInt(paramName.substr(6)); | |
239 var dataset; | |
240 if (isNaN(datasetID)){ | |
241 var dataset; | |
242 for (datasetID in datasets){ | |
243 break; | |
244 } | |
245 } | |
246 dataset = datasets[datasetID]; | |
247 | |
248 if (typeof dataset === "undefined") | |
249 return; | |
250 | |
251 var filterValues = function(paramValue){ | |
252 var filter = JSON.parse(paramValue); | |
253 var filteredObjects = []; | |
254 for(var i = 0; i < dataset.objects.length; i++){ | |
255 var dataObject = dataset.objects[i]; | |
256 if ($.inArray(dataObject.index,filter) != -1){ | |
257 filteredObjects.push(dataObject); | |
258 } | |
259 } | |
260 var filteredDataset = new Dataset(filteredObjects, dataset.label + " (filtered)", dataset.url, dataset.type); | |
261 datasets.push(filteredDataset); | |
262 } | |
263 | |
264 if (paramValue instanceof Array){ | |
265 for (var i=0; i < paramValue.length; i++){ | |
266 filterValues(paramValue[i]); | |
267 } | |
268 } else { | |
269 filterValues(paramValue); | |
270 } | |
271 | |
272 } | |
273 }); | |
274 //Load the (optional!) dataset colors | |
275 $.each($.url().param(),function(paramName, paramValue){ | |
276 if (paramName.toLowerCase().startsWith("color")){ | |
277 //color is 1-based, index is 0-based! | |
278 var datasetID = parseInt(paramName.substring("color".length))-1; | |
279 if (datasets.length > datasetID){ | |
280 if (typeof datasets[datasetID].color === "undefined"){ | |
281 var color = new Object(); | |
282 var colorsSelectedUnselected = paramValue.split(","); | |
283 if (colorsSelectedUnselected.length > 2) | |
284 return; | |
285 | |
286 var color1 = colorsSelectedUnselected[0]; | |
287 if (color1.length != 6) | |
288 return; | |
289 | |
290 color.r1 = parseInt(color1.substr(0,2),16); | |
291 color.g1 = parseInt(color1.substr(2,2),16); | |
292 color.b1 = parseInt(color1.substr(4,2),16); | |
293 | |
294 //check if a unselected color is given | |
295 if (colorsSelectedUnselected.length == 2){ | |
296 var color0 = colorsSelectedUnselected[1]; | |
297 if (color0.length != 6) | |
298 return; | |
299 | |
300 color.r0 = parseInt(color0.substr(0,2),16); | |
301 color.g0 = parseInt(color0.substr(2,2),16); | |
302 color.b0 = parseInt(color0.substr(4,2),16); | |
303 } else { | |
304 //if not: use the selected color "halved" | |
305 color.r0 = Math.round(color.r1/2); | |
306 color.g0 = Math.round(color.g1/2); | |
307 color.b0 = Math.round(color.b1/2); | |
308 } | |
309 | |
310 datasets[datasetID].color = color; | |
311 } | |
312 } | |
313 } | |
314 }); | |
315 //delete undefined entries in the array | |
316 //(can happen if the sequence given in the URL is not complete | |
317 // e.g. kml0=..,kml2=..) | |
318 //this also reorders the array, starting with 0 | |
319 var tempDatasets = []; | |
320 for(var index in datasets){ | |
321 if (datasets[index] instanceof Dataset){ | |
322 tempDatasets.push(datasets[index]); | |
323 } | |
324 } | |
325 datasets = tempDatasets; | |
326 | |
327 if (datasets.length > 0) | |
328 dataLoaderWidget.dataLoader.distributeDatasets(datasets); | |
329 } | |
330 }; |