searchHistoryItems.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. define(['util', 'jquery'], function(util, $) {
  2. /**
  3. * The maximum number of items kept in local history for a specific WH output.
  4. *
  5. * @type {number}
  6. */
  7. var HISTORY_ITEMS_MAX_COUNT = 30;
  8. /**
  9. * Test if a browser local storage is available.
  10. *
  11. * @returns {boolean} Returns true if local storage is available.
  12. */
  13. function localStorageAvailable() {
  14. try {
  15. var storage = window["localStorage"],
  16. x = '__storage_test__';
  17. storage.setItem(x, x);
  18. storage.removeItem(x);
  19. return true;
  20. }
  21. catch (e) {
  22. util.debug(e);
  23. return false;
  24. }
  25. }
  26. /**
  27. * @returns {string} Get the key to use for reading from local storage. It is unique for a WH output.
  28. */
  29. function getSearchHistoryKey() {
  30. var wh_root = $('meta[name=wh-path2root]').attr('content');
  31. if (wh_root == null || wh_root == undefined || wh_root.length == 0) {
  32. wh_root = "index.html";
  33. } else {
  34. wh_root += "index.html";
  35. }
  36. var wh_root_path = resolveRelativePath(wh_root);
  37. return wh_root_path + "_search_history_items";
  38. }
  39. /**
  40. * Resolve a relative path to the current browser location.
  41. *
  42. * @param relPath The relative path to resolve.
  43. * @returns {string} The resolved URL.
  44. */
  45. function resolveRelativePath(relPath) {
  46. var link = document.createElement("a");
  47. link.href = relPath;
  48. return (link.protocol + "//" + link.host + link.pathname + link.search + link.hash);
  49. }
  50. return {
  51. /**
  52. * Add a search query to the history.
  53. *
  54. * @param searchQuery The search query to add.
  55. */
  56. addSearchQueryToHistory: function (searchQuery) {
  57. util.debug("Add search query to history: ", searchQuery);
  58. if (localStorageAvailable()) {
  59. searchQuery = searchQuery.toLowerCase();
  60. var localStorageKey = getSearchHistoryKey();
  61. try {
  62. var localStorageItems = window.localStorage.getItem(localStorageKey);
  63. if (!localStorageItems) {
  64. // Local storage is empty for the current WH output.
  65. var hItemsArray = [];
  66. hItemsArray.push(searchQuery);
  67. var valToSave = JSON.stringify(hItemsArray);
  68. util.debug("Save to local storage: ", valToSave)
  69. window.localStorage.setItem(localStorageKey, valToSave);
  70. } else {
  71. // There are local storage items for current WH output
  72. var lastSearchItemsArray = JSON.parse(localStorageItems);
  73. var idx = lastSearchItemsArray.indexOf(searchQuery);
  74. if (idx != -1) {
  75. // Promote history item
  76. util.debug("Promote history item:", lastSearchItemsArray);
  77. lastSearchItemsArray.splice(idx, 1);
  78. }
  79. // Add first
  80. lastSearchItemsArray.unshift(searchQuery);
  81. // Ensure local history items do not exceed the MAX limit
  82. if (lastSearchItemsArray.length > HISTORY_ITEMS_MAX_COUNT) {
  83. lastSearchItemsArray.splice(HISTORY_ITEMS_MAX_COUNT);
  84. }
  85. // Save to local storage.
  86. var newVal = JSON.stringify(lastSearchItemsArray);
  87. window.localStorage.setItem(localStorageKey, newVal);
  88. }
  89. } catch (e) {
  90. util.debug("Exception when trying to save to local storage: ", e);
  91. window.localStorage.removeItem(localStorageKey);
  92. }
  93. } else {
  94. util.debug("Local storage is not available");
  95. }
  96. },
  97. /**
  98. * Get the search history items from local storage.
  99. *
  100. * @returns {Array} The array with search history items.
  101. */
  102. getHistorySearchItems: function () {
  103. var toRet = [];
  104. if (localStorageAvailable()) {
  105. var localStorageKey = getSearchHistoryKey();
  106. try {
  107. var lastLocalStorage = window.localStorage.getItem(localStorageKey);
  108. if (lastLocalStorage) {
  109. // Convert to array
  110. var lastSearchItemsArray = JSON.parse(lastLocalStorage);
  111. if (Array.isArray(lastSearchItemsArray)) {
  112. toRet = lastSearchItemsArray;
  113. }
  114. }
  115. } catch (e) {
  116. util.debug("Exception when reading from local storage: ", e);
  117. window.localStorage.removeItem(localStorageKey);
  118. }
  119. }
  120. return toRet;
  121. },
  122. /**
  123. * Remove from local storage a history item.
  124. *
  125. * @param historyItem The history item to remove.
  126. * @returns {boolean} True if item was removed.
  127. */
  128. removeSearchHistoryItem: function (historyItem) {
  129. var removed = false;
  130. if (localStorageAvailable()) {
  131. var localStorageKey = getSearchHistoryKey();
  132. try {
  133. var lastLocalStorage = window.localStorage.getItem(localStorageKey);
  134. if (lastLocalStorage) {
  135. // Convert to array
  136. var lastSearchItemsArray = JSON.parse(lastLocalStorage);
  137. if (Array.isArray(lastSearchItemsArray)) {
  138. historyItem = historyItem.toLowerCase();
  139. var idx = lastSearchItemsArray.indexOf(historyItem);
  140. if (idx != -1) {
  141. // Remove from local storage
  142. lastSearchItemsArray.splice(idx, 1);
  143. var newVal = JSON.stringify(lastSearchItemsArray);
  144. window.localStorage.setItem(localStorageKey, newVal);
  145. removed = true;
  146. }
  147. }
  148. }
  149. } catch (e) {
  150. util.debug("Exception when removing from local storage: ", e);
  151. window.localStorage.removeItem(localStorageKey);
  152. }
  153. }
  154. return removed;
  155. }
  156. }
  157. });