diff options
| author | Jannis Leidel | 2014-01-17 02:19:08 -0800 |
|---|---|---|
| committer | Jannis Leidel | 2014-01-17 02:19:08 -0800 |
| commit | cad93f57a0749fbc22a9dbc777193ace38013b0d (patch) | |
| tree | f4404001840fae9b787171026c642270b88bf5de /debug_toolbar | |
| parent | fe6b2ce8c3db95367741012dce6a310cdd50266e (diff) | |
| parent | 70b19cb5bea6a4915549458bb7169a56d40c3178 (diff) | |
| download | django-debug-toolbar-cad93f57a0749fbc22a9dbc777193ace38013b0d.tar.bz2 | |
Merge pull request #530 from djfarrelly/master
Remove dependency on jquery-cookie plugin
Diffstat (limited to 'debug_toolbar')
| -rw-r--r-- | debug_toolbar/static/debug_toolbar/js/jquery.cookie.js | 113 | ||||
| -rw-r--r-- | debug_toolbar/static/debug_toolbar/js/toolbar.js | 49 | ||||
| -rw-r--r-- | debug_toolbar/templates/debug_toolbar/base.html | 1 |
3 files changed, 41 insertions, 122 deletions
diff --git a/debug_toolbar/static/debug_toolbar/js/jquery.cookie.js b/debug_toolbar/static/debug_toolbar/js/jquery.cookie.js deleted file mode 100644 index a9da53b..0000000 --- a/debug_toolbar/static/debug_toolbar/js/jquery.cookie.js +++ /dev/null @@ -1,113 +0,0 @@ -/*! - * jQuery Cookie Plugin v1.4.0 - * https://github.com/carhartl/jquery-cookie - * - * Copyright 2013 Klaus Hartl - * Released under the MIT license - */ -(function (factory) { - if (typeof define === 'function' && define.amd) { - // AMD. Register as anonymous module. - define(['jquery'], factory); - } 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.setDate(t.getDate() + days); - } - - 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/debug_toolbar/static/debug_toolbar/js/toolbar.js b/debug_toolbar/static/debug_toolbar/js/toolbar.js index 6b5dffd..10a9c02 100644 --- a/debug_toolbar/static/debug_toolbar/js/toolbar.js +++ b/debug_toolbar/static/debug_toolbar/js/toolbar.js @@ -1,7 +1,7 @@ (function (factory) { if (typeof define === 'function' && define.amd) { // AMD. Register as anonymous module. - define(['jquery', 'jquery.cookie'], factory); + define(['jquery'], factory); } else { // Browser globals. window.djdt = factory(jQuery); @@ -57,9 +57,9 @@ return false; }); $(document).on('click', '#djDebug .djDebugPanelButton input[type=checkbox]', function() { - $.cookie($(this).attr('data-cookie'), $(this).prop('checked') ? 'on' : 'off', { + djdt.cookie.set($(this).attr('data-cookie'), $(this).prop('checked') ? 'on' : 'off', { path: '/', - expires: 10, + expires: 10 }); }); @@ -152,7 +152,7 @@ $(document).off('mousemove'); if (djdt.handleDragged) { var top = handle.offset().top; - $.cookie('djdttop', top, { + djdt.cookie.set('djdttop', top, { path: '/', expires: 10 }); @@ -180,7 +180,7 @@ return; } }); - if ($.cookie('djdt') == 'hide') { + if (djdt.cookie.get('djdt') == 'hide') { djdt.hide_toolbar(false); } else { djdt.show_toolbar(false); @@ -209,14 +209,14 @@ $('#djDebugToolbar').hide('fast'); $('#djDebugToolbarHandle').show(); // set handle position - var handleTop = $.cookie('djdttop'); + var handleTop = djdt.cookie.get('djdttop'); if (handleTop) { $('#djDebugToolbarHandle').css({top: handleTop + 'px'}); } // Unbind keydown $(document).unbind('keydown.djDebug'); if (setCookie) { - $.cookie('djdt', 'hide', { + djdt.cookie.set('djdt', 'hide', { path: '/', expires: 10 }); @@ -235,7 +235,7 @@ } else { $('#djDebugToolbar').show(); } - $.cookie('djdt', 'show', { + djdt.cookie.set('djdt', 'show', { path: '/', expires: 10 }); @@ -246,6 +246,39 @@ } else { djdt.events.ready.push(callback); } + }, + cookie: { + get: function(key){ + if (document.cookie.indexOf(key) === -1) return null; + + var cookieArray = document.cookie.split('; '), + cookies = {}; + + cookieArray.forEach(function(e){ + var parts = e.split('='); + cookies[ parts[0] ] = parts[1]; + }); + + return cookies[ key ]; + }, + set: function(key, value, options){ + options = options || {}; + + if (typeof options.expires === 'number') { + var days = options.expires, t = options.expires = new Date(); + t.setDate(t.getDate() + days); + } + + document.cookie = [ + encodeURIComponent(key) + '=' + String(value), + options.expires ? '; expires=' + options.expires.toUTCString() : '', + options.path ? '; path=' + options.path : '', + options.domain ? '; domain=' + options.domain : '', + options.secure ? '; secure' : '' + ].join(''); + + return value; + } } }; $(document).ready(djdt.init); diff --git a/debug_toolbar/templates/debug_toolbar/base.html b/debug_toolbar/templates/debug_toolbar/base.html index 52ebb57..6ceb735 100644 --- a/debug_toolbar/templates/debug_toolbar/base.html +++ b/debug_toolbar/templates/debug_toolbar/base.html @@ -6,7 +6,6 @@ <script>//<![CDATA[ if(!window.jQuery) document.write('<scr'+'ipt src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></scr'+'ipt>'); //]]></script> -<script src="{% static 'debug_toolbar/js/jquery.cookie.js' %}"></script> <script src="{% static 'debug_toolbar/js/toolbar.js' %}"></script> <div id="djDebug" style="display:none;" dir="ltr" data-store-id="{{ toolbar.store_id }}" data-render-panel-url="{% url 'djdt:render_panel' %}" |
