From f6f58e169a83cabe5f2d989c15d6a2ab301bb118 Mon Sep 17 00:00:00 2001
From: Davanum Srinivas <dims@linux.vnet.ibm.com>
Date: Wed, 1 Oct 2014 23:40:13 -0400
Subject: [PATCH] Save last setting of Toggle CI button

Use the same trick used in zuul status page to store a cookie
with the last state so when we load another review, we use the last
setting to show or hide the CI comments.

Change-Id: If912b88c3ad68c1dcaa086ad76abb54ecf5b576b
---
 .../openstack_project/files/gerrit/hideci.js  | 36 ++++++++++++++++---
 1 file changed, 32 insertions(+), 4 deletions(-)

diff --git a/modules/openstack_project/files/gerrit/hideci.js b/modules/openstack_project/files/gerrit/hideci.js
index 8dd9f9d115..2affea8f93 100644
--- a/modules/openstack_project/files/gerrit/hideci.js
+++ b/modules/openstack_project/files/gerrit/hideci.js
@@ -243,13 +243,32 @@ var ci_display_results = function(comments) {
     }
 };
 
-var ci_toggle_visibility = function(comments) {
+var set_cookie = function (name, value) {
+    document.cookie = name + "=" + value + "; path=/";
+};
+
+var read_cookie = function (name) {
+    var nameEQ = name + "=";
+    var ca = document.cookie.split(';');
+    for (var i = 0; i < ca.length; i++) {
+        var c = ca[i];
+        while (c.charAt(0) == ' ') {
+            c = c.substring(1, c.length);
+        }
+        if (c.indexOf(nameEQ) == 0) {
+            return c.substring(nameEQ.length, c.length);
+        }
+    }
+    return null;
+};
+
+var ci_toggle_visibility = function(comments, showOrHide) {
     if (!comments) {
         comments = ci_parse_comments();
     }
     $.each(comments, function(i, comment) {
         if (comment.is_ci) {
-            $(comment.ref).toggle();
+            $(comment.ref).toggle(showOrHide);
         }
     });
 };
@@ -270,7 +289,10 @@ var ci_page_loaded = function() {
         $("#toggleci").show();
         var comments = ci_parse_comments();
         ci_display_results(comments);
-        ci_hide_ci_comments(comments);
+        var showOrHide = 'true' == read_cookie('show-ci-comments');
+        if (!showOrHide) {
+            ci_hide_ci_comments(comments);
+        }
     } else {
         $("#toggleci").hide();
     }
@@ -283,7 +305,13 @@ window.onload = function() {
     input.type = "button";
     input.className = "gwt-Button";
     input.value = "Toggle CI";
-    input.onclick = function() { ci_toggle_visibility(null); };
+    input.onclick = function () {
+        // Flip the cookie
+        var showOrHide = 'true' == read_cookie('show-ci-comments');
+        set_cookie('show-ci-comments', showOrHide ? 'false' : 'true');
+        // Hide or Show existing comments based on cookie
+        ci_toggle_visibility(null, !showOrHide);
+    };
     document.body.appendChild(input);
 
     MutationObserver = window.MutationObserver || window.WebKitMutationObserver;