Permalink
Browse files

Remember selections

  • Loading branch information...
winton committed Nov 1, 2010
1 parent 2627afa commit f5cb3d37b5a8d8adf2a240e0600e3e9d07c4bb34
Showing with 66 additions and 2 deletions.
  1. +1 −1 lib/a_b_front_end/view/dashboard.haml
  2. +33 −0 public/js/cookie.js
  3. +32 −1 public/js/dashboard.js
@@ -1,5 +1,5 @@
- @css = %w(dashboard)
-- @js = %w(tmpl dashboard lightbox/jquery.lightbox_me)
+- @js = %w(cookie tmpl dashboard lightbox/jquery.lightbox_me)
/.success This is a test!
.dashboard
.span-12.filter#sites
View
@@ -0,0 +1,33 @@
+function cookie(key, value, options) {
+
+ options = options || {};
+
+ if (!key)
+ return null;
+
+ if (typeof value != 'undefined') {
+
+ if (value === null)
+ options.expires = -1;
+
+ if (typeof options.expires === 'number') {
+ var days = options.expires, t = options.expires = new Date();
+ t.setDate(t.getDate() + days);
+ }
+
+ document.cookie = [
+ key + '=' + encodeURIComponent(value),
+ 'path=' + (options.path ? options.path : '/'),
+ options.domain ? 'domain=' + options.domain : '',
+ options.expires ? 'expires=' + options.expires.toUTCString() : '',
+ options.secure ? 'secure' : ''
+ ].join('; ');
+
+ } else
+ options = value || options;
+
+ var result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)')
+ .exec(document.cookie);
+
+ return result ? decodeURIComponent(result[1]) : null;
+};
View
@@ -23,6 +23,8 @@ window.Dashboard = function(sites) {
$('#tests .edit a').live('click', testEdit);
$('#tests .remove a').live('click', testRemove);
$('.variants').live('keyup', variantKeyUp);
+
+ selectFromCookies();
});
// Events
@@ -104,11 +106,12 @@ window.Dashboard = function(sites) {
function selectableClick() {
var filter = $(this).parents('.filter');
var id = filter.attr('id');
+ var selected = $(this).hasClass('selected');
$(this).toggleClass('selected');
hideNextAll(filter);
- if ($(this).hasClass('selected')) {
+ if (!selected) {
var target = filter.next();
var target_id = target.attr('id');
@@ -143,6 +146,8 @@ window.Dashboard = function(sites) {
}
} else
$('.edit, .remove', filter).addClass('hide');
+
+ selectToCookies(id, selected, $(this).text());
}
function testConditionsChange() {
@@ -690,6 +695,32 @@ window.Dashboard = function(sites) {
});
}
+ function selectFromCookies() {
+ $.each([ 'sites', 'envs', 'categories' ], function(i, id) {
+ var c = cookie(id);
+ if (c)
+ $('#' + id + ' .selectable').each(function(i, item) {
+ item = $(item);
+ if (item.text() == c)
+ item.click();
+ });
+ });
+ }
+
+ function selectToCookies(id, selected, text) {
+ if (!selected)
+ cookie(id, text);
+ else {
+ var found = false;
+ $.each([ 'sites', 'envs', 'categories' ], function(i, item) {
+ if (id == item)
+ found = true;
+ if (found)
+ cookie(item, null);
+ });
+ }
+ }
+
function uniqArray(array) {
var u = {}, a = [];
for(var i = 0, l = array.length; i < l; i++) {

0 comments on commit f5cb3d3

Please sign in to comment.