1/** 2 * .cookieJar - Cookie Jar Plugin 3 * 4 * Version: 1.0.1 5 * Updated: 2007-08-14 6 * 7 * Used to store objects, arrays or multiple values in one cookie, under one name 8 * 9 * Copyright (c) 2007 James Dempster ([email protected], http://www.jdempster.com/category/jquery/cookieJar/) 10 * 11 * Dual licensed under the MIT (MIT-LICENSE.txt) 12 * and GPL (GPL-LICENSE.txt) licenses. 13 **/ 14 15/** 16 * Requirements: 17 * - jQuery (John Resig, http://www.jquery.com/) 18 * - cookie (Klaus Hartl, http://www.stilbuero.de/2006/09/17/cookie-plugin-for-jquery/) 19 * - toJSON (Mark Gibson, http://jollytoad.googlepages.com/json.js) 20 **/ 21 22 23(function($){$.toJSON=function(o) 24{if(typeof(JSON)=='object'&&JSON.stringify) 25return JSON.stringify(o);var type=typeof(o);if(o===null) 26return"null";if(type=="undefined") 27return undefined;if(type=="number"||type=="boolean") 28return o+"";if(type=="string") 29return $.quoteString(o);if(type=='object') 30{if(typeof o.toJSON=="function") 31return $.toJSON(o.toJSON());if(o.constructor===Date) 32{var month=o.getUTCMonth()+1;if(month<10)month='0'+month;var day=o.getUTCDate();if(day<10)day='0'+day;var year=o.getUTCFullYear();var hours=o.getUTCHours();if(hours<10)hours='0'+hours;var minutes=o.getUTCMinutes();if(minutes<10)minutes='0'+minutes;var seconds=o.getUTCSeconds();if(seconds<10)seconds='0'+seconds;var milli=o.getUTCMilliseconds();if(milli<100)milli='0'+milli;if(milli<10)milli='0'+milli;return'"'+year+'-'+month+'-'+day+'T'+ 33hours+':'+minutes+':'+seconds+'.'+milli+'Z"';} 34if(o.constructor===Array) 35{var ret=[];for(var i=0;i<o.length;i++) 36ret.push($.toJSON(o[i])||"null");return"["+ret.join(",")+"]";} 37var pairs=[];for(var k in o){var name;var type=typeof k;if(type=="number") 38name='"'+k+'"';else if(type=="string") 39name=$.quoteString(k);else 40continue;if(typeof o[k]=="function") 41continue;var val=$.toJSON(o[k]);pairs.push(name+":"+val);} 42return"{"+pairs.join(", ")+"}";}};$.evalJSON=function(src) 43{if(typeof(JSON)=='object'&&JSON.parse) 44return JSON.parse(src);return eval("("+src+")");};$.secureEvalJSON=function(src) 45{if(typeof(JSON)=='object'&&JSON.parse) 46return JSON.parse(src);var filtered=src;filtered=filtered.replace(/\\["\\\/bfnrtu]/g,'@');filtered=filtered.replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,']');filtered=filtered.replace(/(?:^|:|,)(?:\s*\[)+/g,'');if(/^[\],:{}\s]*$/.test(filtered)) 47return eval("("+src+")");else 48throw new SyntaxError("Error parsing JSON, source is not valid.");};$.quoteString=function(string) 49{if(string.match(_escapeable)) 50{return'"'+string.replace(_escapeable,function(a) 51{var c=_meta[a];if(typeof c==='string')return c;c=a.charCodeAt();return'\\u00'+Math.floor(c/16).toString(16)+(c%16).toString(16);})+'"';} 52return'"'+string+'"';};var _escapeable=/["\\\x00-\x1f\x7f-\x9f]/g;var _meta={'\b':'\\b','\t':'\\t','\n':'\\n','\f':'\\f','\r':'\\r','"':'\\"','\\':'\\\\'};})(jQuery); 53 54(function($) { 55 $.cookieJar = function(name, options) { 56 if (!$.parseJSON) return false; 57 if (!$.toJSON) return false; 58 if (!$.cookie) return false; 59 return new function() { 60 /** 61 * @access private 62 **/ 63 function log(s) { 64 if (typeof console != 'undefined' && typeof console.log != 'undefined') { 65 console.log('cookiejar:' + self.cookieName + ' ' + s); 66 } else { 67 alert(s); 68 } 69 }; 70 71 /** 72 * @access private 73 **/ 74 function save() { 75 if (self.options.debug) log('save ' + $.toJSON(self.cookieObject)); 76 return $.cookie(self.cookieName, $.toJSON(self.cookieObject), self.options.cookie); 77 }; 78 79 /** 80 * @access private 81 **/ 82 function load() { 83 var cookieJSON = $.cookie(self.cookieName); 84 if (typeof cookieJSON == 'string') { 85 if (self.options.debug) log('load ' + cookieJSON); 86 self.cookieObject = $.parseJSON(cookieJSON, true); 87 } else { 88 if (self.options.debug) log('load new'); 89 self.cookieObject = {}; 90 save(); 91 } 92 } 93 94 /** 95 * cookieJar.set(name, value) 96 * 97 * Sets a value in the cookie jar using a name to identify it 98 * 99 * @access public 100 * @param string name value identifier 101 * @param mixed value any value, array or object 102 * @return bool 103 **/ 104 this.set = function(name, value) { 105 if (self.options.debug) log('set ' + name + ' = ' + value); 106 self.cookieObject[name] = value; 107 return save(); 108 }; 109 110 /** 111 * cookieJar.get(name) 112 * 113 * Gets a value from the cookie jar using a name to identify it 114 * 115 * @access public 116 * @param string name value identifier 117 * @return mixed stored value 118 **/ 119 this.get = function(name) { 120 if (!self.options.cacheCookie) { 121 load(); 122 } 123 if (self.options.debug) log('get ' + name + ' = ' + self.cookieObject[name]); 124 return self.cookieObject[name]; 125 }; 126 127 /** 128 * cookieJar.remove([name]) 129 * 130 * Removes a value from the cookie jar using a name to identify it 131 * No name will clear the cookie jar of all values 132 * 133 * @access public 134 * @param string name value identifier 135 * @return bool 136 **/ 137 this.remove = function(name) { 138 if (self.options.debug) log('remove ' + name); 139 if (typeof name != 'undefined') { 140 delete(self.cookieObject[name]); 141 } else { 142 self.setFromObject({}); 143 } 144 return save(); 145 }; 146 147 /** 148 * cookieJar.setFromObject(object) 149 * 150 * Uses the object as the set of values to store in the cookie jar 151 * 152 * @access public 153 * @param object object new values for the cookie jar 154 * @return bool 155 **/ 156 this.setFromObject = function(object) { 157 if (typeof object == 'object') { 158 if (self.options.debug) log('setFromObject'); 159 self.cookieObject = object; 160 return save(); 161 } 162 }; 163 164 /** 165 * cookieJar.toObject() 166 * 167 * Returns the contents of the cookie jar as an object 168 * 169 * @access public
170 * @return object contents of the cookie jar 171 **/ 172 this.toObject = function() { 173 if (self.options.debug) log('toObject'); 174 return self.cookieObject; 175 }; 176 177 /** 178 * cookieJar.toString() 179 * 180 * Returns the contents of the cookie jar as a JSON encoded string 181 * 182 * @access public 183 * @return string contents of the cookie jar as JSON 184 **/ 185 this.toString = function() { 186 if (self.options.debug) log('toString = ' + $.toJSON(self.cookieObject)); 187 return $.toJSON(self.cookieObject); 188 }; 189 190 /** 191 * cookieJar.destroy() 192 * 193 * Removes the cookie containing the cookie jar from the server 194 * 195 * @access public 196 * @return bool 197 **/ 198 this.destroy = function() { 199 if (self.options.debug) log('destroy'); 200 self.cookieObject = {}; 201 return $.cookie(self.cookieName, null, self.options.cookie); 202 }; 203 204 /** 205 * cookieJar(name, [options]) 206 * 207 * loads a cookie jar for the name provided, creates new if none found 208 * 209 * @param string name 210 * @param object options 211 * @return object cookieJar 212 **/ 213 this.construct = function(name, options) { 214 self.options = $.extend({ 215 cookie: { 216 expires: 365, 217 path: '/' 218 }, 219 cacheCookie: true, 220 cookiePrefix: 'jqCookieJar_', 221 debug: false 222 }, options); 223 224 self.cookieName = self.options.cookiePrefix + name; 225 load(); 226 return self; 227 }; 228 229 var self = this; 230 self.construct(name, options); 231 }; 232 }; 233})(jQuery);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.