From ae443e27640f2cc508ae675c3d94fda4802bdda1 Mon Sep 17 00:00:00 2001 From: Marcin Haba Date: Sun, 13 Dec 2015 20:47:57 +0100 Subject: [PATCH] baculum: Remember sort order for data grids --- gui/baculum/protected/JavaScript/misc.js | 27 +++++++++++++++++++ .../protected/JavaScript/slide-window.js | 23 +++++++++++++--- 2 files changed, 46 insertions(+), 4 deletions(-) diff --git a/gui/baculum/protected/JavaScript/misc.js b/gui/baculum/protected/JavaScript/misc.js index 9995b5f8cb..34f93eba0e 100644 --- a/gui/baculum/protected/JavaScript/misc.js +++ b/gui/baculum/protected/JavaScript/misc.js @@ -35,3 +35,30 @@ var Formatters = { } } } + +var Cookies = { + default_exipration_time: 31536000000, // 1 year in miliseconds + set_cookie: function(name, value, expiration) { + var date = new Date(); + date.setTime(date.getTime() + this.default_exipration_time); + var expires = 'expires=' + date.toUTCString(); + document.cookie = name + '=' + value + '; ' + expires; + }, + get_cookie: function(name) { + name += '='; + var values = document.cookie.split(';'); + var cookie_val = null; + var value; + for (var i = 0; i < values.length; i++) { + value = values[i]; + while (value.charAt(0) == ' ') { + value = value.substr(1); + } + if (value.indexOf(name) == 0) { + cookie_val = value.substring(name.length, value.length); + break; + } + } + return cookie_val; + } +} diff --git a/gui/baculum/protected/JavaScript/slide-window.js b/gui/baculum/protected/JavaScript/slide-window.js index 41cf7c1b3d..d0d329c02d 100644 --- a/gui/baculum/protected/JavaScript/slide-window.js +++ b/gui/baculum/protected/JavaScript/slide-window.js @@ -286,6 +286,7 @@ var SlideWindowClass = Class.create({ }.bind(this, tr)); }.bind(this)); Formatters.set_formatters(); + this.revertSortingFromCookie(); }, isConfigurationOpen: function() { @@ -299,7 +300,7 @@ var SlideWindowClass = Class.create({ return is_open; }, - sortTable: function (col, reverse) { + sortTable: function (col, reverse, set_cookie) { var table = document.getElementById(this.gridEl); var tb = table.tBodies[0], tr = Array.prototype.slice.call(tb.rows, 0), i; reverse = -((+reverse) || -1); @@ -321,8 +322,9 @@ var SlideWindowClass = Class.create({ } return reverse * (val); }); - for(i = 0; i < tr.length; i++) { - var even = ((i % 2) == 0); + var even; + for (i = 0; i < tr.length; i++) { + even = ((i % 2) == 0); if (even) { tr[i].className = this.elements.contentItems; } else { @@ -330,6 +332,9 @@ var SlideWindowClass = Class.create({ } tb.appendChild(tr[i]); } + if (set_cookie === true) { + Cookies.set_cookie(this.gridEl, col + ':' + reverse); + } }, makeSortable: function () { @@ -351,11 +356,21 @@ var SlideWindowClass = Class.create({ while (--i >= downCounter) (function (i) { var dir = 1; th[i].addEventListener('click', function () { - self.sortTable(i, (dir = 1 - dir)); + self.sortTable(i, (dir = 1 - dir), true); }); }(i)); }, + revertSortingFromCookie: function() { + var sorting = Cookies.get_cookie(this.gridEl); + if (sorting != null) { + var sort_param = sorting.split(':'); + var col = parseInt(sort_param[0], 10); + var order = -(parseInt(sort_param[1], 10)); + this.sortTable(col, order); + } + }, + setSearch: function() { var search_pattern = new RegExp(this.search.value) $$('div[id="' + this.windowId + this.elements.containerSuffix + '"] div.' + this.elements.contentItems).each(function(value){ -- 2.39.5