[damned-lies] Remember "Hide completed modules"



commit 840e021a13bbcc0c3c280c0f38cd12b82a1e443a
Author: Grégoire Détrez <gregoire detrez gu se>
Date:   Mon Aug 10 17:29:06 2015 +0200

    Remember "Hide completed modules"
    
    * refactor the javascript come to use jquery
    * add the module jquery.cookie.js
    * stores the current state in a cookie.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=660005

 common/static/js/jquery.cookie.js               |  117 +++++++++++++++++++++++
 common/static/js/main.js                        |   45 +++++----
 templates/base.html                             |    3 +-
 templates/languages/language_release.html       |    9 +--
 templates/languages/language_release_stats.html |    2 +-
 5 files changed, 145 insertions(+), 31 deletions(-)
---
diff --git a/common/static/js/jquery.cookie.js b/common/static/js/jquery.cookie.js
new file mode 100644
index 0000000..c7f3a59
--- /dev/null
+++ b/common/static/js/jquery.cookie.js
@@ -0,0 +1,117 @@
+/*!
+ * jQuery Cookie Plugin v1.4.1
+ * https://github.com/carhartl/jquery-cookie
+ *
+ * Copyright 2013 Klaus Hartl
+ * Released under the MIT license
+ */
+(function (factory) {
+       if (typeof define === 'function' && define.amd) {
+               // AMD
+               define(['jquery'], factory);
+       } else if (typeof exports === 'object') {
+               // CommonJS
+               factory(require('jquery'));
+       } else {
+               // Browser globals
+               factory(jQuery);
+       }
+}(function ($) {
+
+       var pluses = /\+/g;
+
+       function encode(s) {
+               return config.raw ? s : encodeURIComponent(s);
+       }
+
+       function decode(s) {
+               return config.raw ? s : decodeURIComponent(s);
+       }
+
+       function stringifyCookieValue(value) {
+               return encode(config.json ? JSON.stringify(value) : String(value));
+       }
+
+       function parseCookieValue(s) {
+               if (s.indexOf('"') === 0) {
+                       // This is a quoted cookie as according to RFC2068, unescape...
+                       s = s.slice(1, -1).replace(/\\"/g, '"').replace(/\\\\/g, '\\');
+               }
+
+               try {
+                       // Replace server-side written pluses with spaces.
+                       // If we can't decode the cookie, ignore it, it's unusable.
+                       // If we can't parse the cookie, ignore it, it's unusable.
+                       s = decodeURIComponent(s.replace(pluses, ' '));
+                       return config.json ? JSON.parse(s) : s;
+               } catch(e) {}
+       }
+
+       function read(s, converter) {
+               var value = config.raw ? s : parseCookieValue(s);
+               return $.isFunction(converter) ? converter(value) : value;
+       }
+
+       var config = $.cookie = function (key, value, options) {
+
+               // Write
+
+               if (value !== undefined && !$.isFunction(value)) {
+                       options = $.extend({}, config.defaults, options);
+
+                       if (typeof options.expires === 'number') {
+                               var days = options.expires, t = options.expires = new Date();
+                               t.setTime(+t + days * 864e+5);
+                       }
+
+                       return (document.cookie = [
+                               encode(key), '=', stringifyCookieValue(value),
+                               options.expires ? '; expires=' + options.expires.toUTCString() : '', // use 
expires attribute, max-age is not supported by IE
+                               options.path    ? '; path=' + options.path : '',
+                               options.domain  ? '; domain=' + options.domain : '',
+                               options.secure  ? '; secure' : ''
+                       ].join(''));
+               }
+
+               // Read
+
+               var result = key ? undefined : {};
+
+               // To prevent the for loop in the first place assign an empty array
+               // in case there are no cookies at all. Also prevents odd result when
+               // calling $.cookie().
+               var cookies = document.cookie ? document.cookie.split('; ') : [];
+
+               for (var i = 0, l = cookies.length; i < l; i++) {
+                       var parts = cookies[i].split('=');
+                       var name = decode(parts.shift());
+                       var cookie = parts.join('=');
+
+                       if (key && key === name) {
+                               // If second argument (value) is a function it's a converter...
+                               result = read(cookie, value);
+                               break;
+                       }
+
+                       // Prevent storing a cookie that we couldn't decode.
+                       if (!key && (cookie = read(cookie)) !== undefined) {
+                               result[name] = cookie;
+                       }
+               }
+
+               return result;
+       };
+
+       config.defaults = {};
+
+       $.removeCookie = function (key, options) {
+               if ($.cookie(key) === undefined) {
+                       return false;
+               }
+
+               // Must not alter options, thus extending a fresh object...
+               $.cookie(key, '', $.extend({}, options, { expires: -1 }));
+               return !$.cookie(key);
+       };
+
+}));
diff --git a/common/static/js/main.js b/common/static/js/main.js
index 5902b9e..3158167 100644
--- a/common/static/js/main.js
+++ b/common/static/js/main.js
@@ -2,29 +2,30 @@
 // This function shows or hides all modules in a release that are
 // 100% translated
 // ***
-function showHideCompleted(target) {
-    var regex = /complete$/i;
+dl = (function($, undefined){
+    var COOKIE = "DL_HIDE_COMPLETED_MODULES",
+        selector = '.completed-module';
 
-    var tbls = document.getElementsByName(target);
-    for (var tb=0; tb < tbls.length; tb++) {
-        var translations = tbls[tb].getElementsByTagName("tr");
-
-        for(var i=0;i<translations.length;i++) {
-            if ( regex.exec(translations[i].id) ) {
-                if ( translations[i].style.display != 'none' ) {
-                    translations[i].style.display = 'none';
-                }
-                else {
-                    translations[i].style.display = '';
-                }
-            }
-        }
+    hide = function() {
+        $(selector).hide();
+        $.cookie(COOKIE, true);
+        $('#show-completed-modules').show();
+        $('#hide-completed-modules').hide();
+        return false;
     }
 
-    var hide = document.getElementById("hide");
-    var show = document.getElementById("show");
+    show = function() {
+        $(selector).show();
+        $.cookie(COOKIE, false);
+        $('#show-completed-modules').hide();
+        $('#hide-completed-modules').show();
+        return false;
+    }
 
-    hide.style.display = (hide.style.display != 'none' ? 'none' : '' );
-    show.style.display = (show.style.display != 'none' ? 'none' : '' );
-    return false;
-}
+    $(function() {
+        if($.cookie(COOKIE) == "true")
+            hide();
+        $('#show-completed-modules').click(show);
+        $('#hide-completed-modules').click(hide);
+    });
+})($);
diff --git a/templates/base.html b/templates/base.html
index efb4d42..7deee8c 100644
--- a/templates/base.html
+++ b/templates/base.html
@@ -11,10 +11,11 @@
 {% if LANGUAGE_BIDI %}
   <link rel="stylesheet" href="{{ STATIC_URL }}css/rtl.css">
 {% endif %}
-  <script type="text/javascript" src="{{ STATIC_URL }}js/main.js"></script>
   <script type="text/javascript" src="{{ STATIC_URL }}js/jquery.min.js"></script>
   <script type="text/javascript" src="{{ STATIC_URL }}js/jquery.tools.min.js"></script>
+  <script type="text/javascript" src="{{ STATIC_URL }}js/jquery.cookie.js"></script>
   <script type="text/javascript" src="{{ STATIC_URL }}js/overlayhelpers.js"></script>
+  <script type="text/javascript" src="{{ STATIC_URL }}js/main.js"></script>
 
   <script type="text/javascript">
     $(document).ready(function () {
diff --git a/templates/languages/language_release.html b/templates/languages/language_release.html
index 56f0363..53f3025 100644
--- a/templates/languages/language_release.html
+++ b/templates/languages/language_release.html
@@ -26,13 +26,8 @@ $(document).ready(function()
 {% endif %}
 
 <div id="hide-show">
-  <p id="hide">
-   <a href="#" onclick="return showHideCompleted('stats-table');">{% trans "Hide completed modules" %}</a>
-  </p>
-
-  <p id="show" style="display:none">
-   <a href="#" onclick="return showHideCompleted('stats-table');">{% trans "Show completed modules" %}</a>
-  </p>
+   <a href="#" id="hide-completed-modules">{% trans "Hide completed modules" %}</a>
+   <a href="#" id="show-completed-modules" style="display: none;">{% trans "Show completed modules" %}</a>
 </div>
 
 <h3>{{ stats_title }}</h3>
diff --git a/templates/languages/language_release_stats.html b/templates/languages/language_release_stats.html
index c83313a..019a2c1 100644
--- a/templates/languages/language_release_stats.html
+++ b/templates/languages/language_release_stats.html
@@ -42,7 +42,7 @@
      {% for dom in doms %}
       {% with dom.0 as domname and dom.1 as stat %}
       {% if stat and not stat.is_fake %}
-        <tr id="{{ modname }}-{{ stat.domain.id }}{% if stat.tr_percentage == 100 %}-complete{% endif %}">
+        <tr id="{{ modname }}-{{ stat.domain.id }}{% if stat.tr_percentage == 100 %}-complete{% endif %}" {% 
if stat.tr_percentage == 100 %}class="completed-module"{% endif %}>
         <td class="leftcell">
           {% if language and not module.archived %}
           <a href="{% url 'vertimus_by_names' modname branch stat.domain.name language.locale %}">{{ 
stat.module_description }}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]