1
restaurant-search/client/application.js

150 lines
4.7 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
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() {
var keywords = $('#keywords').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-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
$('#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 06:08:09 +00:00
$('#learn').prop('disabled', true);
2014-09-18 08:32:59 +00:00
2014-09-19 06:08:09 +00:00
$('#learnError').slideUp(function() {
var query = {
keyword: $('#keyword').val(),
params: ctx.searchParams
};
$.getJSON('/node/addKeyword', query, function(results) {
if (results.success) {
$('#learnDlg').modal('hide');
}
else {
$('#learnError').slideDown(function() {
$('#learn').prop('disabled', false);
});
}
});
2014-09-18 08:32:59 +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/getKeywords', function(keywords) {
for (var i = 0, count = keywords.length; i < count; ++i) {
$('#keywords').append($('<option></option>', {
value: keywords[i],
text: keywords[i]
}));
}
$('#search').click(onSearch);
2014-09-18 08:47:57 +00:00
2014-09-18 14:35:55 +00:00
$('#learnDlg').on('show.bs.modal', function() {
$('#learn').prop('disabled', true);
$('#keyword').val('');
2014-09-19 06:08:09 +00:00
$('#learnError').hide();
2014-09-18 14:35:55 +00:00
});
2014-09-18 08:32:59 +00:00
$('#learn').click(onLearn);
2014-09-18 08:47:57 +00:00
$('#keyword').bind('input', function() {
$('#learn').prop('disabled', !$(this).val());
});
$('#keywords').selectpicker('refresh');
$('#keywords').change(function() {
2014-09-18 08:47:57 +00:00
$('#search').prop('disabled', !$(this).val());
});
});
}
2014-07-26 06:02:42 +00:00
});
2014-07-28 13:12:44 +00:00
}(window.hscd = window.hscd || {}));