/*
* @file Manages all query entry, `index.html` server querying, and results
* diplay.
*/
FINISH_TYPING_INTERVAL = 650;
searchBar = $("form#search-bar input[type='text']")[0];
resultsDiv = $("div#results")[0];
var typingTimer, lastValue;
//Obtained by parsing python file with pygments
var codeExample = '
1\n 2\n 3\n 4\n 5\n 6\n 7\n 8\n 9\n10\n11\n12\n13\n14\n15\n16\n17\n18\n19\n20\n21\n22\n23\n24\n25\n26\n27\n28\n29\n30\n31\n32\n33\n34\n35\n36\n37\n38\n39\n40 | """\nModule to contain all the project's Flask server plumbing.\n"""\n\nfrom flask import Flask\nfrom flask import render_template, session\n\nfrom bitshift import assets\n# from bitshift.database import Database\n# from bitshift.query import parse_query\n\napp = Flask(__name__)\napp.config.from_object("bitshift.config")\n\napp_env = app.jinja_env\napp_env.line_statement_prefix = "="\napp_env.globals.update(assets=assets)\n\n# database = Database()\n\n@app.route("/")\ndef index():\n return render_template("index.html")\n\n@app.route("/search/<query>")\ndef search(query):\n # tree = parse_query(query)\n # database.search(tree)\n pass\n\n@app.route("/about")\ndef about():\n return render_template("about.html")\n\n@app.route("/developers")\ndef developers():\n return render_template("developers.html")\n\nif __name__ == "__main__":\n app.run(debug=True)\n \n |
'
searchBar.onkeyup = typingTimer;
// Enable infinite scrolling down the results page.
$(window).scroll(function() {
var searchField = $("div#search-field");
if($(window).scrollTop() + $(window).height() == $(document).height() && searchField.hasClass('partly-visible')){
loadMoreResults();
}
});
// Enable capturing the `enter` key.
$("form#search-bar").submit(function(event){
event.preventDefault();
return false;
});
/*
* Clear the existing timer and set a new one the the user types text into the
* search bar.
*/
function typingTimer(event){
clearTimeout(typingTimer);
var enterKeyCode = 13;
if(event.keyCode != enterKeyCode){
if(lastValue != searchBar.value)
typingTimer = setTimeout(finishedTyping, FINISH_TYPING_INTERVAL);
}
else {
event.preventDefault();
finishedTyping();
return false;
}
};
/*
* Callback which queries the server whenver the user stops typing.
*
* Whenever the user doesn't type for a `FINISH_TYPING_INTERVAL` after having
* entered new text in the search bar, send the current query request to the
* server.
*/
function finishedTyping(){
lastValue = searchBar.value;
var searchField = $("div#search-field");
clearResults();
if(searchBar.value){
if(!searchField.hasClass("partly-visible"))
searchField.addClass("partly-visible");
populateResults();
}
else
searchField.removeClass("partly-visible");
}
/*
* Removes any child elements of `div#results`.
*/
function clearResults(){
while(resultsDiv.firstChild)
resultsDiv.removeChild(resultsDiv.firstChild);
}
/*
* Query the server with the current search string, and populate `div#results`
* with its response.
*/
function populateResults(){
var results = queryServer();
for(var result = 0; result < results.length; result++){
var newDiv = results[result];
resultsDiv.appendChild(newDiv);
setTimeout(
(function(divReference){
return function(){
divReference.classList.add("cascade");
};
}(newDiv)), result * 20);
}
}
/*
* AJAX the current query string to the server, and return its response.
*
* @return {Array} The server's response in the form of `div.result` DOM
* elements, to fill `div#results`.
*/
function queryServer(){
var resultDivs = []
for(var result = 0; result < 20; result++){
var newDiv = document.createElement("div"),
table = document.createElement("table"),
row = document.createElement("tr"),
sidebar = document.createElement("td"),
codeElt = document.createElement("td"),
meta = document.createElement("td");
newDiv.classList.add("result");
sidebar.id = 'sidebar';
codeElt.id = 'code';
meta.id = 'meta';
sidebar.innerHTML = '';
codeElt.innerHTML = '' + codeExample + '
';
row.appendChild(sidebar);
row.appendChild(codeElt);
row.appendChild(meta);
table.appendChild(row);
newDiv.appendChild(table);
resultDivs.push(newDiv);
}
return resultDivs;
}
/*
* Adds more results to `div#results`.
*/
function loadMoreResults(){
results = queryServer();
for(var result = 0; result < results.length; result++){
var newDiv = results[result];
resultsDiv.appendChild(newDiv);
setTimeout(
(function(divReference){
return function(){
divReference.classList.add("cascade");
};
}(newDiv)), result * 20);
}
}