diff --git a/static/js/index.js b/static/js/index.js index 44681c0..01b2572 100644 --- a/static/js/index.js +++ b/static/js/index.js @@ -46,6 +46,57 @@ var searchResultsPage = 1; searchBar.onkeyup = typingTimer; }()); +/* + * Set keyboard shortcut mappings. + */ +(function resultsHotkeys(){ + /* + * If the currently viewed result is not the first, scroll to the previous + * result. + */ + var previousResult = function(){ + var currResult = $(".display-all"); + if(currResult.length) + currResult.closest(".result").prev(".result").each(function(){ + $('html,body').stop().animate({ + scrollTop: $(this).offset().top - ( + $(window).height() - $(this).outerHeight(true)) / 2 + }, 140); + }); + }; + + /* + * If the currently viewed result is not the last, scroll to the next + * result. + */ + var nextResult = function(){ + var currResult = $(".display-all"); + if(currResult.length) + currResult.closest(".result").next(".result").each(function(){ + $('html,body').stop().animate({ + scrollTop: $(this).offset().top - ( + $(window).height() - $(this).outerHeight(true)) / 2 + }, 140); + }); + }; + + var hotkeyActions = { + "k" : previousResult, + "j" : nextResult + "h" : previousSymbolMatch, + "l" : nextSymbolMatch + }; + + $(window).keypress(function(key){ + for(var hotkey in hotkeyActions){ + var keyChar = String.fromCharCode(key.keyCode); + console.log(keyChar); + if(keyChar == hotkey) + hotkeyActions[keyChar](); + } + }); +}()); + //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; @@ -64,7 +115,8 @@ var testCodelet = { // 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')){ + if($(window).scrollTop() + $(window).height() == $(document).height() && + searchField.hasClass('partly-visible')){ loadMoreResults(); } });