[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/webroot/rsrc/externals/javelin/lib/ -> Cookie.js (source)

   1  /**
   2   * @provides javelin-cookie
   3   * @requires javelin-install
   4   *           javelin-util
   5   * @javelin
   6   */
   7  
   8  /*
   9   * API/Wrapper for document cookie access and manipulation based heavily on the
  10   * MooTools Cookie.js
  11   *
  12   * github.com/mootools/mootools-core/blob/master/Source/Utilities/Cookie.js
  13   *
  14   * Thx again, Moo.
  15   */
  16  JX.install('Cookie', {
  17  
  18    /**
  19     * Setting cookies involves setting up a cookie object which is eventually
  20     * written.
  21     *
  22     *   var prefs = new JX.Cookie('prefs');
  23     *   prefs.setDaysToLive(5);
  24     *   prefs.setValue('1,0,10,1350');
  25     *   prefs.setSecure();
  26     *   prefs.write();
  27     *
  28     * Retrieving a cookie from the browser requires only a read() call on the
  29     * cookie object. However, because cookies have such a complex API you may
  30     * not be able to get your value this way if a path or domain was set when the
  31     * cookie was written. Good luck with that.
  32     *
  33     *   var prefs_string = new JX.Cookie('prefs').read();
  34     *
  35     * There is no real API in HTTP for deleting a cookie aside from setting the
  36     * cookie to expire immediately. This dispose method will null out the value
  37     * and expire the cookie as well.
  38     *
  39     *   new JX.Cookie('prefs').dispose();
  40     */
  41    construct : function(key) {
  42      if (__DEV__ &&
  43          (!key.length ||
  44           key.match(/^(?:expires|domain|path|secure)$/i) ||
  45           key.match(/[\s,;]/) ||
  46           key.indexOf('$') === 0)) {
  47        JX.$E('JX.Cookie(): Invalid cookie name. "' + key + '" provided.');
  48      }
  49      this.setKey(key);
  50      this.setTarget(document);
  51    },
  52  
  53    properties : {
  54      key : null,
  55      value : null,
  56      domain : null,
  57      path : null,
  58      daysToLive : 0,
  59      secure : true,
  60      target : null
  61    },
  62  
  63    members : {
  64      write : function() {
  65        this.setValue(encodeURIComponent(this.getValue()));
  66  
  67        var cookie_bits = [];
  68        cookie_bits.push(this.getValue());
  69  
  70        if (this.getDomain()) {
  71          cookie_bits.push('Domain=' + this.getDomain());
  72        }
  73  
  74        if (this.getPath()) {
  75          cookie_bits.push('Path=' + this.getPath());
  76        }
  77  
  78        var exp = new Date(JX.now() + this.getDaysToLive() * 1000 * 60 * 60 * 24);
  79        cookie_bits.push('Expires=' + exp.toGMTString());
  80  
  81        if (this.getSecure()) {
  82          cookie_bits.push('Secure');
  83        }
  84  
  85        cookie_str = cookie_bits.join('; ') + ';';
  86        var cookie_str = this.getKey() + '=' + cookie_str;
  87        this.getTarget().cookie = cookie_str;
  88      },
  89  
  90      read : function() {
  91        var key = this.getKey().replace(/([-.*+?^${}()|[\]\/\\])/g, '\\$1');
  92        var val = this.getTarget().cookie.match('(?:^|;)\\s*' + key + '=([^;]*)');
  93        return (val) ? decodeURIComponent(val[1]) : null;
  94      },
  95  
  96      dispose : function() {
  97        this.setValue(null);
  98        this.setDaysToLive(-1);
  99        this.write();
 100      }
 101    }
 102  });


Generated: Sun Nov 30 09:20:46 2014 Cross-referenced by PHPXref 0.7.1