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

247 lines
7.8 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';
var ctx = {
log: []
};
2014-07-28 07:48:24 +00:00
2014-07-28 13:12:44 +00:00
function onAdjust(name, value) {
ctx.features[name] = value;
2014-07-08 04:35:52 +00:00
var query = {
features: ctx.features,
range: ctx.range,
minScore: ctx.minScore,
hintSteps: ctx.hintSteps,
maxResults: ctx.maxResults
2014-07-28 13:12:44 +00:00
};
2014-07-08 04:35:52 +00:00
2014-11-05 11:34:35 +00:00
ctx.grapher.enable(false);
2014-10-02 07:41:47 +00:00
$.getJSON('/search', query, function(results) {
saveSnapshot(results);
outputSnapshot(results);
2014-11-05 11:34:35 +00:00
ctx.grapher.enable(true);
2014-07-28 13:12:44 +00:00
});
}
function onReady() {
$('#history').on('slideStop', onSelectSnapshot);
$('#history').slider({
formatter: function(value) {
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';
}
}
});
$('#forgetKeyword').click(onForget);
$('#forgetDialog').on('show.bs.modal', function() {
$('#forgetError').hide();
$.getJSON('/get_keywords', function(keywords) {
$('#keywordToForget').empty();
for (var i = 0, count = keywords.length; i < count; ++i) {
$('#keywordToForget').append($('<option></option>', {
value: keywords[i],
text: keywords[i]
}));
}
});
});
$('#learnKeyword').click(onLearn);
$('#learnDialog').on('show.bs.modal', function() {
$('#learnError').hide();
$('#learnKeyword').prop('disabled', true);
$('#keywordToLearn').val('');
});
$('#keywordToLearn').bind('input', function() {
$('#learnKeyword').prop('disabled', !$(this).val());
});
$.getJSON('/get_keywords', function(keywords) {
ctx.keywords = keywords;
for (var keyword in keywords) {
$('#keywordsToSearch').append($('<option></option>', { value: keyword, text: keyword }));
}
onSearch();
});
}
2014-07-28 13:12:44 +00:00
function onSearch() {
var keyword = $('#keywordsToSearch').val();
var features = {};
for (var feature in ctx.keywords[keyword]) {
features[feature] = 1.0;
}
var query = {
features: features,
range: { 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.features = query.features;
ctx.range = query.range;
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,
range: ctx.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
ctx.grapher.setColumns(results.columns);
saveSnapshot(results);
outputMatches(results.items, results.count);
2014-07-28 13:12:44 +00:00
$('#query').text(keyword);
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-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(),
params: ctx.features
2014-09-19 06:08:09 +00:00
};
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(ctx.log[index]);
}
function saveSnapshot(results) {
ctx.log.push(results);
var count = ctx.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) {
2014-11-05 11:38:58 +00:00
for (var name in results.columns) {
ctx.features[name] = results.columns[name].value;
2014-11-05 11:38:58 +00:00
}
ctx.grapher.setColumns(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: onReady()
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,
*/