cookies.js 4.4 KB

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