Skip to content

Commit c3036ec

Browse files
authored
Merge pull request #3 from kporras07/21232-improve-dates-handling
21232 improve dates handling
2 parents 7a7e02a + c9115e8 commit c3036ec

File tree

3 files changed

+133
-2
lines changed

3 files changed

+133
-2
lines changed

assets/javascripts/redmine-timesheet-filter-autocomplete.js

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,71 @@ $(document).ready(function() {
88
$('#filters-table tbody').prepend($autocompleteFilter);
99

1010
// Autocomplete source.
11-
var source = '/timesheet/filter/autocomplete';
11+
var source = function(request, response) {
12+
var processed = false;
13+
search = request.term;
14+
const regex = /^(until|since)?\s?(.*)$/mi;
15+
matches = regex.exec(search)
16+
keyword = 'since';
17+
value = ''
18+
if (matches[1]) {
19+
keyword = matches[1];
20+
}
21+
if (matches[2]) {
22+
value = matches[2];
23+
}
24+
if (value) {
25+
parsedDate = Sugar.Date.create(value);
26+
if (parsedDate != 'Invalid Date') {
27+
operator = '';
28+
filter_id = 'spent_on';
29+
parsedValue = dateToYMD(parsedDate);
30+
if (keyword.toLowerCase() === 'since') {
31+
operator = '>=';
32+
}
33+
else if (keyword.toLowerCase() === 'until') {
34+
operator = '<=';
35+
}
36+
id = 'spent_on/' + operator + '/' + parsedValue
37+
data = [
38+
{
39+
id: id,
40+
label: search
41+
}
42+
];
43+
response(data);
44+
processed = true;
45+
}
46+
}
47+
if (!processed) {
48+
// If not returned, process as ajax.
49+
var url = "/timesheet/filter/autocomplete?term=" + search;
50+
$.ajax({
51+
url: url,
52+
success: function(data) {
53+
var parsed = JSON.parse(data);
54+
response(parsed);
55+
},
56+
error: function() {
57+
response([]);
58+
},
59+
});
60+
}
61+
};
1262
const regex = /^\/projects\/(.+)\//;
1363
var matches;
1464
if ((matches = regex.exec(window.location.pathname)) !== null) {
1565
if (matches[1]) {
1666
source = '/timesheet/filter/' + matches[1] + '/autocomplete';
1767
}
1868
}
69+
70+
const dateToYMD = function(date) {
71+
var d = date.getDate();
72+
var m = date.getMonth() + 1; //Month from 0 to 11
73+
var y = date.getFullYear();
74+
return '' + y + '-' + (m<=9 ? '0' + m : m) + '-' + (d <= 9 ? '0' + d : d);
75+
};
1976

2077
$autocompleteInput.autocomplete({
2178
autoFocus: true,

0 commit comments

Comments
 (0)