angular-cookie.js 3.4 KB

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