// Add date subtitle

This commit is contained in:
Jerome Nadaud
2013-10-22 12:12:41 +02:00
parent d8d9436f51
commit b6d6336e26
5 changed files with 134 additions and 4 deletions
+106 -1
View File
@@ -15,4 +15,109 @@ function pie_chart_trends(widget_name, chart_details)
nv.utils.windowResize(chart.update);
return chart;
});
}
}
//TODO unify this with other function in calenda.js and replace date.js functions
Date.parseDate = function(date, format) {
if (format === undefined)
format = 'Y-m-d';
var formatSeparator = format.match(/[.\/\-\s].*?/);
var formatParts = format.split(/\W+/);
var parts = date.split(formatSeparator);
var date = new Date();
if (parts.length === formatParts.length) {
date.setHours(0);
date.setMinutes(0);
date.setSeconds(0);
date.setMilliseconds(0);
for (var i=0; i<=formatParts.length; i++) {
switch(formatParts[i]) {
case 'dd':
case 'd':
case 'j':
date.setDate(parseInt(parts[i], 10)||1);
break;
case 'mm':
case 'm':
date.setMonth((parseInt(parts[i], 10)||1) - 1);
break;
case 'yy':
case 'y':
date.setFullYear(2000 + (parseInt(parts[i], 10)||1));
break;
case 'yyyy':
case 'Y':
date.setFullYear(parseInt(parts[i], 10)||1);
break;
}
}
}
return date;
};
Date.prototype.format = function(format) {
if (format === undefined)
return this.toString();
var formatSeparator = format.match(/[.\/\-\s].*?/);
var formatParts = format.split(/\W+/);
var result = '';
for (var i=0; i<=formatParts.length; i++) {
switch(formatParts[i]) {
case 'dd':
case 'd':
case 'j':
result += this.getDate() + formatSeparator;
break;
case 'mm':
case 'm':
result += (this.getMonth() + 1) + formatSeparator;
break;
case 'yy':
case 'y':
result += this.getFullYear() + formatSeparator;
break;
case 'yyyy':
case 'Y':
result += this.getFullYear() + formatSeparator;
break;
}
}
return result.slice(0, -1);
}
$(document).ready(function() {
if (date_subtitle === undefined)
date_subtitle = 'From %s to %s';
if (date_format === undefined)
date_format = 'Y-m-d';
$('#date-start').change(function() {
start = Date.parseDate($('#date-start').val(), 'Y-m-d');
end = Date.parseDate($('#date-end').val(), 'Y-m-d');
$('#customers-newsletters-subtitle').html(sprintf(date_subtitle, start.format(date_format), end.format(date_format)));
$('#traffic-subtitle').html(sprintf(date_subtitle, start.format(date_format), end.format(date_format)));
});
$('#date-end').change(function() {
start = Date.parseDate($('#date-start').val(), 'Y-m-d');
end = Date.parseDate($('#date-end').val(), 'Y-m-d');
$('#customers-newsletters-subtitle').html(sprintf(date_subtitle, start.format(date_format), end.format(date_format)));
$('#traffic-subtitle').html(sprintf(date_subtitle, start.format(date_format), end.format(date_format)));
});
});