not really known
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
1.6 KiB

  1. define(function () {
  2. 'use strict';
  3. var radioButtonsGroup = {};
  4. // ## RadioButtonsGroup
  5. //
  6. // A group of elements where only one can be active at the same
  7. // time.
  8. //
  9. // When an element is clicked, it becomes the active one. The
  10. // active element gains the 'active' CSS class.
  11. //
  12. // Parameters:
  13. //
  14. // * **elems** Array of elements of the group.
  15. radioButtonsGroup.RadioButtonsGroup = function (elems) {
  16. this.elems = elems;
  17. var active;
  18. for (var i = 0; i < elems.length; i++) {
  19. var elem = elems[i];
  20. elem.addEventListener("click", clickHandler);
  21. // The first element that has 'active' CSS class is made
  22. // the active of the group on startup.
  23. if (active === undefined && elem.classList.contains('active')) {
  24. active = elem;
  25. }
  26. }
  27. // If no element has 'active' CSS class, the first element of
  28. // the array is made the active.
  29. if (active === undefined) {
  30. active = elems[0];
  31. updateClasses();
  32. }
  33. function clickHandler(evt) {
  34. active = evt.target;
  35. updateClasses();
  36. }
  37. function updateClasses() {
  38. for (i = 0; i < elems.length; i++) {
  39. var elem = elems[i];
  40. elem.classList.remove('active');
  41. }
  42. active.classList.add('active');
  43. }
  44. // Get the active element.
  45. this.getActive = function () {
  46. return active;
  47. };
  48. };
  49. return radioButtonsGroup;
  50. });