/**
* An object for displaying the user's coding logs (their history).
* A lightweight component, its only job is to open a dialog.
*
* @constructor
* @this {BlockPyHistory}
* @param {Object} main - The main BlockPy instance
*/
function BlockPyHistory(main) {
this.main = main;
}
var monthNames = [
"Jan", "Feb", "Mar",
"Apr", "May", "June", "July",
"Aug", "Sept", "Oct",
"Nov", "Dec"
];
var weekDays = [
"Sun", "Mon", "Tue",
"Wed", "Thu", "Fri",
"Sat"
];
/**
* Helper function to parse a date/time string and rewrite it as something
* more human readable.
* @param {String} timeString - the string representation of time ("YYYYMMDD HHMMSS")
* @returns {String} - A human-readable time string.
*/
function prettyPrintDateTime(timeString) {
var year = timeString.slice(0, 4),
month = parseInt(timeString.slice(4, 6), 10)-1,
day = timeString.slice(6, 8),
hour = timeString.slice(9, 11),
minutes = timeString.slice(11, 13),
seconds = timeString.slice(13, 15);
var date = new Date(year, month, day, hour, minutes, seconds);
var dayStr = weekDays[date.getDay()];
var monthStr = monthNames[date.getMonth()];
var yearFull = date.getFullYear();
var complete = dayStr+", "+monthStr+" "+date.getDate()+", "+yearFull+" at "+date.toLocaleTimeString();
return complete;
}
/**
* Opens the history dialog box. This requires a trip to the server and
* occurs asynchronously. The users' code is shown in preformatted text
* tags (no code highlighting currently) along with the timestamp.
*/
BlockPyHistory.prototype.openDialog = function() {
var dialog = this.main.components.dialog;
var body = "<pre>a = 0</pre>";
this.main.components.server.getHistory(function (data) {
body = data.reverse().reduce(function (complete, elem) {
var complete_str = prettyPrintDateTime(elem.time);
var new_line = "<b>"+complete_str+"</b><br><pre>"+elem.code+"</pre>";
return complete+"\n"+new_line;
}, "");
dialog.show("Work History", body, function() {});
});
};