1 // ==========================================================================
  2 // Project:   SproutCore - JavaScript Application Framework
  3 // Copyright: ©2006-2011 Strobe Inc. and contributors.
  4 //            ©2008-2011 Apple Inc. All rights reserved.
  5 // License:   Licensed under MIT license (see license.js)
  6 // ==========================================================================
  7 
  8 
  9 /** @class
 10 
 11   Allows for easier handling of the document.cookie object. To create a cookie,
 12   simply call SC.Cookie.create. To retrieve a cookie, use SC.Cookie.find.
 13   Cookies are not added to document.cookie, which SC.Cookie.find uses, until you
 14   have called SC.Cookie#write.
 15 
 16   Heavy inspiration from the
 17   {@link <a href="http://plugins.jquery.com/project/cookie">jQuery cookie plugin</a>}.
 18 
 19   @extends SC.Object
 20   @since Sproutcore 1.0
 21   @author Colin Campbell
 22 */
 23 
 24 SC.Cookie = SC.Object.extend(
 25 /** @scope SC.Cookie.prototype */{
 26 
 27   // ..........................................................
 28   // PROPERTIES
 29   //
 30 
 31   /**
 32     @type String
 33     @default null
 34   */
 35   name: null,
 36 
 37   /**
 38     @type String
 39     @default ''
 40   */
 41   value: '',
 42 
 43   /**
 44     Amount of time until the cookie expires. Set to -1 in order to delete the cookie.
 45 
 46     If passing an Integer, it is interpreted as a number of days.
 47 
 48     @type Integer|SC.DateTime|Date
 49     @default null
 50   */
 51   expires: null,
 52 
 53   /**
 54     @type String
 55     @deafult null
 56   */
 57   path: null,
 58 
 59   /**
 60     @type String
 61     @default null
 62   */
 63   domain: null,
 64 
 65   /**
 66     If true, the secure attribute of the cookie will be set and the cookie transmission will
 67     require a secure protocol (like HTTPS).
 68 
 69     @type Boolean
 70     @default NO
 71   */
 72   secure: NO,
 73 
 74   /**
 75     Walk like a duck
 76 
 77     @type Boolean
 78     @default YES
 79     @readOnly
 80   */
 81   isCookie: YES,
 82 
 83 
 84   // ..........................................................
 85   // METHODS
 86   //
 87 
 88   /**
 89     Sets SC.Cookie#expires to -1, which destroys the cookie.
 90   */
 91   destroy: function() {
 92     this.set('expires', -1);
 93     this.write();
 94 
 95     sc_super();
 96   },
 97 
 98   /**
 99     Writes this SC.Cookie to document.cookie and adds it to SC.Cookie collection. To find this
100     cookie later, or on reload, use SC.Cookie.find.
101 
102     @see SC.Cookie.find
103   */
104   write: function() {
105     var name = this.get('name'),
106         value = this.get('value'),
107         expires = this.get('expires'),
108         path = this.get('path'),
109         domain = this.get('domain'),
110         secure = this.get('secure'),
111         output = '',
112         date;
113 
114     if (expires) {
115       if (typeof expires === SC.T_NUMBER) {
116         date = new Date();
117         date.setTime(date.getTime() + (expires*24*60*60*1000));
118       } else if (SC.DateTime && expires.get && expires.get('milliseconds')) {
119         date = new Date(expires.get('milliseconds'));
120       } else if (expires.toUTCString && expires.toUTCString.apply) {
121         date = expires;
122       }
123 
124       if (date) output = "; expires=" + date.toUTCString();
125     }
126 
127     if (!SC.none(path)) output += '; path=' + path;
128     if (!SC.none(domain)) output += '; domain=' + domain;
129     if (secure === YES) output += '; secure';
130 
131     document.cookie = name + "=" + encodeURIComponent(value) + output;
132 
133     return this;
134   }
135 
136 });
137 
138 SC.Cookie.mixin(
139   /** @scope SC.Cookie */ {
140 
141   /**
142     Finds a cookie that has been stored
143 
144     @param {String} name The name of the cookie
145     @returns SC.Cookie object containing name and value of cookie
146   */
147   find: function(name) {
148     if (document.cookie && document.cookie !== '') {
149       var cookies = document.cookie.split(';');
150       for (var i = 0; i < cookies.length; i++) {
151         var cookie = SC.String.trim(String(cookies[i]));
152         if (cookie.substring(0, name.length + 1) === (name + "=")) {
153           return SC.Cookie.create({
154             name: name,
155             value: decodeURIComponent(cookie.substring(name.length + 1))
156           });
157         }
158       }
159     }
160     return null;
161   }
162 
163 });
164 
165 SC.CookieMonster = {
166   nomNomNom: function(cookie) {
167     var isCookie = SC.kindOf(cookie, SC.Cookie);
168     if (isCookie) {
169       SC.Logger.log("YUM!");
170       return cookie.destroy();
171     }
172     
173     SC.Logger.error("Y U PASS ME NO COOKIE? %@", cookie);
174     return NO;
175   }
176 };