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