Skip to content

21232 improve dates handling #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 58 additions & 1 deletion assets/javascripts/redmine-timesheet-filter-autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,71 @@ $(document).ready(function() {
$('#filters-table tbody').prepend($autocompleteFilter);

// Autocomplete source.
var source = '/timesheet/filter/autocomplete';
var source = function(request, response) {
var processed = false;
search = request.term;
const regex = /^(until|since)?\s?(.*)$/mi;
matches = regex.exec(search)
keyword = 'since';
value = ''
if (matches[1]) {
keyword = matches[1];
}
if (matches[2]) {
value = matches[2];
}
if (value) {
parsedDate = Sugar.Date.create(value);
if (parsedDate != 'Invalid Date') {
operator = '';
filter_id = 'spent_on';
parsedValue = dateToYMD(parsedDate);
if (keyword.toLowerCase() === 'since') {
operator = '>=';
}
else if (keyword.toLowerCase() === 'until') {
operator = '<=';
}
id = 'spent_on/' + operator + '/' + parsedValue
data = [
{
id: id,
label: search
}
];
response(data);
processed = true;
}
}
if (!processed) {
// If not returned, process as ajax.
var url = "/timesheet/filter/autocomplete?term=" + search;
$.ajax({
url: url,
success: function(data) {
var parsed = JSON.parse(data);
response(parsed);
},
error: function() {
response([]);
},
});
}
};
const regex = /^\/projects\/(.+)\//;
var matches;
if ((matches = regex.exec(window.location.pathname)) !== null) {
if (matches[1]) {
source = '/timesheet/filter/' + matches[1] + '/autocomplete';
}
}

const dateToYMD = function(date) {
var d = date.getDate();
var m = date.getMonth() + 1; //Month from 0 to 11
var y = date.getFullYear();
return '' + y + '-' + (m<=9 ? '0' + m : m) + '-' + (d <= 9 ? '0' + d : d);
};

$autocompleteInput.autocomplete({
autoFocus: true,
Expand Down
Loading