help.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
  2. /*
  3. * This file is part of the LibreOffice project.
  4. *
  5. * This Source Code Form is subject to the terms of the Mozilla Public
  6. * License, v. 2.0. If a copy of the MPL was not distributed with this
  7. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  8. */
  9. // Pagination and fuzzy search
  10. var url = window.location.pathname;
  11. var moduleRegex = new RegExp('text\\/(\\w+)\\/');
  12. var regexArray = moduleRegex.exec(url);
  13. var modules = ['CALC', 'WRITER', 'IMPRESS', 'DRAW', 'BASE', 'MATH', 'CHART', 'BASIC', 'SHARED'];
  14. var indexEl = document.getElementsByClassName("index")[0];
  15. var fullLinks = fullLinkify(indexEl, bookmarks, modules, currentModule());
  16. var search = document.getElementById('search-bar');
  17. search.addEventListener('keyup', debounce(filter, 100, indexEl));
  18. // Preserve search input value during the session
  19. search.value = sessionStorage.getItem('searchsave');
  20. if (search.value !== undefined) {
  21. filter(indexEl);
  22. }
  23. window.addEventListener('unload', function(event) {
  24. sessionStorage.setItem('searchsave', search.value);
  25. });
  26. // render the unfiltered index list on page load
  27. fillIndex(indexEl, fullLinks, modules);
  28. function currentModule() {
  29. var module = '';
  30. // get the module name from the URL and remove the first character,
  31. // but first deal with snowflake Base
  32. if(url.indexOf('explorer/database/') !== -1) {
  33. module = 'BASE';
  34. } else {
  35. if (null === regexArray){// comes from search or elsewhere, no defined module in URL
  36. module = 'HARED'
  37. } else {
  38. module = regexArray[1].toUpperCase().substring(1);
  39. }
  40. }
  41. return module;
  42. };
  43. function fullLinkify(indexEl, bookmarks, modules, currentModule) {
  44. var fullLinkified = '';
  45. // if user is not on a shared category page, limit the index to the current module + shared
  46. if(currentModule !== 'HARED') {
  47. bookmarks = bookmarks.filter(function(obj) {
  48. return obj['app'] === currentModule || obj['app'] === 'SHARED';
  49. });
  50. }
  51. bookmarks.forEach(function(obj) {
  52. fullLinkified += '<a href="' + obj['url'] + '" class="' + obj['app'] + '">' + obj['text'] + '</a>';
  53. });
  54. return fullLinkified;
  55. }
  56. function fillIndex(indexEl, content, modules) {
  57. indexEl.innerHTML = content;
  58. var indexKids = indexEl.children;
  59. for (var i = 0, len = indexKids.length; i < len; i++) {
  60. indexKids[i].removeAttribute("id");
  61. }
  62. modules.forEach(function(module) {
  63. var moduleHeader = indexEl.getElementsByClassName(module)[0];
  64. if (typeof moduleHeader !== 'undefined') {
  65. // let's wrap the header in a span, so the ::before element will not become a link
  66. moduleHeader.outerHTML = '<span id="' + module + '" class="' + module + '">' + moduleHeader.outerHTML + '</span>';
  67. }
  68. });
  69. Paginator(indexEl);
  70. }
  71. // filter the index list based on search field input
  72. function filter(indexList) {
  73. var results = null;
  74. var target = search.value.trim();
  75. var filtered = '';
  76. if (target.length < 1) {
  77. fillIndex(indexEl, fullLinks, modules);
  78. return;
  79. }
  80. results = fuzzysort.go(target, bookmarks, {threshold: -15000, key:'text'});
  81. results.forEach(function(result) {
  82. filtered += '<a href="' + result.obj['url'] + '" class="' + result.obj['app'] + '">' + fuzzysort.highlight(result) + '</a>';
  83. });
  84. fillIndex(indexList, filtered, modules);
  85. };
  86. // delay the rendering of the filtered results while user is typing
  87. function debounce(fn, wait, indexList) {
  88. var timeout;
  89. return function() {
  90. clearTimeout(timeout);
  91. timeout = setTimeout(function() {
  92. fn.call(this, indexList);
  93. }, (wait || 150));
  94. };
  95. }
  96. // copy pycode and bascode to clipboard on mouse click
  97. // Show border when copy is done
  98. divcopyable(document.getElementsByClassName("bascode"));
  99. divcopyable(document.getElementsByClassName("pycode"));
  100. function divcopyable(itemcopyable){
  101. for (var i = 0, len = itemcopyable.length; i < len; i++) {
  102. (function() {
  103. var item = itemcopyable[i];
  104. function changeBorder(item, color) {
  105. var saveBorder = item.style.border;
  106. item.style.borderColor = color;
  107. setTimeout(function() {
  108. item.style.border = saveBorder;
  109. }, 150);
  110. }
  111. item.onclick = function() {
  112. document.execCommand("copy");
  113. changeBorder(item, "#18A303");
  114. };
  115. item.addEventListener("copy", function(event) {
  116. event.preventDefault();
  117. if (event.clipboardData) {
  118. event.clipboardData.setData("text/plain", item.textContent);
  119. }
  120. });
  121. }());
  122. }
  123. }
  124. // copy useful content to clipboard on mouse click
  125. var copyable = document.getElementsByClassName("input");
  126. for (var i = 0, len = copyable.length; i < len; i++) {
  127. (function() {
  128. var item = copyable[i];
  129. function changeColor(item, color, colorToChangeBackTo) {
  130. item.style.backgroundColor = color;
  131. setTimeout(function() {
  132. item.style.backgroundColor = colorToChangeBackTo;
  133. }, 150);
  134. }
  135. item.onclick = function() {
  136. document.execCommand("copy");
  137. changeColor(item, "#18A303", "transparent");
  138. };
  139. item.addEventListener("copy", function(event) {
  140. event.preventDefault();
  141. if (event.clipboardData) {
  142. event.clipboardData.setData("text/plain", item.textContent);
  143. }
  144. });
  145. }());
  146. }
  147. // auto-expand contents per subitem
  148. var pathname = window.location.pathname;
  149. var pathRegex = /text\/.*\.html$/;
  150. var linkIndex = 0;
  151. var contentMatch = pathname.match(pathRegex);
  152. function linksMatch(content) {
  153. var linkMatch = new RegExp(content);
  154. var links = document.getElementById("Contents").getElementsByTagName("a");
  155. for (var i = 0, len = links.length; i < len; i++) {
  156. if (links[i].href.match(linkMatch)) {
  157. return i;
  158. }
  159. }
  160. }
  161. linkIndex = linksMatch(contentMatch);
  162. if (typeof linkIndex !== "undefined") {
  163. var current = document.getElementById("Contents").getElementsByTagName("a")[linkIndex];
  164. var cItem = current.parentElement;
  165. var parents = [];
  166. while (cItem.parentElement && !cItem.parentElement.matches("#Contents") && parents.indexOf(cItem.parentElement) == -1) {
  167. parents.push(cItem = cItem.parentElement);
  168. }
  169. var liParents = [].filter.call(parents, function(item) {
  170. return item.matches("li");
  171. });
  172. for (var i = 0, len = liParents.length; i < len; i++) {
  173. var input = liParents[i].querySelectorAll(':scope > input');
  174. document.getElementById(input[0].id).checked = true;
  175. }
  176. current.classList.add('contents-current');
  177. }
  178. /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */