|
| 1 | +/*! |
| 2 | + * Javascript Cookie v1.5.1 |
| 3 | + * https://github.com/js-cookie/js-cookie |
| 4 | + * |
| 5 | + * Copyright 2006, 2014 Klaus Hartl |
| 6 | + * Released under the MIT license |
| 7 | + */ |
| 8 | +(function (factory) { |
| 9 | + var jQuery; |
| 10 | + if (typeof define === 'function' && define.amd) { |
| 11 | + // AMD (Register as an anonymous module) |
| 12 | + define(['jquery'], factory); |
| 13 | + } else if (typeof exports === 'object') { |
| 14 | + // Node/CommonJS |
| 15 | + try { |
| 16 | + jQuery = require('jquery'); |
| 17 | + } catch(e) {} |
| 18 | + module.exports = factory(jQuery); |
| 19 | + } else { |
| 20 | + // Browser globals |
| 21 | + var _OldCookies = window.Cookies; |
| 22 | + var api = window.Cookies = factory(window.jQuery); |
| 23 | + api.noConflict = function() { |
| 24 | + window.Cookies = _OldCookies; |
| 25 | + return api; |
| 26 | + }; |
| 27 | + } |
| 28 | +}(function ($) { |
| 29 | + |
| 30 | + var pluses = /\+/g; |
| 31 | + |
| 32 | + function encode(s) { |
| 33 | + return api.raw ? s : encodeURIComponent(s); |
| 34 | + } |
| 35 | + |
| 36 | + function decode(s) { |
| 37 | + return api.raw ? s : decodeURIComponent(s); |
| 38 | + } |
| 39 | + |
| 40 | + function stringifyCookieValue(value) { |
| 41 | + return encode(api.json ? JSON.stringify(value) : String(value)); |
| 42 | + } |
| 43 | + |
| 44 | + function parseCookieValue(s) { |
| 45 | + if (s.indexOf('"') === 0) { |
| 46 | + // This is a quoted cookie as according to RFC2068, unescape... |
| 47 | + s = s.slice(1, -1).replace(/\\"/g, '"').replace(/\\\\/g, '\\'); |
| 48 | + } |
| 49 | + |
| 50 | + try { |
| 51 | + // Replace server-side written pluses with spaces. |
| 52 | + // If we can't decode the cookie, ignore it, it's unusable. |
| 53 | + // If we can't parse the cookie, ignore it, it's unusable. |
| 54 | + s = decodeURIComponent(s.replace(pluses, ' ')); |
| 55 | + return api.json ? JSON.parse(s) : s; |
| 56 | + } catch(e) {} |
| 57 | + } |
| 58 | + |
| 59 | + function read(s, converter) { |
| 60 | + var value = api.raw ? s : parseCookieValue(s); |
| 61 | + return isFunction(converter) ? converter(value) : value; |
| 62 | + } |
| 63 | + |
| 64 | + function extend() { |
| 65 | + var key, options; |
| 66 | + var i = 0; |
| 67 | + var result = {}; |
| 68 | + for (; i < arguments.length; i++) { |
| 69 | + options = arguments[ i ]; |
| 70 | + for (key in options) { |
| 71 | + result[key] = options[key]; |
| 72 | + } |
| 73 | + } |
| 74 | + return result; |
| 75 | + } |
| 76 | + |
| 77 | + function isFunction(obj) { |
| 78 | + return Object.prototype.toString.call(obj) === '[object Function]'; |
| 79 | + } |
| 80 | + |
| 81 | + var api = function (key, value, options) { |
| 82 | + |
| 83 | + // Write |
| 84 | + |
| 85 | + if (arguments.length > 1 && !isFunction(value)) { |
| 86 | + options = extend(api.defaults, options); |
| 87 | + |
| 88 | + if (typeof options.expires === 'number') { |
| 89 | + var days = options.expires, t = options.expires = new Date(); |
| 90 | + t.setMilliseconds(t.getMilliseconds() + days * 864e+5); |
| 91 | + } |
| 92 | + |
| 93 | + return (document.cookie = [ |
| 94 | + encode(key), '=', stringifyCookieValue(value), |
| 95 | + options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE |
| 96 | + options.path ? '; path=' + options.path : '', |
| 97 | + options.domain ? '; domain=' + options.domain : '', |
| 98 | + options.secure ? '; secure' : '' |
| 99 | + ].join('')); |
| 100 | + } |
| 101 | + |
| 102 | + // Read |
| 103 | + |
| 104 | + var result = key ? undefined : {}, |
| 105 | + // To prevent the for loop in the first place assign an empty array |
| 106 | + // in case there are no cookies at all. Also prevents odd result when |
| 107 | + // calling "get()". |
| 108 | + cookies = document.cookie ? document.cookie.split('; ') : [], |
| 109 | + i = 0, |
| 110 | + l = cookies.length; |
| 111 | + |
| 112 | + for (; i < l; i++) { |
| 113 | + var parts = cookies[i].split('='), |
| 114 | + name = decode(parts.shift()), |
| 115 | + cookie = parts.join('='); |
| 116 | + |
| 117 | + if (key === name) { |
| 118 | + // If second argument (value) is a function it's a converter... |
| 119 | + result = read(cookie, value); |
| 120 | + break; |
| 121 | + } |
| 122 | + |
| 123 | + // Prevent storing a cookie that we couldn't decode. |
| 124 | + if (!key && (cookie = read(cookie)) !== undefined) { |
| 125 | + result[name] = cookie; |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + return result; |
| 130 | + }; |
| 131 | + |
| 132 | + api.get = api.set = api; |
| 133 | + api.defaults = {}; |
| 134 | + |
| 135 | + api.remove = function (key, options) { |
| 136 | + // Must not alter options, thus extending a fresh object... |
| 137 | + api(key, '', extend(options, { expires: -1 })); |
| 138 | + return !api(key); |
| 139 | + }; |
| 140 | + |
| 141 | + if ( $ ) { |
| 142 | + $.cookie = api; |
| 143 | + $.removeCookie = api.remove; |
| 144 | + } |
| 145 | + |
| 146 | + return api; |
| 147 | +})); |
0 commit comments