blob: 240c578cb39aff5b82faa21b543d1b382671a8e7 [file] [log] [blame]
Zachary Heilbrond98423c2014-03-06 17:37:28 -08001$(function() {
2
Eugenia Gabrielovadbd50a42013-10-19 00:30:27 -07003 // Initialize connection to AsterixDB. Just one connection is needed and contains
4 // logic for connecting to each API endpoint. This object A is reused throughout the
Zachary Heilbrond98423c2014-03-06 17:37:28 -08005 // code but does not store information about any individual API call.
Eugenia Gabrielovadbd50a42013-10-19 00:30:27 -07006 A = new AsterixDBConnection({
Zachary Heilbrond98423c2014-03-06 17:37:28 -08007
Eugenia Gabrielovadbd50a42013-10-19 00:30:27 -07008 // We will be using the twitter dataverse, which we can configure either like this
9 // or by following our AsterixDBConnection with a dataverse call, like so:
10 // A = new AsterixDBConnection().dataverse("twitter");
11 "dataverse" : "twitter",
Zachary Heilbrond98423c2014-03-06 17:37:28 -080012
Eugenia Gabrielovadbd50a42013-10-19 00:30:27 -070013 // Due to the setup of this demo using the Bottle server, it is necessary to change the
14 // default endpoint of API calls. The proxy server handles the call to http://localhost:19002
15 // for us, and we reconfigure this connection to connect to the proxy server.
16 "endpoint_root" : "/",
Zachary Heilbrond98423c2014-03-06 17:37:28 -080017
Eugenia Gabrielovadbd50a42013-10-19 00:30:27 -070018 // Finally, we want to make our error function nicer so that we show errors with a call to the
19 // reportUserMessage function. Update the "error" property to do that.
20 "error" : function(data) {
21 // For an error, data will look like this:
22 // {
23 // "error-code" : [error-number, error-text]
24 // "stacktrace" : ...stack trace...
25 // "summary" : ...summary of error...
26 // }
27 // We will report this as an Asterix REST API Error, an error code, and a reason message.
28 // Note the method signature: reportUserMessage(message, isPositiveMessage, target). We will provide
Zachary Heilbrond98423c2014-03-06 17:37:28 -080029 // an error message to display, a positivity value (false in this case, errors are bad), and a
Eugenia Gabrielovadbd50a42013-10-19 00:30:27 -070030 // target html element in which to report the message.
31 var showErrorMessage = "Asterix Error #" + data["error-code"][0] + ": " + data["error-code"][1];
32 var isPositive = false;
33 var showReportAt = "report-message";
Zachary Heilbrond98423c2014-03-06 17:37:28 -080034
Eugenia Gabrielovadbd50a42013-10-19 00:30:27 -070035 reportUserMessage(showErrorMessage, isPositive, showReportAt);
36 }
37 });
Zachary Heilbrond98423c2014-03-06 17:37:28 -080038
Eugenia Gabrielovaab1ae552013-10-20 01:12:19 -070039 // This little bit of code manages period checks of the asynchronous query manager,
40 // which holds onto handles asynchornously received. We can set the handle update
41 // frequency using seconds, and it will let us know when it is ready.
Zachary Heilbrond98423c2014-03-06 17:37:28 -080042 var intervalID = setInterval(
Eugenia Gabrielovaab1ae552013-10-20 01:12:19 -070043 function() {
44 asynchronousQueryIntervalUpdate();
Zachary Heilbrond98423c2014-03-06 17:37:28 -080045 },
Eugenia Gabrielovaab1ae552013-10-20 01:12:19 -070046 asynchronousQueryGetInterval()
47 );
Zachary Heilbrond98423c2014-03-06 17:37:28 -080048
Eugenia Gabrielova8b34c652013-10-19 06:53:27 -070049 // Legend Container
Zachary Heilbrond98423c2014-03-06 17:37:28 -080050 // Create a rainbow from a pretty color scheme.
Eugenia Gabrielova8b34c652013-10-19 06:53:27 -070051 // http://www.colourlovers.com/palette/292482/Terra
52 rainbow = new Rainbow();
53 rainbow.setSpectrum("#E8DDCB", "#CDB380", "#036564", "#033649", "#031634");
54 buildLegend();
Zachary Heilbrond98423c2014-03-06 17:37:28 -080055
Eugenia Gabrielovaf9fcd712013-10-20 02:37:35 -070056 // Initialization of UI Tabas
Eugenia Gabrielovaab1ae552013-10-20 01:12:19 -070057 initDemoPrepareTabs();
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -070058
Eugenia Gabrielovaab1ae552013-10-20 01:12:19 -070059 // UI Elements - Creates Map, Location Auto-Complete, Selection Rectangle
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -070060 var mapOptions = {
genia.likes.science@gmail.com1b30f3d2013-08-17 23:53:37 -070061 center: new google.maps.LatLng(38.89, -77.03),
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -070062 zoom: 4,
genia.likes.science@gmail.com1b30f3d2013-08-17 23:53:37 -070063 mapTypeId: google.maps.MapTypeId.ROADMAP,
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -070064 streetViewControl: false,
Eugenia Gabrielovae11965b2013-10-29 19:07:42 -070065 draggable : false,
66 mapTypeControl: false
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -070067 };
68 map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);
69
70 var input = document.getElementById('location-text-box');
71 var autocomplete = new google.maps.places.Autocomplete(input);
72 autocomplete.bindTo('bounds', map);
Zachary Heilbrond98423c2014-03-06 17:37:28 -080073
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -070074 google.maps.event.addListener(autocomplete, 'place_changed', function() {
75 var place = autocomplete.getPlace();
76 if (place.geometry.viewport) {
77 map.fitBounds(place.geometry.viewport);
78 } else {
79 map.setCenter(place.geometry.location);
80 map.setZoom(17); // Why 17? Because it looks good.
81 }
82 var address = '';
83 if (place.address_components) {
84 address = [(place.address_components[0] && place.address_components[0].short_name || ''),
85 (place.address_components[1] && place.address_components[1].short_name || ''),
86 (place.address_components[2] && place.address_components[2].short_name || '') ].join(' ');
87 }
88 });
Zachary Heilbrond98423c2014-03-06 17:37:28 -080089
Eugenia Gabrielovaea8f9482013-10-28 04:49:18 -070090 // Drawing Manager for selecting map regions. See documentation here:
91 // https://developers.google.com/maps/documentation/javascript/reference#DrawingManager
92 rectangleManager = new google.maps.drawing.DrawingManager({
93 drawingMode : google.maps.drawing.OverlayType.RECTANGLE,
94 drawingControl : false,
95 rectangleOptions : {
96 strokeWeight: 1,
97 clickable: false,
98 editable: true,
99 strokeColor: "2b3f8c",
100 fillColor: "2b3f8c",
101 zIndex: 1
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700102 }
103 });
Eugenia Gabrielovaea8f9482013-10-28 04:49:18 -0700104 rectangleManager.setMap(map);
105 selectionRectangle = null;
Zachary Heilbrond98423c2014-03-06 17:37:28 -0800106
Eugenia Gabrielovaea8f9482013-10-28 04:49:18 -0700107 // Drawing Manager: Just one editable rectangle!
108 google.maps.event.addListener(rectangleManager, 'rectanglecomplete', function(rectangle) {
109 selectionRectangle = rectangle;
110 rectangleManager.setDrawingMode(null);
111 });
Eugenia Gabrielovaab1ae552013-10-20 01:12:19 -0700112
Eugenia Gabrielovaf9fcd712013-10-20 02:37:35 -0700113 // Initialize data structures
114 APIqueryTracker = {};
115 asyncQueryManager = {};
Eugenia Gabrielovaf9fcd712013-10-20 02:37:35 -0700116 map_cells = [];
117 map_tweet_markers = [];
118 map_info_windows = {};
119 review_mode_tweetbooks = [];
Eugenia Gabrielovafcd28682013-10-20 05:36:36 -0700120
Eugenia Gabrielovaf9fcd712013-10-20 02:37:35 -0700121 getAllDataverseTweetbooks();
Eugenia Gabrielovaf9fcd712013-10-20 02:37:35 -0700122 initDemoUIButtonControls();
Zachary Heilbrond98423c2014-03-06 17:37:28 -0800123
Eugenia Gabrielovae11965b2013-10-29 19:07:42 -0700124 google.maps.event.addListenerOnce(map, 'idle', function(){
125 // Show tutorial tab only the first time the map is loaded
126 $('#mode-tabs a:first').tab('show');
127 });
Zachary Heilbrond98423c2014-03-06 17:37:28 -0800128
Eugenia Gabrielovaf9fcd712013-10-20 02:37:35 -0700129});
130
131function initDemoUIButtonControls() {
132
133 // Explore Mode - Update Sliders
Eugenia Gabrielovaab1ae552013-10-20 01:12:19 -0700134 var updateSliderDisplay = function(event, ui) {
135 if (event.target.id == "grid-lat-slider") {
136 $("#gridlat").text(""+ui.value);
137 } else {
138 $("#gridlng").text(""+ui.value);
139 }
140 };
Eugenia Gabrielovaab1ae552013-10-20 01:12:19 -0700141 sliderOptions = {
142 max: 10,
Eugenia Gabrielovaf9fcd712013-10-20 02:37:35 -0700143 min: 2.0,
Eugenia Gabrielovaab1ae552013-10-20 01:12:19 -0700144 step: .1,
Eugenia Gabrielovaf9fcd712013-10-20 02:37:35 -0700145 value: 3.0,
Eugenia Gabrielovaab1ae552013-10-20 01:12:19 -0700146 slidechange: updateSliderDisplay,
147 slide: updateSliderDisplay,
148 start: updateSliderDisplay,
149 stop: updateSliderDisplay
150 };
Eugenia Gabrielovaab1ae552013-10-20 01:12:19 -0700151 $("#gridlat").text(""+sliderOptions.value);
152 $("#gridlng").text(""+sliderOptions.value);
153 $(".grid-slider").slider(sliderOptions);
Eugenia Gabrielovaf9fcd712013-10-20 02:37:35 -0700154
155 // Explore Mode - Query Builder Date Pickers
Eugenia Gabrielovaab1ae552013-10-20 01:12:19 -0700156 var dateOptions = {
157 dateFormat: "yy-mm-dd",
158 defaultDate: "2012-01-02",
159 navigationAsDateFormat: true,
160 constrainInput: true
161 };
162 var start_dp = $("#start-date").datepicker(dateOptions);
163 start_dp.val(dateOptions.defaultDate);
164 dateOptions['defaultDate'] = "2012-12-31";
165 var end_dp= $("#end-date").datepicker(dateOptions);
166 end_dp.val(dateOptions.defaultDate);
Zachary Heilbrond98423c2014-03-06 17:37:28 -0800167
Eugenia Gabrielovaf9fcd712013-10-20 02:37:35 -0700168 // Explore Mode: Toggle Selection/Location Search
Eugenia Gabrielova3d7bfe52013-10-28 02:36:44 -0700169 $('#selection-button').on('change', function (e) {
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700170 $("#location-text-box").attr("disabled", "disabled");
Eugenia Gabrielovaea8f9482013-10-28 04:49:18 -0700171 rectangleManager.setMap(map);
172 if (selectionRectangle) {
173 selectionRectangle.setMap(null);
174 selectionRectangle = null;
175 } else {
176 rectangleManager.setDrawingMode(google.maps.drawing.OverlayType.RECTANGLE);
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700177 }
178 });
Eugenia Gabrielova3d7bfe52013-10-28 02:36:44 -0700179 $('#location-button').on('change', function (e) {
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700180 $("#location-text-box").removeAttr("disabled");
Eugenia Gabrielovaea8f9482013-10-28 04:49:18 -0700181 rectangleManager.setMap(null);
Eugenia Gabrielovae11965b2013-10-29 19:07:42 -0700182 selectionRectangle = null;
183 rectangleManager.setDrawingMode(google.maps.drawing.OverlayType.RECTANGLE);
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700184 });
Eugenia Gabrielovaf9fcd712013-10-20 02:37:35 -0700185 $("#selection-button").trigger("click");
Eugenia Gabrielovaab1ae552013-10-20 01:12:19 -0700186
Eugenia Gabrielovaf9fcd712013-10-20 02:37:35 -0700187 // Review Mode: New Tweetbook
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700188 $('#new-tweetbook-button').on('click', function (e) {
189 onCreateNewTweetBook($('#new-tweetbook-entry').val());
Zachary Heilbrond98423c2014-03-06 17:37:28 -0800190
genia.likes.science@gmail.com2de4c182013-10-04 11:39:11 -0700191 $('#new-tweetbook-entry').val("");
192 $('#new-tweetbook-entry').attr("placeholder", "Name a new tweetbook");
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700193 });
Eugenia Gabrielovaf9fcd712013-10-20 02:37:35 -0700194
195 // Explore Mode - Clear Button
Zachary Heilbrond98423c2014-03-06 17:37:28 -0800196 $("#clear-button").click(mapWidgetResetMap);
Eugenia Gabrielovaf9fcd712013-10-20 02:37:35 -0700197
198 // Explore Mode: Query Submission
Eugenia Gabrielovaab1ae552013-10-20 01:12:19 -0700199 $("#submit-button").on("click", function () {
Eugenia Gabrielovaf9fcd712013-10-20 02:37:35 -0700200
Eugenia Gabrielovafcd28682013-10-20 05:36:36 -0700201 $("#report-message").html('');
202 $("#submit-button").attr("disabled", true);
Eugenia Gabrielovaea8f9482013-10-28 04:49:18 -0700203 rectangleManager.setDrawingMode(null);
Zachary Heilbrond98423c2014-03-06 17:37:28 -0800204
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700205 var kwterm = $("#keyword-textbox").val();
Eugenia Gabrielovafcd28682013-10-20 05:36:36 -0700206 var startdp = $("#start-date").datepicker("getDate");
207 var enddp = $("#end-date").datepicker("getDate");
208 var startdt = $.datepicker.formatDate("yy-mm-dd", startdp)+"T00:00:00Z";
209 var enddt = $.datepicker.formatDate("yy-mm-dd", enddp)+"T23:59:59Z";
210
211 var formData = {
212 "keyword": kwterm,
213 "startdt": startdt,
214 "enddt": enddt,
215 "gridlat": $("#grid-lat-slider").slider("value"),
216 "gridlng": $("#grid-lng-slider").slider("value")
217 };
218
219 // Get Map Bounds
220 var bounds;
Eugenia Gabrielovaea8f9482013-10-28 04:49:18 -0700221 if ($('#selection-label').hasClass("active") && selectionRectangle) {
222 bounds = selectionRectangle.getBounds();
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700223 } else {
Eugenia Gabrielovafcd28682013-10-20 05:36:36 -0700224 bounds = map.getBounds();
225 }
Zachary Heilbrond98423c2014-03-06 17:37:28 -0800226
Eugenia Gabrielovafcd28682013-10-20 05:36:36 -0700227 var swLat = Math.abs(bounds.getSouthWest().lat());
228 var swLng = Math.abs(bounds.getSouthWest().lng());
229 var neLat = Math.abs(bounds.getNorthEast().lat());
230 var neLng = Math.abs(bounds.getNorthEast().lng());
Zachary Heilbrond98423c2014-03-06 17:37:28 -0800231
Eugenia Gabrielovafcd28682013-10-20 05:36:36 -0700232 formData["swLat"] = Math.min(swLat, neLat);
233 formData["swLng"] = Math.max(swLng, neLng);
234 formData["neLat"] = Math.max(swLat, neLat);
235 formData["neLng"] = Math.min(swLng, neLng);
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700236
Eugenia Gabrielovafcd28682013-10-20 05:36:36 -0700237 var build_cherry_mode = "synchronous";
238 if ($('#asbox').is(":checked")) {
239 build_cherry_mode = "asynchronous";
Eugenia Gabrielovafcd28682013-10-20 05:36:36 -0700240 }
Zachary Heilbrond98423c2014-03-06 17:37:28 -0800241
Eugenia Gabrielovafcd28682013-10-20 05:36:36 -0700242 var f = buildAQLQueryFromForm(formData);
Zachary Heilbrond98423c2014-03-06 17:37:28 -0800243
Eugenia Gabrielovafcd28682013-10-20 05:36:36 -0700244 APIqueryTracker = {
245 "query" : "use dataverse twitter;\n" + f.val(),
246 "data" : formData
247 };
Zachary Heilbrond98423c2014-03-06 17:37:28 -0800248
Eugenia Gabrielovafcd28682013-10-20 05:36:36 -0700249 if (build_cherry_mode == "synchronous") {
250 A.query(f.val(), cherryQuerySyncCallback, build_cherry_mode);
251 } else {
252 A.query(f.val(), cherryQueryAsyncCallback, build_cherry_mode);
253 }
Zachary Heilbrond98423c2014-03-06 17:37:28 -0800254
Eugenia Gabrielovafcd28682013-10-20 05:36:36 -0700255 // Clears selection rectangle on query execution, rather than waiting for another clear call.
Eugenia Gabrielovaea8f9482013-10-28 04:49:18 -0700256 if (selectionRectangle) {
257 selectionRectangle.setMap(null);
258 selectionRectangle = null;
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700259 }
260 });
Eugenia Gabrielovaf9fcd712013-10-20 02:37:35 -0700261}
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700262
Eugenia Gabrielovaab1ae552013-10-20 01:12:19 -0700263/**
264* Builds AsterixDB REST Query from explore mode form.
265*/
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700266function buildAQLQueryFromForm(parameters) {
genia.likes.science@gmail.com1b30f3d2013-08-17 23:53:37 -0700267
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700268 var bounds = {
Zachary Heilbrond98423c2014-03-06 17:37:28 -0800269 "ne" : { "lat" : parameters["neLat"], "lng" : -1*parameters["neLng"]},
genia.likes.science@gmail.com1b30f3d2013-08-17 23:53:37 -0700270 "sw" : { "lat" : parameters["swLat"], "lng" : -1*parameters["swLng"]}
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700271 };
Zachary Heilbrond98423c2014-03-06 17:37:28 -0800272
273 var rectangle =
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700274 new FunctionExpression("create-rectangle",
275 new FunctionExpression("create-point", bounds["sw"]["lat"], bounds["sw"]["lng"]),
276 new FunctionExpression("create-point", bounds["ne"]["lat"], bounds["ne"]["lng"]));
Zachary Heilbrond98423c2014-03-06 17:37:28 -0800277
Eugenia Gabrielovafcd28682013-10-20 05:36:36 -0700278 // You can chain these all together, but let's take them one at a time.
279 // Let's start with a ForClause. Here we go through each tweet $t in the
280 // dataset TweetMessageShifted.
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700281 var aql = new FLWOGRExpression()
Eugenia Gabrielovafcd28682013-10-20 05:36:36 -0700282 .ForClause("$t", new AExpression("dataset TweetMessagesShifted"));
283
284 // We know we have bounds for our region, so we can add that LetClause next.
285 aql = aql.LetClause("$region", rectangle);
286
287 // Now, let's change it up. The keyword term doesn't always show up, so it might be blank.
288 // We'll attach a new let clause for it, and then a WhereClause.
289 if (parameters["keyword"].length > 0) {
290 aql = aql
291 .LetClause("$keyword", new AExpression('"' + parameters["keyword"] + '"'))
292 .WhereClause().and(
293 new FunctionExpression("spatial-intersect", "$t.sender-location", "$region"),
294 new AExpression('$t.send-time > datetime("' + parameters["startdt"] + '")'),
295 new AExpression('$t.send-time < datetime("' + parameters["enddt"] + '")'),
Zachary Heilbrond98423c2014-03-06 17:37:28 -0800296 new FunctionExpression("contains", "$t.message-text", "$keyword")
Eugenia Gabrielovafcd28682013-10-20 05:36:36 -0700297 );
298 } else {
299 aql = aql
300 .WhereClause().and(
301 new FunctionExpression("spatial-intersect", "$t.sender-location", "$region"),
302 new AExpression('$t.send-time > datetime("' + parameters["startdt"] + '")'),
303 new AExpression('$t.send-time < datetime("' + parameters["enddt"] + '")')
304 );
305 }
306
307 // Finally, we'll group our results into spatial cells.
308 aql = aql.GroupClause(
309 "$c",
Zachary Heilbrond98423c2014-03-06 17:37:28 -0800310 new FunctionExpression("spatial-cell", "$t.sender-location",
311 new FunctionExpression("create-point", "24.5", "-125.5"),
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700312 parameters["gridlat"].toFixed(1), parameters["gridlng"].toFixed(1)),
Zachary Heilbrond98423c2014-03-06 17:37:28 -0800313 "with",
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700314 "$t"
Eugenia Gabrielovafcd28682013-10-20 05:36:36 -0700315 );
316
317 // ...and return a resulting cell and a count of results in that cell.
318 aql = aql.ReturnClause({ "cell" : "$c", "count" : "count($t)" });
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700319
320 return aql;
321}
322
Eugenia Gabrielovad07556a2013-10-11 03:45:06 -0700323/**
Eugenia Gabrielovad07556a2013-10-11 03:45:06 -0700324* getAllDataverseTweetbooks
Zachary Heilbrond98423c2014-03-06 17:37:28 -0800325*
Eugenia Gabrielovad07556a2013-10-11 03:45:06 -0700326* Returns all datasets of type TweetbookEntry, populates review_mode_tweetbooks
327*/
328function getAllDataverseTweetbooks(fn_tweetbooks) {
Eugenia Gabrielova12de0302013-10-18 02:25:39 -0700329
330 // This creates a query to the Metadata for datasets of type
331 // TweetBookEntry. Note that if we throw in a WhereClause (commented out below)
332 // there is an odd error. This is being fixed and will be removed from this demo.
333 var getTweetbooksQuery = new FLWOGRExpression()
Eugenia Gabrielovad07556a2013-10-11 03:45:06 -0700334 .ForClause("$ds", new AExpression("dataset Metadata.Dataset"))
Eugenia Gabrielova12de0302013-10-18 02:25:39 -0700335 //.WhereClause(new AExpression('$ds.DataTypeName = "TweetbookEntry"'))
Eugenia Gabrielovad07556a2013-10-11 03:45:06 -0700336 .ReturnClause({
Eugenia Gabrielova12de0302013-10-18 02:25:39 -0700337 "DataTypeName" : "$ds.DataTypeName",
338 "DatasetName" : "$ds.DatasetName"
Eugenia Gabrielovad07556a2013-10-11 03:45:06 -0700339 });
Zachary Heilbrond98423c2014-03-06 17:37:28 -0800340
Eugenia Gabrielova12de0302013-10-18 02:25:39 -0700341 // Now create a function that will be called when tweetbooks succeed.
342 // In this case, we want to parse out the results object from the Asterix
343 // REST API response.
344 var tweetbooksSuccess = function(r) {
Eugenia Gabrielovad07556a2013-10-11 03:45:06 -0700345 // Parse tweetbook metadata results
Eugenia Gabrielova12de0302013-10-18 02:25:39 -0700346 $.each(r.results, function(i, data) {
347 if ($.parseJSON(data)["DataTypeName"] == "TweetbookEntry") {
348 review_mode_tweetbooks.push($.parseJSON(data)["DatasetName"]);
349 }
Eugenia Gabrielovad07556a2013-10-11 03:45:06 -0700350 });
Zachary Heilbrond98423c2014-03-06 17:37:28 -0800351
Eugenia Gabrielova12de0302013-10-18 02:25:39 -0700352 // Now, if any tweetbooks already exist, opulate review screen.
Eugenia Gabrielovad07556a2013-10-11 03:45:06 -0700353 $('#review-tweetbook-titles').html('');
Eugenia Gabrielova12de0302013-10-18 02:25:39 -0700354 $.each(review_mode_tweetbooks, function(i, tweetbook) {
355 addTweetBookDropdownItem(tweetbook);
356 });
357 };
Zachary Heilbrond98423c2014-03-06 17:37:28 -0800358
359 // Now, we are ready to run a query.
Eugenia Gabrielova12de0302013-10-18 02:25:39 -0700360 A.meta(getTweetbooksQuery.val(), tweetbooksSuccess);
Eugenia Gabrielovad07556a2013-10-11 03:45:06 -0700361}
362
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700363/**
364* Checks through each asynchronous query to see if they are ready yet
365*/
366function asynchronousQueryIntervalUpdate() {
367 for (var handle_key in asyncQueryManager) {
Zachary Heilbrond98423c2014-03-06 17:37:28 -0800368 if (!asyncQueryManager[handle_key].hasOwnProperty("ready")) {
369 asynchronousQueryGetAPIQueryStatus( asyncQueryManager[handle_key]["handle"], handle_key );
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700370 }
371 }
372}
373
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700374/**
375* Returns current time interval to check for asynchronous query readiness
376* @returns {number} milliseconds between asychronous query checks
377*/
378function asynchronousQueryGetInterval() {
379 var seconds = 10;
380 return seconds * 1000;
381}
382
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700383/**
384* Retrieves status of an asynchronous query, using an opaque result handle from API
385* @param {Object} handle, an object previously returned from an async call
386* @param {number} handle_id, the integer ID parsed from the handle object
387*/
388function asynchronousQueryGetAPIQueryStatus (handle, handle_id) {
389
Zachary Heilbrond98423c2014-03-06 17:37:28 -0800390 A.query_status(
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700391 {
392 "handle" : JSON.stringify(handle)
393 },
394 function (res) {
395 if (res["status"] == "SUCCESS") {
396 // We don't need to check if this one is ready again, it's not going anywhere...
397 // Unless the life cycle of handles has changed drastically
398 asyncQueryManager[handle_id]["ready"] = true;
Zachary Heilbrond98423c2014-03-06 17:37:28 -0800399
400 // Indicate success.
genia.likes.science@gmail.com4259a542013-08-18 20:06:58 -0700401 $('#handle_' + handle_id).removeClass("btn-disabled").prop('disabled', false).addClass("btn-success");
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700402 }
Zachary Heilbrond98423c2014-03-06 17:37:28 -0800403 }
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700404 );
405}
406
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700407/**
408* On-success callback after async API query
409* @param {object} res, a result object containing an opaque result handle to Asterix
410*/
411function cherryQueryAsyncCallback(res) {
Zachary Heilbrond98423c2014-03-06 17:37:28 -0800412
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700413 // Parse handle, handle id and query from async call result
genia.likes.science@gmail.com8125bd92013-08-21 18:04:27 -0700414 var handle_query = APIqueryTracker["query"];
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700415 var handle = res;
Zachary Heilbrond98423c2014-03-06 17:37:28 -0800416 var handle_id = res["handle"].toString().split(',')[0];
417
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700418 // Add to stored map of existing handles
419 asyncQueryManager[handle_id] = {
420 "handle" : handle,
Eugenia Gabrielovae11965b2013-10-29 19:07:42 -0700421 "query" : handle_query, // This will show up when query control button is clicked.
genia.likes.science@gmail.com8125bd92013-08-21 18:04:27 -0700422 "data" : APIqueryTracker["data"]
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700423 };
Zachary Heilbrond98423c2014-03-06 17:37:28 -0800424
425 // Create a container for this async query handle
genia.likes.science@gmail.com4259a542013-08-18 20:06:58 -0700426 $('<div/>')
427 .css("margin-left", "1em")
428 .css("margin-bottom", "1em")
429 .css("display", "block")
430 .attr({
431 "class" : "btn-group",
432 "id" : "async_container_" + handle_id
433 })
434 .appendTo("#async-handle-controls");
Zachary Heilbrond98423c2014-03-06 17:37:28 -0800435
genia.likes.science@gmail.com4259a542013-08-18 20:06:58 -0700436 // Adds the main button for this async handle
437 var handle_action_button = '<button class="btn btn-disabled" id="handle_' + handle_id + '">Handle ' + handle_id + '</button>';
438 $('#async_container_' + handle_id).append(handle_action_button);
439 $('#handle_' + handle_id).prop('disabled', true);
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700440 $('#handle_' + handle_id).on('click', function (e) {
genia.likes.science@gmail.com4259a542013-08-18 20:06:58 -0700441
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700442 // make sure query is ready to be run
443 if (asyncQueryManager[handle_id]["ready"]) {
Zachary Heilbrond98423c2014-03-06 17:37:28 -0800444
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700445 APIqueryTracker = {
446 "query" : asyncQueryManager[handle_id]["query"],
447 "data" : asyncQueryManager[handle_id]["data"]
448 };
Zachary Heilbrond98423c2014-03-06 17:37:28 -0800449
genia.likes.science@gmail.com84711b82013-08-22 03:47:41 -0700450 if (!asyncQueryManager[handle_id].hasOwnProperty("result")) {
451 // Generate new Asterix Core API Query
452 A.query_result(
453 { "handle" : JSON.stringify(asyncQueryManager[handle_id]["handle"]) },
454 function(res) {
455 asyncQueryManager[handle_id]["result"] = res;
Zachary Heilbrond98423c2014-03-06 17:37:28 -0800456
Eugenia Gabrielovafcd28682013-10-20 05:36:36 -0700457 var resultTransform = {
458 "results" : res.results[0]
459 };
Zachary Heilbrond98423c2014-03-06 17:37:28 -0800460
Eugenia Gabrielovafcd28682013-10-20 05:36:36 -0700461 cherryQuerySyncCallback(resultTransform);
genia.likes.science@gmail.com84711b82013-08-22 03:47:41 -0700462 }
463 );
464 } else {
Zachary Heilbrond98423c2014-03-06 17:37:28 -0800465
Eugenia Gabrielovafcd28682013-10-20 05:36:36 -0700466 var resultTransform = {
467 "results" : asyncQueryManager[handle_id]["result"].results[0]
468 };
Zachary Heilbrond98423c2014-03-06 17:37:28 -0800469
Eugenia Gabrielovafcd28682013-10-20 05:36:36 -0700470 cherryQuerySyncCallback(resultTransform);
genia.likes.science@gmail.com84711b82013-08-22 03:47:41 -0700471 }
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700472 }
473 });
Zachary Heilbrond98423c2014-03-06 17:37:28 -0800474
genia.likes.science@gmail.com4259a542013-08-18 20:06:58 -0700475 // Adds a removal button for this async handle
genia.likes.science@gmail.com8125bd92013-08-21 18:04:27 -0700476 var asyncDeleteButton = addDeleteButton(
477 "trashhandle_" + handle_id,
478 "async_container_" + handle_id,
479 function (e) {
480 $('#async_container_' + handle_id).remove();
481 delete asyncQueryManager[handle_id];
482 }
483 );
Zachary Heilbrond98423c2014-03-06 17:37:28 -0800484
Eugenia Gabrielovad07556a2013-10-11 03:45:06 -0700485 $('#async_container_' + handle_id).append('<br/>');
Zachary Heilbrond98423c2014-03-06 17:37:28 -0800486
genia.likes.science@gmail.com8125bd92013-08-21 18:04:27 -0700487 $("#submit-button").attr("disabled", false);
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700488}
489
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700490/**
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700491* A spatial data cleaning and mapping call
492* @param {Object} res, a result object from a cherry geospatial query
493*/
494function cherryQuerySyncCallback(res) {
Zachary Heilbrond98423c2014-03-06 17:37:28 -0800495
Eugenia Gabrielovae11965b2013-10-29 19:07:42 -0700496 // First, we check if any results came back in.
497 // If they didn't, return.
498 if (!res.hasOwnProperty("results")) {
499 reportUserMessage("Oops, no results found for those parameters.", false, "report-message");
500 return;
501 }
Zachary Heilbrond98423c2014-03-06 17:37:28 -0800502
Eugenia Gabrielova12de0302013-10-18 02:25:39 -0700503 // Initialize coordinates and weights, to store
504 // coordinates of map cells and their weights
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700505 var coordinates = [];
Eugenia Gabrielova8b34c652013-10-19 06:53:27 -0700506 var maxWeight = 0;
Eugenia Gabrielovad6e88e02013-10-19 08:26:54 -0700507 var minWeight = Number.MAX_VALUE;
Eugenia Gabrielovaf9fcd712013-10-20 02:37:35 -0700508
Eugenia Gabrielova12de0302013-10-18 02:25:39 -0700509 // Parse resulting JSON objects. Here is an example record:
Eugenia Gabrielovacacc9f82013-10-21 14:10:21 -0700510 // { "cell": rectangle("21.5,-98.5 24.5,-95.5"), "count": 78i64 }
Eugenia Gabrielova12de0302013-10-18 02:25:39 -0700511 $.each(res.results, function(i, data) {
Eugenia Gabrielovacacc9f82013-10-21 14:10:21 -0700512
513 // We need to clean the JSON a bit to parse it properly in javascript
514 var cleanRecord = $.parseJSON(data
515 .replace('rectangle(', '')
516 .replace(')', '')
517 .replace('i64', ''));
518
519 var recordCount = cleanRecord["count"];
520 var rectangle = cleanRecord["cell"]
521 .replace(' ', ',')
522 .split(',')
523 .map( parseFloat );
524
Zachary Heilbrond98423c2014-03-06 17:37:28 -0800525 // Now, using the record count and coordinates, we can create a
Eugenia Gabrielovacacc9f82013-10-21 14:10:21 -0700526 // coordinate system for this spatial cell.
Eugenia Gabrielova12de0302013-10-18 02:25:39 -0700527 var coordinate = {
Eugenia Gabrielovacacc9f82013-10-21 14:10:21 -0700528 "latSW" : rectangle[0],
529 "lngSW" : rectangle[1],
530 "latNE" : rectangle[2],
531 "lngNE" : rectangle[3],
532 "weight" : recordCount
533 };
Zachary Heilbrond98423c2014-03-06 17:37:28 -0800534
Eugenia Gabrielovacacc9f82013-10-21 14:10:21 -0700535 // We track the minimum and maximum weight to support our legend.
Eugenia Gabrielova8b34c652013-10-19 06:53:27 -0700536 maxWeight = Math.max(coordinate["weight"], maxWeight);
Eugenia Gabrielovad6e88e02013-10-19 08:26:54 -0700537 minWeight = Math.min(coordinate["weight"], minWeight);
Eugenia Gabrielovacacc9f82013-10-21 14:10:21 -0700538
539 // Save completed coordinate and move to next one.
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700540 coordinates.push(coordinate);
Eugenia Gabrielova12de0302013-10-18 02:25:39 -0700541 });
Eugenia Gabrielovacacc9f82013-10-21 14:10:21 -0700542
Eugenia Gabrielovad6e88e02013-10-19 08:26:54 -0700543 triggerUIUpdate(coordinates, maxWeight, minWeight);
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700544}
545
546/**
547* Triggers a map update based on a set of spatial query result cells
548* @param [Array] mapPlotData, an array of coordinate and weight objects
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700549* @param [Array] plotWeights, a list of weights of the spatial cells - e.g., number of tweets
550*/
Eugenia Gabrielovad6e88e02013-10-19 08:26:54 -0700551function triggerUIUpdate(mapPlotData, maxWeight, minWeight) {
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700552 /** Clear anything currently on the map **/
553 mapWidgetClearMap();
Zachary Heilbrond98423c2014-03-06 17:37:28 -0800554
Eugenia Gabrielova8b34c652013-10-19 06:53:27 -0700555 // Initialize info windows.
genia.likes.science@gmail.com65e04182013-10-04 04:31:41 -0700556 map_info_windows = {};
Zachary Heilbrond98423c2014-03-06 17:37:28 -0800557
Eugenia Gabrielova8b34c652013-10-19 06:53:27 -0700558 $.each(mapPlotData, function (m) {
Zachary Heilbrond98423c2014-03-06 17:37:28 -0800559
Eugenia Gabrielova8b34c652013-10-19 06:53:27 -0700560 var point_center = new google.maps.LatLng(
Zachary Heilbrond98423c2014-03-06 17:37:28 -0800561 (mapPlotData[m].latSW + mapPlotData[m].latNE)/2.0,
Eugenia Gabrielova8b34c652013-10-19 06:53:27 -0700562 (mapPlotData[m].lngSW + mapPlotData[m].lngNE)/2.0);
genia.likes.science@gmail.com1b30f3d2013-08-17 23:53:37 -0700563
Eugenia Gabrielova8b34c652013-10-19 06:53:27 -0700564 var map_circle_options = {
565 center: point_center,
566 anchorPoint: point_center,
567 radius: mapWidgetComputeCircleRadius(mapPlotData[m], maxWeight),
568 map: map,
569 fillOpacity: 0.85,
Zachary Heilbron046724c2014-03-10 13:42:23 -0700570 fillColor: "#" + rainbow.colourAt(Math.ceil(100 * (mapPlotData[m].weight / maxWeight))),
Eugenia Gabrielova8b34c652013-10-19 06:53:27 -0700571 clickable: true
572 };
573 var map_circle = new google.maps.Circle(map_circle_options);
574 map_circle.val = mapPlotData[m];
Zachary Heilbrond98423c2014-03-06 17:37:28 -0800575
Eugenia Gabrielova8b34c652013-10-19 06:53:27 -0700576 map_info_windows[m] = new google.maps.InfoWindow({
577 content: mapPlotData[m].weight + " tweets",
578 position: point_center
579 });
580
581 // Clicking on a circle drills down map to that value, hovering over it displays a count
582 // of tweets at that location.
583 google.maps.event.addListener(map_circle, 'click', function (event) {
584 $.each(map_info_windows, function(i) {
585 map_info_windows[i].close();
genia.likes.science@gmail.comec46c772013-09-07 18:13:00 -0700586 });
Eugenia Gabrielova8b34c652013-10-19 06:53:27 -0700587 onMapPointDrillDown(map_circle.val);
588 });
Zachary Heilbrond98423c2014-03-06 17:37:28 -0800589
Eugenia Gabrielova8b34c652013-10-19 06:53:27 -0700590 google.maps.event.addListener(map_circle, 'mouseover', function(event) {
591 if (!map_info_windows[m].getMap()) {
592 map_info_windows[m].setPosition(map_circle.center);
593 map_info_windows[m].open(map);
594 }
595 });
Zachary Heilbrond98423c2014-03-06 17:37:28 -0800596 google.maps.event.addListener(map, 'mousemove', function(event) {
597 map_info_windows[m].close();
Zachary Heilbron046724c2014-03-10 13:42:23 -0700598
Zachary Heilbrond98423c2014-03-06 17:37:28 -0800599 });
600
Eugenia Gabrielova8b34c652013-10-19 06:53:27 -0700601 // Add this marker to global marker cells
Eugenia Gabrielovad6e88e02013-10-19 08:26:54 -0700602 map_cells.push(map_circle);
Zachary Heilbrond98423c2014-03-06 17:37:28 -0800603
Eugenia Gabrielovad6e88e02013-10-19 08:26:54 -0700604 // Show legend
605 $("#legend-min").html(minWeight);
606 $("#legend-max").html(maxWeight);
Zachary Heilbrond98423c2014-03-06 17:37:28 -0800607 $("#rainbow-legend-container").show();
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700608 });
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700609}
610
611/**
612* prepares an Asterix API query to drill down in a rectangular spatial zone
613*
Eugenia Gabrielovab2457982013-10-21 14:36:09 -0700614* @params {object} marker_borders a set of bounds for a region from a previous api result
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700615*/
616function onMapPointDrillDown(marker_borders) {
Eugenia Gabrielovaf29a55f2013-10-28 03:18:18 -0700617
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700618 var zoneData = APIqueryTracker["data"];
Zachary Heilbrond98423c2014-03-06 17:37:28 -0800619
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700620 var zswBounds = new google.maps.LatLng(marker_borders.latSW, marker_borders.lngSW);
621 var zneBounds = new google.maps.LatLng(marker_borders.latNE, marker_borders.lngNE);
Zachary Heilbrond98423c2014-03-06 17:37:28 -0800622
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700623 var zoneBounds = new google.maps.LatLngBounds(zswBounds, zneBounds);
624 zoneData["swLat"] = zoneBounds.getSouthWest().lat();
625 zoneData["swLng"] = zoneBounds.getSouthWest().lng();
626 zoneData["neLat"] = zoneBounds.getNorthEast().lat();
627 zoneData["neLng"] = zoneBounds.getNorthEast().lng();
628 var zB = {
629 "sw" : {
630 "lat" : zoneBounds.getSouthWest().lat(),
631 "lng" : zoneBounds.getSouthWest().lng()
632 },
633 "ne" : {
634 "lat" : zoneBounds.getNorthEast().lat(),
635 "lng" : zoneBounds.getNorthEast().lng()
636 }
637 };
Zachary Heilbrond98423c2014-03-06 17:37:28 -0800638
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700639 mapWidgetClearMap();
Zachary Heilbrond98423c2014-03-06 17:37:28 -0800640
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700641 var customBounds = new google.maps.LatLngBounds();
642 var zoomSWBounds = new google.maps.LatLng(zoneData["swLat"], zoneData["swLng"]);
Zachary Heilbrond98423c2014-03-06 17:37:28 -0800643 var zoomNEBounds = new google.maps.LatLng(zoneData["neLat"], zoneData["neLng"]);
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700644 customBounds.extend(zoomSWBounds);
645 customBounds.extend(zoomNEBounds);
646 map.fitBounds(customBounds);
Zachary Heilbrond98423c2014-03-06 17:37:28 -0800647
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700648 var df = getDrillDownQuery(zoneData, zB);
649
genia.likes.science@gmail.com8125bd92013-08-21 18:04:27 -0700650 APIqueryTracker = {
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700651 "query_string" : "use dataverse twitter;\n" + df.val(),
Eugenia Gabrielovafcd28682013-10-20 05:36:36 -0700652 "marker_path" : "static/img/mobile2.png"
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700653 };
Zachary Heilbrond98423c2014-03-06 17:37:28 -0800654
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700655 A.query(df.val(), onTweetbookQuerySuccessPlot);
656}
657
Eugenia Gabrielovab2457982013-10-21 14:36:09 -0700658/**
659* Generates an aql query for zooming on a spatial cell and obtaining tweets contained therein.
660* @param parameters, the original query parameters
661* @param bounds, the bounds of the zone to zoom in on.
662*/
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700663function getDrillDownQuery(parameters, bounds) {
664
665 var zoomRectangle = new FunctionExpression("create-rectangle",
666 new FunctionExpression("create-point", bounds["sw"]["lat"], bounds["sw"]["lng"]),
667 new FunctionExpression("create-point", bounds["ne"]["lat"], bounds["ne"]["lng"]));
Zachary Heilbrond98423c2014-03-06 17:37:28 -0800668
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700669 var drillDown = new FLWOGRExpression()
genia.likes.science@gmail.com1b30f3d2013-08-17 23:53:37 -0700670 .ForClause("$t", new AExpression("dataset TweetMessagesShifted"))
Eugenia Gabrielovafcd28682013-10-20 05:36:36 -0700671 .LetClause("$region", zoomRectangle);
672
673 if (parameters["keyword"].length == 0) {
674 drillDown = drillDown
675 .WhereClause().and(
676 new FunctionExpression('spatial-intersect', '$t.sender-location', '$region'),
677 new AExpression().set('$t.send-time > datetime("' + parameters["startdt"] + '")'),
678 new AExpression().set('$t.send-time < datetime("' + parameters["enddt"] + '")')
679 );
680 } else {
681 drillDown = drillDown
682 .LetClause("$keyword", new AExpression('"' + parameters["keyword"] + '"'))
683 .WhereClause().and(
684 new FunctionExpression('spatial-intersect', '$t.sender-location', '$region'),
685 new AExpression().set('$t.send-time > datetime("' + parameters["startdt"] + '")'),
686 new AExpression().set('$t.send-time < datetime("' + parameters["enddt"] + '")'),
687 new FunctionExpression('contains', '$t.message-text', '$keyword')
Zachary Heilbrond98423c2014-03-06 17:37:28 -0800688 );
Eugenia Gabrielovafcd28682013-10-20 05:36:36 -0700689 }
690
691 drillDown = drillDown
692 .ReturnClause({
Zachary Heilbrond98423c2014-03-06 17:37:28 -0800693 "tweetId" : "$t.tweetid",
Eugenia Gabrielovafcd28682013-10-20 05:36:36 -0700694 "tweetText" : "$t.message-text",
695 "tweetLoc" : "$t.sender-location"
696 });
697
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700698 return drillDown;
699}
700
Eugenia Gabrielovab2457982013-10-21 14:36:09 -0700701/**
702* Given a location where a tweet exists, opens a modal to examine or update a tweet's content.
703* @param t0, a tweetobject that has a location, text, id, and optionally a comment.
704*/
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700705function onDrillDownAtLocation(tO) {
706
707 var tweetId = tO["tweetEntryId"];
708 var tweetText = tO["tweetText"];
Zachary Heilbrond98423c2014-03-06 17:37:28 -0800709
genia.likes.science@gmail.com0f67d9e2013-10-04 17:08:50 -0700710 // First, set tweet in drilldown modal to be this tweet's text
711 $('#modal-body-tweet').html('Tweet #' + tweetId + ": " + tweetText);
Zachary Heilbrond98423c2014-03-06 17:37:28 -0800712
Eugenia Gabrielovad07556a2013-10-11 03:45:06 -0700713 // Next, empty any leftover tweetbook comments or error/success messages
genia.likes.science@gmail.com0f67d9e2013-10-04 17:08:50 -0700714 $("#modal-body-add-to").val('');
715 $("#modal-body-add-note").val('');
Eugenia Gabrielovad07556a2013-10-11 03:45:06 -0700716 $("#modal-body-message-holder").html("");
Zachary Heilbrond98423c2014-03-06 17:37:28 -0800717
genia.likes.science@gmail.com0f67d9e2013-10-04 17:08:50 -0700718 // Next, if there is an existing tweetcomment reported, show it.
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700719 if (tO.hasOwnProperty("tweetComment")) {
Zachary Heilbrond98423c2014-03-06 17:37:28 -0800720
Eugenia Gabrielovad07556a2013-10-11 03:45:06 -0700721 // Show correct panel
genia.likes.science@gmail.com0f67d9e2013-10-04 17:08:50 -0700722 $("#modal-existing-note").show();
Eugenia Gabrielovad07556a2013-10-11 03:45:06 -0700723 $("#modal-save-tweet-panel").hide();
Zachary Heilbrond98423c2014-03-06 17:37:28 -0800724
Eugenia Gabrielovad07556a2013-10-11 03:45:06 -0700725 // Fill in existing tweet comment
genia.likes.science@gmail.com0f67d9e2013-10-04 17:08:50 -0700726 $("#modal-body-tweet-note").val(tO["tweetComment"]);
Zachary Heilbrond98423c2014-03-06 17:37:28 -0800727
genia.likes.science@gmail.com0f67d9e2013-10-04 17:08:50 -0700728 // Change Tweetbook Badge
729 $("#modal-current-tweetbook").val(APIqueryTracker["active_tweetbook"]);
Zachary Heilbrond98423c2014-03-06 17:37:28 -0800730
genia.likes.science@gmail.com0f67d9e2013-10-04 17:08:50 -0700731 // Add deletion functionality
732 $("#modal-body-trash-icon").on('click', function () {
Zachary Heilbrond98423c2014-03-06 17:37:28 -0800733 // Send comment deletion to asterix
genia.likes.science@gmail.com0f67d9e2013-10-04 17:08:50 -0700734 var deleteTweetCommentOnId = '"' + tweetId + '"';
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700735 var toDelete = new DeleteStatement(
736 "$mt",
genia.likes.science@gmail.com0f67d9e2013-10-04 17:08:50 -0700737 APIqueryTracker["active_tweetbook"],
738 new AExpression("$mt.tweetid = " + deleteTweetCommentOnId.toString())
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700739 );
genia.likes.science@gmail.com0f67d9e2013-10-04 17:08:50 -0700740 A.update(
741 toDelete.val()
742 );
Zachary Heilbrond98423c2014-03-06 17:37:28 -0800743
genia.likes.science@gmail.com0f67d9e2013-10-04 17:08:50 -0700744 // Hide comment from map
745 $('#drilldown_modal').modal('hide');
Zachary Heilbrond98423c2014-03-06 17:37:28 -0800746
genia.likes.science@gmail.com0f67d9e2013-10-04 17:08:50 -0700747 // Replot tweetbook
748 onPlotTweetbook(APIqueryTracker["active_tweetbook"]);
749 });
Zachary Heilbrond98423c2014-03-06 17:37:28 -0800750
genia.likes.science@gmail.com0f67d9e2013-10-04 17:08:50 -0700751 } else {
Eugenia Gabrielovad07556a2013-10-11 03:45:06 -0700752 // Show correct panel
genia.likes.science@gmail.com0f67d9e2013-10-04 17:08:50 -0700753 $("#modal-existing-note").hide();
Eugenia Gabrielovad07556a2013-10-11 03:45:06 -0700754 $("#modal-save-tweet-panel").show();
Zachary Heilbrond98423c2014-03-06 17:37:28 -0800755
Eugenia Gabrielovad07556a2013-10-11 03:45:06 -0700756 // Now, when adding a comment on an available tweet to a tweetbook
Eugenia Gabrielovadbd50a42013-10-19 00:30:27 -0700757 $('#save-comment-tweetbook-modal').unbind('click');
Eugenia Gabrielovad07556a2013-10-11 03:45:06 -0700758 $("#save-comment-tweetbook-modal").on('click', function(e) {
Zachary Heilbrond98423c2014-03-06 17:37:28 -0800759
Eugenia Gabrielovad07556a2013-10-11 03:45:06 -0700760 // Stuff to save about new comment
761 var save_metacomment_target_tweetbook = $("#modal-body-add-to").val();
762 var save_metacomment_target_comment = '"' + $("#modal-body-add-note").val() + '"';
763 var save_metacomment_target_tweet = '"' + tweetId + '"';
Zachary Heilbrond98423c2014-03-06 17:37:28 -0800764
Eugenia Gabrielovad07556a2013-10-11 03:45:06 -0700765 // Make sure content is entered, and then save this comment.
Eugenia Gabrielova12de0302013-10-18 02:25:39 -0700766 if ($("#modal-body-add-note").val() == "") {
Eugenia Gabrielovadbd50a42013-10-19 00:30:27 -0700767
768 reportUserMessage("Please enter a comment about the tweet", false, "report-message");
Zachary Heilbrond98423c2014-03-06 17:37:28 -0800769
Eugenia Gabrielova12de0302013-10-18 02:25:39 -0700770 } else if ($("#modal-body-add-to").val() == "") {
Zachary Heilbrond98423c2014-03-06 17:37:28 -0800771
Eugenia Gabrielovadbd50a42013-10-19 00:30:27 -0700772 reportUserMessage("Please enter a tweetbook.", false, "report-message");
Zachary Heilbrond98423c2014-03-06 17:37:28 -0800773
genia.likes.science@gmail.com0f67d9e2013-10-04 17:08:50 -0700774 } else {
Zachary Heilbrond98423c2014-03-06 17:37:28 -0800775
Eugenia Gabrielovad07556a2013-10-11 03:45:06 -0700776 // Check if tweetbook exists. If not, create it.
777 if (!(existsTweetbook(save_metacomment_target_tweetbook))) {
778 onCreateNewTweetBook(save_metacomment_target_tweetbook);
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700779 }
Zachary Heilbrond98423c2014-03-06 17:37:28 -0800780
Eugenia Gabrielovad07556a2013-10-11 03:45:06 -0700781 var toInsert = new InsertStatement(
782 save_metacomment_target_tweetbook,
Zachary Heilbrond98423c2014-03-06 17:37:28 -0800783 {
784 "tweetid" : save_metacomment_target_tweet.toString(),
785 "comment-text" : save_metacomment_target_comment
Eugenia Gabrielovad07556a2013-10-11 03:45:06 -0700786 }
787 );
Zachary Heilbrond98423c2014-03-06 17:37:28 -0800788
Eugenia Gabrielovadbd50a42013-10-19 00:30:27 -0700789 A.update(toInsert.val(), function () {
Zachary Heilbrond98423c2014-03-06 17:37:28 -0800790 var successMessage = "Saved comment on <b>Tweet #" + tweetId +
Eugenia Gabrielovadbd50a42013-10-19 00:30:27 -0700791 "</b> in dataset <b>" + save_metacomment_target_tweetbook + "</b>.";
792 reportUserMessage(successMessage, true, "report-message");
Zachary Heilbrond98423c2014-03-06 17:37:28 -0800793
Eugenia Gabrielovadbd50a42013-10-19 00:30:27 -0700794 $("#modal-body-add-to").val('');
795 $("#modal-body-add-note").val('');
796 $('#save-comment-tweetbook-modal').unbind('click');
Zachary Heilbrond98423c2014-03-06 17:37:28 -0800797
Eugenia Gabrielovadbd50a42013-10-19 00:30:27 -0700798 // Close modal
799 $('#drilldown_modal').modal('hide');
800 });
Zachary Heilbrond98423c2014-03-06 17:37:28 -0800801 }
Eugenia Gabrielovad07556a2013-10-11 03:45:06 -0700802 });
803 }
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700804}
805
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700806/**
807* Adds a new tweetbook entry to the menu and creates a dataset of type TweetbookEntry.
808*/
809function onCreateNewTweetBook(tweetbook_title) {
Zachary Heilbrond98423c2014-03-06 17:37:28 -0800810
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700811 var tweetbook_title = tweetbook_title.split(' ').join('_');
812
813 A.ddl(
814 "create dataset " + tweetbook_title + "(TweetbookEntry) primary key tweetid;",
815 function () {}
816 );
Zachary Heilbrond98423c2014-03-06 17:37:28 -0800817
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700818 if (!(existsTweetbook(tweetbook_title))) {
819 review_mode_tweetbooks.push(tweetbook_title);
820 addTweetBookDropdownItem(tweetbook_title);
821 }
822}
823
Eugenia Gabrielovab2457982013-10-21 14:36:09 -0700824/**
825* Removes a tweetbook from both demo and from
826* dataverse metadata.
827*/
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700828function onDropTweetBook(tweetbook_title) {
829
830 // AQL Call
831 A.ddl(
832 "drop dataset " + tweetbook_title + " if exists;",
833 function () {}
834 );
Zachary Heilbrond98423c2014-03-06 17:37:28 -0800835
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700836 // Removes tweetbook from review_mode_tweetbooks
837 var remove_position = $.inArray(tweetbook_title, review_mode_tweetbooks);
838 if (remove_position >= 0) review_mode_tweetbooks.splice(remove_position, 1);
Zachary Heilbrond98423c2014-03-06 17:37:28 -0800839
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700840 // Clear UI with review tweetbook titles
841 $('#review-tweetbook-titles').html('');
842 for (r in review_mode_tweetbooks) {
843 addTweetBookDropdownItem(review_mode_tweetbooks[r]);
844 }
845}
846
Eugenia Gabrielovab2457982013-10-21 14:36:09 -0700847/**
848* Adds a tweetbook action button to the dropdown box in review mode.
849* @param tweetbook, a string representing a tweetbook
850*/
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700851function addTweetBookDropdownItem(tweetbook) {
genia.likes.science@gmail.com2de4c182013-10-04 11:39:11 -0700852 // Add placeholder for this tweetbook
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700853 $('<div/>')
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700854 .attr({
855 "class" : "btn-group",
856 "id" : "rm_holder_" + tweetbook
857 }).appendTo("#review-tweetbook-titles");
Zachary Heilbrond98423c2014-03-06 17:37:28 -0800858
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700859 // Add plotting button for this tweetbook
genia.likes.science@gmail.com2de4c182013-10-04 11:39:11 -0700860 var plot_button = '<button class="btn btn-default" id="rm_plotbook_' + tweetbook + '">' + tweetbook + '</button>';
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700861 $("#rm_holder_" + tweetbook).append(plot_button);
genia.likes.science@gmail.com2de4c182013-10-04 11:39:11 -0700862 $("#rm_plotbook_" + tweetbook).width("200px");
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700863 $("#rm_plotbook_" + tweetbook).on('click', function(e) {
864 onPlotTweetbook(tweetbook);
865 });
Zachary Heilbrond98423c2014-03-06 17:37:28 -0800866
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700867 // Add trash button for this tweetbook
genia.likes.science@gmail.com8125bd92013-08-21 18:04:27 -0700868 var onTrashTweetbookButton = addDeleteButton(
869 "rm_trashbook_" + tweetbook,
870 "rm_holder_" + tweetbook,
871 function(e) {
872 onDropTweetBook(tweetbook);
873 }
874 );
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700875}
876
Eugenia Gabrielovab2457982013-10-21 14:36:09 -0700877/**
878* Generates AsterixDB query to plot existing tweetbook commnets
879* @param tweetbook, a string representing a tweetbook
880*/
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700881function onPlotTweetbook(tweetbook) {
Zachary Heilbrond98423c2014-03-06 17:37:28 -0800882
genia.likes.science@gmail.com8125bd92013-08-21 18:04:27 -0700883 // Clear map for this one
Eugenia Gabrielovae11965b2013-10-29 19:07:42 -0700884 mapWidgetClearMap();
genia.likes.science@gmail.com8125bd92013-08-21 18:04:27 -0700885
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700886 var plotTweetQuery = new FLWOGRExpression()
genia.likes.science@gmail.com1b30f3d2013-08-17 23:53:37 -0700887 .ForClause("$t", new AExpression("dataset TweetMessagesShifted"))
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700888 .ForClause("$m", new AExpression("dataset " + tweetbook))
889 .WhereClause(new AExpression("$m.tweetid = $t.tweetid"))
890 .ReturnClause({
891 "tweetId" : "$m.tweetid",
892 "tweetText" : "$t.message-text",
Eugenia Gabrielovacacc9f82013-10-21 14:10:21 -0700893 "tweetCom" : "$m.comment-text",
894 "tweetLoc" : "$t.sender-location"
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700895 });
Zachary Heilbrond98423c2014-03-06 17:37:28 -0800896
genia.likes.science@gmail.com8125bd92013-08-21 18:04:27 -0700897 APIqueryTracker = {
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700898 "query_string" : "use dataverse twitter;\n" + plotTweetQuery.val(),
genia.likes.science@gmail.com4833b412013-08-09 06:20:58 -0700899 "marker_path" : "static/img/mobile_green2.png",
genia.likes.science@gmail.com8125bd92013-08-21 18:04:27 -0700900 "active_tweetbook" : tweetbook
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700901 };
Zachary Heilbrond98423c2014-03-06 17:37:28 -0800902
903 A.query(plotTweetQuery.val(), onTweetbookQuerySuccessPlot);
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700904}
905
Eugenia Gabrielovab2457982013-10-21 14:36:09 -0700906/**
Zachary Heilbrond98423c2014-03-06 17:37:28 -0800907* Given an output response set of tweet data,
Eugenia Gabrielovab2457982013-10-21 14:36:09 -0700908* prepares markers on map to represent individual tweets.
909* @param res, a JSON Object
910*/
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700911function onTweetbookQuerySuccessPlot (res) {
912
Eugenia Gabrielovae11965b2013-10-29 19:07:42 -0700913 // First, we check if any results came back in.
914 // If they didn't, return.
915 if (!res.hasOwnProperty("results")) {
916 reportUserMessage("Oops, no data matches this query.", false, "report-message");
917 return;
918 }
919
Eugenia Gabrielovafcd28682013-10-20 05:36:36 -0700920 // Parse out tweet Ids, texts, and locations
921 var tweets = [];
Eugenia Gabrielovacacc9f82013-10-21 14:10:21 -0700922 var al = 1;
923
Eugenia Gabrielovafcd28682013-10-20 05:36:36 -0700924 $.each(res.results, function(i, data) {
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700925
Eugenia Gabrielovacacc9f82013-10-21 14:10:21 -0700926 // First, clean up the data
927 //{ "tweetId": "100293", "tweetText": " like at&t the touch-screen is amazing", "tweetLoc": point("31.59,-84.23") }
928 // We need to turn the point object at the end into a string
929 var json = $.parseJSON(data
930 .replace(': point(',': ')
931 .replace(') }', ' }'));
Eugenia Gabrielovafcd28682013-10-20 05:36:36 -0700932
Eugenia Gabrielovacacc9f82013-10-21 14:10:21 -0700933 // Now, we construct a tweet object
Eugenia Gabrielovafcd28682013-10-20 05:36:36 -0700934 var tweetData = {
Eugenia Gabrielovacacc9f82013-10-21 14:10:21 -0700935 "tweetEntryId" : parseInt(json.tweetId),
936 "tweetText" : json.tweetText,
937 "tweetLat" : json.tweetLoc.split(",")[0],
938 "tweetLng" : json.tweetLoc.split(",")[1]
Eugenia Gabrielovafcd28682013-10-20 05:36:36 -0700939 };
940
941 // If we are parsing out tweetbook data with comments, we need to check
942 // for those here as well.
943 if (json.hasOwnProperty("tweetCom")) {
Eugenia Gabrielovacacc9f82013-10-21 14:10:21 -0700944 tweetData["tweetComment"] = json.tweetCom;
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700945 }
Eugenia Gabrielovafcd28682013-10-20 05:36:36 -0700946
947 tweets.push(tweetData)
948 });
949
Eugenia Gabrielovafcd28682013-10-20 05:36:36 -0700950 // Create a marker for each tweet
951 $.each(tweets, function(i, t) {
952 // Create a phone marker at tweet's position
953 var map_tweet_m = new google.maps.Marker({
954 position: new google.maps.LatLng(tweets[i]["tweetLat"], tweets[i]["tweetLng"]),
955 map: map,
Eugenia Gabrielovacacc9f82013-10-21 14:10:21 -0700956 icon: APIqueryTracker["marker_path"],
Eugenia Gabrielovafcd28682013-10-20 05:36:36 -0700957 clickable: true,
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700958 });
Eugenia Gabrielovafcd28682013-10-20 05:36:36 -0700959 map_tweet_m["test"] = t;
960
961 // Open Tweet exploration window on click
Eugenia Gabrielovafcd28682013-10-20 05:36:36 -0700962 google.maps.event.addListener(map_tweet_m, 'click', function (event) {
963 onClickTweetbookMapMarker(map_tweet_markers[i]["test"]);
964 });
965
966 // Add marker to index of tweets
Zachary Heilbrond98423c2014-03-06 17:37:28 -0800967 map_tweet_markers.push(map_tweet_m);
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700968 });
969}
970
Eugenia Gabrielovab2457982013-10-21 14:36:09 -0700971/**
972* Checks if a tweetbook exists
973* @param tweetbook, a String
974*/
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700975function existsTweetbook(tweetbook) {
976 if (parseInt($.inArray(tweetbook, review_mode_tweetbooks)) == -1) {
977 return false;
978 } else {
979 return true;
980 }
981}
982
Eugenia Gabrielovafcd28682013-10-20 05:36:36 -0700983/**
984* When a marker is clicked on in the tweetbook, it will launch a modal
985* view to examine or edit the appropriate tweet
986*/
987function onClickTweetbookMapMarker(t) {
988 onDrillDownAtLocation(t)
genia.likes.science@gmail.com1400a752013-10-04 04:59:12 -0700989 $('#drilldown_modal').modal();
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700990}
991
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700992/**
993* Explore mode: Initial map creation and screen alignment
994*/
995function onOpenExploreMap () {
genia.likes.science@gmail.com724476d2013-10-04 03:31:16 -0700996 var explore_column_height = $('#explore-well').height();
Zachary Heilbrond98423c2014-03-06 17:37:28 -0800997 var right_column_width = $('#right-col').width();
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -0700998 $('#map_canvas').height(explore_column_height + "px");
genia.likes.science@gmail.com724476d2013-10-04 03:31:16 -0700999 $('#map_canvas').width(right_column_width + "px");
Zachary Heilbrond98423c2014-03-06 17:37:28 -08001000
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -07001001 $('#review-well').height(explore_column_height + "px");
Zachary Heilbrond98423c2014-03-06 17:37:28 -08001002 $('#review-well').css('max-height', explore_column_height + "px");
genia.likes.science@gmail.com724476d2013-10-04 03:31:16 -07001003 $('#right-col').height(explore_column_height + "px");
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -07001004}
1005
1006/**
Eugenia Gabrielovaab1ae552013-10-20 01:12:19 -07001007* initializes demo - adds some extra events when review/explore
1008* mode are clicked, initializes tabs, aligns map box, moves
1009* active tab to about tab
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -07001010*/
Eugenia Gabrielovaab1ae552013-10-20 01:12:19 -07001011function initDemoPrepareTabs() {
genia.likes.science@gmail.com2de4c182013-10-04 11:39:11 -07001012
Eugenia Gabrielovaab1ae552013-10-20 01:12:19 -07001013 // Tab behavior for About, Explore, and Demo
1014 $('#mode-tabs a').click(function (e) {
1015 e.preventDefault()
1016 $(this).tab('show')
1017 })
1018
1019 // Explore mode should show explore-mode query-builder UI
1020 $('#explore-mode').click(function(e) {
1021 $('#review-well').hide();
1022 $('#explore-well').show();
Eugenia Gabrielovaea8f9482013-10-28 04:49:18 -07001023 rectangleManager.setMap(map);
1024 rectangleManager.setDrawingMode(google.maps.drawing.OverlayType.RECTANGLE);
Eugenia Gabrielovaab1ae552013-10-20 01:12:19 -07001025 mapWidgetResetMap();
1026 });
Zachary Heilbrond98423c2014-03-06 17:37:28 -08001027
Eugenia Gabrielovaab1ae552013-10-20 01:12:19 -07001028 // Review mode should show review well and hide explore well
1029 $('#review-mode').click(function(e) {
1030 $('#explore-well').hide();
1031 $('#review-well').show();
1032 mapWidgetResetMap();
Eugenia Gabrielovaea8f9482013-10-28 04:49:18 -07001033 rectangleManager.setMap(null);
Eugenia Gabrielovae11965b2013-10-29 19:07:42 -07001034 rectangleManager.setDrawingMode(null);
Eugenia Gabrielovaab1ae552013-10-20 01:12:19 -07001035 });
1036
1037 // Does some alignment necessary for the map canvas
1038 onOpenExploreMap();
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -07001039}
1040
Zachary Heilbrond98423c2014-03-06 17:37:28 -08001041/**
genia.likes.science@gmail.comdd669072013-08-21 22:53:34 -07001042* Creates a delete icon button using default trash icon
genia.likes.science@gmail.com8125bd92013-08-21 18:04:27 -07001043* @param {String} id, id for this element
1044* @param {String} attachTo, id string of an element to which I can attach this button.
1045* @param {Function} onClick, a function to fire when this icon is clicked
1046*/
1047function addDeleteButton(iconId, attachTo, onClick) {
Zachary Heilbrond98423c2014-03-06 17:37:28 -08001048
Eugenia Gabrielovad07556a2013-10-11 03:45:06 -07001049 var trashIcon = '<button class="btn btn-default" id="' + iconId + '"><span class="glyphicon glyphicon-trash"></span></button>';
genia.likes.science@gmail.com8125bd92013-08-21 18:04:27 -07001050 $('#' + attachTo).append(trashIcon);
Zachary Heilbrond98423c2014-03-06 17:37:28 -08001051
Eugenia Gabrielovad07556a2013-10-11 03:45:06 -07001052 // When this trash button is clicked, the function is called.
genia.likes.science@gmail.com8125bd92013-08-21 18:04:27 -07001053 $('#' + iconId).on('click', onClick);
1054}
1055
genia.likes.science@gmail.comdd669072013-08-21 22:53:34 -07001056/**
Eugenia Gabrielovadbd50a42013-10-19 00:30:27 -07001057* Creates a message and attaches it to data management area.
genia.likes.science@gmail.comdd669072013-08-21 22:53:34 -07001058* @param {String} message, a message to post
Eugenia Gabrielovadbd50a42013-10-19 00:30:27 -07001059* @param {Boolean} isPositiveMessage, whether or not this is a positive message.
1060* @param {String} target, the target div to attach this message.
genia.likes.science@gmail.comdd669072013-08-21 22:53:34 -07001061*/
Eugenia Gabrielovadbd50a42013-10-19 00:30:27 -07001062function reportUserMessage(message, isPositiveMessage, target) {
1063 // Clear out any existing messages
Eugenia Gabrielovad07556a2013-10-11 03:45:06 -07001064 $('#' + target).html('');
Zachary Heilbrond98423c2014-03-06 17:37:28 -08001065
Eugenia Gabrielovadbd50a42013-10-19 00:30:27 -07001066 // Select appropriate alert-type
1067 var alertType = "alert-success";
1068 if (!isPositiveMessage) {
1069 alertType = "alert-danger";
1070 }
Zachary Heilbrond98423c2014-03-06 17:37:28 -08001071
Eugenia Gabrielovadbd50a42013-10-19 00:30:27 -07001072 // Append the appropriate message
Eugenia Gabrielovad07556a2013-10-11 03:45:06 -07001073 $('<div/>')
Eugenia Gabrielovadbd50a42013-10-19 00:30:27 -07001074 .attr("class", "alert " + alertType)
Eugenia Gabrielovad07556a2013-10-11 03:45:06 -07001075 .html('<button type="button" class="close" data-dismiss="alert">&times;</button>' + message)
1076 .appendTo('#' + target);
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -07001077}
1078
1079/**
Eugenia Gabrielovad07556a2013-10-11 03:45:06 -07001080* mapWidgetResetMap
1081*
1082* [No Parameters]
1083*
1084* Clears ALL map elements - plotted items, overlays, then resets position
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -07001085*/
genia.likes.science@gmail.com1b30f3d2013-08-17 23:53:37 -07001086function mapWidgetResetMap() {
Zachary Heilbrond98423c2014-03-06 17:37:28 -08001087
genia.likes.science@gmail.com1b30f3d2013-08-17 23:53:37 -07001088 mapWidgetClearMap();
Zachary Heilbrond98423c2014-03-06 17:37:28 -08001089
genia.likes.science@gmail.com1b30f3d2013-08-17 23:53:37 -07001090 // Reset map center and zoom
1091 map.setCenter(new google.maps.LatLng(38.89, -77.03));
1092 map.setZoom(4);
Zachary Heilbrond98423c2014-03-06 17:37:28 -08001093
Eugenia Gabrielovaea8f9482013-10-28 04:49:18 -07001094 // Selection button
1095 $("#selection-button").trigger("click");
1096 rectangleManager.setMap(map);
1097 rectangleManager.setDrawingMode(google.maps.drawing.OverlayType.RECTANGLE);
genia.likes.science@gmail.com1b30f3d2013-08-17 23:53:37 -07001098}
1099
Eugenia Gabrielovad07556a2013-10-11 03:45:06 -07001100/**
1101* mapWidgetClearMap
Eugenia Gabrielovad07556a2013-10-11 03:45:06 -07001102* Removes data/markers
1103*/
genia.likes.science@gmail.com1b30f3d2013-08-17 23:53:37 -07001104function mapWidgetClearMap() {
1105
1106 // Remove previously plotted data/markers
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -07001107 for (c in map_cells) {
1108 map_cells[c].setMap(null);
1109 }
1110 map_cells = [];
Zachary Heilbrond98423c2014-03-06 17:37:28 -08001111
Eugenia Gabrielova8b34c652013-10-19 06:53:27 -07001112 $.each(map_info_windows, function(i) {
1113 map_info_windows[i].close();
1114 });
1115 map_info_windows = {};
Zachary Heilbrond98423c2014-03-06 17:37:28 -08001116
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -07001117 for (m in map_tweet_markers) {
1118 map_tweet_markers[m].setMap(null);
1119 }
1120 map_tweet_markers = [];
Zachary Heilbrond98423c2014-03-06 17:37:28 -08001121
Eugenia Gabrielovaea8f9482013-10-28 04:49:18 -07001122 // Hide legend
Eugenia Gabrielovad6e88e02013-10-19 08:26:54 -07001123 $("#rainbow-legend-container").hide();
Zachary Heilbrond98423c2014-03-06 17:37:28 -08001124
Eugenia Gabrielovaea8f9482013-10-28 04:49:18 -07001125 // Reenable submit button
Eugenia Gabrielova12de0302013-10-18 02:25:39 -07001126 $("#submit-button").attr("disabled", false);
Zachary Heilbrond98423c2014-03-06 17:37:28 -08001127
Eugenia Gabrielovaea8f9482013-10-28 04:49:18 -07001128 // Hide selection rectangle
1129 if (selectionRectangle) {
1130 selectionRectangle.setMap(null);
1131 selectionRectangle = null;
1132 }
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -07001133}
1134
1135/**
Eugenia Gabrielova8b34c652013-10-19 06:53:27 -07001136* buildLegend
Eugenia Gabrielova8b34c652013-10-19 06:53:27 -07001137*
1138* Generates gradient, button action for legend bar
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -07001139*/
Eugenia Gabrielova8b34c652013-10-19 06:53:27 -07001140function buildLegend() {
Zachary Heilbrond98423c2014-03-06 17:37:28 -08001141
Eugenia Gabrielova8b34c652013-10-19 06:53:27 -07001142 // Fill in legend area with colors
1143 var gradientColor;
Zachary Heilbrond98423c2014-03-06 17:37:28 -08001144
Eugenia Gabrielovad6e88e02013-10-19 08:26:54 -07001145 for (i = 0; i<100; i++) {
1146 //$("#rainbow-legend-container").append("" + rainbow.colourAt(i));
1147 $("#legend-gradient").append('<div style="display:inline-block; max-width:2px; background-color:#' + rainbow.colourAt(i) +';">&nbsp;</div>');
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -07001148 }
Zachary Heilbrond98423c2014-03-06 17:37:28 -08001149}
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -07001150
1151/**
1152* Computes radius for a given data point from a spatial cell
1153* @param {Object} keys => ["latSW" "lngSW" "latNE" "lngNE" "weight"]
1154* @returns {number} radius between 2 points in metres
1155*/
Eugenia Gabrielova8b34c652013-10-19 06:53:27 -07001156function mapWidgetComputeCircleRadius(spatialCell, wLimit) {
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -07001157
1158 // Define Boundary Points
1159 var point_center = new google.maps.LatLng((spatialCell.latSW + spatialCell.latNE)/2.0, (spatialCell.lngSW + spatialCell.lngNE)/2.0);
1160 var point_left = new google.maps.LatLng((spatialCell.latSW + spatialCell.latNE)/2.0, spatialCell.lngSW);
1161 var point_top = new google.maps.LatLng(spatialCell.latNE, (spatialCell.lngSW + spatialCell.lngNE)/2.0);
Zachary Heilbrond98423c2014-03-06 17:37:28 -08001162
1163 // Circle scale modifier =
Eugenia Gabrielova8b34c652013-10-19 06:53:27 -07001164 var scale = 500 + 500*(spatialCell.weight / wLimit);
Zachary Heilbrond98423c2014-03-06 17:37:28 -08001165
Eugenia Gabrielova8b34c652013-10-19 06:53:27 -07001166 // Return proportionate value so that circles mostly line up.
Eugenia Gabrielovaf29a55f2013-10-28 03:18:18 -07001167 return scale * Math.min(distanceBetweenPoints(point_center, point_left), distanceBetweenPoints(point_center, point_top));
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -07001168}
1169
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -07001170/**
Eugenia Gabrielovaf29a55f2013-10-28 03:18:18 -07001171* Calculates the distance between two latlng locations in km, using Google Geometry API.
1172* @param p1, a LatLng
1173* @param p2, a LatLng
genia.likes.science@gmail.com6d6aa8e2013-07-23 01:23:21 -07001174*/
Eugenia Gabrielovaf29a55f2013-10-28 03:18:18 -07001175function distanceBetweenPoints (p1, p2) {
1176 return 0.001 * google.maps.geometry.spherical.computeDistanceBetween(p1, p2);
Eugenia Gabrielova3d7bfe52013-10-28 02:36:44 -07001177};