Commit 9696bddc authored by m.stenta's avatar m.stenta
Browse files

Issue #2615606: Automatically check/uncheck "Done" based on timestamp

parent e2046731
(function ($) {
Drupal.behaviors.log_done = {
attach: function (context, settings) {
/**
* This script is added to the log form.
* It automatically checks/unchecks the "Done" checkbox
* depending on the date that is entered. Dates in the
* future are assumed to be "not done", whereas dates in
* the past (or present) are assumed to be "done".
*/
// When a date field is changed...
$('#edit-timestamp select', context).change(event, function() {
// Get the select list values.
var month = $(event.target).closest('#edit-timestamp').find('#edit-timestamp-month').val();
var day = $(event.target).closest('#edit-timestamp').find('#edit-timestamp-day').val();
var year = $(event.target).closest('#edit-timestamp').find('#edit-timestamp-year').val();
// Convert the date to a timestamp.
var timestamp = Date.parse(year + '-' + month + '-' + day);
// Get the timestamp at the start of today.
var now = new Date();
var today = Date.parse(new Date(now.getFullYear(), now.getMonth(), now.getDate()).toString());
// Get the "Done" checkbox element.
var done = $(event.target).closest('form').find('#edit-done');
// If the log is taking place after today, uncheck the "Done" checkbox.
// Otherwise, check it.
if (timestamp > today) {
done.prop('checked', false);
}
else {
done.prop('checked', true);
}
});
}
};
})(jQuery);
......@@ -132,9 +132,11 @@ function log_form(array $form, array &$form_state, Log $log) {
);
// If the log is new, and the log type marks new logs as "done" by default,
// mark the log as done.
// mark the log as done and add some Javascript to auto-check/uncheck based
// on the timestamp.
if (empty($log->id) && !empty($log_type->done)) {
$log->done = TRUE;
drupal_add_js(drupal_get_path('module', 'log') . '/log.done.js');
}
$form['done'] = array(
'#type' => 'checkbox',
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment