1
restaurant-search/client/application.js

113 lines
3.5 KiB
JavaScript
Raw Normal View History

2014-07-08 04:35:52 +00:00
'use strict';
2014-07-28 07:38:03 +00:00
2014-07-28 13:12:44 +00:00
(function(hscd) {
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
2014-07-28 13:12:44 +00:00
var params = {
searchParams: ctx.searchParams,
searchRange: ctx.searchRange,
minScore: ctx.minScore,
hintSteps: ctx.hintSteps,
maxResults: ctx.maxResults
};
2014-07-08 04:35:52 +00:00
2014-07-28 13:12:44 +00:00
$.getJSON('/node/search', params, function(results) {
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() {
var params = {
keywords: $('#keywords').val(),
2014-07-28 13:12:44 +00:00
searchRange: { min: -1.0, max: 1.0 },
2014-09-13 09:25:33 +00:00
minScore: parseFloat($('#minScore').val()),
2014-07-28 13:12:44 +00:00
hintSteps: parseInt($('#hintSteps').val()),
maxResults: parseInt($('#maxResults').val())
};
$.getJSON('/node/search', params, function(results) {
ctx.searchParams = results.params;
ctx.searchRange = params.searchRange;
ctx.minScore = params.minScore;
ctx.hintSteps = params.hintSteps;
ctx.maxResults = params.maxResults;
2014-09-16 02:17:33 +00:00
ctx.grapher = new 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
if (params.keywords) {
$('#query').text(params.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
function outputResults(results, count) {
2014-07-28 13:12:44 +00:00
$('#results').empty();
2014-07-28 07:21:08 +00:00
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());
$('#results').append(template({'results': results}));
}
$(document).on({
ajaxStart: function() {
$('#spinner').show();
},
ajaxStop: function() {
$('#spinner').hide();
},
ready: function() {
$('#keywords').selectpicker();
$.getJSON('/node/keywords', function(keywords) {
for (var i = 0, count = keywords.length; i < count; ++i) {
$('#keywords').append($('<option></option>', {
value: keywords[i],
text: keywords[i]
}));
}
$('#keywords').selectpicker('refresh');
$('#keywords').change(function() {
$('#search').prop('disabled', $(this).val() === null);
});
$('#search').click(onSearch);
});
}
2014-07-26 06:02:42 +00:00
});
2014-07-28 13:12:44 +00:00
}(window.hscd = window.hscd || {}));