2015-10-13 10:46:49 -07:00
|
|
|
var section_names = function(q) {
|
|
|
|
if (!q) {
|
|
|
|
return [];
|
|
|
|
}
|
2015-10-12 12:19:25 -07:00
|
|
|
var matches = [];
|
|
|
|
q = q.toLowerCase();
|
|
|
|
$.each(section_map, function(k, v) {
|
|
|
|
if (k.toLowerCase().indexOf(q) != -1) {
|
|
|
|
matches.push(k);
|
|
|
|
}
|
|
|
|
});
|
2015-10-12 22:33:45 -07:00
|
|
|
matches.sort(function(a, b) {
|
|
|
|
// shortest to longest
|
|
|
|
return a.length - b.length;
|
|
|
|
});
|
2015-10-13 10:46:49 -07:00
|
|
|
return matches;
|
|
|
|
}
|
|
|
|
var section_names_cb = function(q, cb) {
|
|
|
|
cb(section_names(q));
|
2015-10-12 12:19:25 -07:00
|
|
|
}
|
2015-10-13 10:46:49 -07:00
|
|
|
var go_to_section = function() {
|
|
|
|
query = $('#searchbox').val();
|
|
|
|
results = section_names(query);
|
|
|
|
if (results.length == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
result = results[0];
|
|
|
|
location.hash = '#' + section_map[result];
|
|
|
|
if (result != query) {
|
|
|
|
$('#searchbox').val(result);
|
2015-10-12 12:19:25 -07:00
|
|
|
}
|
|
|
|
}
|
2015-08-13 23:00:54 -07:00
|
|
|
$(function(){
|
2015-10-12 12:19:25 -07:00
|
|
|
$('#searchbox').typeahead(
|
|
|
|
{hint: false, highlight: true, minLength: 1},
|
2015-10-13 10:46:49 -07:00
|
|
|
{name: "contents", source: section_names_cb, limit: 6}
|
2015-10-12 12:19:25 -07:00
|
|
|
).on('typeahead:selected', function(e, data) {
|
2015-10-13 10:46:49 -07:00
|
|
|
go_to_section();
|
2015-08-13 23:00:54 -07:00
|
|
|
});
|
2015-10-13 10:46:49 -07:00
|
|
|
$('#searchbox').change(go_to_section);
|
2015-08-13 23:00:54 -07:00
|
|
|
});
|
|
|
|
// add "Run" button to execute examples on jqplay.org
|
|
|
|
$(function() {
|
2015-08-19 23:27:10 -07:00
|
|
|
$.each($('.manual-example table'), function(index, value) {
|
2015-08-13 23:00:54 -07:00
|
|
|
$value = $(value)
|
|
|
|
var j = $value.find('tr:nth-child(2) td:first').text();
|
2018-09-03 11:58:02 +02:00
|
|
|
var q = $value.find('.jqprogram').text().replace(/^jq /, '').replace(/(\r\n|\n|\r)/gm," ").replace(/^'(.+)'$/, '$1');
|
2015-08-13 23:00:54 -07:00
|
|
|
var url = 'https://jqplay.org/jq?q=' + encodeURIComponent(q) +'&j=' + encodeURIComponent(j)
|
|
|
|
var $last_tr = $value.find('tr:last');
|
2015-08-20 11:14:14 -07:00
|
|
|
$last_tr.after('<tr class="jqplay-btn"><th><a href="' + url + '" class="btn btn-primary btn-sm">Run</a></th><th></th></tr><tr><th></th><th></th></tr>');
|
2015-08-13 23:00:54 -07:00
|
|
|
});
|
|
|
|
});
|