angular-cookie.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Copyright 2013 Ivan Pusic
  3. * Contributors:
  4. * Matjaz Lipus
  5. */
  6. angular.module('ivpusic.cookie', ['ipCookie']);
  7. angular.module('ipCookie', ['ng']).
  8. factory('ipCookie', ['$document',
  9. function ($document) {
  10. 'use strict';
  11. function tryDecodeURIComponent(value) {
  12. try {
  13. return decodeURIComponent(value);
  14. } catch(e) {
  15. // Ignore any invalid uri component
  16. }
  17. }
  18. return (function () {
  19. function cookieFun(key, value, options) {
  20. var cookies,
  21. list,
  22. i,
  23. cookie,
  24. pos,
  25. name,
  26. hasCookies,
  27. all,
  28. expiresFor;
  29. options = options || {};
  30. var dec = options.decode || tryDecodeURIComponent;
  31. var enc = options.encode || encodeURIComponent;
  32. if (value !== undefined) {
  33. // we are setting value
  34. value = typeof value === 'object' ? JSON.stringify(value) : String(value);
  35. if (typeof options.expires === 'number') {
  36. expiresFor = options.expires;
  37. options.expires = new Date();
  38. // Trying to delete a cookie; set a date far in the past
  39. if (expiresFor === -1) {
  40. options.expires = new Date('Thu, 01 Jan 1970 00:00:00 GMT');
  41. // A new
  42. } else if (options.expirationUnit !== undefined) {
  43. if (options.expirationUnit === 'hours') {
  44. options.expires.setHours(options.expires.getHours() + expiresFor);
  45. } else if (options.expirationUnit === 'minutes') {
  46. options.expires.setMinutes(options.expires.getMinutes() + expiresFor);
  47. } else if (options.expirationUnit === 'seconds') {
  48. options.expires.setSeconds(options.expires.getSeconds() + expiresFor);
  49. } else if (options.expirationUnit === 'milliseconds') {
  50. options.expires.setMilliseconds(options.expires.getMilliseconds() + expiresFor);
  51. } else {
  52. options.expires.setDate(options.expires.getDate() + expiresFor);
  53. }
  54. } else {
  55. options.expires.setDate(options.expires.getDate() + expiresFor);
  56. }
  57. }
  58. return ($document[0].cookie = [
  59. enc(key),
  60. '=',
  61. enc(value),
  62. options.expires ? '; expires=' + options.expires.toUTCString() : '',
  63. options.path ? '; path=' + options.path : '',
  64. options.domain ? '; domain=' + options.domain : '',
  65. options.secure ? '; secure' : ''
  66. ].join(''));
  67. }
  68. list = [];
  69. all = $document[0].cookie;
  70. if (all) {
  71. list = all.split('; ');
  72. }
  73. cookies = {};
  74. hasCookies = false;
  75. for (i = 0; i < list.length; ++i) {
  76. if (list[i]) {
  77. cookie = list[i];
  78. pos = cookie.indexOf('=');
  79. name = cookie.substring(0, pos);
  80. value = dec(cookie.substring(pos + 1));
  81. if(angular.isUndefined(value))
  82. continue;
  83. if (key === undefined || key === name) {
  84. try {
  85. cookies[name] = JSON.parse(value);
  86. } catch (e) {
  87. cookies[name] = value;
  88. }
  89. if (key === name) {
  90. return cookies[name];
  91. }
  92. hasCookies = true;
  93. }
  94. }
  95. }
  96. if (hasCookies && key === undefined) {
  97. return cookies;
  98. }
  99. }
  100. cookieFun.remove = function (key, options) {
  101. var hasCookie = cookieFun(key) !== undefined;
  102. if (hasCookie) {
  103. if (!options) {
  104. options = {};
  105. }
  106. options.expires = -1;
  107. cookieFun(key, '', options);
  108. }
  109. return hasCookie;
  110. };
  111. return cookieFun;
  112. }());
  113. }
  114. ]);