|
| 1 | +/*! |
| 2 | + * JavaScript Cookie v2.0.0-beta.1 |
| 3 | + * https://github.com/js-cookie/js-cookie |
| 4 | + * |
| 5 | + * Copyright 2006, 2015 Klaus Hartl |
| 6 | + * Released under the MIT license |
| 7 | + */ |
| 8 | +(function (factory) { |
| 9 | + if (typeof define === 'function' && define.amd) { |
| 10 | + define(factory); |
| 11 | + } else if (typeof exports === 'object') { |
| 12 | + module.exports = factory(); |
| 13 | + } else { |
| 14 | + var _OldCookies = window.Cookies; |
| 15 | + var api = window.Cookies = factory(window.jQuery); |
| 16 | + api.noConflict = function () { |
| 17 | + window.Cookies = _OldCookies; |
| 18 | + return api; |
| 19 | + }; |
| 20 | + } |
| 21 | +}(function () { |
| 22 | + function extend () { |
| 23 | + var i = 0; |
| 24 | + var result = {}; |
| 25 | + for (; i < arguments.length; i++) { |
| 26 | + var attributes = arguments[ i ]; |
| 27 | + for (var key in attributes) { |
| 28 | + result[key] = attributes[key]; |
| 29 | + } |
| 30 | + } |
| 31 | + return result; |
| 32 | + } |
| 33 | + |
| 34 | + function init (converter) { |
| 35 | + function api (key, value, attributes) { |
| 36 | + var result; |
| 37 | + |
| 38 | + // Write |
| 39 | + |
| 40 | + if (arguments.length > 1) { |
| 41 | + attributes = extend({ |
| 42 | + path: '/' |
| 43 | + }, api.defaults, attributes); |
| 44 | + |
| 45 | + if (typeof attributes.expires === 'number') { |
| 46 | + var expires = new Date(); |
| 47 | + expires.setMilliseconds(expires.getMilliseconds() + attributes.expires * 864e+5); |
| 48 | + attributes.expires = expires; |
| 49 | + } |
| 50 | + |
| 51 | + try { |
| 52 | + result = JSON.stringify(value); |
| 53 | + if (/^[\{\[]/.test(result)) { |
| 54 | + value = result; |
| 55 | + } |
| 56 | + } catch (e) {} |
| 57 | + |
| 58 | + value = encodeURIComponent(String(value)); |
| 59 | + value = value.replace(/%(23|24|26|2B|3A|3C|3E|3D|2F|3F|40|5B|5D|5E|60|7B|7D|7C)/g, decodeURIComponent); |
| 60 | + |
| 61 | + key = encodeURIComponent(String(key)); |
| 62 | + key = key.replace(/%(23|24|26|2B|5E|60|7C)/g, decodeURIComponent); |
| 63 | + key = key.replace(/[\(\)]/g, escape); |
| 64 | + |
| 65 | + return (document.cookie = [ |
| 66 | + key, '=', value, |
| 67 | + attributes.expires && '; expires=' + attributes.expires.toUTCString(), // use expires attribute, max-age is not supported by IE |
| 68 | + attributes.path && '; path=' + attributes.path, |
| 69 | + attributes.domain && '; domain=' + attributes.domain, |
| 70 | + attributes.secure && '; secure' |
| 71 | + ].join('')); |
| 72 | + } |
| 73 | + |
| 74 | + // Read |
| 75 | + |
| 76 | + if (!key) { |
| 77 | + result = {}; |
| 78 | + } |
| 79 | + |
| 80 | + // To prevent the for loop in the first place assign an empty array |
| 81 | + // in case there are no cookies at all. Also prevents odd result when |
| 82 | + // calling "get()" |
| 83 | + var cookies = document.cookie ? document.cookie.split('; ') : []; |
| 84 | + var rdecode = /(%[0-9A-Z]{2})+/g; |
| 85 | + var i = 0; |
| 86 | + |
| 87 | + for (; i < cookies.length; i++) { |
| 88 | + var parts = cookies[i].split('='); |
| 89 | + var name = parts[0].replace(rdecode, decodeURIComponent); |
| 90 | + var cookie = parts.slice(1).join('='); |
| 91 | + |
| 92 | + if (cookie.charAt(0) === '"') { |
| 93 | + cookie = cookie.slice(1, -1); |
| 94 | + } |
| 95 | + |
| 96 | + cookie = converter && converter(cookie, name) || cookie.replace(rdecode, decodeURIComponent); |
| 97 | + |
| 98 | + if (this.json) { |
| 99 | + try { |
| 100 | + cookie = JSON.parse(cookie); |
| 101 | + } catch (e) {} |
| 102 | + } |
| 103 | + |
| 104 | + if (key === name) { |
| 105 | + result = cookie; |
| 106 | + break; |
| 107 | + } |
| 108 | + |
| 109 | + if (!key) { |
| 110 | + result[name] = cookie; |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + return result; |
| 115 | + } |
| 116 | + |
| 117 | + api.get = api.set = api; |
| 118 | + api.getJSON = function () { |
| 119 | + return api.apply({ |
| 120 | + json: true |
| 121 | + }, [].slice.call(arguments)); |
| 122 | + }; |
| 123 | + api.defaults = {}; |
| 124 | + |
| 125 | + api.remove = function (key, attributes) { |
| 126 | + api(key, '', extend(attributes, { |
| 127 | + expires: -1 |
| 128 | + })); |
| 129 | + }; |
| 130 | + |
| 131 | + api.withConverter = init; |
| 132 | + |
| 133 | + return api; |
| 134 | + } |
| 135 | + |
| 136 | + return init(); |
| 137 | +})); |
0 commit comments