Advertisement
Guest User

Untitled

a guest
Feb 10th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.56 KB | None | 0 0
  1. if (!Array.prototype.indexOf) {
  2. Array.prototype.indexOf = function(elt) {
  3. var len = this.length >>> 0;
  4. var from = Number(arguments[1]) || 0;
  5. from = (from < 0) ? Math.ceil(from) : Math.floor(from);
  6. if (from < 0) from += len;
  7. for (; from < len; from++) {
  8. if (from in this && this[from] === elt) return from;
  9. }
  10. return -1;
  11. };
  12. }
  13.  
  14. function CookieIDsHistory(name, options) {
  15. if ((typeof name == 'undefined') || (name.length < 1)) {
  16. throw 'Please set cookie name.';
  17. }
  18. options = options || {};
  19. this._cookieName = encodeURIComponent(name);
  20. this._expiresDays = options.days || 0;
  21. this._itemsLimit = options.limit || 15;
  22. this._uniqueValues = !!options.unique;
  23. this._setCookie = function(name, value, days) {
  24. var expires, host;
  25. if (days) {
  26. var date = new Date();
  27. date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
  28. expires = '; expires=' + date.toGMTString();
  29. } else {
  30. expires = '';
  31. }
  32. host = location.hostname.replace(/^www\./i, '');
  33. document.cookie = name + '=' + value + expires + '; path=/' + '; domain=.' + host;
  34. };
  35. this._getCookie = function(name) {
  36. var cookieName = name + '=';
  37. var cookieArray = document.cookie.split(';');
  38. for (var i = 0; i < cookieArray.length; i++) {
  39. var cookieItem = cookieArray[i];
  40. while (cookieItem.charAt(0) == ' ') cookieItem = cookieItem.substring(1, cookieItem.length);
  41. if (cookieItem.indexOf(cookieName) == 0) return cookieItem.substring(cookieName.length, cookieItem.length);
  42. }
  43. return '';
  44. };
  45. this._delCookie = function(name) {
  46. this._setCookie(name, '', -1);
  47. };
  48. this._getCookieData = function() {
  49. var items;
  50. if (!Array.isArray) {
  51. Array.isArray = function(arg) {
  52. return Object.prototype.toString.call(arg) === '[object Array]';
  53. };
  54. }
  55. try {
  56. items = JSON.parse(this._getCookie(this._cookieName)) || [];
  57. } catch (err) {
  58. items = [];
  59. }
  60. if (!Array.isArray(items)) {
  61. items = [];
  62. }
  63. return items;
  64. };
  65. this._setCookieData = function(items) {
  66. this._setCookie(this._cookieName, JSON.stringify(items), this._expiresDays);
  67. };
  68. this.add = function(id) {
  69. var items = this._getCookieData();
  70. if (items.length > this._itemsLimit) {
  71. items.shift();
  72. }
  73. if (this._uniqueValues) {
  74. while (items.indexOf(id) > -1) {
  75. items.splice(items.indexOf(id), 1);
  76. }
  77. }
  78. items.push(id);
  79. this._setCookieData(items);
  80. return this;
  81. };
  82. this.remove = function(id) {
  83. var items = this._getCookieData();
  84. while (items.indexOf(id) > -1) {
  85. items.splice(items.indexOf(id), 1);
  86. }
  87. this._setCookieData(items);
  88. return this;
  89. };
  90. this.removeOne = function(id) {
  91. var items = this._getCookieData();
  92. if (items.indexOf(id) != -1) {
  93. items.splice(items.indexOf(id), 1);
  94. }
  95. this._setCookieData(items);
  96. return this;
  97. };
  98. this.count = function(id) {
  99. var items = this._getCookieData(),
  100. counter = 0;
  101. for (var i = 0; i < items.length; i++) {
  102. if (items[i] == id) {
  103. counter++;
  104. }
  105. }
  106. return counter;
  107. };
  108. this.length = function() {
  109. var items = this._getCookieData();
  110. return items.length;
  111. };
  112. this.is = function(id) {
  113. return !!this.count(id);
  114. };
  115. this.unset = function () {
  116. this._delCookie(this._cookieName);
  117. return this;
  118. };
  119. }
  120.  
  121.  
  122. /* Usage example: */
  123.  
  124. var ch = new CookieIDsHistory('_test', { days: 5, limit: 50, unique: true });
  125.  
  126. // days - How many days cookie will live
  127. // limit - How many IDs will be collected before cutting of old IDs will start
  128. // unique - Can IDs be repeated in the array or not
  129.  
  130. // Adding IDs to collect.
  131. ch.add(103);
  132. ch.add(345);
  133. ch.add(101);
  134. ch.add(235);
  135. ch.add(101);
  136. ch.add(867);
  137. ch.add(124);
  138.  
  139. // or simply
  140. ch.add(103).add(345).add(101).add(235).add(101).add(867).add(124);
  141.  
  142. // Check ID existing.
  143. ch.is(101);
  144.  
  145. // Count IDs in collection. Note: it makes sense with unique parameter in False state.
  146. ch.count(101);
  147.  
  148. // Remove only first ID in collection. Note: it makes sense with unique parameter in False state.
  149. ch.removeOne(101);
  150.  
  151. // Remove all specified IDs in collection.
  152. ch.remove(101);
  153.  
  154. // Remove cookie.
  155. ch.unset();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement