trivia.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. var GLOBAL_ACTIONS = {
  2. 'play': function () {
  3. wavesurfer.playPause();
  4. },
  5. 'back': function () {
  6. wavesurfer.skipBackward();
  7. },
  8. 'forth': function () {
  9. wavesurfer.skipForward();
  10. },
  11. 'toggle-mute': function () {
  12. wavesurfer.toggleMute();
  13. }
  14. };
  15. // Bind actions to buttons and keypresses
  16. document.addEventListener('DOMContentLoaded', function () {
  17. document.addEventListener('keydown', function (e) {
  18. var map = {
  19. 32: 'play', // space
  20. 37: 'back', // left
  21. 39: 'forth' // right
  22. };
  23. var action = map[e.keyCode];
  24. if (action in GLOBAL_ACTIONS) {
  25. if (document == e.target || document.body == e.target) {
  26. e.preventDefault();
  27. }
  28. GLOBAL_ACTIONS[action](e);
  29. }
  30. });
  31. [].forEach.call(document.querySelectorAll('[data-action]'), function (el) {
  32. el.addEventListener('click', function (e) {
  33. var action = e.currentTarget.dataset.action;
  34. if (action in GLOBAL_ACTIONS) {
  35. e.preventDefault();
  36. GLOBAL_ACTIONS[action](e);
  37. }
  38. });
  39. });
  40. });
  41. // Misc
  42. document.addEventListener('DOMContentLoaded', function () {
  43. // Web Audio not supported
  44. if (!window.AudioContext && !window.webkitAudioContext) {
  45. var demo = document.querySelector('#demo');
  46. if (demo) {
  47. demo.innerHTML = '<img src="/example/screenshot.png" />';
  48. }
  49. }
  50. // Navbar links
  51. var ul = document.querySelector('.nav-pills');
  52. var pills = ul.querySelectorAll('li');
  53. var active = pills[0];
  54. if (location.search) {
  55. var first = location.search.split('&')[0];
  56. var link = ul.querySelector('a[href="' + first + '"]');
  57. if (link) {
  58. active = link.parentNode;
  59. }
  60. }
  61. active && active.classList.add('active');
  62. });