summaryrefslogtreecommitdiff
path: root/yaksh/static
diff options
context:
space:
mode:
Diffstat (limited to 'yaksh/static')
-rw-r--r--yaksh/static/yaksh/js/course.js75
1 files changed, 75 insertions, 0 deletions
diff --git a/yaksh/static/yaksh/js/course.js b/yaksh/static/yaksh/js/course.js
index f0d03e2..1c64a3e 100644
--- a/yaksh/static/yaksh/js/course.js
+++ b/yaksh/static/yaksh/js/course.js
@@ -66,4 +66,79 @@ $("#send_mail").click(function(){
return status;
});
+// Download course status as csv
+function exportTableToCSV($table, filename) {
+ var $headers = $table.find('tr:has(th)')
+ ,$rows = $table.find('tr:has(td)')
+
+ // Temporary delimiter characters unlikely to be typed by keyboard
+ // This is to avoid accidentally splitting the actual contents
+ ,tmpColDelim = String.fromCharCode(11) // vertical tab character
+ ,tmpRowDelim = String.fromCharCode(0) // null character
+
+ // actual delimiter characters for CSV format
+ ,colDelim = '","'
+ ,rowDelim = '"\r\n"';
+
+ // Grab text from table into CSV formatted string
+ var csv = '"';
+ csv += formatRows($headers.map(grabRow));
+ csv += rowDelim;
+ csv += formatRows($rows.map(grabRow)) + '"';
+
+ // Data URI
+ var csvData = 'data:application/csv;charset=utf-8,' + encodeURIComponent(csv);
+
+ // For IE (tested 10+)
+ if (window.navigator.msSaveOrOpenBlob) {
+ var blob = new Blob([decodeURIComponent(encodeURI(csv))], {
+ type: "text/csv;charset=utf-8;"
+ });
+ navigator.msSaveBlob(blob, filename);
+ } else {
+ $(this)
+ .attr({
+ 'download': filename,'href': csvData
+ });
+ }
+
+ function formatRows(rows){
+ return rows.get().join(tmpRowDelim)
+ .split(tmpRowDelim).join(rowDelim)
+ .split(tmpColDelim).join(colDelim);
+ }
+ // Grab and format a row from the table
+ function grabRow(i,row){
+ var $row = $(row);
+ var $cols = $row.find('td');
+ if(!$cols.length) $cols = $row.find('th');
+
+ return $cols.map(grabCol)
+ .get().join(tmpColDelim);
+ }
+ // Grab and format a column from the table
+ function grabCol(j,col){
+ var $col = $(col),
+ $text = $col.text();
+
+ return $text.replace('"', '""').replace("View Unit Status", '').replace("View Units", ""); // escape double quotes
+
+ }
+}
+
+
+$("#export").click(function (event) {
+ var outputFile = $("#course_name").val().replace(" ", "_") + '.csv';
+
+ exportTableToCSV.apply(this, [$('#course_table'), outputFile]);
});
+
+// Table sorter for course details
+$("table").tablesorter({});
+
+});
+
+function view_status(unit){
+ title_list = $(unit).attr("title").split("/");
+ $(unit).attr("title", title_list.join("\n"));
+}