]> git.sur5r.net Git - bacula/bacula/commitdiff
baculum: Remember sort order for data grids
authorMarcin Haba <marcin.haba@bacula.pl>
Sun, 13 Dec 2015 19:47:57 +0000 (20:47 +0100)
committerMarcin Haba <marcin.haba@bacula.pl>
Sun, 13 Dec 2015 19:47:57 +0000 (20:47 +0100)
gui/baculum/protected/JavaScript/misc.js
gui/baculum/protected/JavaScript/slide-window.js

index 9995b5f8cbf65c5821516a82ad97270466d3a967..34f93eba0e3472748ddf346fd8f453bfb0b951a3 100644 (file)
@@ -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;
+       }
+}
index 41cf7c1b3d0d23f26227f1dad426a4744b7c779f..d0d329c02ddcc630ed75b950aee8d26820c646e1 100644 (file)
@@ -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){