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

167 lines
5.4 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';
2015-01-15 11:06:22 +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;
2015-01-15 11:06:22 +00:00
2014-11-08 05:33:36 +00:00
$.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 = {
2015-01-15 11:06:22 +00:00
geo: geo,
query: {}
2014-11-17 07:13:23 +00:00
};
2015-01-15 11:06:22 +00:00
$('#minScore,#hintSteps,#walkingDist,#maxResults').change(onSearch);
2014-11-08 07:13:06 +00:00
2015-01-15 11:06:22 +00:00
onSearch();
2014-11-09 03:01:26 +00:00
}
2014-11-08 14:23:56 +00:00
2015-01-15 11:06:22 +00:00
function onSearch() {
2014-11-08 05:33:36 +00:00
_ctx.query = {
2015-01-15 11:06:22 +00:00
features: _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
}
2015-01-15 11:06:22 +00:00
$.getJSON('/search', _ctx.query, function(results) {
if (!_.has(_ctx, 'grapher')) {
_ctx.grapher = new grapher.Grapher({
canvas: new Snap('#svg'),
steps: _ctx.query.hintSteps,
range: _ctx.query.range,
onValueChanged: onAdjust,
useLocalScale: true,
useRelativeScale: true
});
$('#useLocalScale').click(function() {
var useLocalScale = $('#useLocalScale').is(':checked');
_ctx.grapher.setUseLocalScale(useLocalScale);
});
$('#useRelativeScale').click(function() {
var useRelativeScale = $('#useRelativeScale').is(':checked');
_ctx.grapher.setUseRelativeScale(useRelativeScale);
});
var columns = {};
for (var feature in results.columns) {
var column = results.columns[feature];
_ctx.query.features[feature] = column.value;
columns[feature] = {
value: column.value,
hints: column.hints
};
}
2014-11-08 05:33:36 +00:00
2015-01-15 11:06:22 +00:00
_ctx.grapher.setColumns(columns);
}
2014-11-08 05:33:36 +00:00
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 saveSnapshot(results) {
2015-02-01 10:43:34 +00:00
window.history.pushState(results);
}
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);
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();
2015-01-15 11:06:22 +00:00
$('#results').append(
template({ results: results })
);
if (results.length === 0) {
$('#resultPanel').slideUp();
}
else {
$('#resultPanel').slideDown();
}
2014-07-28 13:12:44 +00:00
}
2015-02-01 10:43:34 +00:00
window.onpopstate = function(state) {
if (state.state) {
outputSnapshot(state.state, false);
}
};
$(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 || {}));