jquery.cookie.shim.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /**
  2. * DO NOT EDIT THIS FILE.
  3. * See the following change record for more information,
  4. * https://www.drupal.org/node/2815083
  5. * @preserve
  6. **/
  7. (function ($, Drupal, cookies) {
  8. var isFunction = function isFunction(obj) {
  9. return Object.prototype.toString.call(obj) === '[object Function]';
  10. };
  11. var parseCookieValue = function parseCookieValue(value, parseJson) {
  12. if (value.indexOf('"') === 0) {
  13. value = value.slice(1, -1).replace(/\\"/g, '"').replace(/\\\\/g, '\\');
  14. }
  15. try {
  16. value = decodeURIComponent(value.replace(/\+/g, ' '));
  17. return parseJson ? JSON.parse(value) : value;
  18. } catch (e) {}
  19. };
  20. var reader = function reader(cookieValue, cookieName, converter, readUnsanitized, parseJson) {
  21. var value = readUnsanitized ? cookieValue : parseCookieValue(cookieValue, parseJson);
  22. if (converter !== undefined && isFunction(converter)) {
  23. return converter(value, cookieName);
  24. }
  25. return value;
  26. };
  27. $.cookie = function (key) {
  28. var value = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : undefined;
  29. var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : undefined;
  30. key = key && !$.cookie.raw ? encodeURIComponent(key) : key;
  31. if (value !== undefined && !isFunction(value)) {
  32. var attributes = Object.assign({}, $.cookie.defaults, options);
  33. if (typeof attributes.expires === 'string' && attributes.expires !== '') {
  34. attributes.expires = new Date(attributes.expires);
  35. }
  36. var cookieSetter = cookies.withConverter({
  37. write: function write(cookieValue) {
  38. return encodeURIComponent(cookieValue);
  39. }
  40. });
  41. value = $.cookie.json && !$.cookie.raw ? JSON.stringify(value) : String(value);
  42. return cookieSetter.set(key, value, attributes);
  43. }
  44. var userProvidedConverter = value;
  45. var cookiesShim = cookies.withConverter({
  46. read: function read(cookieValue, cookieName) {
  47. return reader(cookieValue, cookieName, userProvidedConverter, $.cookie.raw, $.cookie.json);
  48. }
  49. });
  50. if (key !== undefined) {
  51. return cookiesShim.get(key);
  52. }
  53. var results = cookiesShim.get();
  54. Object.keys(results).forEach(function (resultKey) {
  55. if (results[resultKey] === undefined) {
  56. delete results[resultKey];
  57. }
  58. });
  59. return results;
  60. };
  61. $.cookie.defaults = Object.assign({ path: '' }, cookies.defaults);
  62. $.cookie.json = false;
  63. $.cookie.raw = false;
  64. $.removeCookie = function (key, options) {
  65. cookies.remove(key, Object.assign({}, $.cookie.defaults, options));
  66. return !cookies.get(key);
  67. };
  68. })(jQuery, Drupal, window.Cookies);