date_admin.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. (function ($) {
  2. Drupal.behaviors.dateAdmin = {};
  3. Drupal.behaviors.dateAdmin.attach = function (context, settings) {
  4. // Remove timezone handling options for fields without hours granularity.
  5. var $hour = $('#edit-field-settings-granularity-hour').once('date-admin');
  6. if ($hour.length) {
  7. new Drupal.dateAdmin.TimezoneHandler($hour);
  8. }
  9. };
  10. Drupal.dateAdmin = {};
  11. /**
  12. * Constructor for the TimezoneHandler object.
  13. *
  14. * This object is responsible for showing the timezone handling options dropdown
  15. * when the user has chosen to collect hours as part of the date field, and
  16. * hiding it otherwise.
  17. */
  18. Drupal.dateAdmin.TimezoneHandler = function ($checkbox) {
  19. this.$checkbox = $checkbox;
  20. this.$dropdown = $('#edit-field-settings-tz-handling');
  21. this.$timezoneDiv = $('.form-item-field-settings-tz-handling');
  22. // Store the initial value of the timezone handling dropdown.
  23. this.storeTimezoneHandling();
  24. // Toggle the timezone handling section when the user clicks the "Hour"
  25. // checkbox.
  26. this.$checkbox.bind('click', $.proxy(this.clickHandler, this));
  27. // Trigger the click handler so that if the checkbox is unchecked on initial
  28. // page load, the timezone handling section will be hidden.
  29. this.clickHandler();
  30. };
  31. /**
  32. * Event handler triggered when the user clicks the "Hour" checkbox.
  33. */
  34. Drupal.dateAdmin.TimezoneHandler.prototype.clickHandler = function () {
  35. if (this.$checkbox.is(':checked')) {
  36. this.restoreTimezoneHandlingOptions();
  37. }
  38. else {
  39. this.hideTimezoneHandlingOptions();
  40. }
  41. };
  42. /**
  43. * Hide the timezone handling options section of the form.
  44. */
  45. Drupal.dateAdmin.TimezoneHandler.prototype.hideTimezoneHandlingOptions = function () {
  46. this.storeTimezoneHandling();
  47. this.$dropdown.val('none');
  48. this.$timezoneDiv.hide();
  49. };
  50. /**
  51. * Show the timezone handling options section of the form.
  52. */
  53. Drupal.dateAdmin.TimezoneHandler.prototype.restoreTimezoneHandlingOptions = function () {
  54. var val = this.getTimezoneHandling();
  55. this.$dropdown.val(val);
  56. this.$timezoneDiv.show();
  57. };
  58. /**
  59. * Store the current value of the timezone handling dropdown.
  60. */
  61. Drupal.dateAdmin.TimezoneHandler.prototype.storeTimezoneHandling = function () {
  62. this._timezoneHandling = this.$dropdown.val();
  63. };
  64. /**
  65. * Return the stored value of the timezone handling dropdown.
  66. */
  67. Drupal.dateAdmin.TimezoneHandler.prototype.getTimezoneHandling = function () {
  68. return this._timezoneHandling;
  69. };
  70. })(jQuery);