1

Removing history, search works again

This commit is contained in:
Alex Yatskov 2015-01-15 20:06:22 +09:00
parent 4f4713ec46
commit 2a41b75ae5
3 changed files with 75 additions and 82 deletions

View File

@ -55,12 +55,6 @@
<div class="checkbox"> <div class="checkbox">
<label><input type="checkbox" id="useRelativeScale" checked="checked">Use relative scale</label> <label><input type="checkbox" id="useRelativeScale" checked="checked">Use relative scale</label>
</div> </div>
<div id="history" class="form-group" style="display: none;">
<label for="historyIndex">History</label>
<div class="text-center">
<input id="historyIndex" style="width: 90%;" type="text" data-slider-reversed="true" data-slider-max="0">
</div>
</div>
</div> </div>
</div> </div>
</div> </div>

View File

@ -22,10 +22,11 @@
(function(hscd) { (function(hscd) {
'use strict'; 'use strict';
var _ctx = { }; var _ctx = {};
function onAdjust(name, value) { function onAdjust(name, value) {
_ctx.query.features[name] = value; _ctx.query.features[name] = value;
$.getJSON('/search', _ctx.query, function(results) { $.getJSON('/search', _ctx.query, function(results) {
saveSnapshot(results); saveSnapshot(results);
outputSnapshot(results, true); outputSnapshot(results, true);
@ -34,37 +35,19 @@
function onReady(geo) { function onReady(geo) {
_ctx = { _ctx = {
log: [], log: [],
geo: geo geo: geo,
query: {}
}; };
$('#historyIndex').slider({ $('#minScore,#hintSteps,#walkingDist,#maxResults').change(onSearch);
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';
}
}
});
$.getJSON('/get_parameters', function(parameters) { onSearch();
_ctx.parameters = parameters;
onSearch();
$('#minScore,#hintSteps,#walkingDist,#maxResults').change(function() { onSearch(getFeaturesGrapher); });
$('#historyIndex').on('slideStop', onSelectSnapshot);
});
} }
function onSearch(provider) { function onSearch() {
_ctx.query = { _ctx.query = {
features: _clone(_ctx.query.features), features: _ctx.query.features || {},
range: { min: -1.0, max: 1.0 }, range: { min: -1.0, max: 1.0 },
walkingDist: parseFloat($('#walkingDist').val()), walkingDist: parseFloat($('#walkingDist').val()),
minScore: parseFloat($('#minScore').val()), minScore: parseFloat($('#minScore').val()),
@ -79,58 +62,47 @@
}; };
} }
if (!_.has(_ctx, 'grapher')) { $.getJSON('/search', _ctx.query, function(results) {
_ctx.grapher = new grapher.Grapher({ if (!_.has(_ctx, 'grapher')) {
canvas: new Snap('#svg'), _ctx.grapher = new grapher.Grapher({
steps: _ctx.query.hintSteps, canvas: new Snap('#svg'),
range: _ctx.query.range, steps: _ctx.query.hintSteps,
onValueChanged: onAdjust, range: _ctx.query.range,
useLocalScale: true, onValueChanged: onAdjust,
useRelativeScale: true useLocalScale: true,
}); useRelativeScale: true
});
$('#useLocalScale').click(function() { $('#useLocalScale').click(function() {
var useLocalScale = $('#useLocalScale').is(':checked'); var useLocalScale = $('#useLocalScale').is(':checked');
_ctx.grapher.setUseLocalScale(useLocalScale); _ctx.grapher.setUseLocalScale(useLocalScale);
}); });
$('#useRelativeScale').click(function() {
var useRelativeScale = $('#useRelativeScale').is(':checked');
_ctx.grapher.setUseRelativeScale(useRelativeScale);
});
var columns = {}; $('#useRelativeScale').click(function() {
for (var feature in _ctx.query.features) { var useRelativeScale = $('#useRelativeScale').is(':checked');
columns[feature] = { _ctx.grapher.setUseRelativeScale(useRelativeScale);
value: 0.0, });
hints: []
}; var columns = {};
for (var feature in results.columns) {
var column = results.columns[feature];
_ctx.query.features[feature] = column.value;
columns[feature] = {
value: column.value,
hints: column.hints
};
}
_ctx.grapher.setColumns(columns);
} }
_ctx.grapher.setColumns(columns);
}
$.getJSON('/search', _ctx.query, function(results) {
saveSnapshot(results); saveSnapshot(results);
outputSnapshot(results, false); outputSnapshot(results, false);
}); });
} }
function onSelectSnapshot() {
var index = $('#historyIndex').slider('getValue');
outputSnapshot(_ctx.log[index], false);
}
function saveSnapshot(results) { function saveSnapshot(results) {
_ctx.log.push(results); _ctx.log.push(results);
var count = _ctx.log.length;
var history = $('#historyIndex').slider();
history.slider('setAttribute', 'max', count - 1);
history.slider('setValue', count - 1);
if (count > 1) {
$('#history').show();
}
} }
function outputSnapshot(results, omitValues) { function outputSnapshot(results, omitValues) {
@ -150,24 +122,29 @@
if (results.length < count) { if (results.length < count) {
searchResultCnt += ' of ' + count; searchResultCnt += ' of ' + count;
} }
$('#resultCount').text(searchResultCnt); $('#resultCount').text(searchResultCnt);
$('#resultPanel').slideDown();
var template = Handlebars.compile($('#template').html()); var template = Handlebars.compile($('#template').html());
$('#results').empty(); $('#results').empty();
$('#results').append(template({results: results})); $('#results').append(
template({ results: results })
);
if (results.length === 0) {
$('#resultPanel').slideUp();
}
else {
$('#resultPanel').slideDown();
}
} }
$(document).on({ $(document).on({
ajaxStart: function() { ajaxStart: function() {
$('#spinner').show(); $('#spinner').show();
}, },
ajaxStop: function() { ajaxStop: function() {
$('#spinner').hide(); $('#spinner').hide();
}, },
ready: function() { ready: function() {
if (navigator.geolocation) { if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition( navigator.geolocation.getCurrentPosition(

View File

@ -32,17 +32,18 @@ var pool = null;
function innerProduct(values1, values2) { function innerProduct(values1, values2) {
var result = 0.0; var result = 0.0;
console.assert(_.keys(values1).length == _.keys(values2).length);
for (var feature in values1) { for (var feature in values1) {
result += values1[feature] * values2[feature]; if (feature in values2) {
result += values1[feature] * values2[feature];
}
} }
return result; return result;
} }
function walkMatches(data, features, minScore, callback) { function walkMatches(data, features, minScore, callback) {
for (var i = 0, count = data.records.length; i < count; ++i) { for (var i = 0, count = data.length; i < count; ++i) {
var record = data.records[i]; var record = data[i];
var score = innerProduct(features, record.features); var score = innerProduct(features, record.features);
if (score >= minScore) { if (score >= minScore) {
@ -62,6 +63,7 @@ function countRecords(data, features, minScore) {
function findRecords(data, features, minScore) { function findRecords(data, features, minScore) {
var results = []; var results = [];
walkMatches(data, features, minScore, function(record, score) { walkMatches(data, features, minScore, function(record, score) {
results.push({ results.push({
name: record.name, name: record.name,
@ -189,7 +191,27 @@ function computeRecordGeo(records, context) {
}); });
} }
function sanitizeQuery(query) {
var keys = [
'delicious',
'accomodating',
'affordable',
'atmospheric',
'nearby',
'accessible'
];
var features = {};
_.each(keys, function(key) {
features[key] = _.has(query.features, key) ? query.features[key] : 0.0;
});
query.features = features;
}
function execQuery(query, callback) { function execQuery(query, callback) {
sanitizeQuery(query);
var context = { var context = {
geo: query.geo, geo: query.geo,
walkingDist: query.walkingDist * 1000.0 walkingDist: query.walkingDist * 1000.0