genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 1 | $(function() { |
| 2 | |
Eugenia Gabrielova | dbd50a4 | 2013-10-19 00:30:27 -0700 | [diff] [blame] | 3 | // 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 |
| 5 | // code but does not store information about any individual API call. |
| 6 | A = new AsterixDBConnection({ |
| 7 | |
| 8 | // 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", |
| 12 | |
| 13 | // 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" : "/", |
| 17 | |
| 18 | // 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 |
| 29 | // an error message to display, a positivity value (false in this case, errors are bad), and a |
| 30 | // 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"; |
| 34 | |
| 35 | reportUserMessage(showErrorMessage, isPositive, showReportAt); |
| 36 | } |
| 37 | }); |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 38 | |
Eugenia Gabrielova | ab1ae55 | 2013-10-20 01:12:19 -0700 | [diff] [blame] | 39 | // 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. |
| 42 | var intervalID = setInterval( |
| 43 | function() { |
| 44 | asynchronousQueryIntervalUpdate(); |
| 45 | }, |
| 46 | asynchronousQueryGetInterval() |
| 47 | ); |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 48 | |
Eugenia Gabrielova | 8b34c65 | 2013-10-19 06:53:27 -0700 | [diff] [blame] | 49 | // Legend Container |
| 50 | // Create a rainbow from a pretty color scheme. |
| 51 | // http://www.colourlovers.com/palette/292482/Terra |
| 52 | rainbow = new Rainbow(); |
| 53 | rainbow.setSpectrum("#E8DDCB", "#CDB380", "#036564", "#033649", "#031634"); |
| 54 | buildLegend(); |
| 55 | |
Eugenia Gabrielova | f9fcd71 | 2013-10-20 02:37:35 -0700 | [diff] [blame] | 56 | // Initialization of UI Tabas |
Eugenia Gabrielova | ab1ae55 | 2013-10-20 01:12:19 -0700 | [diff] [blame] | 57 | initDemoPrepareTabs(); |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 58 | |
Eugenia Gabrielova | ab1ae55 | 2013-10-20 01:12:19 -0700 | [diff] [blame] | 59 | // UI Elements - Creates Map, Location Auto-Complete, Selection Rectangle |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 60 | var mapOptions = { |
genia.likes.science@gmail.com | 1b30f3d | 2013-08-17 23:53:37 -0700 | [diff] [blame] | 61 | center: new google.maps.LatLng(38.89, -77.03), |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 62 | zoom: 4, |
genia.likes.science@gmail.com | 1b30f3d | 2013-08-17 23:53:37 -0700 | [diff] [blame] | 63 | mapTypeId: google.maps.MapTypeId.ROADMAP, |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 64 | streetViewControl: false, |
| 65 | draggable : false |
| 66 | }; |
| 67 | map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions); |
| 68 | |
| 69 | var input = document.getElementById('location-text-box'); |
| 70 | var autocomplete = new google.maps.places.Autocomplete(input); |
| 71 | autocomplete.bindTo('bounds', map); |
| 72 | |
| 73 | google.maps.event.addListener(autocomplete, 'place_changed', function() { |
| 74 | var place = autocomplete.getPlace(); |
| 75 | if (place.geometry.viewport) { |
| 76 | map.fitBounds(place.geometry.viewport); |
| 77 | } else { |
| 78 | map.setCenter(place.geometry.location); |
| 79 | map.setZoom(17); // Why 17? Because it looks good. |
| 80 | } |
| 81 | var address = ''; |
| 82 | if (place.address_components) { |
| 83 | address = [(place.address_components[0] && place.address_components[0].short_name || ''), |
| 84 | (place.address_components[1] && place.address_components[1].short_name || ''), |
| 85 | (place.address_components[2] && place.address_components[2].short_name || '') ].join(' '); |
| 86 | } |
| 87 | }); |
| 88 | |
| 89 | // UI Elements - Selection Rectangle Drawing |
| 90 | shouldDraw = false; |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 91 | selectionRect = null; |
Eugenia Gabrielova | ab1ae55 | 2013-10-20 01:12:19 -0700 | [diff] [blame] | 92 | var startLatLng; |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 93 | var selectionRadio = $("#selection-button"); |
| 94 | var firstClick = true; |
| 95 | |
| 96 | google.maps.event.addListener(map, 'mousedown', function (event) { |
| 97 | // only allow drawing if selection is selected |
| 98 | if (selectionRadio.hasClass("active")) { |
| 99 | startLatLng = event.latLng; |
| 100 | shouldDraw = true; |
| 101 | } |
| 102 | }); |
| 103 | |
| 104 | google.maps.event.addListener(map, 'mousemove', drawRect); |
| 105 | function drawRect (event) { |
| 106 | if (shouldDraw) { |
| 107 | if (!selectionRect) { |
| 108 | var selectionRectOpts = { |
| 109 | bounds: new google.maps.LatLngBounds(startLatLng, event.latLng), |
| 110 | map: map, |
| 111 | strokeWeight: 1, |
| 112 | strokeColor: "2b3f8c", |
| 113 | fillColor: "2b3f8c" |
| 114 | }; |
| 115 | selectionRect = new google.maps.Rectangle(selectionRectOpts); |
| 116 | google.maps.event.addListener(selectionRect, 'mouseup', function () { |
| 117 | shouldDraw = false; |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 118 | }); |
| 119 | } else { |
genia.likes.science@gmail.com | 4ada78c | 2013-09-07 13:53:48 -0700 | [diff] [blame] | 120 | |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 121 | if (startLatLng.lng() < event.latLng.lng()) { |
| 122 | selectionRect.setBounds(new google.maps.LatLngBounds(startLatLng, event.latLng)); |
| 123 | } else { |
| 124 | selectionRect.setBounds(new google.maps.LatLngBounds(event.latLng, startLatLng)); |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | }; |
Eugenia Gabrielova | ab1ae55 | 2013-10-20 01:12:19 -0700 | [diff] [blame] | 129 | |
Eugenia Gabrielova | ab1ae55 | 2013-10-20 01:12:19 -0700 | [diff] [blame] | 130 | // Open about tab to start user on a tutorial |
| 131 | //$('#mode-tabs a:first').tab('show') // Select first tab |
| 132 | |
Eugenia Gabrielova | f9fcd71 | 2013-10-20 02:37:35 -0700 | [diff] [blame] | 133 | // Initialize data structures |
| 134 | APIqueryTracker = {}; |
| 135 | asyncQueryManager = {}; |
Eugenia Gabrielova | f9fcd71 | 2013-10-20 02:37:35 -0700 | [diff] [blame] | 136 | map_cells = []; |
| 137 | map_tweet_markers = []; |
| 138 | map_info_windows = {}; |
| 139 | review_mode_tweetbooks = []; |
Eugenia Gabrielova | fcd2868 | 2013-10-20 05:36:36 -0700 | [diff] [blame^] | 140 | |
Eugenia Gabrielova | f9fcd71 | 2013-10-20 02:37:35 -0700 | [diff] [blame] | 141 | getAllDataverseTweetbooks(); |
Eugenia Gabrielova | f9fcd71 | 2013-10-20 02:37:35 -0700 | [diff] [blame] | 142 | initDemoUIButtonControls(); |
| 143 | }); |
| 144 | |
| 145 | function initDemoUIButtonControls() { |
| 146 | |
| 147 | // Explore Mode - Update Sliders |
Eugenia Gabrielova | ab1ae55 | 2013-10-20 01:12:19 -0700 | [diff] [blame] | 148 | var updateSliderDisplay = function(event, ui) { |
| 149 | if (event.target.id == "grid-lat-slider") { |
| 150 | $("#gridlat").text(""+ui.value); |
| 151 | } else { |
| 152 | $("#gridlng").text(""+ui.value); |
| 153 | } |
| 154 | }; |
Eugenia Gabrielova | ab1ae55 | 2013-10-20 01:12:19 -0700 | [diff] [blame] | 155 | sliderOptions = { |
| 156 | max: 10, |
Eugenia Gabrielova | f9fcd71 | 2013-10-20 02:37:35 -0700 | [diff] [blame] | 157 | min: 2.0, |
Eugenia Gabrielova | ab1ae55 | 2013-10-20 01:12:19 -0700 | [diff] [blame] | 158 | step: .1, |
Eugenia Gabrielova | f9fcd71 | 2013-10-20 02:37:35 -0700 | [diff] [blame] | 159 | value: 3.0, |
Eugenia Gabrielova | ab1ae55 | 2013-10-20 01:12:19 -0700 | [diff] [blame] | 160 | slidechange: updateSliderDisplay, |
| 161 | slide: updateSliderDisplay, |
| 162 | start: updateSliderDisplay, |
| 163 | stop: updateSliderDisplay |
| 164 | }; |
Eugenia Gabrielova | ab1ae55 | 2013-10-20 01:12:19 -0700 | [diff] [blame] | 165 | $("#gridlat").text(""+sliderOptions.value); |
| 166 | $("#gridlng").text(""+sliderOptions.value); |
| 167 | $(".grid-slider").slider(sliderOptions); |
Eugenia Gabrielova | f9fcd71 | 2013-10-20 02:37:35 -0700 | [diff] [blame] | 168 | |
| 169 | // Explore Mode - Query Builder Date Pickers |
Eugenia Gabrielova | ab1ae55 | 2013-10-20 01:12:19 -0700 | [diff] [blame] | 170 | var dateOptions = { |
| 171 | dateFormat: "yy-mm-dd", |
| 172 | defaultDate: "2012-01-02", |
| 173 | navigationAsDateFormat: true, |
| 174 | constrainInput: true |
| 175 | }; |
| 176 | var start_dp = $("#start-date").datepicker(dateOptions); |
| 177 | start_dp.val(dateOptions.defaultDate); |
| 178 | dateOptions['defaultDate'] = "2012-12-31"; |
| 179 | var end_dp= $("#end-date").datepicker(dateOptions); |
| 180 | end_dp.val(dateOptions.defaultDate); |
Eugenia Gabrielova | f9fcd71 | 2013-10-20 02:37:35 -0700 | [diff] [blame] | 181 | |
| 182 | // Explore Mode: Toggle Selection/Location Search |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 183 | $('#selection-button').on('click', function (e) { |
| 184 | $("#location-text-box").attr("disabled", "disabled"); |
| 185 | if (selectionRect) { |
| 186 | selectionRect.setMap(map); |
| 187 | } |
| 188 | }); |
| 189 | $('#location-button').on('click', function (e) { |
| 190 | $("#location-text-box").removeAttr("disabled"); |
| 191 | if (selectionRect) { |
| 192 | selectionRect.setMap(null); |
| 193 | } |
| 194 | }); |
Eugenia Gabrielova | f9fcd71 | 2013-10-20 02:37:35 -0700 | [diff] [blame] | 195 | $("#selection-button").trigger("click"); |
Eugenia Gabrielova | ab1ae55 | 2013-10-20 01:12:19 -0700 | [diff] [blame] | 196 | |
Eugenia Gabrielova | f9fcd71 | 2013-10-20 02:37:35 -0700 | [diff] [blame] | 197 | // Review Mode: New Tweetbook |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 198 | $('#new-tweetbook-button').on('click', function (e) { |
| 199 | onCreateNewTweetBook($('#new-tweetbook-entry').val()); |
| 200 | |
genia.likes.science@gmail.com | 2de4c18 | 2013-10-04 11:39:11 -0700 | [diff] [blame] | 201 | $('#new-tweetbook-entry').val(""); |
| 202 | $('#new-tweetbook-entry').attr("placeholder", "Name a new tweetbook"); |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 203 | }); |
Eugenia Gabrielova | f9fcd71 | 2013-10-20 02:37:35 -0700 | [diff] [blame] | 204 | |
| 205 | // Explore Mode - Clear Button |
| 206 | $("#clear-button").click(mapWidgetResetMap); |
| 207 | |
| 208 | // Explore Mode: Query Submission |
Eugenia Gabrielova | ab1ae55 | 2013-10-20 01:12:19 -0700 | [diff] [blame] | 209 | $("#submit-button").on("click", function () { |
Eugenia Gabrielova | f9fcd71 | 2013-10-20 02:37:35 -0700 | [diff] [blame] | 210 | |
Eugenia Gabrielova | fcd2868 | 2013-10-20 05:36:36 -0700 | [diff] [blame^] | 211 | $("#report-message").html(''); |
| 212 | $("#submit-button").attr("disabled", true); |
| 213 | |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 214 | var kwterm = $("#keyword-textbox").val(); |
Eugenia Gabrielova | fcd2868 | 2013-10-20 05:36:36 -0700 | [diff] [blame^] | 215 | var startdp = $("#start-date").datepicker("getDate"); |
| 216 | var enddp = $("#end-date").datepicker("getDate"); |
| 217 | var startdt = $.datepicker.formatDate("yy-mm-dd", startdp)+"T00:00:00Z"; |
| 218 | var enddt = $.datepicker.formatDate("yy-mm-dd", enddp)+"T23:59:59Z"; |
| 219 | |
| 220 | var formData = { |
| 221 | "keyword": kwterm, |
| 222 | "startdt": startdt, |
| 223 | "enddt": enddt, |
| 224 | "gridlat": $("#grid-lat-slider").slider("value"), |
| 225 | "gridlng": $("#grid-lng-slider").slider("value") |
| 226 | }; |
| 227 | |
| 228 | // Get Map Bounds |
| 229 | var bounds; |
| 230 | if ($('#selection-button').hasClass("active") && selectionRect) { |
| 231 | bounds = selectionRect.getBounds(); |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 232 | } else { |
Eugenia Gabrielova | fcd2868 | 2013-10-20 05:36:36 -0700 | [diff] [blame^] | 233 | bounds = map.getBounds(); |
| 234 | } |
genia.likes.science@gmail.com | 1400a75 | 2013-10-04 04:59:12 -0700 | [diff] [blame] | 235 | |
Eugenia Gabrielova | fcd2868 | 2013-10-20 05:36:36 -0700 | [diff] [blame^] | 236 | var swLat = Math.abs(bounds.getSouthWest().lat()); |
| 237 | var swLng = Math.abs(bounds.getSouthWest().lng()); |
| 238 | var neLat = Math.abs(bounds.getNorthEast().lat()); |
| 239 | var neLng = Math.abs(bounds.getNorthEast().lng()); |
Eugenia Gabrielova | f9fcd71 | 2013-10-20 02:37:35 -0700 | [diff] [blame] | 240 | |
Eugenia Gabrielova | fcd2868 | 2013-10-20 05:36:36 -0700 | [diff] [blame^] | 241 | formData["swLat"] = Math.min(swLat, neLat); |
| 242 | formData["swLng"] = Math.max(swLng, neLng); |
| 243 | formData["neLat"] = Math.max(swLat, neLat); |
| 244 | formData["neLng"] = Math.min(swLng, neLng); |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 245 | |
Eugenia Gabrielova | fcd2868 | 2013-10-20 05:36:36 -0700 | [diff] [blame^] | 246 | var build_cherry_mode = "synchronous"; |
| 247 | if ($('#asbox').is(":checked")) { |
| 248 | build_cherry_mode = "asynchronous"; |
| 249 | //$('#show-query-button').attr("disabled", false); |
| 250 | } else { |
| 251 | //$('#show-query-button').attr("disabled", true); |
| 252 | } |
Eugenia Gabrielova | f9fcd71 | 2013-10-20 02:37:35 -0700 | [diff] [blame] | 253 | |
Eugenia Gabrielova | fcd2868 | 2013-10-20 05:36:36 -0700 | [diff] [blame^] | 254 | var f = buildAQLQueryFromForm(formData); |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 255 | |
Eugenia Gabrielova | fcd2868 | 2013-10-20 05:36:36 -0700 | [diff] [blame^] | 256 | APIqueryTracker = { |
| 257 | "query" : "use dataverse twitter;\n" + f.val(), |
| 258 | "data" : formData |
| 259 | }; |
Eugenia Gabrielova | f9fcd71 | 2013-10-20 02:37:35 -0700 | [diff] [blame] | 260 | |
Eugenia Gabrielova | fcd2868 | 2013-10-20 05:36:36 -0700 | [diff] [blame^] | 261 | // TODO Make dialog work correctly. |
| 262 | //$('#dialog').html(APIqueryTracker["query"]); |
genia.likes.science@gmail.com | 233fe97 | 2013-09-07 13:57:46 -0700 | [diff] [blame] | 263 | |
Eugenia Gabrielova | fcd2868 | 2013-10-20 05:36:36 -0700 | [diff] [blame^] | 264 | if (build_cherry_mode == "synchronous") { |
| 265 | A.query(f.val(), cherryQuerySyncCallback, build_cherry_mode); |
| 266 | } else { |
| 267 | A.query(f.val(), cherryQueryAsyncCallback, build_cherry_mode); |
| 268 | } |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 269 | |
Eugenia Gabrielova | fcd2868 | 2013-10-20 05:36:36 -0700 | [diff] [blame^] | 270 | // Clears selection rectangle on query execution, rather than waiting for another clear call. |
| 271 | if (selectionRect) { |
| 272 | selectionRect.setMap(null); |
| 273 | selectionRect = null; |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 274 | } |
| 275 | }); |
Eugenia Gabrielova | f9fcd71 | 2013-10-20 02:37:35 -0700 | [diff] [blame] | 276 | } |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 277 | |
Eugenia Gabrielova | ab1ae55 | 2013-10-20 01:12:19 -0700 | [diff] [blame] | 278 | /** |
| 279 | * Builds AsterixDB REST Query from explore mode form. |
| 280 | */ |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 281 | function buildAQLQueryFromForm(parameters) { |
genia.likes.science@gmail.com | 1b30f3d | 2013-08-17 23:53:37 -0700 | [diff] [blame] | 282 | |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 283 | var bounds = { |
genia.likes.science@gmail.com | 1b30f3d | 2013-08-17 23:53:37 -0700 | [diff] [blame] | 284 | "ne" : { "lat" : parameters["neLat"], "lng" : -1*parameters["neLng"]}, |
| 285 | "sw" : { "lat" : parameters["swLat"], "lng" : -1*parameters["swLng"]} |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 286 | }; |
| 287 | |
| 288 | var rectangle = |
| 289 | new FunctionExpression("create-rectangle", |
| 290 | new FunctionExpression("create-point", bounds["sw"]["lat"], bounds["sw"]["lng"]), |
| 291 | new FunctionExpression("create-point", bounds["ne"]["lat"], bounds["ne"]["lng"])); |
| 292 | |
Eugenia Gabrielova | fcd2868 | 2013-10-20 05:36:36 -0700 | [diff] [blame^] | 293 | // You can chain these all together, but let's take them one at a time. |
| 294 | // Let's start with a ForClause. Here we go through each tweet $t in the |
| 295 | // dataset TweetMessageShifted. |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 296 | var aql = new FLWOGRExpression() |
Eugenia Gabrielova | fcd2868 | 2013-10-20 05:36:36 -0700 | [diff] [blame^] | 297 | .ForClause("$t", new AExpression("dataset TweetMessagesShifted")); |
| 298 | |
| 299 | // We know we have bounds for our region, so we can add that LetClause next. |
| 300 | aql = aql.LetClause("$region", rectangle); |
| 301 | |
| 302 | // Now, let's change it up. The keyword term doesn't always show up, so it might be blank. |
| 303 | // We'll attach a new let clause for it, and then a WhereClause. |
| 304 | if (parameters["keyword"].length > 0) { |
| 305 | aql = aql |
| 306 | .LetClause("$keyword", new AExpression('"' + parameters["keyword"] + '"')) |
| 307 | .WhereClause().and( |
| 308 | new FunctionExpression("spatial-intersect", "$t.sender-location", "$region"), |
| 309 | new AExpression('$t.send-time > datetime("' + parameters["startdt"] + '")'), |
| 310 | new AExpression('$t.send-time < datetime("' + parameters["enddt"] + '")'), |
| 311 | new FunctionExpression("contains", "$t.message-text", "$keyword") |
| 312 | ); |
| 313 | } else { |
| 314 | aql = aql |
| 315 | .WhereClause().and( |
| 316 | new FunctionExpression("spatial-intersect", "$t.sender-location", "$region"), |
| 317 | new AExpression('$t.send-time > datetime("' + parameters["startdt"] + '")'), |
| 318 | new AExpression('$t.send-time < datetime("' + parameters["enddt"] + '")') |
| 319 | ); |
| 320 | } |
| 321 | |
| 322 | // Finally, we'll group our results into spatial cells. |
| 323 | aql = aql.GroupClause( |
| 324 | "$c", |
| 325 | new FunctionExpression("spatial-cell", "$t.sender-location", |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 326 | new FunctionExpression("create-point", "24.5", "-125.5"), |
| 327 | parameters["gridlat"].toFixed(1), parameters["gridlng"].toFixed(1)), |
Eugenia Gabrielova | fcd2868 | 2013-10-20 05:36:36 -0700 | [diff] [blame^] | 328 | "with", |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 329 | "$t" |
Eugenia Gabrielova | fcd2868 | 2013-10-20 05:36:36 -0700 | [diff] [blame^] | 330 | ); |
| 331 | |
| 332 | // ...and return a resulting cell and a count of results in that cell. |
| 333 | aql = aql.ReturnClause({ "cell" : "$c", "count" : "count($t)" }); |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 334 | |
| 335 | return aql; |
| 336 | } |
| 337 | |
Eugenia Gabrielova | d07556a | 2013-10-11 03:45:06 -0700 | [diff] [blame] | 338 | /** |
Eugenia Gabrielova | d07556a | 2013-10-11 03:45:06 -0700 | [diff] [blame] | 339 | * getAllDataverseTweetbooks |
Eugenia Gabrielova | d07556a | 2013-10-11 03:45:06 -0700 | [diff] [blame] | 340 | * |
| 341 | * Returns all datasets of type TweetbookEntry, populates review_mode_tweetbooks |
| 342 | */ |
| 343 | function getAllDataverseTweetbooks(fn_tweetbooks) { |
Eugenia Gabrielova | 12de030 | 2013-10-18 02:25:39 -0700 | [diff] [blame] | 344 | |
| 345 | // This creates a query to the Metadata for datasets of type |
| 346 | // TweetBookEntry. Note that if we throw in a WhereClause (commented out below) |
| 347 | // there is an odd error. This is being fixed and will be removed from this demo. |
| 348 | var getTweetbooksQuery = new FLWOGRExpression() |
Eugenia Gabrielova | d07556a | 2013-10-11 03:45:06 -0700 | [diff] [blame] | 349 | .ForClause("$ds", new AExpression("dataset Metadata.Dataset")) |
Eugenia Gabrielova | 12de030 | 2013-10-18 02:25:39 -0700 | [diff] [blame] | 350 | //.WhereClause(new AExpression('$ds.DataTypeName = "TweetbookEntry"')) |
Eugenia Gabrielova | d07556a | 2013-10-11 03:45:06 -0700 | [diff] [blame] | 351 | .ReturnClause({ |
Eugenia Gabrielova | 12de030 | 2013-10-18 02:25:39 -0700 | [diff] [blame] | 352 | "DataTypeName" : "$ds.DataTypeName", |
| 353 | "DatasetName" : "$ds.DatasetName" |
Eugenia Gabrielova | d07556a | 2013-10-11 03:45:06 -0700 | [diff] [blame] | 354 | }); |
| 355 | |
Eugenia Gabrielova | 12de030 | 2013-10-18 02:25:39 -0700 | [diff] [blame] | 356 | // Now create a function that will be called when tweetbooks succeed. |
| 357 | // In this case, we want to parse out the results object from the Asterix |
| 358 | // REST API response. |
| 359 | var tweetbooksSuccess = function(r) { |
Eugenia Gabrielova | d07556a | 2013-10-11 03:45:06 -0700 | [diff] [blame] | 360 | // Parse tweetbook metadata results |
Eugenia Gabrielova | 12de030 | 2013-10-18 02:25:39 -0700 | [diff] [blame] | 361 | $.each(r.results, function(i, data) { |
| 362 | if ($.parseJSON(data)["DataTypeName"] == "TweetbookEntry") { |
| 363 | review_mode_tweetbooks.push($.parseJSON(data)["DatasetName"]); |
| 364 | } |
Eugenia Gabrielova | d07556a | 2013-10-11 03:45:06 -0700 | [diff] [blame] | 365 | }); |
| 366 | |
Eugenia Gabrielova | 12de030 | 2013-10-18 02:25:39 -0700 | [diff] [blame] | 367 | // Now, if any tweetbooks already exist, opulate review screen. |
Eugenia Gabrielova | d07556a | 2013-10-11 03:45:06 -0700 | [diff] [blame] | 368 | $('#review-tweetbook-titles').html(''); |
Eugenia Gabrielova | 12de030 | 2013-10-18 02:25:39 -0700 | [diff] [blame] | 369 | $.each(review_mode_tweetbooks, function(i, tweetbook) { |
| 370 | addTweetBookDropdownItem(tweetbook); |
| 371 | }); |
| 372 | }; |
| 373 | |
| 374 | // Now, we are ready to run a query. |
| 375 | A.meta(getTweetbooksQuery.val(), tweetbooksSuccess); |
Eugenia Gabrielova | d07556a | 2013-10-11 03:45:06 -0700 | [diff] [blame] | 376 | } |
| 377 | |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 378 | /** |
| 379 | * Checks through each asynchronous query to see if they are ready yet |
| 380 | */ |
| 381 | function asynchronousQueryIntervalUpdate() { |
| 382 | for (var handle_key in asyncQueryManager) { |
| 383 | if (!asyncQueryManager[handle_key].hasOwnProperty("ready")) { |
| 384 | asynchronousQueryGetAPIQueryStatus( asyncQueryManager[handle_key]["handle"], handle_key ); |
| 385 | } |
| 386 | } |
| 387 | } |
| 388 | |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 389 | /** |
| 390 | * Returns current time interval to check for asynchronous query readiness |
| 391 | * @returns {number} milliseconds between asychronous query checks |
| 392 | */ |
| 393 | function asynchronousQueryGetInterval() { |
| 394 | var seconds = 10; |
| 395 | return seconds * 1000; |
| 396 | } |
| 397 | |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 398 | /** |
| 399 | * Retrieves status of an asynchronous query, using an opaque result handle from API |
| 400 | * @param {Object} handle, an object previously returned from an async call |
| 401 | * @param {number} handle_id, the integer ID parsed from the handle object |
| 402 | */ |
| 403 | function asynchronousQueryGetAPIQueryStatus (handle, handle_id) { |
| 404 | |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 405 | A.query_status( |
| 406 | { |
| 407 | "handle" : JSON.stringify(handle) |
| 408 | }, |
| 409 | function (res) { |
| 410 | if (res["status"] == "SUCCESS") { |
| 411 | // We don't need to check if this one is ready again, it's not going anywhere... |
| 412 | // Unless the life cycle of handles has changed drastically |
| 413 | asyncQueryManager[handle_id]["ready"] = true; |
| 414 | |
| 415 | // Indicate success. |
genia.likes.science@gmail.com | 4259a54 | 2013-08-18 20:06:58 -0700 | [diff] [blame] | 416 | $('#handle_' + handle_id).removeClass("btn-disabled").prop('disabled', false).addClass("btn-success"); |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 417 | } |
| 418 | } |
| 419 | ); |
| 420 | } |
| 421 | |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 422 | /** |
| 423 | * On-success callback after async API query |
| 424 | * @param {object} res, a result object containing an opaque result handle to Asterix |
| 425 | */ |
| 426 | function cherryQueryAsyncCallback(res) { |
| 427 | |
| 428 | // Parse handle, handle id and query from async call result |
genia.likes.science@gmail.com | 8125bd9 | 2013-08-21 18:04:27 -0700 | [diff] [blame] | 429 | var handle_query = APIqueryTracker["query"]; |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 430 | var handle = res; |
| 431 | var handle_id = res["handle"].toString().split(',')[0]; |
| 432 | |
| 433 | // Add to stored map of existing handles |
| 434 | asyncQueryManager[handle_id] = { |
| 435 | "handle" : handle, |
| 436 | "query" : handle_query, |
genia.likes.science@gmail.com | 8125bd9 | 2013-08-21 18:04:27 -0700 | [diff] [blame] | 437 | "data" : APIqueryTracker["data"] |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 438 | }; |
| 439 | |
genia.likes.science@gmail.com | 4259a54 | 2013-08-18 20:06:58 -0700 | [diff] [blame] | 440 | // Create a container for this async query handle |
| 441 | $('<div/>') |
| 442 | .css("margin-left", "1em") |
| 443 | .css("margin-bottom", "1em") |
| 444 | .css("display", "block") |
| 445 | .attr({ |
| 446 | "class" : "btn-group", |
| 447 | "id" : "async_container_" + handle_id |
| 448 | }) |
| 449 | .appendTo("#async-handle-controls"); |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 450 | |
genia.likes.science@gmail.com | 4259a54 | 2013-08-18 20:06:58 -0700 | [diff] [blame] | 451 | // Adds the main button for this async handle |
| 452 | var handle_action_button = '<button class="btn btn-disabled" id="handle_' + handle_id + '">Handle ' + handle_id + '</button>'; |
| 453 | $('#async_container_' + handle_id).append(handle_action_button); |
| 454 | $('#handle_' + handle_id).prop('disabled', true); |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 455 | $('#handle_' + handle_id).on('click', function (e) { |
genia.likes.science@gmail.com | 4259a54 | 2013-08-18 20:06:58 -0700 | [diff] [blame] | 456 | |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 457 | // make sure query is ready to be run |
| 458 | if (asyncQueryManager[handle_id]["ready"]) { |
| 459 | |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 460 | APIqueryTracker = { |
| 461 | "query" : asyncQueryManager[handle_id]["query"], |
| 462 | "data" : asyncQueryManager[handle_id]["data"] |
| 463 | }; |
Eugenia Gabrielova | d07556a | 2013-10-11 03:45:06 -0700 | [diff] [blame] | 464 | // TODO |
| 465 | //$('#dialog').html(APIqueryTracker["query"]); |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 466 | |
genia.likes.science@gmail.com | 84711b8 | 2013-08-22 03:47:41 -0700 | [diff] [blame] | 467 | if (!asyncQueryManager[handle_id].hasOwnProperty("result")) { |
| 468 | // Generate new Asterix Core API Query |
| 469 | A.query_result( |
| 470 | { "handle" : JSON.stringify(asyncQueryManager[handle_id]["handle"]) }, |
| 471 | function(res) { |
| 472 | asyncQueryManager[handle_id]["result"] = res; |
Eugenia Gabrielova | fcd2868 | 2013-10-20 05:36:36 -0700 | [diff] [blame^] | 473 | |
| 474 | var resultTransform = { |
| 475 | "results" : res.results[0] |
| 476 | }; |
| 477 | |
| 478 | cherryQuerySyncCallback(resultTransform); |
genia.likes.science@gmail.com | 84711b8 | 2013-08-22 03:47:41 -0700 | [diff] [blame] | 479 | } |
| 480 | ); |
| 481 | } else { |
Eugenia Gabrielova | fcd2868 | 2013-10-20 05:36:36 -0700 | [diff] [blame^] | 482 | |
| 483 | var resultTransform = { |
| 484 | "results" : asyncQueryManager[handle_id]["result"].results[0] |
| 485 | }; |
| 486 | |
| 487 | cherryQuerySyncCallback(resultTransform); |
genia.likes.science@gmail.com | 84711b8 | 2013-08-22 03:47:41 -0700 | [diff] [blame] | 488 | } |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 489 | } |
| 490 | }); |
genia.likes.science@gmail.com | 4259a54 | 2013-08-18 20:06:58 -0700 | [diff] [blame] | 491 | |
| 492 | // Adds a removal button for this async handle |
genia.likes.science@gmail.com | 8125bd9 | 2013-08-21 18:04:27 -0700 | [diff] [blame] | 493 | var asyncDeleteButton = addDeleteButton( |
| 494 | "trashhandle_" + handle_id, |
| 495 | "async_container_" + handle_id, |
| 496 | function (e) { |
| 497 | $('#async_container_' + handle_id).remove(); |
| 498 | delete asyncQueryManager[handle_id]; |
| 499 | } |
| 500 | ); |
Eugenia Gabrielova | d07556a | 2013-10-11 03:45:06 -0700 | [diff] [blame] | 501 | |
| 502 | $('#async_container_' + handle_id).append('<br/>'); |
| 503 | |
genia.likes.science@gmail.com | 8125bd9 | 2013-08-21 18:04:27 -0700 | [diff] [blame] | 504 | $("#submit-button").attr("disabled", false); |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 505 | } |
| 506 | |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 507 | /** |
Eugenia Gabrielova | 12de030 | 2013-10-18 02:25:39 -0700 | [diff] [blame] | 508 | * cleanJSON |
Eugenia Gabrielova | 12de030 | 2013-10-18 02:25:39 -0700 | [diff] [blame] | 509 | * @param json, a JSON string that is not correctly formatted. |
Eugenia Gabrielova | 12de030 | 2013-10-18 02:25:39 -0700 | [diff] [blame] | 510 | * Quick and dirty little function to clean up an Asterix JSON quirk. |
| 511 | */ |
| 512 | function cleanJSON(json) { |
| 513 | return json |
| 514 | .replace("rectangle", '"rectangle"') |
| 515 | .replace("point:", '"point":') |
| 516 | .replace("point:", '"point":') |
| 517 | .replace("int64", '"int64"'); |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | /** |
| 521 | * A spatial data cleaning and mapping call |
| 522 | * @param {Object} res, a result object from a cherry geospatial query |
| 523 | */ |
| 524 | function cherryQuerySyncCallback(res) { |
genia.likes.science@gmail.com | d42b402 | 2013-08-09 05:05:23 -0700 | [diff] [blame] | 525 | |
Eugenia Gabrielova | 12de030 | 2013-10-18 02:25:39 -0700 | [diff] [blame] | 526 | // Initialize coordinates and weights, to store |
| 527 | // coordinates of map cells and their weights |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 528 | var coordinates = []; |
Eugenia Gabrielova | 8b34c65 | 2013-10-19 06:53:27 -0700 | [diff] [blame] | 529 | var maxWeight = 0; |
Eugenia Gabrielova | d6e88e0 | 2013-10-19 08:26:54 -0700 | [diff] [blame] | 530 | var minWeight = Number.MAX_VALUE; |
Eugenia Gabrielova | f9fcd71 | 2013-10-20 02:37:35 -0700 | [diff] [blame] | 531 | |
Eugenia Gabrielova | 12de030 | 2013-10-18 02:25:39 -0700 | [diff] [blame] | 532 | // Parse resulting JSON objects. Here is an example record: |
| 533 | // { "cell": { rectangle: [{ point: [22.5, 64.5]}, { point: [24.5, 66.5]}]}, "count": { int64: 5 }} |
| 534 | $.each(res.results, function(i, data) { |
| 535 | |
| 536 | // First, parse a JSON object from a cleaned up string. |
| 537 | var record = $.parseJSON(cleanJSON(data)); |
| 538 | |
| 539 | // Parse Coordinates and Weights into a record |
| 540 | var sw = record.cell.rectangle[0].point; |
| 541 | var ne = record.cell.rectangle[1].point; |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 542 | |
Eugenia Gabrielova | 12de030 | 2013-10-18 02:25:39 -0700 | [diff] [blame] | 543 | var coordinate = { |
| 544 | "latSW" : sw[0], |
| 545 | "lngSW" : sw[1], |
| 546 | "latNE" : ne[0], |
| 547 | "lngNE" : ne[1], |
| 548 | "weight" : record.count.int64 |
| 549 | } |
| 550 | |
Eugenia Gabrielova | 8b34c65 | 2013-10-19 06:53:27 -0700 | [diff] [blame] | 551 | maxWeight = Math.max(coordinate["weight"], maxWeight); |
Eugenia Gabrielova | d6e88e0 | 2013-10-19 08:26:54 -0700 | [diff] [blame] | 552 | minWeight = Math.min(coordinate["weight"], minWeight); |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 553 | coordinates.push(coordinate); |
Eugenia Gabrielova | 12de030 | 2013-10-18 02:25:39 -0700 | [diff] [blame] | 554 | }); |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 555 | |
Eugenia Gabrielova | d6e88e0 | 2013-10-19 08:26:54 -0700 | [diff] [blame] | 556 | triggerUIUpdate(coordinates, maxWeight, minWeight); |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | /** |
| 560 | * Triggers a map update based on a set of spatial query result cells |
| 561 | * @param [Array] mapPlotData, an array of coordinate and weight objects |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 562 | * @param [Array] plotWeights, a list of weights of the spatial cells - e.g., number of tweets |
| 563 | */ |
Eugenia Gabrielova | d6e88e0 | 2013-10-19 08:26:54 -0700 | [diff] [blame] | 564 | function triggerUIUpdate(mapPlotData, maxWeight, minWeight) { |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 565 | /** Clear anything currently on the map **/ |
| 566 | mapWidgetClearMap(); |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 567 | |
Eugenia Gabrielova | 8b34c65 | 2013-10-19 06:53:27 -0700 | [diff] [blame] | 568 | // Initialize info windows. |
genia.likes.science@gmail.com | 65e0418 | 2013-10-04 04:31:41 -0700 | [diff] [blame] | 569 | map_info_windows = {}; |
| 570 | |
Eugenia Gabrielova | 8b34c65 | 2013-10-19 06:53:27 -0700 | [diff] [blame] | 571 | $.each(mapPlotData, function (m) { |
| 572 | |
| 573 | var point_center = new google.maps.LatLng( |
| 574 | (mapPlotData[m].latSW + mapPlotData[m].latNE)/2.0, |
| 575 | (mapPlotData[m].lngSW + mapPlotData[m].lngNE)/2.0); |
genia.likes.science@gmail.com | 1b30f3d | 2013-08-17 23:53:37 -0700 | [diff] [blame] | 576 | |
Eugenia Gabrielova | 8b34c65 | 2013-10-19 06:53:27 -0700 | [diff] [blame] | 577 | var map_circle_options = { |
| 578 | center: point_center, |
| 579 | anchorPoint: point_center, |
| 580 | radius: mapWidgetComputeCircleRadius(mapPlotData[m], maxWeight), |
| 581 | map: map, |
| 582 | fillOpacity: 0.85, |
| 583 | fillColor: rainbow.colourAt(Math.ceil(100 * (mapPlotData[m].weight / maxWeight))), |
| 584 | clickable: true |
| 585 | }; |
| 586 | var map_circle = new google.maps.Circle(map_circle_options); |
| 587 | map_circle.val = mapPlotData[m]; |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 588 | |
Eugenia Gabrielova | 8b34c65 | 2013-10-19 06:53:27 -0700 | [diff] [blame] | 589 | map_info_windows[m] = new google.maps.InfoWindow({ |
| 590 | content: mapPlotData[m].weight + " tweets", |
| 591 | position: point_center |
| 592 | }); |
| 593 | |
| 594 | // Clicking on a circle drills down map to that value, hovering over it displays a count |
| 595 | // of tweets at that location. |
| 596 | google.maps.event.addListener(map_circle, 'click', function (event) { |
| 597 | $.each(map_info_windows, function(i) { |
| 598 | map_info_windows[i].close(); |
genia.likes.science@gmail.com | ec46c77 | 2013-09-07 18:13:00 -0700 | [diff] [blame] | 599 | }); |
Eugenia Gabrielova | 8b34c65 | 2013-10-19 06:53:27 -0700 | [diff] [blame] | 600 | onMapPointDrillDown(map_circle.val); |
| 601 | }); |
genia.likes.science@gmail.com | ec46c77 | 2013-09-07 18:13:00 -0700 | [diff] [blame] | 602 | |
Eugenia Gabrielova | 8b34c65 | 2013-10-19 06:53:27 -0700 | [diff] [blame] | 603 | google.maps.event.addListener(map_circle, 'mouseover', function(event) { |
| 604 | if (!map_info_windows[m].getMap()) { |
| 605 | map_info_windows[m].setPosition(map_circle.center); |
| 606 | map_info_windows[m].open(map); |
| 607 | } |
| 608 | }); |
| 609 | |
| 610 | // Add this marker to global marker cells |
Eugenia Gabrielova | d6e88e0 | 2013-10-19 08:26:54 -0700 | [diff] [blame] | 611 | map_cells.push(map_circle); |
| 612 | |
| 613 | // Show legend |
| 614 | $("#legend-min").html(minWeight); |
| 615 | $("#legend-max").html(maxWeight); |
| 616 | $("#rainbow-legend-container").show(); |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 617 | }); |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 618 | } |
| 619 | |
| 620 | /** |
| 621 | * prepares an Asterix API query to drill down in a rectangular spatial zone |
| 622 | * |
| 623 | * @params {object} marker_borders [LEGACY] a set of bounds for a region from a previous api result |
| 624 | */ |
| 625 | function onMapPointDrillDown(marker_borders) { |
| 626 | var zoneData = APIqueryTracker["data"]; |
| 627 | |
| 628 | var zswBounds = new google.maps.LatLng(marker_borders.latSW, marker_borders.lngSW); |
| 629 | var zneBounds = new google.maps.LatLng(marker_borders.latNE, marker_borders.lngNE); |
| 630 | |
| 631 | var zoneBounds = new google.maps.LatLngBounds(zswBounds, zneBounds); |
| 632 | zoneData["swLat"] = zoneBounds.getSouthWest().lat(); |
| 633 | zoneData["swLng"] = zoneBounds.getSouthWest().lng(); |
| 634 | zoneData["neLat"] = zoneBounds.getNorthEast().lat(); |
| 635 | zoneData["neLng"] = zoneBounds.getNorthEast().lng(); |
| 636 | var zB = { |
| 637 | "sw" : { |
| 638 | "lat" : zoneBounds.getSouthWest().lat(), |
| 639 | "lng" : zoneBounds.getSouthWest().lng() |
| 640 | }, |
| 641 | "ne" : { |
| 642 | "lat" : zoneBounds.getNorthEast().lat(), |
| 643 | "lng" : zoneBounds.getNorthEast().lng() |
| 644 | } |
| 645 | }; |
| 646 | |
| 647 | mapWidgetClearMap(); |
| 648 | |
| 649 | var customBounds = new google.maps.LatLngBounds(); |
| 650 | var zoomSWBounds = new google.maps.LatLng(zoneData["swLat"], zoneData["swLng"]); |
| 651 | var zoomNEBounds = new google.maps.LatLng(zoneData["neLat"], zoneData["neLng"]); |
| 652 | customBounds.extend(zoomSWBounds); |
| 653 | customBounds.extend(zoomNEBounds); |
| 654 | map.fitBounds(customBounds); |
| 655 | |
| 656 | var df = getDrillDownQuery(zoneData, zB); |
| 657 | |
genia.likes.science@gmail.com | 8125bd9 | 2013-08-21 18:04:27 -0700 | [diff] [blame] | 658 | APIqueryTracker = { |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 659 | "query_string" : "use dataverse twitter;\n" + df.val(), |
Eugenia Gabrielova | fcd2868 | 2013-10-20 05:36:36 -0700 | [diff] [blame^] | 660 | "marker_path" : "static/img/mobile2.png" |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 661 | }; |
| 662 | |
| 663 | A.query(df.val(), onTweetbookQuerySuccessPlot); |
| 664 | } |
| 665 | |
| 666 | function getDrillDownQuery(parameters, bounds) { |
| 667 | |
| 668 | var zoomRectangle = new FunctionExpression("create-rectangle", |
| 669 | new FunctionExpression("create-point", bounds["sw"]["lat"], bounds["sw"]["lng"]), |
| 670 | new FunctionExpression("create-point", bounds["ne"]["lat"], bounds["ne"]["lng"])); |
| 671 | |
| 672 | var drillDown = new FLWOGRExpression() |
genia.likes.science@gmail.com | 1b30f3d | 2013-08-17 23:53:37 -0700 | [diff] [blame] | 673 | .ForClause("$t", new AExpression("dataset TweetMessagesShifted")) |
Eugenia Gabrielova | fcd2868 | 2013-10-20 05:36:36 -0700 | [diff] [blame^] | 674 | .LetClause("$region", zoomRectangle); |
| 675 | |
| 676 | if (parameters["keyword"].length == 0) { |
| 677 | drillDown = drillDown |
| 678 | .WhereClause().and( |
| 679 | new FunctionExpression('spatial-intersect', '$t.sender-location', '$region'), |
| 680 | new AExpression().set('$t.send-time > datetime("' + parameters["startdt"] + '")'), |
| 681 | new AExpression().set('$t.send-time < datetime("' + parameters["enddt"] + '")') |
| 682 | ); |
| 683 | } else { |
| 684 | drillDown = drillDown |
| 685 | .LetClause("$keyword", new AExpression('"' + parameters["keyword"] + '"')) |
| 686 | .WhereClause().and( |
| 687 | new FunctionExpression('spatial-intersect', '$t.sender-location', '$region'), |
| 688 | new AExpression().set('$t.send-time > datetime("' + parameters["startdt"] + '")'), |
| 689 | new AExpression().set('$t.send-time < datetime("' + parameters["enddt"] + '")'), |
| 690 | new FunctionExpression('contains', '$t.message-text', '$keyword') |
| 691 | ); |
| 692 | } |
| 693 | |
| 694 | drillDown = drillDown |
| 695 | .ReturnClause({ |
| 696 | "tweetId" : "$t.tweetid", |
| 697 | "tweetText" : "$t.message-text", |
| 698 | "tweetLoc" : "$t.sender-location" |
| 699 | }); |
| 700 | |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 701 | return drillDown; |
| 702 | } |
| 703 | |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 704 | function onDrillDownAtLocation(tO) { |
| 705 | |
| 706 | var tweetId = tO["tweetEntryId"]; |
| 707 | var tweetText = tO["tweetText"]; |
| 708 | |
genia.likes.science@gmail.com | 0f67d9e | 2013-10-04 17:08:50 -0700 | [diff] [blame] | 709 | // First, set tweet in drilldown modal to be this tweet's text |
| 710 | $('#modal-body-tweet').html('Tweet #' + tweetId + ": " + tweetText); |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 711 | |
Eugenia Gabrielova | d07556a | 2013-10-11 03:45:06 -0700 | [diff] [blame] | 712 | // Next, empty any leftover tweetbook comments or error/success messages |
genia.likes.science@gmail.com | 0f67d9e | 2013-10-04 17:08:50 -0700 | [diff] [blame] | 713 | $("#modal-body-add-to").val(''); |
| 714 | $("#modal-body-add-note").val(''); |
Eugenia Gabrielova | d07556a | 2013-10-11 03:45:06 -0700 | [diff] [blame] | 715 | $("#modal-body-message-holder").html(""); |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 716 | |
genia.likes.science@gmail.com | 0f67d9e | 2013-10-04 17:08:50 -0700 | [diff] [blame] | 717 | // Next, if there is an existing tweetcomment reported, show it. |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 718 | if (tO.hasOwnProperty("tweetComment")) { |
genia.likes.science@gmail.com | 8125bd9 | 2013-08-21 18:04:27 -0700 | [diff] [blame] | 719 | |
Eugenia Gabrielova | d07556a | 2013-10-11 03:45:06 -0700 | [diff] [blame] | 720 | // Show correct panel |
genia.likes.science@gmail.com | 0f67d9e | 2013-10-04 17:08:50 -0700 | [diff] [blame] | 721 | $("#modal-existing-note").show(); |
Eugenia Gabrielova | d07556a | 2013-10-11 03:45:06 -0700 | [diff] [blame] | 722 | $("#modal-save-tweet-panel").hide(); |
genia.likes.science@gmail.com | 8125bd9 | 2013-08-21 18:04:27 -0700 | [diff] [blame] | 723 | |
Eugenia Gabrielova | d07556a | 2013-10-11 03:45:06 -0700 | [diff] [blame] | 724 | // Fill in existing tweet comment |
genia.likes.science@gmail.com | 0f67d9e | 2013-10-04 17:08:50 -0700 | [diff] [blame] | 725 | $("#modal-body-tweet-note").val(tO["tweetComment"]); |
| 726 | |
| 727 | // Change Tweetbook Badge |
| 728 | $("#modal-current-tweetbook").val(APIqueryTracker["active_tweetbook"]); |
| 729 | |
| 730 | // Add deletion functionality |
| 731 | $("#modal-body-trash-icon").on('click', function () { |
| 732 | // Send comment deletion to asterix |
| 733 | var deleteTweetCommentOnId = '"' + tweetId + '"'; |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 734 | var toDelete = new DeleteStatement( |
| 735 | "$mt", |
genia.likes.science@gmail.com | 0f67d9e | 2013-10-04 17:08:50 -0700 | [diff] [blame] | 736 | APIqueryTracker["active_tweetbook"], |
| 737 | new AExpression("$mt.tweetid = " + deleteTweetCommentOnId.toString()) |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 738 | ); |
genia.likes.science@gmail.com | 0f67d9e | 2013-10-04 17:08:50 -0700 | [diff] [blame] | 739 | A.update( |
| 740 | toDelete.val() |
| 741 | ); |
| 742 | |
| 743 | // Hide comment from map |
| 744 | $('#drilldown_modal').modal('hide'); |
| 745 | |
| 746 | // Replot tweetbook |
| 747 | onPlotTweetbook(APIqueryTracker["active_tweetbook"]); |
| 748 | }); |
| 749 | |
| 750 | } else { |
Eugenia Gabrielova | d07556a | 2013-10-11 03:45:06 -0700 | [diff] [blame] | 751 | // Show correct panel |
genia.likes.science@gmail.com | 0f67d9e | 2013-10-04 17:08:50 -0700 | [diff] [blame] | 752 | $("#modal-existing-note").hide(); |
Eugenia Gabrielova | d07556a | 2013-10-11 03:45:06 -0700 | [diff] [blame] | 753 | $("#modal-save-tweet-panel").show(); |
genia.likes.science@gmail.com | 0f67d9e | 2013-10-04 17:08:50 -0700 | [diff] [blame] | 754 | |
Eugenia Gabrielova | d07556a | 2013-10-11 03:45:06 -0700 | [diff] [blame] | 755 | // Now, when adding a comment on an available tweet to a tweetbook |
Eugenia Gabrielova | dbd50a4 | 2013-10-19 00:30:27 -0700 | [diff] [blame] | 756 | $('#save-comment-tweetbook-modal').unbind('click'); |
Eugenia Gabrielova | d07556a | 2013-10-11 03:45:06 -0700 | [diff] [blame] | 757 | $("#save-comment-tweetbook-modal").on('click', function(e) { |
genia.likes.science@gmail.com | 0f67d9e | 2013-10-04 17:08:50 -0700 | [diff] [blame] | 758 | |
Eugenia Gabrielova | d07556a | 2013-10-11 03:45:06 -0700 | [diff] [blame] | 759 | // Stuff to save about new comment |
| 760 | var save_metacomment_target_tweetbook = $("#modal-body-add-to").val(); |
| 761 | var save_metacomment_target_comment = '"' + $("#modal-body-add-note").val() + '"'; |
| 762 | var save_metacomment_target_tweet = '"' + tweetId + '"'; |
| 763 | |
| 764 | // Make sure content is entered, and then save this comment. |
Eugenia Gabrielova | 12de030 | 2013-10-18 02:25:39 -0700 | [diff] [blame] | 765 | if ($("#modal-body-add-note").val() == "") { |
Eugenia Gabrielova | dbd50a4 | 2013-10-19 00:30:27 -0700 | [diff] [blame] | 766 | |
| 767 | reportUserMessage("Please enter a comment about the tweet", false, "report-message"); |
Eugenia Gabrielova | 12de030 | 2013-10-18 02:25:39 -0700 | [diff] [blame] | 768 | |
| 769 | } else if ($("#modal-body-add-to").val() == "") { |
| 770 | |
Eugenia Gabrielova | dbd50a4 | 2013-10-19 00:30:27 -0700 | [diff] [blame] | 771 | reportUserMessage("Please enter a tweetbook.", false, "report-message"); |
Eugenia Gabrielova | 12de030 | 2013-10-18 02:25:39 -0700 | [diff] [blame] | 772 | |
genia.likes.science@gmail.com | 0f67d9e | 2013-10-04 17:08:50 -0700 | [diff] [blame] | 773 | } else { |
Eugenia Gabrielova | d07556a | 2013-10-11 03:45:06 -0700 | [diff] [blame] | 774 | |
| 775 | // Check if tweetbook exists. If not, create it. |
| 776 | if (!(existsTweetbook(save_metacomment_target_tweetbook))) { |
| 777 | onCreateNewTweetBook(save_metacomment_target_tweetbook); |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 778 | } |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 779 | |
Eugenia Gabrielova | d07556a | 2013-10-11 03:45:06 -0700 | [diff] [blame] | 780 | var toInsert = new InsertStatement( |
| 781 | save_metacomment_target_tweetbook, |
| 782 | { |
| 783 | "tweetid" : save_metacomment_target_tweet.toString(), |
| 784 | "comment-text" : save_metacomment_target_comment |
| 785 | } |
| 786 | ); |
Eugenia Gabrielova | dbd50a4 | 2013-10-19 00:30:27 -0700 | [diff] [blame] | 787 | |
| 788 | A.update(toInsert.val(), function () { |
| 789 | var successMessage = "Saved comment on <b>Tweet #" + tweetId + |
| 790 | "</b> in dataset <b>" + save_metacomment_target_tweetbook + "</b>."; |
| 791 | reportUserMessage(successMessage, true, "report-message"); |
Eugenia Gabrielova | d07556a | 2013-10-11 03:45:06 -0700 | [diff] [blame] | 792 | |
Eugenia Gabrielova | dbd50a4 | 2013-10-19 00:30:27 -0700 | [diff] [blame] | 793 | $("#modal-body-add-to").val(''); |
| 794 | $("#modal-body-add-note").val(''); |
| 795 | $('#save-comment-tweetbook-modal').unbind('click'); |
| 796 | |
| 797 | // Close modal |
| 798 | $('#drilldown_modal').modal('hide'); |
| 799 | }); |
Eugenia Gabrielova | d07556a | 2013-10-11 03:45:06 -0700 | [diff] [blame] | 800 | } |
| 801 | }); |
| 802 | } |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 803 | } |
| 804 | |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 805 | /** |
| 806 | * Adds a new tweetbook entry to the menu and creates a dataset of type TweetbookEntry. |
| 807 | */ |
| 808 | function onCreateNewTweetBook(tweetbook_title) { |
| 809 | |
| 810 | var tweetbook_title = tweetbook_title.split(' ').join('_'); |
| 811 | |
| 812 | A.ddl( |
| 813 | "create dataset " + tweetbook_title + "(TweetbookEntry) primary key tweetid;", |
| 814 | function () {} |
| 815 | ); |
| 816 | |
| 817 | if (!(existsTweetbook(tweetbook_title))) { |
| 818 | review_mode_tweetbooks.push(tweetbook_title); |
| 819 | addTweetBookDropdownItem(tweetbook_title); |
| 820 | } |
| 821 | } |
| 822 | |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 823 | function onDropTweetBook(tweetbook_title) { |
| 824 | |
| 825 | // AQL Call |
| 826 | A.ddl( |
| 827 | "drop dataset " + tweetbook_title + " if exists;", |
| 828 | function () {} |
| 829 | ); |
| 830 | |
| 831 | // Removes tweetbook from review_mode_tweetbooks |
| 832 | var remove_position = $.inArray(tweetbook_title, review_mode_tweetbooks); |
| 833 | if (remove_position >= 0) review_mode_tweetbooks.splice(remove_position, 1); |
| 834 | |
| 835 | // Clear UI with review tweetbook titles |
| 836 | $('#review-tweetbook-titles').html(''); |
| 837 | for (r in review_mode_tweetbooks) { |
| 838 | addTweetBookDropdownItem(review_mode_tweetbooks[r]); |
| 839 | } |
| 840 | } |
| 841 | |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 842 | function addTweetBookDropdownItem(tweetbook) { |
genia.likes.science@gmail.com | 2de4c18 | 2013-10-04 11:39:11 -0700 | [diff] [blame] | 843 | // Add placeholder for this tweetbook |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 844 | $('<div/>') |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 845 | .attr({ |
| 846 | "class" : "btn-group", |
| 847 | "id" : "rm_holder_" + tweetbook |
| 848 | }).appendTo("#review-tweetbook-titles"); |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 849 | |
| 850 | // Add plotting button for this tweetbook |
genia.likes.science@gmail.com | 2de4c18 | 2013-10-04 11:39:11 -0700 | [diff] [blame] | 851 | var plot_button = '<button class="btn btn-default" id="rm_plotbook_' + tweetbook + '">' + tweetbook + '</button>'; |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 852 | $("#rm_holder_" + tweetbook).append(plot_button); |
genia.likes.science@gmail.com | 2de4c18 | 2013-10-04 11:39:11 -0700 | [diff] [blame] | 853 | $("#rm_plotbook_" + tweetbook).width("200px"); |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 854 | $("#rm_plotbook_" + tweetbook).on('click', function(e) { |
| 855 | onPlotTweetbook(tweetbook); |
| 856 | }); |
| 857 | |
| 858 | // Add trash button for this tweetbook |
genia.likes.science@gmail.com | 8125bd9 | 2013-08-21 18:04:27 -0700 | [diff] [blame] | 859 | var onTrashTweetbookButton = addDeleteButton( |
| 860 | "rm_trashbook_" + tweetbook, |
| 861 | "rm_holder_" + tweetbook, |
| 862 | function(e) { |
| 863 | onDropTweetBook(tweetbook); |
| 864 | } |
| 865 | ); |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 866 | } |
| 867 | |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 868 | function onPlotTweetbook(tweetbook) { |
genia.likes.science@gmail.com | 8125bd9 | 2013-08-21 18:04:27 -0700 | [diff] [blame] | 869 | |
| 870 | // Clear map for this one |
| 871 | mapWidgetResetMap(); |
| 872 | |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 873 | var plotTweetQuery = new FLWOGRExpression() |
genia.likes.science@gmail.com | 1b30f3d | 2013-08-17 23:53:37 -0700 | [diff] [blame] | 874 | .ForClause("$t", new AExpression("dataset TweetMessagesShifted")) |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 875 | .ForClause("$m", new AExpression("dataset " + tweetbook)) |
| 876 | .WhereClause(new AExpression("$m.tweetid = $t.tweetid")) |
| 877 | .ReturnClause({ |
| 878 | "tweetId" : "$m.tweetid", |
| 879 | "tweetText" : "$t.message-text", |
| 880 | "tweetLoc" : "$t.sender-location", |
| 881 | "tweetCom" : "$m.comment-text" |
| 882 | }); |
| 883 | |
genia.likes.science@gmail.com | 8125bd9 | 2013-08-21 18:04:27 -0700 | [diff] [blame] | 884 | APIqueryTracker = { |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 885 | "query_string" : "use dataverse twitter;\n" + plotTweetQuery.val(), |
genia.likes.science@gmail.com | 4833b41 | 2013-08-09 06:20:58 -0700 | [diff] [blame] | 886 | "marker_path" : "static/img/mobile_green2.png", |
genia.likes.science@gmail.com | 8125bd9 | 2013-08-21 18:04:27 -0700 | [diff] [blame] | 887 | "active_tweetbook" : tweetbook |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 888 | }; |
| 889 | |
| 890 | A.query(plotTweetQuery.val(), onTweetbookQuerySuccessPlot); |
| 891 | } |
| 892 | |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 893 | function onTweetbookQuerySuccessPlot (res) { |
| 894 | |
Eugenia Gabrielova | fcd2868 | 2013-10-20 05:36:36 -0700 | [diff] [blame^] | 895 | // Parse out tweet Ids, texts, and locations |
| 896 | var tweets = []; |
| 897 | al = 1; |
| 898 | $.each(res.results, function(i, data) { |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 899 | |
Eugenia Gabrielova | fcd2868 | 2013-10-20 05:36:36 -0700 | [diff] [blame^] | 900 | var json = $.parseJSON(cleanJSON(data)); |
| 901 | |
| 902 | var tweetData = { |
| 903 | "tweetEntryId" : json["tweetId"], |
| 904 | "tweetText" : json["tweetText"], |
| 905 | "tweetLat" : json["tweetLoc"]["point"][0], |
| 906 | "tweetLng" : json["tweetLoc"]["point"][1] |
| 907 | }; |
| 908 | |
| 909 | // If we are parsing out tweetbook data with comments, we need to check |
| 910 | // for those here as well. |
| 911 | if (json.hasOwnProperty("tweetCom")) { |
| 912 | tweetData["tweetComment"] = json["tweetCom"]; |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 913 | } |
Eugenia Gabrielova | fcd2868 | 2013-10-20 05:36:36 -0700 | [diff] [blame^] | 914 | |
| 915 | tweets.push(tweetData) |
| 916 | }); |
| 917 | |
| 918 | // Prepare to map the tweets |
| 919 | var micon = APIqueryTracker["marker_path"]; |
| 920 | APIqueryTracker["markers_data"] = []; |
| 921 | |
| 922 | // Create a marker for each tweet |
| 923 | $.each(tweets, function(i, t) { |
| 924 | // Create a phone marker at tweet's position |
| 925 | var map_tweet_m = new google.maps.Marker({ |
| 926 | position: new google.maps.LatLng(tweets[i]["tweetLat"], tweets[i]["tweetLng"]), |
| 927 | map: map, |
| 928 | icon: micon, |
| 929 | clickable: true, |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 930 | }); |
Eugenia Gabrielova | fcd2868 | 2013-10-20 05:36:36 -0700 | [diff] [blame^] | 931 | map_tweet_m["test"] = t; |
| 932 | |
| 933 | // Open Tweet exploration window on click |
| 934 | APIqueryTracker["markers_data"].push(tweets[i]); |
| 935 | google.maps.event.addListener(map_tweet_m, 'click', function (event) { |
| 936 | onClickTweetbookMapMarker(map_tweet_markers[i]["test"]); |
| 937 | }); |
| 938 | |
| 939 | // Add marker to index of tweets |
| 940 | map_tweet_markers.push(map_tweet_m); |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 941 | }); |
| 942 | } |
| 943 | |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 944 | function existsTweetbook(tweetbook) { |
| 945 | if (parseInt($.inArray(tweetbook, review_mode_tweetbooks)) == -1) { |
| 946 | return false; |
| 947 | } else { |
| 948 | return true; |
| 949 | } |
| 950 | } |
| 951 | |
Eugenia Gabrielova | fcd2868 | 2013-10-20 05:36:36 -0700 | [diff] [blame^] | 952 | /** |
| 953 | * When a marker is clicked on in the tweetbook, it will launch a modal |
| 954 | * view to examine or edit the appropriate tweet |
| 955 | */ |
| 956 | function onClickTweetbookMapMarker(t) { |
| 957 | onDrillDownAtLocation(t) |
genia.likes.science@gmail.com | 1400a75 | 2013-10-04 04:59:12 -0700 | [diff] [blame] | 958 | $('#drilldown_modal').modal(); |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 959 | } |
| 960 | |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 961 | /** |
| 962 | * Explore mode: Initial map creation and screen alignment |
| 963 | */ |
| 964 | function onOpenExploreMap () { |
genia.likes.science@gmail.com | 724476d | 2013-10-04 03:31:16 -0700 | [diff] [blame] | 965 | var explore_column_height = $('#explore-well').height(); |
| 966 | var right_column_width = $('#right-col').width(); |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 967 | $('#map_canvas').height(explore_column_height + "px"); |
genia.likes.science@gmail.com | 724476d | 2013-10-04 03:31:16 -0700 | [diff] [blame] | 968 | $('#map_canvas').width(right_column_width + "px"); |
| 969 | |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 970 | $('#review-well').height(explore_column_height + "px"); |
Eugenia Gabrielova | ab1ae55 | 2013-10-20 01:12:19 -0700 | [diff] [blame] | 971 | $('#review-well').css('max-height', explore_column_height + "px"); |
genia.likes.science@gmail.com | 724476d | 2013-10-04 03:31:16 -0700 | [diff] [blame] | 972 | $('#right-col').height(explore_column_height + "px"); |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 973 | } |
| 974 | |
| 975 | /** |
Eugenia Gabrielova | ab1ae55 | 2013-10-20 01:12:19 -0700 | [diff] [blame] | 976 | * initializes demo - adds some extra events when review/explore |
| 977 | * mode are clicked, initializes tabs, aligns map box, moves |
| 978 | * active tab to about tab |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 979 | */ |
Eugenia Gabrielova | ab1ae55 | 2013-10-20 01:12:19 -0700 | [diff] [blame] | 980 | function initDemoPrepareTabs() { |
genia.likes.science@gmail.com | 2de4c18 | 2013-10-04 11:39:11 -0700 | [diff] [blame] | 981 | |
Eugenia Gabrielova | ab1ae55 | 2013-10-20 01:12:19 -0700 | [diff] [blame] | 982 | // Tab behavior for About, Explore, and Demo |
| 983 | $('#mode-tabs a').click(function (e) { |
| 984 | e.preventDefault() |
| 985 | $(this).tab('show') |
| 986 | }) |
| 987 | |
| 988 | // Explore mode should show explore-mode query-builder UI |
| 989 | $('#explore-mode').click(function(e) { |
| 990 | $('#review-well').hide(); |
| 991 | $('#explore-well').show(); |
| 992 | mapWidgetResetMap(); |
| 993 | }); |
| 994 | |
| 995 | // Review mode should show review well and hide explore well |
| 996 | $('#review-mode').click(function(e) { |
| 997 | $('#explore-well').hide(); |
| 998 | $('#review-well').show(); |
| 999 | mapWidgetResetMap(); |
| 1000 | }); |
| 1001 | |
| 1002 | // Does some alignment necessary for the map canvas |
| 1003 | onOpenExploreMap(); |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 1004 | } |
| 1005 | |
genia.likes.science@gmail.com | dd66907 | 2013-08-21 22:53:34 -0700 | [diff] [blame] | 1006 | /** |
| 1007 | * Creates a delete icon button using default trash icon |
genia.likes.science@gmail.com | 8125bd9 | 2013-08-21 18:04:27 -0700 | [diff] [blame] | 1008 | * @param {String} id, id for this element |
| 1009 | * @param {String} attachTo, id string of an element to which I can attach this button. |
| 1010 | * @param {Function} onClick, a function to fire when this icon is clicked |
| 1011 | */ |
| 1012 | function addDeleteButton(iconId, attachTo, onClick) { |
genia.likes.science@gmail.com | 8125bd9 | 2013-08-21 18:04:27 -0700 | [diff] [blame] | 1013 | |
Eugenia Gabrielova | d07556a | 2013-10-11 03:45:06 -0700 | [diff] [blame] | 1014 | var trashIcon = '<button class="btn btn-default" id="' + iconId + '"><span class="glyphicon glyphicon-trash"></span></button>'; |
genia.likes.science@gmail.com | 8125bd9 | 2013-08-21 18:04:27 -0700 | [diff] [blame] | 1015 | $('#' + attachTo).append(trashIcon); |
| 1016 | |
Eugenia Gabrielova | d07556a | 2013-10-11 03:45:06 -0700 | [diff] [blame] | 1017 | // When this trash button is clicked, the function is called. |
genia.likes.science@gmail.com | 8125bd9 | 2013-08-21 18:04:27 -0700 | [diff] [blame] | 1018 | $('#' + iconId).on('click', onClick); |
| 1019 | } |
| 1020 | |
genia.likes.science@gmail.com | dd66907 | 2013-08-21 22:53:34 -0700 | [diff] [blame] | 1021 | /** |
Eugenia Gabrielova | dbd50a4 | 2013-10-19 00:30:27 -0700 | [diff] [blame] | 1022 | * Creates a message and attaches it to data management area. |
genia.likes.science@gmail.com | dd66907 | 2013-08-21 22:53:34 -0700 | [diff] [blame] | 1023 | * @param {String} message, a message to post |
Eugenia Gabrielova | dbd50a4 | 2013-10-19 00:30:27 -0700 | [diff] [blame] | 1024 | * @param {Boolean} isPositiveMessage, whether or not this is a positive message. |
| 1025 | * @param {String} target, the target div to attach this message. |
genia.likes.science@gmail.com | dd66907 | 2013-08-21 22:53:34 -0700 | [diff] [blame] | 1026 | */ |
Eugenia Gabrielova | dbd50a4 | 2013-10-19 00:30:27 -0700 | [diff] [blame] | 1027 | function reportUserMessage(message, isPositiveMessage, target) { |
| 1028 | // Clear out any existing messages |
Eugenia Gabrielova | d07556a | 2013-10-11 03:45:06 -0700 | [diff] [blame] | 1029 | $('#' + target).html(''); |
Eugenia Gabrielova | dbd50a4 | 2013-10-19 00:30:27 -0700 | [diff] [blame] | 1030 | |
| 1031 | // Select appropriate alert-type |
| 1032 | var alertType = "alert-success"; |
| 1033 | if (!isPositiveMessage) { |
| 1034 | alertType = "alert-danger"; |
| 1035 | } |
| 1036 | |
| 1037 | // Append the appropriate message |
Eugenia Gabrielova | d07556a | 2013-10-11 03:45:06 -0700 | [diff] [blame] | 1038 | $('<div/>') |
Eugenia Gabrielova | dbd50a4 | 2013-10-19 00:30:27 -0700 | [diff] [blame] | 1039 | .attr("class", "alert " + alertType) |
Eugenia Gabrielova | d07556a | 2013-10-11 03:45:06 -0700 | [diff] [blame] | 1040 | .html('<button type="button" class="close" data-dismiss="alert">×</button>' + message) |
| 1041 | .appendTo('#' + target); |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 1042 | } |
| 1043 | |
| 1044 | /** |
Eugenia Gabrielova | d07556a | 2013-10-11 03:45:06 -0700 | [diff] [blame] | 1045 | * mapWidgetResetMap |
| 1046 | * |
| 1047 | * [No Parameters] |
| 1048 | * |
| 1049 | * Clears ALL map elements - plotted items, overlays, then resets position |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 1050 | */ |
genia.likes.science@gmail.com | 1b30f3d | 2013-08-17 23:53:37 -0700 | [diff] [blame] | 1051 | function mapWidgetResetMap() { |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 1052 | |
| 1053 | if (selectionRect) { |
| 1054 | selectionRect.setMap(null); |
| 1055 | selectionRect = null; |
| 1056 | } |
genia.likes.science@gmail.com | 1b30f3d | 2013-08-17 23:53:37 -0700 | [diff] [blame] | 1057 | |
| 1058 | mapWidgetClearMap(); |
| 1059 | |
| 1060 | // Reset map center and zoom |
| 1061 | map.setCenter(new google.maps.LatLng(38.89, -77.03)); |
| 1062 | map.setZoom(4); |
| 1063 | } |
| 1064 | |
Eugenia Gabrielova | d07556a | 2013-10-11 03:45:06 -0700 | [diff] [blame] | 1065 | /** |
| 1066 | * mapWidgetClearMap |
Eugenia Gabrielova | d07556a | 2013-10-11 03:45:06 -0700 | [diff] [blame] | 1067 | * Removes data/markers |
| 1068 | */ |
genia.likes.science@gmail.com | 1b30f3d | 2013-08-17 23:53:37 -0700 | [diff] [blame] | 1069 | function mapWidgetClearMap() { |
| 1070 | |
| 1071 | // Remove previously plotted data/markers |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 1072 | for (c in map_cells) { |
| 1073 | map_cells[c].setMap(null); |
| 1074 | } |
| 1075 | map_cells = []; |
genia.likes.science@gmail.com | 65e0418 | 2013-10-04 04:31:41 -0700 | [diff] [blame] | 1076 | |
Eugenia Gabrielova | 8b34c65 | 2013-10-19 06:53:27 -0700 | [diff] [blame] | 1077 | $.each(map_info_windows, function(i) { |
| 1078 | map_info_windows[i].close(); |
| 1079 | }); |
| 1080 | map_info_windows = {}; |
| 1081 | |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 1082 | for (m in map_tweet_markers) { |
| 1083 | map_tweet_markers[m].setMap(null); |
| 1084 | } |
| 1085 | map_tweet_markers = []; |
Eugenia Gabrielova | 12de030 | 2013-10-18 02:25:39 -0700 | [diff] [blame] | 1086 | |
Eugenia Gabrielova | d6e88e0 | 2013-10-19 08:26:54 -0700 | [diff] [blame] | 1087 | $("#rainbow-legend-container").hide(); |
| 1088 | |
Eugenia Gabrielova | 12de030 | 2013-10-18 02:25:39 -0700 | [diff] [blame] | 1089 | $("#submit-button").attr("disabled", false); |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 1090 | } |
| 1091 | |
| 1092 | /** |
Eugenia Gabrielova | 8b34c65 | 2013-10-19 06:53:27 -0700 | [diff] [blame] | 1093 | * buildLegend |
Eugenia Gabrielova | 8b34c65 | 2013-10-19 06:53:27 -0700 | [diff] [blame] | 1094 | * |
| 1095 | * Generates gradient, button action for legend bar |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 1096 | */ |
Eugenia Gabrielova | 8b34c65 | 2013-10-19 06:53:27 -0700 | [diff] [blame] | 1097 | function buildLegend() { |
genia.likes.science@gmail.com | 84711b8 | 2013-08-22 03:47:41 -0700 | [diff] [blame] | 1098 | |
Eugenia Gabrielova | 8b34c65 | 2013-10-19 06:53:27 -0700 | [diff] [blame] | 1099 | // Fill in legend area with colors |
| 1100 | var gradientColor; |
| 1101 | |
Eugenia Gabrielova | d6e88e0 | 2013-10-19 08:26:54 -0700 | [diff] [blame] | 1102 | for (i = 0; i<100; i++) { |
| 1103 | //$("#rainbow-legend-container").append("" + rainbow.colourAt(i)); |
| 1104 | $("#legend-gradient").append('<div style="display:inline-block; max-width:2px; background-color:#' + rainbow.colourAt(i) +';"> </div>'); |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 1105 | } |
Eugenia Gabrielova | 8b34c65 | 2013-10-19 06:53:27 -0700 | [diff] [blame] | 1106 | |
| 1107 | // Window clear button closes all info count windows |
| 1108 | $("#windows-off-btn").on("click", function(e) { |
| 1109 | $.each(map_info_windows, function(i) { |
| 1110 | map_info_windows[i].close(); |
| 1111 | }); |
| 1112 | }); |
| 1113 | } |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 1114 | |
| 1115 | /** |
| 1116 | * Computes radius for a given data point from a spatial cell |
| 1117 | * @param {Object} keys => ["latSW" "lngSW" "latNE" "lngNE" "weight"] |
| 1118 | * @returns {number} radius between 2 points in metres |
| 1119 | */ |
Eugenia Gabrielova | 8b34c65 | 2013-10-19 06:53:27 -0700 | [diff] [blame] | 1120 | function mapWidgetComputeCircleRadius(spatialCell, wLimit) { |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 1121 | |
| 1122 | // Define Boundary Points |
| 1123 | var point_center = new google.maps.LatLng((spatialCell.latSW + spatialCell.latNE)/2.0, (spatialCell.lngSW + spatialCell.lngNE)/2.0); |
| 1124 | var point_left = new google.maps.LatLng((spatialCell.latSW + spatialCell.latNE)/2.0, spatialCell.lngSW); |
| 1125 | var point_top = new google.maps.LatLng(spatialCell.latNE, (spatialCell.lngSW + spatialCell.lngNE)/2.0); |
| 1126 | |
Eugenia Gabrielova | 8b34c65 | 2013-10-19 06:53:27 -0700 | [diff] [blame] | 1127 | // Circle scale modifier = |
| 1128 | var scale = 500 + 500*(spatialCell.weight / wLimit); |
| 1129 | |
| 1130 | // Return proportionate value so that circles mostly line up. |
| 1131 | return scale * Math.min(distanceBetweenPoints_(point_center, point_left), distanceBetweenPoints_(point_center, point_top)); |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 1132 | } |
| 1133 | |
genia.likes.science@gmail.com | 6d6aa8e | 2013-07-23 01:23:21 -0700 | [diff] [blame] | 1134 | /** |
| 1135 | * Calculates the distance between two latlng locations in km. |
| 1136 | * @see http://www.movable-type.co.uk/scripts/latlong.html |
| 1137 | * |
| 1138 | * @param {google.maps.LatLng} p1 The first lat lng point. |
| 1139 | * @param {google.maps.LatLng} p2 The second lat lng point. |
| 1140 | * @return {number} The distance between the two points in km. |
| 1141 | * @private |
| 1142 | */ |
| 1143 | function distanceBetweenPoints_(p1, p2) { |
| 1144 | if (!p1 || !p2) { |
| 1145 | return 0; |
| 1146 | } |
| 1147 | |
| 1148 | var R = 6371; // Radius of the Earth in km |
| 1149 | var dLat = (p2.lat() - p1.lat()) * Math.PI / 180; |
| 1150 | var dLon = (p2.lng() - p1.lng()) * Math.PI / 180; |
| 1151 | var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + |
| 1152 | Math.cos(p1.lat() * Math.PI / 180) * Math.cos(p2.lat() * Math.PI / 180) * |
| 1153 | Math.sin(dLon / 2) * Math.sin(dLon / 2); |
| 1154 | var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); |
| 1155 | var d = R * c; |
| 1156 | return d; |
Eugenia Gabrielova | ab1ae55 | 2013-10-20 01:12:19 -0700 | [diff] [blame] | 1157 | }; |