comparison lib/GeoTemCo/js/Storytelling/StorytellingWidget.js @ 0:b57c7821382f

initial
author Dirk Wintergruen <dwinter@mpiwg-berlin.mpg.de>
date Thu, 28 May 2015 10:28:12 +0200
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:b57c7821382f
1 /*
2 * StorytellingWidget.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 StorytellingWidget
24 * StorytellingWidget 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 Storytelling widget div
29 * @param {JSON} options user specified configuration that overwrites options in StorytellingConfig.js
30 */
31 StorytellingWidget = function(core, div, options) {
32
33 this.datasets;
34 this.core = core;
35 this.core.setWidget(this);
36 this.currentStatus = new Object();
37
38 this.options = (new StorytellingConfig(options)).options;
39 this.gui = new StorytellingGui(this, div, this.options);
40
41 this.datasetLink;
42
43 Publisher.Subscribe('mapChanged', this, function(mapName) {
44 this.client.currentStatus["mapChanged"] = mapName;
45 this.client.createLink();
46 });
47
48 var currentStatus = $.url().param("currentStatus");
49 if (typeof currentStatus !== "undefined"){
50 this.currentStatus = $.deparam(currentStatus);
51 $.each(this.currentStatus,function(action,data){
52 Publisher.Publish(action, data, this);
53 });
54 }
55 }
56
57 StorytellingWidget.prototype = {
58
59 initWidget : function(data) {
60 var storytellingWidget = this;
61 var gui = storytellingWidget.gui;
62
63 storytellingWidget.datasets = data;
64
65 $(gui.storytellingContainer).empty();
66
67 var magneticLinkParam = "";
68 var datasetIndex = 0;
69 var linkCount = 1;
70 $(storytellingWidget.datasets).each(function(){
71 var dataset = this;
72
73 if (magneticLinkParam.length > 0)
74 magneticLinkParam += "&";
75
76 var paragraph = $("<p></p>");
77 paragraph.append(dataset.label);
78 if (typeof dataset.url !== "undefined"){
79 //TODO: makes only sense for KML or CSV URLs, so "type" of
80 //URL should be preserved (in dataset).
81 //startsWith and endsWith defined in SIMILE Ajax (string.js)
82 var type="csv";
83 if (typeof dataset.type !== "undefined")
84 type = dataset.type;
85 else {
86 if (dataset.url.toLowerCase().endsWith("kml"))
87 type = "kml";
88 }
89
90 magneticLinkParam += type+linkCount+"=";
91 linkCount++;
92 magneticLinkParam += dataset.url;
93
94 var tableLinkDiv = document.createElement('a');
95 tableLinkDiv.title = dataset.url;
96 tableLinkDiv.href = dataset.url;
97 tableLinkDiv.target = '_';
98 tableLinkDiv.setAttribute('class', 'externalLink');
99 paragraph.append(tableLinkDiv);
100 } else {
101 if (storytellingWidget.options.dariahStorage){
102 var uploadToDARIAH = document.createElement('a');
103 $(uploadToDARIAH).append("Upload to DARIAH Storage");
104 uploadToDARIAH.title = "";
105 uploadToDARIAH.href = dataset.url;
106
107 var localDatasetIndex = new Number(datasetIndex);
108 $(uploadToDARIAH).click(function(){
109 var csv = GeoTemConfig.createCSVfromDataset(localDatasetIndex);
110 // taken from dariah.storage.js
111 var storageURL = "http://ref.dariah.eu/storage/"
112 $.ajax({
113 url: storageURL,
114 type: 'POST',
115 contentType: 'text/csv',
116 data: csv,
117 success: function(data, status, xhr) {
118 var location = xhr.getResponseHeader('Location');
119 // the dariah storage id
120 dsid = location.substring(location.lastIndexOf('/')+1);
121
122 //add URL to dataset
123 storytellingWidget.datasets[localDatasetIndex].url = location;
124 storytellingWidget.datasets[localDatasetIndex].type = "csv";
125 //refresh list
126 storytellingWidget.initWidget(storytellingWidget.datasets);
127 },
128 error: function (data, text, error) {
129 alert('error creating new file in dariah storage because ' + text);
130 console.log(data);
131 console.log(text);
132 console.log(error);
133 }
134 });
135 //discard link click-event
136 return(false);
137 });
138 paragraph.append(uploadToDARIAH);
139 }
140 // TODO: if layout is more usable, both options could be used ("else" removed)
141 else if (storytellingWidget.options.localStorage){
142 var saveToLocalStorage = document.createElement('a');
143 $(saveToLocalStorage).append("Save to Local Storage");
144 saveToLocalStorage.title = "";
145 saveToLocalStorage.href = dataset.url;
146
147 var localDatasetIndex = new Number(datasetIndex);
148 $(saveToLocalStorage).click(function(){
149 var csv = GeoTemConfig.createCSVfromDataset(localDatasetIndex);
150
151 var storageName = "GeoBrowser_dataset_"+GeoTemConfig.datasets[localDatasetIndex].label;
152 $.remember({
153 name:storageName,
154 value:csv
155 });
156
157 //add URL to dataset
158 storytellingWidget.datasets[localDatasetIndex].url = storageName;
159 storytellingWidget.datasets[localDatasetIndex].type = "local";
160 //refresh list
161 storytellingWidget.initWidget(storytellingWidget.datasets);
162
163 //discard link click-event
164 return(false);
165 });
166 paragraph.append(saveToLocalStorage);
167 }
168 }
169
170 $(gui.storytellingContainer).append(paragraph);
171 datasetIndex++;
172 });
173
174 this.datasetLink = magneticLinkParam;
175 this.createLink();
176 },
177
178 createLink : function() {
179 $(this.gui.storytellingContainer).find('.magneticLink').remove();
180
181 var magneticLink = document.createElement('a');
182 magneticLink.setAttribute('class', 'magneticLink');
183 $(magneticLink).append("Magnetic Link");
184 magneticLink.title = "Use this link to reload currently loaded (online) data.";
185 magneticLink.href = "?"+this.datasetLink;
186 var currentStatusParam = $.param(this.currentStatus);
187 if (currentStatusParam.length > 0)
188 magneticLink.href += "&currentStatus="+currentStatusParam;
189 magneticLink.target = '_';
190 $(this.gui.storytellingContainer).prepend(magneticLink);
191 },
192
193 highlightChanged : function(objects) {
194 },
195
196 selectionChanged : function(selection) {
197 },
198 };