1
restaurant-search/static/scripts/search.js

221 lines
7.4 KiB
JavaScript
Raw Normal View History

2014-10-03 02:43:56 +00:00
/*
* Copyright (c) 2015 Alex Yatskov <alex@foosoft.net>
2015-03-11 07:18:44 +00:00
* Author: 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
2015-06-29 10:09:34 +00:00
function stateChanged(name, value, mode) {
2014-11-08 05:33:36 +00:00
_ctx.query.features[name] = value;
2015-06-29 04:06:26 +00:00
_ctx.query.modes[name] = mode;
2015-01-15 11:06:22 +00:00
2015-03-24 03:45:18 +00:00
$.post('/query', JSON.stringify(_ctx.query), function(results) {
saveSnapshot(results);
2014-11-10 10:14:45 +00:00
outputSnapshot(results, true);
2015-03-24 03:45:18 +00:00
}, 'json');
2014-07-28 13:12:44 +00:00
}
2015-06-29 10:09:34 +00:00
function ready(geo) {
2015-03-27 03:03:23 +00:00
_ctx = {
sortKey: 'score',
sortAsc: false,
query: {},
geo: geo
};
2015-03-26 03:18:43 +00:00
Handlebars.registerHelper('prettyFloat', function(precision, options) {
return parseFloat(options.fn(this)).toFixed(precision);
});
2014-11-17 07:13:23 +00:00
2015-09-20 03:36:45 +00:00
Handlebars.registerHelper('urlEncode', function(options) {
return encodeURIComponent(options.fn(this));
});
2015-04-19 01:30:36 +00:00
$('#minScore,#resolution,#walkingDist,#maxResults').change(onSearch);
2015-03-11 02:56:52 +00:00
$('#profileDlg').on('hidden.bs.modal', onSearch);
2015-03-17 06:23:57 +00:00
$('#resetStorage').click(function() {
if (confirm('Are you sure you want to reset your profile?')) {
localStorage.clear();
$('iframe').attr('src', $('iframe').attr('src'));
}
});
2014-11-08 07:13:06 +00:00
2015-03-11 02:56:52 +00:00
window.accessReview = function(id) {
2015-03-26 03:18:43 +00:00
$.post('/access', JSON.stringify({id: id, profile: getProfile()}));
onSearch();
2015-03-11 02:56:52 +00:00
};
2015-03-09 10:20:19 +00:00
2015-03-27 03:03:23 +00:00
window.sortReviewsBy = function(sortKey) {
if (sortKey === _ctx.sortKey) {
_ctx.sortAsc = !_ctx.sortAsc;
}
else {
_ctx.sortKey = sortKey;
}
onSearch();
};
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-06-29 04:06:26 +00:00
modes: _ctx.query.modes || {},
2015-03-27 03:03:23 +00:00
sortKey: _ctx.sortKey,
sortAsc: _ctx.sortAsc,
2015-03-11 07:18:44 +00:00
profile: getProfile(),
2015-01-05 07:26:47 +00:00
walkingDist: parseFloat($('#walkingDist').val()),
minScore: parseFloat($('#minScore').val()),
2015-04-19 01:30:36 +00:00
resolution: parseInt($('#resolution').val()),
2015-01-05 07:26:47 +00:00
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-03-24 03:45:18 +00:00
$.post('/query', JSON.stringify(_ctx.query), function(results) {
2015-01-15 11:06:22 +00:00
if (!_.has(_ctx, 'grapher')) {
_ctx.grapher = new grapher.Grapher({
2015-06-29 10:09:34 +00:00
canvas: new Snap('#svg'),
stateChanged: stateChanged,
displayType: $('#displayType').val(),
useLocalScale: $('#useLocalScale').is(':checked')
2015-01-15 11:06:22 +00:00
});
$('#useLocalScale').click(function() {
2015-03-21 11:46:20 +00:00
_ctx.grapher.setUseLocalScale($('#useLocalScale').is(':checked'));
2015-01-15 11:06:22 +00:00
});
$('#useRelativeScale').click(function() {
2015-03-21 11:46:20 +00:00
_ctx.grapher.setUseRelativeScale($('#useRelativeScale').is(':checked'));
});
$('#displayType').change(function() {
_ctx.grapher.setDisplayType($('#displayType').val());
2015-01-15 11:06:22 +00:00
});
var columns = {};
for (var feature in results.columns) {
var column = results.columns[feature];
_ctx.query.features[feature] = column.value;
columns[feature] = {
2015-06-27 04:53:26 +00:00
value: column.value,
hints: column.hints,
2015-06-29 04:06:26 +00:00
bracket: column.bracket,
mode: column.mode
2015-01-15 11:06:22 +00:00
};
}
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);
2015-03-24 03:45:18 +00:00
}, 'json');
2014-07-28 13:12:44 +00:00
}
2014-07-28 07:21:08 +00:00
function saveSnapshot(results) {
2015-03-26 08:58:37 +00:00
window.history.pushState(results, null, null);
}
2014-10-04 10:57:31 +00:00
2014-11-10 10:14:45 +00:00
function outputSnapshot(results, omitValues) {
2015-08-24 07:29:28 +00:00
var records = results.records || [];
2015-06-28 04:30:22 +00:00
_ctx.query.minScore = results.minScore;
$('#minScore').val(results.minScore);
2014-11-10 10:14:45 +00:00
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-04 10:57:31 +00:00
2015-08-24 07:29:28 +00:00
var searchResultCnt = String(records.length);
if (records.length < results.count) {
2015-03-26 03:18:43 +00:00
searchResultCnt += ' of ' + results.count;
}
2014-11-08 10:16:35 +00:00
$('#resultCount').text(searchResultCnt);
2015-07-31 04:13:31 +00:00
$('#elapsedTime').text(Math.round(results.elapsedTime / 1000000) + ' ms');
2014-07-28 07:21:08 +00:00
2014-07-28 13:12:44 +00:00
var template = Handlebars.compile($('#template').html());
2015-03-25 11:25:27 +00:00
$('#records').empty();
2015-08-24 07:29:28 +00:00
$('#records').append(template({records: records}));
2015-01-15 11:06:22 +00:00
2015-03-27 03:46:59 +00:00
$('span.sort-icon').css('visibility', 'hidden');
var currentColumn = $('span.sort-icon[data-sort="' + _ctx.sortKey + '"]').css('visibility', 'visible');
if (_ctx.sortAsc) {
currentColumn.removeClass('glyphicon-chevron-down').addClass('glyphicon-chevron-up');
}
else {
currentColumn.removeClass('glyphicon-chevron-up').addClass('glyphicon-chevron-down');
}
2015-08-24 07:29:28 +00:00
if (records.length === 0) {
2015-01-15 11:06:22 +00:00
$('#resultPanel').slideUp();
}
else {
$('#resultPanel').slideDown();
}
2014-07-28 13:12:44 +00:00
}
2015-03-11 07:18:44 +00:00
function getProfile() {
2015-03-24 11:52:40 +00:00
return JSON.parse(localStorage.profile || '{}');
2015-03-11 07:18: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(
2015-06-29 10:09:34 +00:00
function(geo) { ready(geo); },
function(err) { ready(null); },
2015-01-05 09:08:24 +00:00
{ enableHighAccuracy: true }
);
}
else {
2015-06-29 10:09:34 +00:00
ready(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 || {}));