]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - js/extlib/jquery.cookie.js
Merge request 9 on Gitorious
[quix0rs-gnu-social.git] / js / extlib / jquery.cookie.js
1 /*!
2  * jQuery Cookie Plugin v1.3.1
3  * https://github.com/carhartl/jquery-cookie
4  *
5  * Copyright 2013 Klaus Hartl
6  * Released under the MIT license
7  */
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 ($) {
17
18         var pluses = /\+/g;
19
20         function decode(s) {
21                 if (config.raw) {
22                         return s;
23                 }
24                 try {
25                         // If we can't decode the cookie, ignore it, it's unusable.
26                         return decodeURIComponent(s.replace(pluses, ' '));
27                 } catch(e) {}
28         }
29
30         function decodeAndParse(s) {
31                 if (s.indexOf('"') === 0) {
32                         // This is a quoted cookie as according to RFC2068, unescape...
33                         s = s.slice(1, -1).replace(/\\"/g, '"').replace(/\\\\/g, '\\');
34                 }
35
36                 s = decode(s);
37
38                 try {
39                         // If we can't parse the cookie, ignore it, it's unusable.
40                         return config.json ? JSON.parse(s) : s;
41                 } catch(e) {}
42         }
43
44         var config = $.cookie = function (key, value, options) {
45
46                 // Write
47                 if (value !== undefined) {
48                         options = $.extend({}, config.defaults, options);
49
50                         if (typeof options.expires === 'number') {
51                                 var days = options.expires, t = options.expires = new Date();
52                                 t.setDate(t.getDate() + days);
53                         }
54
55                         value = config.json ? JSON.stringify(value) : String(value);
56
57                         return (document.cookie = [
58                                 config.raw ? key : encodeURIComponent(key),
59                                 '=',
60                                 config.raw ? value : encodeURIComponent(value),
61                                 options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
62                                 options.path    ? '; path=' + options.path : '',
63                                 options.domain  ? '; domain=' + options.domain : '',
64                                 options.secure  ? '; secure' : ''
65                         ].join(''));
66                 }
67
68                 // Read
69
70                 var result = key ? undefined : {};
71
72                 // To prevent the for loop in the first place assign an empty array
73                 // in case there are no cookies at all. Also prevents odd result when
74                 // calling $.cookie().
75                 var cookies = document.cookie ? document.cookie.split('; ') : [];
76
77                 for (var i = 0, l = cookies.length; i < l; i++) {
78                         var parts = cookies[i].split('=');
79                         var name = decode(parts.shift());
80                         var cookie = parts.join('=');
81
82                         if (key && key === name) {
83                                 result = decodeAndParse(cookie);
84                                 break;
85                         }
86
87                         // Prevent storing a cookie that we couldn't decode.
88                         if (!key && (cookie = decodeAndParse(cookie)) !== undefined) {
89                                 result[name] = cookie;
90                         }
91                 }
92
93                 return result;
94         };
95
96         config.defaults = {};
97
98         $.removeCookie = function (key, options) {
99                 if ($.cookie(key) !== undefined) {
100                         // Must not alter options, thus extending a fresh object...
101                         $.cookie(key, '', $.extend({}, options, { expires: -1 }));
102                         return true;
103                 }
104                 return false;
105         };
106
107 }));