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

254 lines
8.5 KiB
JavaScript
Raw Normal View History

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-07-28 13:12:44 +00:00
var ctx = {};
var log = [];
2014-07-28 07:48:24 +00:00
2014-07-28 13:12:44 +00:00
function onAdjust(name, value) {
ctx.searchParams[name] = value;
2014-07-08 04:35:52 +00:00
var query = {
2014-07-28 13:12:44 +00:00
searchParams: ctx.searchParams,
searchRange: ctx.searchRange,
minScore: ctx.minScore,
hintSteps: ctx.hintSteps,
maxResults: ctx.maxResults
};
2014-07-08 04:35:52 +00:00
2014-10-02 07:41:47 +00:00
$.getJSON('/search', query, function(results) {
saveSnapshot(results);
outputSnapshot(results);
2014-07-28 13:12:44 +00:00
});
}
function onSearch() {
2014-10-31 02:51:11 +00:00
var keywords = $('#keywordsToSearch').val() || [];
var searchParams = {};
for (var i = 0, count = keywords.length; i < count; ++i) {
searchParams[keywords[i]] = 1.0;
}
var query = {
searchParams: searchParams,
searchRange: { min: -1.0, max: 1.0 },
minScore: parseFloat($('#minScore').val()),
hintSteps: parseInt($('#hintSteps').val()),
maxResults: parseInt($('#maxResults').val())
2014-07-28 13:12:44 +00:00
};
2014-10-02 07:41:47 +00:00
$.getJSON('/search', query, function(results) {
ctx.searchParams = query.searchParams;
ctx.searchRange = query.searchRange;
ctx.minScore = query.minScore;
ctx.hintSteps = query.hintSteps;
ctx.maxResults = query.maxResults;
2014-07-28 13:12:44 +00:00
2014-10-31 02:51:11 +00:00
ctx.grapher = new grapher.Grapher({
2014-10-31 03:11:48 +00:00
canvas: new Snap('#svg'),
2014-10-31 06:58:25 +00:00
steps: ctx.hintSteps,
2014-10-31 06:25:59 +00:00
range: ctx.searchRange,
2014-10-31 02:51:11 +00:00
useLocalScale: true,
useRelativeScale: true
});
2014-07-28 13:12:44 +00:00
ctx.grapher.setColumns(results.columns);
ctx.grapher.setValueChangedListener(onAdjust);
saveSnapshot(results);
outputMatches(results.items, results.count);
2014-07-28 13:12:44 +00:00
$('#query').text(keywords.join(', '));
2014-07-28 13:12:44 +00:00
$('#useLocalScale').click(function() {
var useLocalScale = $('#useLocalScale').is(':checked');
ctx.grapher.setUseLocalScale(useLocalScale);
});
$('#useRelativeScale').click(function() {
var useRelativeScale = $('#useRelativeScale').is(':checked');
ctx.grapher.setUseRelativeScale(useRelativeScale);
});
2014-10-31 03:11:48 +00:00
$('#input').fadeOut(function() {
$('#output').fadeIn();
});
2014-07-28 13:12:44 +00:00
});
}
2014-07-28 07:21:08 +00:00
2014-09-18 08:32:59 +00:00
function onLearn() {
2014-09-19 11:15:34 +00:00
$('#learnKeyword').prop('disabled', true);
2014-09-19 06:08:09 +00:00
$('#learnError').slideUp(function() {
var query = {
2014-09-19 11:15:34 +00:00
keyword: $('#keywordToLearn').val(),
2014-09-19 06:08:09 +00:00
params: ctx.searchParams
};
2014-09-19 12:29:51 +00:00
2014-10-02 07:41:47 +00:00
$.getJSON('/add_keyword', query, function(results) {
2014-09-19 06:08:09 +00:00
if (results.success) {
2014-09-19 11:15:34 +00:00
$('#learnDialog').modal('hide');
2014-09-19 06:08:09 +00:00
}
else {
$('#learnError').slideDown(function() {
2014-09-19 11:15:34 +00:00
$('#learnKeyword').prop('disabled', false);
2014-09-19 06:08:09 +00:00
});
}
});
2014-09-18 08:32:59 +00:00
});
}
2014-09-19 12:29:51 +00:00
function onForget() {
$('#forgetKeyword').prop('disabled', true);
2014-09-20 09:11:13 +00:00
$('#forgetError').slideUp(function() {
var query = {
keyword: $('#keywordToForget').val()
};
2014-09-19 12:29:51 +00:00
2014-10-02 07:41:47 +00:00
$.getJSON('/remove_keyword', query, function(results) {
2014-09-20 09:11:13 +00:00
if (results.success) {
$('#forgetDialog').modal('hide');
}
else {
$('#forgetError').slideDown(function() {
$('#forgetKeyword').prop('disabled', false);
});
}
});
2014-09-19 12:29:51 +00:00
});
}
function onSelectSnapshot() {
var index = $('#history').slider('getValue');
outputSnapshot(log[index]);
}
function saveSnapshot(results) {
log.push(results);
var count = log.length;
var history = $('#history').slider();
history.slider('setAttribute', 'max', count - 1);
history.slider('setValue', count - 1);
if (count > 1) {
$('#history').parent().slideDown();
}
}
2014-10-04 10:57:31 +00:00
function outputSnapshot(results) {
ctx.grapher.updateColumns(results.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;
}
$('#count').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();
2014-07-28 13:12:44 +00:00
$('#results').append(template({'results': results}));
}
$(document).on({
ajaxStart: function() {
$('#spinner').show();
},
ajaxStop: function() {
$('#spinner').hide();
},
ready: function() {
2014-09-19 11:26:46 +00:00
$('#keywordsToSearch').selectpicker();
2014-10-15 07:36:56 +00:00
$('#history').slider({
formatter: function(value) {
var delta = log.length - (value + 1);
switch (delta) {
case 0:
return 'Most recent query';
2014-10-15 07:49:42 +00:00
case 1:
return 'Previous query';
2014-10-15 07:41:40 +00:00
default:
2014-10-15 07:49:42 +00:00
return String(delta) + ' queries back';
2014-10-15 07:36:56 +00:00
}
}
});
2014-10-02 07:41:47 +00:00
$.getJSON('/get_keywords', function(keywords) {
2014-09-19 12:29:51 +00:00
$('#searchKeywords').click(onSearch);
for (var i = 0, count = keywords.length; i < count; ++i) {
2014-09-19 11:26:46 +00:00
$('#keywordsToSearch').append($('<option></option>', {
value: keywords[i],
text: keywords[i]
}));
}
2014-09-19 12:29:51 +00:00
$('#keywordsToSearch').selectpicker('refresh');
$('#keywordsToSearch').change(function() {
$('#searchKeywords').prop('disabled', !$(this).val());
});
2014-09-19 12:29:51 +00:00
$('#forgetKeyword').click(onForget);
$('#forgetDialog').on('show.bs.modal', function() {
$('#forgetError').hide();
2014-10-02 07:41:47 +00:00
$.getJSON('/get_keywords', function(keywords) {
2014-09-20 09:11:13 +00:00
$('#forgetKeyword').prop('disabled', keywords.length === 0);
2014-09-20 09:13:50 +00:00
$('#keywordToForget').empty();
2014-09-19 12:29:51 +00:00
for (var i = 0, count = keywords.length; i < count; ++i) {
$('#keywordToForget').append($('<option></option>', {
value: keywords[i],
text: keywords[i]
}));
}
});
2014-09-18 14:35:55 +00:00
});
$('#history').on('slideStop', onSelectSnapshot);
2014-09-19 11:15:34 +00:00
$('#learnKeyword').click(onLearn);
$('#keywordToLearn').bind('input', function() {
$('#learnKeyword').prop('disabled', !$(this).val());
2014-09-18 08:47:57 +00:00
});
2014-09-19 12:29:51 +00:00
$('#learnDialog').on('show.bs.modal', function() {
$('#learnKeyword').prop('disabled', true);
$('#keywordToLearn').val('');
$('#learnError').hide();
});
});
}
2014-07-26 06:02:42 +00:00
});
2014-07-28 13:12:44 +00:00
}(window.hscd = window.hscd || {}));
2014-09-28 07:58:56 +00:00
/*
global
$,
Handlebars,
document,
grapher,
window,
*/