Path: blob/master/webroot/rsrc/externals/javelin/lib/Cookie.js
12242 views
/**1* @provides javelin-cookie2* @requires javelin-install3* javelin-util4* @javelin5*/67/*8* API/Wrapper for document cookie access and manipulation based heavily on the9* MooTools Cookie.js10*11* github.com/mootools/mootools-core/blob/master/Source/Utilities/Cookie.js12*13* Thx again, Moo.14*/15JX.install('Cookie', {1617/**18* Setting cookies involves setting up a cookie object which is eventually19* written.20*21* var prefs = new JX.Cookie('prefs');22* prefs.setDaysToLive(5);23* prefs.setValue('1,0,10,1350');24* prefs.setSecure();25* prefs.write();26*27* Retrieving a cookie from the browser requires only a read() call on the28* cookie object. However, because cookies have such a complex API you may29* not be able to get your value this way if a path or domain was set when the30* cookie was written. Good luck with that.31*32* var prefs_string = new JX.Cookie('prefs').read();33*34* There is no real API in HTTP for deleting a cookie aside from setting the35* cookie to expire immediately. This dispose method will null out the value36* and expire the cookie as well.37*38* new JX.Cookie('prefs').dispose();39*/40construct : function(key) {41if (__DEV__ &&42(!key.length ||43key.match(/^(?:expires|domain|path|secure)$/i) ||44key.match(/[\s,;]/) ||45key.indexOf('$') === 0)) {46JX.$E('JX.Cookie(): Invalid cookie name. "' + key + '" provided.');47}48this.setKey(key);49this.setTarget(document);50},5152properties : {53key : null,54value : null,55domain : null,56path : null,57daysToLive : 0,58secure : true,59target : null60},6162members : {63write : function() {64this.setValue(encodeURIComponent(this.getValue()));6566var cookie_bits = [];67cookie_bits.push(this.getValue());6869if (this.getDomain()) {70cookie_bits.push('Domain=' + this.getDomain());71}7273if (this.getPath()) {74cookie_bits.push('Path=' + this.getPath());75}7677var exp = new Date(JX.now() + this.getDaysToLive() * 1000 * 60 * 60 * 24);78cookie_bits.push('Expires=' + exp.toGMTString());7980if (this.getSecure()) {81cookie_bits.push('Secure');82}8384var cookie_str = cookie_bits.join('; ') + ';';85cookie_str = this.getKey() + '=' + cookie_str;86this.getTarget().cookie = cookie_str;87},8889read : function() {90var key = this.getKey().replace(/([-.*+?^${}()|[\]\/\\])/g, '\\$1');91var val = this.getTarget().cookie.match('(?:^|;)\\s*' + key + '=([^;]*)');92return (val) ? decodeURIComponent(val[1]) : null;93},9495dispose : function() {96this.setValue(null);97this.setDaysToLive(-1);98this.write();99}100}101});102103104