static/js/behaviour.js

  1. /**
  2. * @file Eunomia's client-side logic
  3. * @author Antonio Olmo Titos <a@olmo-titos.info>
  4. * @exports static/js/behaviour
  5. */
  6. /* jshint node: true */
  7. /* globals requirejs: false, document: false, window: false, EUNOMIA_DEBUG: false */
  8. 'use strict';
  9. /**
  10. * Tweak the behaviour of the login page
  11. * @param {Object} $ - the <a href="https://jquery.com/">jQuery</a> object
  12. */
  13. const processLoginPage = function($) {
  14. $('[data-id]').click(function() {
  15. const gate = $.post('#', {name: $(this).data('id')}, function(data) {
  16. window.location.href = data;
  17. });
  18. gate.fail(function(err) {
  19. console.dir(err); // eslint-disable-line no-console
  20. });
  21. });
  22. };
  23. /**
  24. * Find invisible elements containing Markdown, render it, and show the element
  25. * @param {Object} $ - the <a href="https://jquery.com/">jQuery</a> object
  26. * @param {Object} md - the <a href="https://github.com/markdown-it/markdown-it">markdown-it</a> object
  27. */
  28. const processMD = function($, md) {
  29. $('.md').each(function() {
  30. const content = this.textContent;
  31. console.log(content); // eslint-disable-line no-console
  32. $(this).html(md.render(content));
  33. $(this).removeClass('missing');
  34. });
  35. };
  36. /**
  37. * Set up the page
  38. * @param {Object} $ - the <a href="https://jquery.com/">jQuery</a> object
  39. * @param {Object} markdownit - the <a href="https://github.com/markdown-it/markdown-it">markdown-it</a> object
  40. */
  41. const init = function($, foo, markdownit) {
  42. $(document).ready(function() {
  43. const md = markdownit(),
  44. html = $('html');
  45. html.removeClass('no-js').addClass('js');
  46. processLoginPage($);
  47. processMD($, md);
  48. $('table.tablesorter').tablesorter();
  49. });
  50. };
  51. // Set up RequireJS:
  52. requirejs.config({
  53. paths: {
  54. // "Bootstrap's JavaScript requires jQuery version 1.9.1 or higher, but lower than version 3".
  55. jquery: 'https://code.jquery.com/jquery-2.2.4' + (EUNOMIA_DEBUG ? '' : '.min'),
  56. bootstrap: 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap' + (EUNOMIA_DEBUG ? '' : '.min'),
  57. 'markdown-it': 'https://cdnjs.cloudflare.com/ajax/libs/markdown-it/8.3.1/markdown-it' + (EUNOMIA_DEBUG ? '' : '.min'),
  58. 'tablesorter': 'https://cdnjs.cloudflare.com/ajax/libs/jquery.tablesorter/2.28.14/js/jquery.tablesorter' + (EUNOMIA_DEBUG ? '' : '.min')
  59. },
  60. shim: {
  61. 'bootstrap': ['jquery'],
  62. 'tablesorter': ['jquery']
  63. }
  64. });
  65. // Load dependencies asynchronously via RequireJS:
  66. requirejs(['jquery', 'bootstrap', 'markdown-it', 'tablesorter'], init);