2014-10-03 02:43:56 +00:00
|
|
|
/*
|
|
|
|
|
|
|
|
The MIT License (MIT)
|
|
|
|
|
|
|
|
Copyright (c) 2014 Alex Yatskov
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2014-07-28 13:12:44 +00:00
|
|
|
(function(hscd) {
|
2014-09-28 07:58:56 +00:00
|
|
|
'use strict';
|
|
|
|
|
2014-11-17 07:13:23 +00:00
|
|
|
var _ctx = { };
|
2014-07-28 07:48:24 +00:00
|
|
|
|
2014-07-28 13:12:44 +00:00
|
|
|
function onAdjust(name, value) {
|
2014-11-08 05:33:36 +00:00
|
|
|
_ctx.query.features[name] = value;
|
|
|
|
$.getJSON('/search', _ctx.query, function(results) {
|
2014-10-15 06:52:45 +00:00
|
|
|
saveSnapshot(results);
|
2014-11-10 10:14:45 +00:00
|
|
|
outputSnapshot(results, true);
|
2014-11-09 03:01:26 +00:00
|
|
|
setCustomized(true);
|
2014-07-28 13:12:44 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-11-17 07:13:23 +00:00
|
|
|
function onReady(geo) {
|
|
|
|
_ctx = {
|
|
|
|
log: [],
|
|
|
|
geo: geo
|
|
|
|
};
|
|
|
|
|
2014-11-08 10:16:35 +00:00
|
|
|
$('#historyIndex').slider({
|
2014-11-08 02:23:42 +00:00
|
|
|
formatter: function(value) {
|
2014-11-08 05:33:36 +00:00
|
|
|
var delta = _ctx.log.length - (value + 1);
|
2014-11-08 02:23:42 +00:00
|
|
|
switch (delta) {
|
|
|
|
case 0:
|
|
|
|
return 'Most recent query';
|
|
|
|
case 1:
|
|
|
|
return 'Previous query';
|
|
|
|
default:
|
|
|
|
return String(delta) + ' queries back';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-11-08 05:33:36 +00:00
|
|
|
$.getJSON('/get_parameters', function(parameters) {
|
|
|
|
_ctx.parameters = parameters;
|
|
|
|
for (var keyword in parameters.keywords) {
|
2014-11-08 07:13:06 +00:00
|
|
|
$('#searchKeyword').append($('<option></option>', { value: keyword, text: keyword }));
|
2014-11-08 02:23:42 +00:00
|
|
|
}
|
|
|
|
|
2014-11-09 03:01:26 +00:00
|
|
|
onSearch();
|
2014-11-08 07:13:06 +00:00
|
|
|
|
2015-01-05 09:17:40 +00:00
|
|
|
$('#searchKeyword').change(function() { onSearch(); });
|
|
|
|
$('#minScore,#hintSteps,#walkingDist,#maxResults').change(function() { onSearch(getFeaturesGrapher); });
|
2014-11-08 10:16:35 +00:00
|
|
|
$('#historyIndex').on('slideStop', onSelectSnapshot);
|
2014-11-09 03:01:26 +00:00
|
|
|
$('#learn').click(onLearn);
|
|
|
|
$('#forget').click(onForget);
|
2014-11-09 04:19:45 +00:00
|
|
|
$('#customized').click(onReset);
|
2014-11-09 03:01:26 +00:00
|
|
|
});
|
|
|
|
}
|
2014-11-08 14:23:56 +00:00
|
|
|
|
2014-11-09 04:19:45 +00:00
|
|
|
function onReset() {
|
|
|
|
if (confirm('Reset customizations?')) {
|
|
|
|
onSearch();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-09 03:01:26 +00:00
|
|
|
function onLearn() {
|
2014-11-10 10:45:07 +00:00
|
|
|
var keyword = prompt('Learn keyword as');
|
2014-11-09 03:01:26 +00:00
|
|
|
if (keyword === null) {
|
|
|
|
return;
|
|
|
|
}
|
2014-11-08 14:23:56 +00:00
|
|
|
|
2014-11-09 03:01:26 +00:00
|
|
|
var query = {
|
|
|
|
keyword: keyword,
|
|
|
|
features: _ctx.query.features
|
|
|
|
};
|
|
|
|
|
|
|
|
$.getJSON('/add_keyword', query, function(results) {
|
|
|
|
if (results.success) {
|
2014-11-09 03:57:28 +00:00
|
|
|
_ctx.parameters.keywords[keyword] = _.clone(query.features);
|
2014-11-09 03:01:26 +00:00
|
|
|
$('#searchKeyword').append($('<option></option>', { value: keyword, text: keyword }));
|
|
|
|
$('#searchKeyword').val(keyword);
|
2014-11-09 03:42:35 +00:00
|
|
|
setCustomized(false);
|
2014-11-09 03:01:26 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
alert('Failed to learn keyword');
|
|
|
|
}
|
2014-11-08 02:23:42 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-11-09 03:01:26 +00:00
|
|
|
function onForget() {
|
2014-11-09 03:15:27 +00:00
|
|
|
var keyword = $('#searchKeyword').val();
|
|
|
|
if (!confirm('Are you sure you want to delete keyword "' + keyword + '"?')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var query = {
|
|
|
|
keyword: keyword
|
|
|
|
};
|
2014-11-09 03:01:26 +00:00
|
|
|
|
2014-11-09 03:15:27 +00:00
|
|
|
$.getJSON('/remove_keyword', query, function(results) {
|
|
|
|
if (results.success) {
|
|
|
|
$('#searchKeyword option:selected').remove();
|
|
|
|
onSearch();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
alert('Failed to forget keyword');
|
|
|
|
}
|
|
|
|
});
|
2014-11-09 03:01:26 +00:00
|
|
|
}
|
|
|
|
|
2015-01-05 09:08:24 +00:00
|
|
|
function getFeaturesKeyword() {
|
2014-11-08 07:13:06 +00:00
|
|
|
var keyword = $('#searchKeyword').val();
|
2015-01-05 09:08:24 +00:00
|
|
|
return _.clone(_ctx.parameters.keywords[keyword]);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getFeaturesGrapher() {
|
|
|
|
return _.clone(_ctx.query.features);
|
|
|
|
}
|
2014-11-08 05:33:36 +00:00
|
|
|
|
2015-01-05 09:08:24 +00:00
|
|
|
function onSearch(provider) {
|
2014-11-08 05:33:36 +00:00
|
|
|
_ctx.query = {
|
2015-01-05 09:08:24 +00:00
|
|
|
features: (provider || getFeaturesKeyword)(),
|
2015-01-05 07:26:47 +00:00
|
|
|
range: { min: -1.0, max: 1.0 },
|
|
|
|
walkingDist: parseFloat($('#walkingDist').val()),
|
|
|
|
minScore: parseFloat($('#minScore').val()),
|
|
|
|
hintSteps: parseInt($('#hintSteps').val()),
|
|
|
|
maxResults: parseInt($('#maxResults').val())
|
2014-07-28 13:12:44 +00:00
|
|
|
};
|
|
|
|
|
2014-11-17 07:13:23 +00:00
|
|
|
if (_ctx.geo) {
|
2014-11-17 07:29:02 +00:00
|
|
|
_ctx.query.geo = {
|
|
|
|
latitude: _ctx.geo.coords.latitude,
|
|
|
|
longitude: _ctx.geo.coords.longitude
|
|
|
|
};
|
2014-11-17 07:13:23 +00:00
|
|
|
}
|
|
|
|
|
2014-11-08 05:33:36 +00:00
|
|
|
if (!_.has(_ctx, 'grapher')) {
|
|
|
|
_ctx.grapher = new grapher.Grapher({
|
2014-10-31 03:11:48 +00:00
|
|
|
canvas: new Snap('#svg'),
|
2014-11-08 05:33:36 +00:00
|
|
|
steps: _ctx.query.hintSteps,
|
|
|
|
range: _ctx.query.range,
|
2014-10-31 07:36:20 +00:00
|
|
|
onValueChanged: onAdjust,
|
2014-10-31 02:51:11 +00:00
|
|
|
useLocalScale: true,
|
|
|
|
useRelativeScale: true
|
|
|
|
});
|
2014-07-28 13:12:44 +00:00
|
|
|
|
|
|
|
$('#useLocalScale').click(function() {
|
|
|
|
var useLocalScale = $('#useLocalScale').is(':checked');
|
2014-11-08 05:33:36 +00:00
|
|
|
_ctx.grapher.setUseLocalScale(useLocalScale);
|
2014-07-28 13:12:44 +00:00
|
|
|
});
|
|
|
|
$('#useRelativeScale').click(function() {
|
|
|
|
var useRelativeScale = $('#useRelativeScale').is(':checked');
|
2014-11-08 05:33:36 +00:00
|
|
|
_ctx.grapher.setUseRelativeScale(useRelativeScale);
|
2014-07-28 13:12:44 +00:00
|
|
|
});
|
2014-11-08 03:31:57 +00:00
|
|
|
|
2014-11-08 05:33:36 +00:00
|
|
|
var columns = {};
|
|
|
|
for (var feature in _ctx.query.features) {
|
2014-11-10 10:31:42 +00:00
|
|
|
columns[feature] = {
|
|
|
|
value: 0.0,
|
|
|
|
hints: []
|
|
|
|
};
|
2014-11-08 05:33:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_ctx.grapher.setColumns(columns);
|
|
|
|
}
|
|
|
|
|
|
|
|
$.getJSON('/search', _ctx.query, function(results) {
|
|
|
|
saveSnapshot(results);
|
2014-11-10 10:14:45 +00:00
|
|
|
outputSnapshot(results, false);
|
2014-11-09 03:01:26 +00:00
|
|
|
setCustomized(false);
|
2014-07-28 13:12:44 +00:00
|
|
|
});
|
|
|
|
}
|
2014-07-28 07:21:08 +00:00
|
|
|
|
2014-10-15 06:52:45 +00:00
|
|
|
function onSelectSnapshot() {
|
2014-11-08 10:16:35 +00:00
|
|
|
var index = $('#historyIndex').slider('getValue');
|
2014-11-10 10:14:45 +00:00
|
|
|
outputSnapshot(_ctx.log[index], false);
|
2014-11-09 03:01:26 +00:00
|
|
|
setCustomized(true);
|
2014-10-15 06:52:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function saveSnapshot(results) {
|
2014-11-08 05:33:36 +00:00
|
|
|
_ctx.log.push(results);
|
2014-10-15 06:52:45 +00:00
|
|
|
|
2014-11-08 05:33:36 +00:00
|
|
|
var count = _ctx.log.length;
|
2014-11-08 10:16:35 +00:00
|
|
|
var history = $('#historyIndex').slider();
|
2014-10-15 07:26:47 +00:00
|
|
|
history.slider('setAttribute', 'max', count - 1);
|
|
|
|
history.slider('setValue', count - 1);
|
2014-10-15 06:52:45 +00:00
|
|
|
|
2014-10-15 07:26:47 +00:00
|
|
|
if (count > 1) {
|
2014-11-09 03:15:27 +00:00
|
|
|
$('#history').show();
|
2014-10-15 06:52:45 +00:00
|
|
|
}
|
2014-10-15 07:26:47 +00:00
|
|
|
}
|
2014-10-04 10:57:31 +00:00
|
|
|
|
2014-11-09 03:01:26 +00:00
|
|
|
function setCustomized(customized) {
|
|
|
|
if (customized) {
|
|
|
|
$('#customized').show();
|
2014-11-09 03:36:41 +00:00
|
|
|
$('#forget').hide();
|
2014-11-09 03:01:26 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
$('#customized').hide();
|
2014-11-09 03:36:41 +00:00
|
|
|
$('#forget').show();
|
2014-11-09 03:01:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-10 10:14:45 +00:00
|
|
|
function outputSnapshot(results, omitValues) {
|
|
|
|
var columns = {};
|
2014-11-05 11:38:58 +00:00
|
|
|
for (var name in results.columns) {
|
2014-11-10 10:14:45 +00:00
|
|
|
var column = results.columns[name];
|
|
|
|
columns[name] = omitValues ? _.omit(column, 'value') : column;
|
|
|
|
_ctx.query.features[name] = column.value;
|
2014-11-05 11:38:58 +00:00
|
|
|
}
|
|
|
|
|
2014-11-10 10:14:45 +00:00
|
|
|
_ctx.grapher.setColumns(columns);
|
2014-10-15 06:52:45 +00:00
|
|
|
outputMatches(results.items, results.count);
|
2014-10-04 10:57:31 +00:00
|
|
|
}
|
|
|
|
|
2014-10-15 06:52:45 +00:00
|
|
|
function outputMatches(results, count) {
|
2014-09-16 02:00:15 +00:00
|
|
|
var searchResultCnt = String(results.length);
|
|
|
|
if (results.length < count) {
|
|
|
|
searchResultCnt += ' of ' + count;
|
|
|
|
}
|
2014-11-08 10:16:35 +00:00
|
|
|
|
|
|
|
$('#resultCount').text(searchResultCnt);
|
|
|
|
$('#resultPanel').slideDown();
|
2014-07-28 07:21:08 +00:00
|
|
|
|
2014-07-28 13:12:44 +00:00
|
|
|
var template = Handlebars.compile($('#template').html());
|
2014-09-19 11:26:46 +00:00
|
|
|
$('#results').empty();
|
2014-11-17 07:29:02 +00:00
|
|
|
$('#results').append(template({results: results}));
|
2014-07-28 13:12:44 +00:00
|
|
|
}
|
|
|
|
|
2014-09-12 07:54:21 +00:00
|
|
|
$(document).on({
|
2014-11-08 07:13:06 +00:00
|
|
|
ajaxStart: function() {
|
|
|
|
$('#spinner').show();
|
|
|
|
},
|
|
|
|
|
|
|
|
ajaxStop: function() {
|
|
|
|
$('#spinner').hide();
|
|
|
|
},
|
|
|
|
|
2014-11-17 07:13:23 +00:00
|
|
|
ready: function() {
|
2014-11-17 07:33:15 +00:00
|
|
|
if (navigator.geolocation) {
|
|
|
|
navigator.geolocation.getCurrentPosition(
|
|
|
|
function(geo) { onReady(geo); },
|
|
|
|
function(err) { onReady(null); },
|
2015-01-05 09:08:24 +00:00
|
|
|
{ enableHighAccuracy: true }
|
2014-11-17 07:33:15 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
onReady(null);
|
|
|
|
}
|
2014-11-17 07:13:23 +00:00
|
|
|
}
|
2014-07-26 06:02:42 +00:00
|
|
|
});
|
2014-07-28 13:12:44 +00:00
|
|
|
}(window.hscd = window.hscd || {}));
|