[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/src/bp-core/js/vendor/ -> jquery-cookie.js (source)

   1  /* jshint undef: false */
   2  
   3  /*!
   4   * jQuery Cookie Plugin v1.4.1
   5   * https://github.com/carhartl/jquery-cookie
   6   *
   7   * Copyright 2013 Klaus Hartl
   8   * Released under the MIT license
   9   */
  10  (function(factory) {
  11      // AMD.
  12      if (typeof define === 'function' && define.amd) {
  13          define(['jquery'], factory);
  14      // CommonJS.
  15      } else if (typeof exports === 'object') {
  16          factory(require('jquery'));
  17      // Browser globals.
  18      } else {
  19          factory(jQuery);
  20      }
  21  }(function($) {
  22  
  23      var pluses = /\+/g;
  24  
  25  	function encode(s) {
  26          return config.raw ? s : encodeURIComponent(s);
  27      }
  28  
  29  	function decode(s) {
  30          return config.raw ? s : decodeURIComponent(s);
  31      }
  32  
  33  	function stringifyCookieValue(value) {
  34          return encode(config.json ? JSON.stringify(value) : String(value));
  35      }
  36  
  37  	function parseCookieValue(s) {
  38          if (s.indexOf('"') === 0) {
  39              // This is a quoted cookie as according to RFC2068, unescape...
  40              s = s.slice(1, -1).replace(/\\"/g, '"').replace(/\\\\/g, '\\');
  41          }
  42  
  43          try {
  44              // Replace server-side written pluses with spaces.
  45              // If we can't decode the cookie, ignore it, it's unusable.
  46              // If we can't parse the cookie, ignore it, it's unusable.
  47              s = decodeURIComponent(s.replace(pluses, ' '));
  48              return config.json ? JSON.parse(s) : s;
  49          } catch (e) {
  50          }
  51      }
  52  
  53  	function read(s, converter) {
  54          var value = config.raw ? s : parseCookieValue(s);
  55          return typeof converter === 'function' ? converter(value) : value;
  56      }
  57  
  58      var config = $.cookie = function(key, value, options) {
  59  
  60          // Write.
  61  
  62          if (value !== undefined && typeof value !== 'function' ) {
  63              options = $.extend({}, config.defaults, options);
  64  
  65              if (typeof options.expires === 'number') {
  66                  var days = options.expires, t = options.expires = new Date();
  67                  t.setTime(+t + days * 864e+5);
  68              }
  69  
  70              return (document.cookie = [
  71                  encode(key), '=', stringifyCookieValue(value),
  72                  options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
  73                  options.path ? '; path=' + options.path : '',
  74                  options.domain ? '; domain=' + options.domain : '',
  75                  options.secure ? '; secure' : ''
  76              ].join(''));
  77          }
  78  
  79          // Read.
  80  
  81          var result = key ? undefined : {};
  82  
  83          // To prevent the for loop in the first place assign an empty array
  84          // in case there are no cookies at all. Also prevents odd result when
  85          // calling $.cookie().
  86          var cookies = document.cookie ? document.cookie.split('; ') : [];
  87  
  88          for (var i = 0, l = cookies.length; i < l; i++) {
  89              var parts = cookies[i].split('=');
  90              var name = decode(parts.shift());
  91              var cookie = parts.join('=');
  92  
  93              if (key && key === name) {
  94                  // If second argument (value) is a function it's a converter...
  95                  result = read(cookie, value);
  96                  break;
  97              }
  98  
  99              // Prevent storing a cookie that we couldn't decode.
 100              if (!key && (cookie = read(cookie)) !== undefined) {
 101                  result[name] = cookie;
 102              }
 103          }
 104  
 105          return result;
 106      };
 107  
 108      config.defaults = {};
 109  
 110      $.removeCookie = function(key, options) {
 111          if ($.cookie(key) === undefined) {
 112              return false;
 113          }
 114  
 115          // Must not alter options, thus extending a fresh object...
 116          $.cookie(key, '', $.extend({}, options, {expires: -1}));
 117          return !$.cookie(key);
 118      };
 119  
 120  }));


Generated: Thu Nov 21 01:00:57 2024 Cross-referenced by PHPXref 0.7.1