Skip to content

Commit 363c4a8

Browse files
committed
Merge pull request #440 from singingwolfboy/amd-js
Restructure JS to be AMD-compatible
2 parents ead8752 + 62b2e37 commit 363c4a8

File tree

5 files changed

+398
-400
lines changed

5 files changed

+398
-400
lines changed

Makefile

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,6 @@ coverage:
2222
coverage run --branch --source=debug_toolbar `which django-admin.py` test tests
2323
coverage html
2424

25-
compress_js:
26-
yuicompressor debug_toolbar/static/debug_toolbar/js/jquery.js > debug_toolbar/static/debug_toolbar/js/toolbar.min.js
27-
yuicompressor debug_toolbar/static/debug_toolbar/js/toolbar.js >> debug_toolbar/static/debug_toolbar/js/toolbar.min.js
28-
2925
compress_css:
3026
yuicompressor --type css debug_toolbar/static/debug_toolbar/css/toolbar.css > debug_toolbar/static/debug_toolbar/css/toolbar.min.css
3127

Lines changed: 104 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,96 +1,113 @@
1-
/**
2-
* Cookie plugin
3-
*
4-
* Copyright (c) 2006 Klaus Hartl (stilbuero.de)
5-
* Dual licensed under the MIT and GPL licenses:
6-
* http://www.opensource.org/licenses/mit-license.php
7-
* http://www.gnu.org/licenses/gpl.html
1+
/*!
2+
* jQuery Cookie Plugin v1.4.0
3+
* https://github.com/carhartl/jquery-cookie
84
*
5+
* Copyright 2013 Klaus Hartl
6+
* Released under the MIT license
97
*/
8+
(function (factory) {
9+
if (typeof define === 'function' && define.amd) {
10+
// AMD. Register as anonymous module.
11+
define(['jquery'], factory);
12+
} else {
13+
// Browser globals.
14+
factory(jQuery);
15+
}
16+
}(function ($) {
1017

11-
/**
12-
* Create a cookie with the given name and value and other optional parameters.
13-
*
14-
* @example $.cookie('the_cookie', 'the_value');
15-
* @desc Set the value of a cookie.
16-
* @example $.cookie('the_cookie', 'the_value', { expires: 7, path: '/', domain: 'jquery.com', secure: true });
17-
* @desc Create a cookie with all available options.
18-
* @example $.cookie('the_cookie', 'the_value');
19-
* @desc Create a session cookie.
20-
* @example $.cookie('the_cookie', null);
21-
* @desc Delete a cookie by passing null as value. Keep in mind that you have to use the same path and domain
22-
* used when the cookie was set.
23-
*
24-
* @param String name The name of the cookie.
25-
* @param String value The value of the cookie.
26-
* @param Object options An object literal containing key/value pairs to provide optional cookie attributes.
27-
* @option Number|Date expires Either an integer specifying the expiration date from now on in days or a Date object.
28-
* If a negative value is specified (e.g. a date in the past), the cookie will be deleted.
29-
* If set to null or omitted, the cookie will be a session cookie and will not be retained
30-
* when the the browser exits.
31-
* @option String path The value of the path atribute of the cookie (default: path of page that created the cookie).
32-
* @option String domain The value of the domain attribute of the cookie (default: domain of page that created the cookie).
33-
* @option Boolean secure If true, the secure attribute of the cookie will be set and the cookie transmission will
34-
* require a secure protocol (like HTTPS).
35-
* @type undefined
36-
*
37-
* @name $.cookie
38-
* @cat Plugins/Cookie
39-
* @author Klaus Hartl/klaus.hartl@stilbuero.de
40-
*/
18+
var pluses = /\+/g;
4119

42-
/**
43-
* Get the value of a cookie with the given name.
44-
*
45-
* @example $.cookie('the_cookie');
46-
* @desc Get the value of a cookie.
47-
*
48-
* @param String name The name of the cookie.
49-
* @return The value of the cookie.
50-
* @type String
51-
*
52-
* @name $.cookie
53-
* @cat Plugins/Cookie
54-
* @author Klaus Hartl/klaus.hartl@stilbuero.de
55-
*/
56-
jQuery.cookie = function(name, value, options) {
57-
if (typeof value != 'undefined') { // name and value given, set cookie
58-
options = options || {};
59-
if (value === null) {
60-
value = '';
61-
options.expires = -1;
20+
function encode(s) {
21+
return config.raw ? s : encodeURIComponent(s);
22+
}
23+
24+
function decode(s) {
25+
return config.raw ? s : decodeURIComponent(s);
26+
}
27+
28+
function stringifyCookieValue(value) {
29+
return encode(config.json ? JSON.stringify(value) : String(value));
30+
}
31+
32+
function parseCookieValue(s) {
33+
if (s.indexOf('"') === 0) {
34+
// This is a quoted cookie as according to RFC2068, unescape...
35+
s = s.slice(1, -1).replace(/\\"/g, '"').replace(/\\\\/g, '\\');
6236
}
63-
var expires = '';
64-
if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
65-
var date;
66-
if (typeof options.expires == 'number') {
67-
date = new Date();
68-
date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
69-
} else {
70-
date = options.expires;
37+
38+
try {
39+
// Replace server-side written pluses with spaces.
40+
// If we can't decode the cookie, ignore it, it's unusable.
41+
// If we can't parse the cookie, ignore it, it's unusable.
42+
s = decodeURIComponent(s.replace(pluses, ' '));
43+
return config.json ? JSON.parse(s) : s;
44+
} catch(e) {}
45+
}
46+
47+
function read(s, converter) {
48+
var value = config.raw ? s : parseCookieValue(s);
49+
return $.isFunction(converter) ? converter(value) : value;
50+
}
51+
52+
var config = $.cookie = function (key, value, options) {
53+
54+
// Write
55+
if (value !== undefined && !$.isFunction(value)) {
56+
options = $.extend({}, config.defaults, options);
57+
58+
if (typeof options.expires === 'number') {
59+
var days = options.expires, t = options.expires = new Date();
60+
t.setDate(t.getDate() + days);
7161
}
72-
expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
62+
63+
return (document.cookie = [
64+
encode(key), '=', stringifyCookieValue(value),
65+
options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
66+
options.path ? '; path=' + options.path : '',
67+
options.domain ? '; domain=' + options.domain : '',
68+
options.secure ? '; secure' : ''
69+
].join(''));
7370
}
74-
// CAUTION: Needed to parenthesize options.path and options.domain
75-
// in the following expressions, otherwise they evaluate to undefined
76-
// in the packed version for some reason...
77-
var path = options.path ? '; path=' + (options.path) : '';
78-
var domain = options.domain ? '; domain=' + (options.domain) : '';
79-
var secure = options.secure ? '; secure' : '';
80-
document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
81-
} else { // only name given, get cookie
82-
var cookieValue = null;
83-
if (document.cookie && document.cookie != '') {
84-
var cookies = document.cookie.split(';');
85-
for (var i = 0; i < cookies.length; i++) {
86-
var cookie = jQuery.trim(cookies[i]);
87-
// Does this cookie string begin with the name we want?
88-
if (cookie.substring(0, name.length + 1) == (name + '=')) {
89-
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
90-
break;
91-
}
71+
72+
// Read
73+
74+
var result = key ? undefined : {};
75+
76+
// To prevent the for loop in the first place assign an empty array
77+
// in case there are no cookies at all. Also prevents odd result when
78+
// calling $.cookie().
79+
var cookies = document.cookie ? document.cookie.split('; ') : [];
80+
81+
for (var i = 0, l = cookies.length; i < l; i++) {
82+
var parts = cookies[i].split('=');
83+
var name = decode(parts.shift());
84+
var cookie = parts.join('=');
85+
86+
if (key && key === name) {
87+
// If second argument (value) is a function it's a converter...
88+
result = read(cookie, value);
89+
break;
90+
}
91+
92+
// Prevent storing a cookie that we couldn't decode.
93+
if (!key && (cookie = read(cookie)) !== undefined) {
94+
result[name] = cookie;
9295
}
9396
}
94-
return cookieValue;
95-
}
96-
};
97+
98+
return result;
99+
};
100+
101+
config.defaults = {};
102+
103+
$.removeCookie = function (key, options) {
104+
if ($.cookie(key) === undefined) {
105+
return false;
106+
}
107+
108+
// Must not alter options, thus extending a fresh object...
109+
$.cookie(key, '', $.extend({}, options, { expires: -1 }));
110+
return !$.cookie(key);
111+
};
112+
113+
}));

0 commit comments

Comments
 (0)