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

190 lines
6.2 KiB
JavaScript
Raw Normal View History

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 = {};
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
$.getJSON('/node/search', query, function(results) {
2014-07-28 13:12:44 +00:00
var hintData = {};
for (var keyword in results.columns) {
hintData[keyword] = results.columns[keyword].hints;
2014-07-28 13:12:44 +00:00
}
2014-07-08 04:35:52 +00:00
2014-07-28 13:12:44 +00:00
ctx.grapher.setColumnHints(hintData);
outputResults(results.items, results.count);
2014-07-28 13:12:44 +00:00
});
}
function onSearch() {
2014-09-19 11:26:46 +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
};
$.getJSON('/node/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-09-27 01:10:45 +00:00
ctx.grapher = new grapher.Grapher('grapher', ctx.searchRange, 150, true, true);
2014-07-28 13:12:44 +00:00
ctx.grapher.setColumns(results.columns);
ctx.grapher.setValueChangedListener(onAdjust);
outputResults(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);
});
$('#input').fadeOut(function() {
$('#output').fadeIn();
});
});
}
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 12:29:51 +00:00
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-09-19 06:08:09 +00:00
$.getJSON('/node/addKeyword', query, function(results) {
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-09-20 09:11:13 +00:00
$.getJSON('/node/removeKeyword', query, function(results) {
if (results.success) {
$('#forgetDialog').modal('hide');
}
else {
$('#forgetError').slideDown(function() {
$('#forgetKeyword').prop('disabled', false);
});
}
});
2014-09-19 12:29:51 +00:00
});
}
function outputResults(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();
$.getJSON('/node/getKeywords', 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-09-19 12:29:51 +00:00
$.getJSON('/node/getKeywords', 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
});
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,
*/