From 77c31a73206cb75b025723136d03a1f2ae2c4261 Mon Sep 17 00:00:00 2001 From: mvantellingen Date: Sun, 6 Feb 2011 16:01:36 +0100 Subject: [PATCH] Add the ability for panels to be activated or de-activated via a client-side cookie. (For the next to implement profiling panel) --- flaskext/debugtoolbar/panels/__init__.py | 6 ++++ flaskext/debugtoolbar/static/css/toolbar.css | 21 ++++++++++++ flaskext/debugtoolbar/static/img/tick-red.png | Bin 0 -> 1515 bytes flaskext/debugtoolbar/static/img/tick.png | Bin 0 -> 634 bytes flaskext/debugtoolbar/static/js/toolbar.js | 32 ++++++++++++++++++ flaskext/debugtoolbar/templates/base.html | 5 ++- flaskext/debugtoolbar/toolbar.py | 7 +++- 7 files changed, 69 insertions(+), 2 deletions(-) create mode 100755 flaskext/debugtoolbar/static/img/tick-red.png create mode 100755 flaskext/debugtoolbar/static/img/tick.png diff --git a/flaskext/debugtoolbar/panels/__init__.py b/flaskext/debugtoolbar/panels/__init__.py index 51e50a5..1c61480 100644 --- a/flaskext/debugtoolbar/panels/__init__.py +++ b/flaskext/debugtoolbar/panels/__init__.py @@ -7,6 +7,12 @@ class DebugPanel(object): # name = Base has_content = False # If content returns something, set to true in subclass + # If the client is able to activate/de-activate the panel + user_enable = False + + # If the client enabled the panel + is_active = False + # We'll maintain a local context instance so we can expose our template # context variables to panels which need them: context = {} diff --git a/flaskext/debugtoolbar/static/css/toolbar.css b/flaskext/debugtoolbar/static/css/toolbar.css index 7a0597a..491fdc9 100644 --- a/flaskext/debugtoolbar/static/css/toolbar.css +++ b/flaskext/debugtoolbar/static/css/toolbar.css @@ -92,6 +92,27 @@ font-variant:small-caps; } +#flDebug #flDebugToolbar li .switch { + font-size: 10px; + position: absolute; + display: block; + color: white; + height: 16px; + width: 16px; + cursor: pointer; + top: 15px; + right: 2px; +} + +#flDebug #flDebugToolbar li .switch.active { + background-image: url(../img/tick.png); +} + +#flDebug #flDebugToolbar li .switch.inactive { + background-image: url(../img/tick-red.png); +} + + #flDebug #flDebugToolbarHandle { position:fixed; background:#fff; diff --git a/flaskext/debugtoolbar/static/img/tick-red.png b/flaskext/debugtoolbar/static/img/tick-red.png new file mode 100755 index 0000000000000000000000000000000000000000..8ec99b4a697cda95c7140a77bd998e207ef4bc8c GIT binary patch literal 1515 zcmaJ>e{2(F7`}E`U~Gayzz$iDtHfw*@2p0Qr>_lwTd^WbYX|^=bH1q18bzl?S9{{3@xL78rmC^Faoz zjL{Ls&(PtPHD{S(0LWq4+JF@BR^uT)W~Q?kb0QX3*#J;nnuyb(21WvdOoZiJ(7^E& z1hQclRA==f-Z;rb*@{+yS=L%p8)|I`Im1wC30R!KRe=~I(O@Fh$ccEu1&!(A>OA`z zhQKk1)Zl_1nhJP*Aju01Xf-3I5Mo0>8)`xG5@f%)AhRQK!?H)v#DhD#9eTa1xy`aiVcTfnvmv zz{Vw(=fJEY9psxN7o<9Um_jV>XCqNboD}E8@nWfEgA;TdM$HH;jA63*O|ZP)$1`HF zNwg^WnMdaTS6{4ci8HXD5&0$|r0(J>)kabkE%%40;*Nnu4!o2`hzNQy#H zH{u}NZa3-ipmqytwL!3I1J3rxlzy3br8Z%9I@$f|V)FbjoLX){JspKnUJn4;XDTV8 zHgPSOR+Q45CG8!uyH^XX)9dwVxyU$Y!;eRIZQ69JaPHg%^t%rs=ECXNI>C`tFehZz_O5w#f#{5V3Yo9@ zu0#n0j{KzjyQFP+h)JG1P;>qDzwcfT?(OVJz5T&c`(Jp`NDiFejaAEogE}86C@6YG zTq7Q|jHb_~ADnw@7zZM1dcZh3LXF8S7J?zs!#$@-k)cHJ4x6a9OWKUBT4Ijk1#pt!wYtYd4K$mX5uyr2F@fdffp{&DSHtl4|Bn9=ukpCx`#%PTUr-D(@b7;C zhJXJjrnw{;1KBM=6&|E`fsNrGL!Y6%zUh}QUl`(@V)PmQFtotEKmafTHP0uP}7&%m4pcKQ#X)BiAJ2y+PrtBI>9eEIt2-_c7)?*Lsf z5vX5*Af@m-wBJRV%#Fiz=BdPr0!2^a2d-yv7gSU);Z6{`~Oy?E5qSnHUln4*Y%!){F!Y1~5WXoC44g zb7l_)X~q^V6MmWR7e3wj|L|Wb!|^}Y86N$^h+kv_Kw-fP!~#If$6(B4$)LlK#&G6; zC&ShMSAb$5tA9Xg5dOsg@-z^@3;;QS9f&!gG&3|;{Da~@Q2ZB({s%XJ5&#fj0J|In U+D>(nEdT%j07*qoM6N<$g0@&8X#fBK literal 0 HcmV?d00001 diff --git a/flaskext/debugtoolbar/static/js/toolbar.js b/flaskext/debugtoolbar/static/js/toolbar.js index 42557f4..e21311d 100644 --- a/flaskext/debugtoolbar/static/js/toolbar.js +++ b/flaskext/debugtoolbar/static/js/toolbar.js @@ -17,6 +17,7 @@ $.cookie = function(name, value, options) { if (typeof value != 'undefined') { options = options || {}; if (value === null) { value = ''; options.expires = -1; } var expires = ''; if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) { var date; if (typeof options.expires == 'number') { date = new Date(); date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000)); } else { date = options.expires; } expires = '; expires=' + date.toUTCString(); } var path = options.path ? '; path=' + (options.path) : ''; var domain = options.domain ? '; domain=' + (options.domain) : ''; var secure = options.secure ? '; secure' : ''; document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join(''); } else { var cookieValue = null; if (document.cookie && document.cookie != '') { var cookies = document.cookie.split(';'); for (var i = 0; i < cookies.length; i++) { var cookie = $.trim(cookies[i]); if (cookie.substring(0, name.length + 1) == (name + '=')) { cookieValue = decodeURIComponent(cookie.substring(name.length + 1)); break; } } } return cookieValue; } }; $('head').append(''); var COOKIE_NAME = 'fldt'; + var COOKIE_NAME_ACTIVE = COOKIE_NAME +'_active'; var fldt = { init: function() { $('#flDebug').show(); @@ -37,6 +38,37 @@ } return false; }); + $('#flDebugPanelList li .switch').click(function() { + var $panel = $(this).parent(); + var $this = $(this); + var dom_id = $panel.get(0).className; + + // Turn cookie content into an array of active panels + var active_str = $.cookie(COOKIE_NAME_ACTIVE); + var active = (active_str) ? active_str.split(';') : []; + active = $.grep(active, function(n,i) { return n != dom_id; }); + + if ($this.hasClass('active')) { + $this.removeClass('active'); + $this.addClass('inactive'); + } + else { + active.push(dom_id); + $this.removeClass('inactive'); + $this.addClass('active'); + } + + if (active.length > 0) { + $.cookie(COOKIE_NAME_ACTIVE, active.join(';'), { + path: '/', expires: 10 + }); + } + else { + $.cookie(COOKIE_NAME_ACTIVE, null, { + path: '/', expires: -1 + }); + } + }); $('#flDebug a.flDebugClose').click(function() { $(document).trigger('close.flDebug'); $('#flDebugToolbar li').removeClass('active'); diff --git a/flaskext/debugtoolbar/templates/base.html b/flaskext/debugtoolbar/templates/base.html index a6f6e64..4578a81 100644 --- a/flaskext/debugtoolbar/templates/base.html +++ b/flaskext/debugtoolbar/templates/base.html @@ -10,7 +10,7 @@
  • DEBUG
  • {% endif %} {% for panel in panels %} -
  • +
  • {% if panel.has_content %} {% else %} @@ -25,6 +25,9 @@ {% else %} {% endif %} + {% if panel.user_activate %} + + {% endif %}
  • {% endfor %} diff --git a/flaskext/debugtoolbar/toolbar.py b/flaskext/debugtoolbar/toolbar.py index b0b8458..0d910d3 100644 --- a/flaskext/debugtoolbar/toolbar.py +++ b/flaskext/debugtoolbar/toolbar.py @@ -5,7 +5,7 @@ class DebugToolbar(object): self.jinja_env = jinja_env self.request = request self.panels = [] - + # default config settings self.config = { 'DEBUG_TB_INTERCEPT_REDIRECTS': True, @@ -34,6 +34,8 @@ class DebugToolbar(object): Populate debug panels """ + activated = self.request.cookies.get('fldt_active', '').split(';') + for panel_path in self.default_panels: dot = panel_path.rindex('.') panel_module, panel_classname = panel_path[:dot], panel_path[dot+1:] @@ -44,6 +46,9 @@ class DebugToolbar(object): panel_instance = panel_class( context=self.template_context, jinja_env=self.jinja_env) + + if panel_instance.dom_id() in activated: + panel_instance.is_active = True self.panels.append(panel_instance) def render_toolbar(self):