diff -u b/date.js b/date.js --- b/date.js +++ b/date.js @@ -30,22 +30,24 @@ if (this.$end.length == 0) { return; } - this.selects = {}; - this.bindClickHandlers(); - // Start out with identical start and end dates. - this.syncEndDate(); - // Hide the help text about blank end date values being the same as the - // start date; users with JS enabled will never see blank values for the - // end date. - $('.js-hide').hide(); + this.initializeSelects(); + // Only act on date fields where the end date is completely blank or already + // the same as the start date. Otherwise, we do not want to override whatever + // the default value was. + if (this.endDateIsBlank() || this.endDateIsSame()) { + this.bindClickHandlers(); + // Start out with identical start and end dates. + this.syncEndDate(); + } }; /** - * Add a click handler to each of the start date's select dropdowns. + * Store all the select dropdowns in an array on the object, for later use. */ -Drupal.date.EndDateHandler.prototype.bindClickHandlers = function () { +Drupal.date.EndDateHandler.prototype.initializeSelects = function () { var $starts = this.$start.find('select'); var $end, $start, endId, i, id; + this.selects = {}; for (i = 0; i < $starts.length; i++) { $start = $($starts[i]); id = $start.attr('id'); @@ -56,8 +58,49 @@ 'start': $start, 'end': $end }; - $start.bind('click.endDateHandler', this.startClickHandler.bind(this)); - $end.bind('focus', this.endFocusHandler.bind(this)); + } +}; + +/** + * Returns true if all dropdowns in the end date widget are blank. + */ +Drupal.date.EndDateHandler.prototype.endDateIsBlank = function () { + var id; + for (id in this.selects) { + if (this.selects.hasOwnProperty(id)) { + if (this.selects[id].end.val() != '') { + return false; + } + } + } + return true; +}; + +/** + * Returns true if the end date widget has the same value as the start date. + */ +Drupal.date.EndDateHandler.prototype.endDateIsSame = function () { + var id; + for (id in this.selects) { + if (this.selects.hasOwnProperty(id)) { + if (this.selects[id].end.val() != this.selects[id].start.val()) { + return false; + } + } + } + return true; +}; + +/** + * Add a click handler to each of the start date's select dropdowns. + */ +Drupal.date.EndDateHandler.prototype.bindClickHandlers = function () { + var id; + for (id in this.selects) { + if (this.selects.hasOwnProperty(id)) { + this.selects[id].start.bind('click.endDateHandler', this.startClickHandler.bind(this)); + this.selects[id].end.bind('focus', this.endFocusHandler.bind(this)); + } } }; diff -u b/date_api/date_api_elements.inc b/date_api/date_api_elements.inc --- b/date_api/date_api_elements.inc +++ b/date_api/date_api_elements.inc @@ -367,7 +367,6 @@ $element['#element_validate'] = array('date_select_validate'); } - $element['#attached']['js'][] = drupal_get_path('module', 'date') . '/date.js'; return $element; } @@ -443,7 +442,6 @@ $element['#element_validate'] = array('date_select_validate'); } - $element['#attached']['js'][] = drupal_get_path('module', 'date') . '/date.js'; return $element; } diff -u b/date_elements.inc b/date_elements.inc --- b/date_elements.inc +++ b/date_elements.inc @@ -294,6 +294,7 @@ case 'date_select_repeat': $element[$from_field]['#type'] = 'date_select'; $element[$from_field]['#theme_wrappers'] = array('date_select'); + $element['#attached']['js'][] = drupal_get_path('module', 'date') . '/date.js'; break; case 'date_popup': case 'date_popup_repeat': @@ -318,6 +319,8 @@ $element[$to_field]['#required'] = FALSE; $element[$to_field]['#weight'] += .2; $element[$to_field]['#prefix'] = ''; + // Users with JS enabled will never see initially blank values for the end + // date (see Drupal.date.EndDateHandler()), so hide the message for them. $description .= ' ' . t("Empty 'End date' values will use the 'Start date' values.") . ''; $element['#fieldset_description'] = $description; if ($field['settings']['todate'] == 'optional') {