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';
|
|
|
|
|
2014-11-08 05:33:36 +00:00
|
|
|
var _custom = '[custom]';
|
|
|
|
var _ctx = { log: [] };
|
2014-07-28 07:48:24 +00:00
|
|
|
|
2014-07-28 13:12:44 +00:00
|
|
|
function onAdjust(name, value) {
|
2014-11-08 05:33:36 +00:00
|
|
|
_ctx.query.features[name] = value;
|
|
|
|
$.getJSON('/search', _ctx.query, function(results) {
|
2014-10-15 06:52:45 +00:00
|
|
|
saveSnapshot(results);
|
|
|
|
outputSnapshot(results);
|
2014-11-09 03:01:26 +00:00
|
|
|
setCustomized(true);
|
2014-07-28 13:12:44 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-11-08 02:23:42 +00:00
|
|
|
function onReady() {
|
2014-11-08 10:16:35 +00:00
|
|
|
$('#historyIndex').slider({
|
2014-11-08 02:23:42 +00:00
|
|
|
formatter: function(value) {
|
2014-11-08 05:33:36 +00:00
|
|
|
var delta = _ctx.log.length - (value + 1);
|
2014-11-08 02:23:42 +00:00
|
|
|
switch (delta) {
|
|
|
|
case 0:
|
|
|
|
return 'Most recent query';
|
|
|
|
case 1:
|
|
|
|
return 'Previous query';
|
|
|
|
default:
|
|
|
|
return String(delta) + ' queries back';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-11-08 05:33:36 +00:00
|
|
|
$.getJSON('/get_parameters', function(parameters) {
|
|
|
|
_ctx.parameters = parameters;
|
|
|
|
for (var keyword in parameters.keywords) {
|
2014-11-08 07:13:06 +00:00
|
|
|
$('#searchKeyword').append($('<option></option>', { value: keyword, text: keyword }));
|
2014-11-08 02:23:42 +00:00
|
|
|
}
|
|
|
|
|
2014-11-09 03:01:26 +00:00
|
|
|
onSearch();
|
2014-11-08 07:13:06 +00:00
|
|
|
|
2014-11-09 03:01:26 +00:00
|
|
|
$('#searchKeyword,#minScore,#hintSteps,#maxResults').change(onSearch);
|
2014-11-08 10:16:35 +00:00
|
|
|
$('#historyIndex').on('slideStop', onSelectSnapshot);
|
2014-11-09 03:01:26 +00:00
|
|
|
$('#learn').click(onLearn);
|
|
|
|
$('#forget').click(onForget);
|
|
|
|
});
|
|
|
|
}
|
2014-11-08 14:23:56 +00:00
|
|
|
|
2014-11-09 03:01:26 +00:00
|
|
|
function onLearn() {
|
|
|
|
var keyword = prompt('Input keyword to learn as (alphanumeric, no spaces)');
|
|
|
|
if (keyword === null) {
|
|
|
|
return;
|
|
|
|
}
|
2014-11-08 14:23:56 +00:00
|
|
|
|
2014-11-09 03:01:26 +00:00
|
|
|
var query = {
|
|
|
|
keyword: keyword,
|
|
|
|
features: _ctx.query.features
|
|
|
|
};
|
|
|
|
|
|
|
|
$.getJSON('/add_keyword', query, function(results) {
|
|
|
|
if (results.success) {
|
|
|
|
_ctx.parameters.keywords[keyword] = query.features;
|
|
|
|
$('#searchKeyword').append($('<option></option>', { value: keyword, text: keyword }));
|
|
|
|
$('#searchKeyword').val(keyword);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
alert('Failed to learn keyword');
|
|
|
|
}
|
2014-11-08 02:23:42 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-11-09 03:01:26 +00:00
|
|
|
function onForget() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function onSearch() {
|
2014-11-08 07:13:06 +00:00
|
|
|
var keyword = $('#searchKeyword').val();
|
2014-11-08 05:33:36 +00:00
|
|
|
|
|
|
|
_ctx.query = {
|
2014-11-08 07:13:06 +00:00
|
|
|
features: _ctx.parameters.keywords[keyword],
|
2014-11-08 02:23:42 +00:00
|
|
|
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-11-08 05:33:36 +00:00
|
|
|
if (!_.has(_ctx, 'grapher')) {
|
|
|
|
_ctx.grapher = new grapher.Grapher({
|
2014-10-31 03:11:48 +00:00
|
|
|
canvas: new Snap('#svg'),
|
2014-11-08 05:33:36 +00:00
|
|
|
steps: _ctx.query.hintSteps,
|
|
|
|
range: _ctx.query.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
|
|
|
|
|
|
|
$('#useLocalScale').click(function() {
|
|
|
|
var useLocalScale = $('#useLocalScale').is(':checked');
|
2014-11-08 05:33:36 +00:00
|
|
|
_ctx.grapher.setUseLocalScale(useLocalScale);
|
2014-07-28 13:12:44 +00:00
|
|
|
});
|
|
|
|
$('#useRelativeScale').click(function() {
|
|
|
|
var useRelativeScale = $('#useRelativeScale').is(':checked');
|
2014-11-08 05:33:36 +00:00
|
|
|
_ctx.grapher.setUseRelativeScale(useRelativeScale);
|
2014-07-28 13:12:44 +00:00
|
|
|
});
|
2014-11-08 03:31:57 +00:00
|
|
|
|
2014-11-08 05:33:36 +00:00
|
|
|
var columns = {};
|
|
|
|
for (var feature in _ctx.query.features) {
|
|
|
|
columns[feature] = { value: 0.0, hints: [] };
|
|
|
|
}
|
|
|
|
|
|
|
|
_ctx.grapher.setColumns(columns);
|
|
|
|
}
|
|
|
|
|
|
|
|
$.getJSON('/search', _ctx.query, function(results) {
|
|
|
|
saveSnapshot(results);
|
2014-11-09 02:32:16 +00:00
|
|
|
outputSnapshot(results);
|
2014-11-09 03:01:26 +00:00
|
|
|
setCustomized(false);
|
2014-07-28 13:12:44 +00:00
|
|
|
});
|
|
|
|
}
|
2014-07-28 07:21:08 +00:00
|
|
|
|
2014-10-15 06:52:45 +00:00
|
|
|
function onSelectSnapshot() {
|
2014-11-08 10:16:35 +00:00
|
|
|
var index = $('#historyIndex').slider('getValue');
|
2014-11-08 05:33:36 +00:00
|
|
|
outputSnapshot(_ctx.log[index]);
|
2014-11-09 03:01:26 +00:00
|
|
|
setCustomized(true);
|
2014-10-15 06:52:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function saveSnapshot(results) {
|
2014-11-08 05:33:36 +00:00
|
|
|
_ctx.log.push(results);
|
2014-10-15 06:52:45 +00:00
|
|
|
|
2014-11-08 05:33:36 +00:00
|
|
|
var count = _ctx.log.length;
|
2014-11-08 10:16:35 +00:00
|
|
|
var history = $('#historyIndex').slider();
|
2014-10-15 07:26:47 +00:00
|
|
|
history.slider('setAttribute', 'max', count - 1);
|
|
|
|
history.slider('setValue', count - 1);
|
2014-10-15 06:52:45 +00:00
|
|
|
|
2014-10-15 07:26:47 +00:00
|
|
|
if (count > 1) {
|
2014-11-08 10:16:35 +00:00
|
|
|
$('#history').slideDown();
|
2014-10-15 06:52:45 +00:00
|
|
|
}
|
2014-10-15 07:26:47 +00:00
|
|
|
}
|
2014-10-04 10:57:31 +00:00
|
|
|
|
2014-11-09 03:01:26 +00:00
|
|
|
function setCustomized(customized) {
|
|
|
|
$('#forget').prop('disabled', customized);
|
|
|
|
if (customized) {
|
|
|
|
$('#customized').show();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$('#customized').hide();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-15 07:26:47 +00:00
|
|
|
function outputSnapshot(results) {
|
2014-11-05 11:38:58 +00:00
|
|
|
for (var name in results.columns) {
|
2014-11-08 05:33:36 +00:00
|
|
|
_ctx.query.features[name] = results.columns[name].value;
|
2014-11-05 11:38:58 +00:00
|
|
|
}
|
|
|
|
|
2014-11-08 05:33:36 +00:00
|
|
|
_ctx.grapher.setColumns(results.columns);
|
2014-10-15 06:52:45 +00:00
|
|
|
outputMatches(results.items, results.count);
|
2014-10-04 10:57:31 +00:00
|
|
|
}
|
|
|
|
|
2014-10-15 06:52:45 +00:00
|
|
|
function outputMatches(results, count) {
|
2014-09-16 02:00:15 +00:00
|
|
|
var searchResultCnt = String(results.length);
|
|
|
|
if (results.length < count) {
|
|
|
|
searchResultCnt += ' of ' + count;
|
|
|
|
}
|
2014-11-08 10:16:35 +00:00
|
|
|
|
|
|
|
$('#resultCount').text(searchResultCnt);
|
|
|
|
$('#resultPanel').slideDown();
|
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}));
|
|
|
|
}
|
|
|
|
|
2014-09-12 07:54:21 +00:00
|
|
|
$(document).on({
|
2014-11-08 07:13:06 +00:00
|
|
|
ajaxStart: function() {
|
|
|
|
if (_.has(_ctx, 'grapher')) {
|
|
|
|
_ctx.grapher.enable(false);
|
|
|
|
}
|
|
|
|
$('#spinner').show();
|
|
|
|
},
|
|
|
|
|
|
|
|
ajaxStop: function() {
|
|
|
|
if (_.has(_ctx, 'grapher')) {
|
|
|
|
_ctx.grapher.enable(true);
|
|
|
|
}
|
|
|
|
$('#spinner').hide();
|
|
|
|
},
|
|
|
|
|
|
|
|
ready: onReady()
|
2014-07-26 06:02:42 +00:00
|
|
|
});
|
2014-09-12 07:54:21 +00:00
|
|
|
|
2014-07-28 13:12:44 +00:00
|
|
|
}(window.hscd = window.hscd || {}));
|