shaarli.js 1.2 KB

1234567891011121314151617181920212223242526272829303132
  1. window.onload = function () {
  2. var continent = document.getElementById('continent');
  3. var city = document.getElementById('city');
  4. if (continent != null && city != null) {
  5. continent.addEventListener('change', function(event) {
  6. hideTimezoneCities(city, continent.options[continent.selectedIndex].value, true);
  7. });
  8. hideTimezoneCities(city, continent.options[continent.selectedIndex].value, false);
  9. }
  10. };
  11. /**
  12. * Add the class 'hidden' to city options not attached to the current selected continent.
  13. *
  14. * @param cities List of <option> elements
  15. * @param currentContinent Current selected continent
  16. * @param reset Set to true to reset the selected value
  17. */
  18. function hideTimezoneCities(cities, currentContinent, reset = false) {
  19. var first = true;
  20. [].forEach.call(cities, function(option) {
  21. if (option.getAttribute('data-continent') != currentContinent) {
  22. option.className = 'hidden';
  23. } else {
  24. option.className = '';
  25. if (reset === true && first === true) {
  26. option.setAttribute('selected', 'selected');
  27. first = false;
  28. }
  29. }
  30. });
  31. }