03.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /**
  2. * Something like PHP array_values().
  3. *
  4. * @param {Object} a
  5. * @returns {Array}
  6. */
  7. function values(a) {
  8. "use strict";
  9. var result = [];
  10. for (var i in a) {
  11. if (a.hasOwnProperty(i)) {
  12. result.push(a[i]);
  13. }
  14. }
  15. return result;
  16. }
  17. $(document).ready(function () {
  18. 'use strict';
  19. // Enable hover effect on the style switcher
  20. $('#switcher').hover(function() {
  21. $(this).addClass('hover');
  22. }, function() {
  23. $(this).removeClass('hover');
  24. });
  25. // Allow the style switcher to expand and collapse.
  26. var toggleSwitcher = function(event) {
  27. if (!$(event.target).is('button')) {
  28. $('#switcher').find('button').toggleClass('hidden');
  29. }
  30. };
  31. $('#switcher').on('click', toggleSwitcher);
  32. // Simulate a click so we start in a collapsed state.
  33. $('#switcher').click();
  34. // The setBodyClass() function changes the page style.
  35. // The style switcher state is also updated.
  36. var setBodyClass = function (className) {
  37. $('body').removeClass().addClass(className);
  38. $('#switcher').find('button').removeClass('selected');
  39. $('#switcher-' + className).addClass('selected');
  40. $('#switcher').off('click', toggleSwitcher);
  41. if (className === 'default') {
  42. $('#switcher').on('click', toggleSwitcher);
  43. }
  44. };
  45. // begin with the switcher-default button "selected"
  46. $('#switcher-default').addClass('selected');
  47. // Map key codes to their corresponding buttons to click
  48. var triggers = {
  49. D: 'default',
  50. N: 'narrow',
  51. L: 'large'
  52. };
  53. // Call setBodyClass() when a button is clicked.
  54. $('#switcher').click(function(event) {
  55. if ($(event.target).is('button')) {
  56. var bodyClass = event.target.id.split('-')[1];
  57. setBodyClass(bodyClass);
  58. }
  59. });
  60. // Call setBodyClass() when a key is pressed.
  61. // Use keydown/keyup to identify key, keypress to identify resulting letter.
  62. $(document).keyup(function (event) {
  63. var key = String.fromCharCode(event.which);
  64. if (key in triggers) {
  65. setBodyClass(triggers[key]);
  66. }
  67. });
  68. // Exercise 1.
  69. $('.author').click(function () {
  70. $(this).toggleClass('selected');
  71. });
  72. // Exercise 2.
  73. $('.chapter-title').dblclick(function () {
  74. $(this).siblings().toggleClass('hidden');
  75. });
  76. // Exercise 3.
  77. var triggerValues = values(triggers);
  78. setBodyClass('default');
  79. $('body').keyup(function (event) {
  80. if (event.which !== 39) {
  81. return;
  82. }
  83. // Assume the only classes are those handled by this code. Not true in the
  84. // general case but sufficient for the exercise.
  85. var currentClass = $('body').attr('class');
  86. var currentIndex = triggerValues.indexOf(currentClass);
  87. var nextIndex = (currentIndex + 1) % triggerValues.length;
  88. var nextClass = triggerValues[nextIndex];
  89. setBodyClass(nextClass);
  90. });
  91. // Exercise 4.
  92. var tracker = function(event) {
  93. console.log(event.pageX, event.pageY);
  94. };
  95. var jP = $('p');
  96. jP.mouseenter(function () {
  97. $(this).mousemove(tracker);
  98. }).mouseleave(function () {
  99. $(this).off('mousemove');
  100. });
  101. // Exercise 5.
  102. var trackerY = 0;
  103. $('html').mousedown(function (event) {
  104. trackerY = event.pageY;
  105. console.log('Pressed at ' + trackerY);
  106. }).mouseup(function (event) {
  107. console.log('Released at ' + event.pageY);
  108. if (event.pageY < trackerY) {
  109. jP.addClass('hidden');
  110. }
  111. else if (event.pageY > trackerY) {
  112. jP.removeClass('hidden');
  113. }
  114. });
  115. });