1
restaurant-search/client/scripts/application.js

185 lines
6.0 KiB
JavaScript
Raw Normal View History

2014-10-03 02:43:56 +00:00
/*
* Copyright (c) 2015 Alex Yatskov <alex@foosoft.net>
*
* 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-10-03 02:43:56 +00:00
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) {
saveSnapshot(results);
2014-11-10 10:14:45 +00:00
outputSnapshot(results, 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({
formatter: function(value) {
2014-11-08 05:33:36 +00:00
var delta = _ctx.log.length - (value + 1);
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;
2014-11-09 03:01:26 +00:00
onSearch();
2014-11-08 07:13:06 +00:00
$('#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
});
}
2014-11-08 14:23:56 +00:00
2015-01-05 09:08:24 +00:00
function onSearch(provider) {
2014-11-08 05:33:36 +00:00
_ctx.query = {
features: _clone(_ctx.query.features),
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) {
_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-07-28 13:12:44 +00:00
});
}
2014-07-28 07:21:08 +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);
}
function saveSnapshot(results) {
2014-11-08 05:33:36 +00:00
_ctx.log.push(results);
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();
history.slider('setAttribute', 'max', count - 1);
history.slider('setValue', count - 1);
if (count > 1) {
2014-11-09 03:15:27 +00:00
$('#history').show();
}
}
2014-10-04 10:57:31 +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);
outputMatches(results.items, results.count);
2014-10-04 10:57:31 +00:00
}
function outputMatches(results, count) {
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();
$('#results').append(template({results: results}));
2014-07-28 13:12:44 +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() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(
function(geo) { onReady(geo); },
function(err) { onReady(null); },
2015-01-05 09:08:24 +00:00
{ enableHighAccuracy: true }
);
}
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 || {}));