github.com/apremalal/vamps-core@v1.0.1-0.20161221121535-d430b56ec174/server/webapps/app/base/plugins/cookie/js.cookie.js (about) 1 /*! 2 * JavaScript Cookie v2.1.2 3 * https://github.com/js-cookie/js-cookie 4 * 5 * Copyright 2006, 2015 Klaus Hartl & Fagner Brack 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(); 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 if (typeof document === 'undefined') { 38 return; 39 } 40 41 // Write 42 43 if (arguments.length > 1) { 44 attributes = extend({ 45 path: '/' 46 }, api.defaults, attributes); 47 48 if (typeof attributes.expires === 'number') { 49 var expires = new Date(); 50 expires.setMilliseconds(expires.getMilliseconds() + attributes.expires * 864e+5); 51 attributes.expires = expires; 52 } 53 54 try { 55 result = JSON.stringify(value); 56 if (/^[\{\[]/.test(result)) { 57 value = result; 58 } 59 } catch (e) {} 60 61 if (!converter.write) { 62 value = encodeURIComponent(String(value)) 63 .replace(/%(23|24|26|2B|3A|3C|3E|3D|2F|3F|40|5B|5D|5E|60|7B|7D|7C)/g, decodeURIComponent); 64 } else { 65 value = converter.write(value, key); 66 } 67 68 key = encodeURIComponent(String(key)); 69 key = key.replace(/%(23|24|26|2B|5E|60|7C)/g, decodeURIComponent); 70 key = key.replace(/[\(\)]/g, escape); 71 72 return (document.cookie = [ 73 key, '=', value, 74 attributes.expires && '; expires=' + attributes.expires.toUTCString(), // use expires attribute, max-age is not supported by IE 75 attributes.path && '; path=' + attributes.path, 76 attributes.domain && '; domain=' + attributes.domain, 77 attributes.secure ? '; secure' : '' 78 ].join('')); 79 } 80 81 // Read 82 83 if (!key) { 84 result = {}; 85 } 86 87 // To prevent the for loop in the first place assign an empty array 88 // in case there are no cookies at all. Also prevents odd result when 89 // calling "get()" 90 var cookies = document.cookie ? document.cookie.split('; ') : []; 91 var rdecode = /(%[0-9A-Z]{2})+/g; 92 var i = 0; 93 94 for (; i < cookies.length; i++) { 95 var parts = cookies[i].split('='); 96 var cookie = parts.slice(1).join('='); 97 98 if (cookie.charAt(0) === '"') { 99 cookie = cookie.slice(1, -1); 100 } 101 102 try { 103 var name = parts[0].replace(rdecode, decodeURIComponent); 104 cookie = converter.read ? 105 converter.read(cookie, name) : converter(cookie, name) || 106 cookie.replace(rdecode, decodeURIComponent); 107 108 if (this.json) { 109 try { 110 cookie = JSON.parse(cookie); 111 } catch (e) {} 112 } 113 114 if (key === name) { 115 result = cookie; 116 break; 117 } 118 119 if (!key) { 120 result[name] = cookie; 121 } 122 } catch (e) {} 123 } 124 125 return result; 126 } 127 128 api.set = api; 129 api.get = function (key) { 130 return api(key); 131 }; 132 api.getJSON = function () { 133 return api.apply({ 134 json: true 135 }, [].slice.call(arguments)); 136 }; 137 api.defaults = {}; 138 139 api.remove = function (key, attributes) { 140 api(key, '', extend(attributes, { 141 expires: -1 142 })); 143 }; 144 145 api.withConverter = init; 146 147 return api; 148 } 149 150 return init(function () {}); 151 }));