No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

154 líneas
4.4 KiB

  1. /**
  2. * Copyright © Magento, Inc. All rights reserved.
  3. * See COPYING.txt for license details.
  4. */
  5. define([
  6. 'jquery',
  7. 'mage/mage',
  8. 'js-cookie/cookie-wrapper'
  9. ], function ($) {
  10. 'use strict';
  11. /**
  12. * Helper for cookies manipulation
  13. * @returns {CookieHelper}
  14. * @constructor
  15. */
  16. var CookieHelper = function () {
  17. /**
  18. * Cookie default values.
  19. * @type {Object}
  20. */
  21. this.defaults = {
  22. expires: null,
  23. path: '/',
  24. domain: null,
  25. secure: false,
  26. lifetime: null,
  27. samesite: 'lax'
  28. };
  29. /**
  30. * Calculate cookie expiration date based on its lifetime.
  31. * @param {Object} options - Cookie option values
  32. * @return {Date|null} Calculated cookie expiration date or null if no lifetime provided.
  33. * @private
  34. */
  35. function lifetimeToExpires(options, defaults) {
  36. var expires,
  37. lifetime;
  38. lifetime = options.lifetime || defaults.lifetime;
  39. if (lifetime && lifetime > 0) {
  40. expires = options.expires || new Date();
  41. return new Date(expires.getTime() + lifetime * 1000);
  42. }
  43. return null;
  44. }
  45. /**
  46. * Set a cookie's value by cookie name based on optional cookie options.
  47. * @param {String} name - The name of the cookie.
  48. * @param {String} value - The cookie's value.
  49. * @param {Object} options - Optional options (e.g. lifetime, expires, path, etc.)
  50. */
  51. this.set = function (name, value, options) {
  52. var expires,
  53. path,
  54. domain,
  55. secure,
  56. samesite;
  57. options = $.extend({}, this.defaults, options || {});
  58. expires = lifetimeToExpires(options, this.defaults) || options.expires;
  59. path = options.path;
  60. domain = options.domain;
  61. secure = options.secure;
  62. samesite = options.samesite;
  63. document.cookie = name + '=' + encodeURIComponent(value) +
  64. (expires ? '; expires=' + expires.toUTCString() : '') +
  65. (path ? '; path=' + path : '') +
  66. (domain ? '; domain=' + domain : '') +
  67. (secure ? '; secure' : '') +
  68. '; samesite=' + (samesite ? samesite : 'lax');
  69. };
  70. /**
  71. * Get a cookie's value by cookie name.
  72. * @param {String} name - The name of the cookie.
  73. * @return {(null|String)}
  74. */
  75. this.get = function (name) {
  76. var arg = name + '=',
  77. aLength = arg.length,
  78. cookie = document.cookie,
  79. cLength = cookie.length,
  80. i = 0,
  81. j = 0;
  82. while (i < cLength) {
  83. j = i + aLength;
  84. if (cookie.substring(i, j) === arg) {
  85. return this.getCookieVal(j);
  86. }
  87. i = cookie.indexOf(' ', i) + 1;
  88. if (i === 0) {
  89. break;
  90. }
  91. }
  92. return null;
  93. };
  94. /**
  95. * Clear a cookie's value by name.
  96. * @param {String} name - The name of the cookie being cleared.
  97. */
  98. this.clear = function (name) {
  99. if (this.get(name)) {
  100. this.set(name, '', {
  101. expires: new Date('Jan 01 1970 00:00:01 GMT')
  102. });
  103. }
  104. };
  105. /**
  106. * Return URI decoded cookie component value (e.g. expires, path, etc.) based on a
  107. * numeric offset in the document's cookie value.
  108. * @param {Number} offset - Offset into the document's cookie value.
  109. * @return {String}
  110. */
  111. this.getCookieVal = function (offset) {
  112. var cookie = document.cookie,
  113. endstr = cookie.indexOf(';', offset);
  114. if (endstr === -1) {
  115. endstr = cookie.length;
  116. }
  117. return decodeURIComponent(cookie.substring(offset, endstr));
  118. };
  119. return this;
  120. };
  121. $.extend(true, $, {
  122. mage: {
  123. cookies: new CookieHelper()
  124. }
  125. });
  126. return function (pageOptions) {
  127. $.extend($.mage.cookies.defaults, pageOptions);
  128. $.extend($.cookie.defaults, $.mage.cookies.defaults);
  129. };
  130. });