search.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. * Handles finding a text string anywhere in the slides and showing the next occurrence to the user
  3. * by navigatating to that slide and highlighting it.
  4. *
  5. * By Jon Snyder <snyder.jon@gmail.com>, February 2013
  6. */
  7. var RevealSearch = (function() {
  8. var matchedSlides;
  9. var currentMatchedIndex;
  10. var searchboxDirty;
  11. var myHilitor;
  12. // Original JavaScript code by Chirp Internet: www.chirp.com.au
  13. // Please acknowledge use of this code by including this header.
  14. // 2/2013 jon: modified regex to display any match, not restricted to word boundaries.
  15. function Hilitor(id, tag)
  16. {
  17. var targetNode = document.getElementById(id) || document.body;
  18. var hiliteTag = tag || "EM";
  19. var skipTags = new RegExp("^(?:" + hiliteTag + "|SCRIPT|FORM|SPAN)$");
  20. var colors = ["#ff6", "#a0ffff", "#9f9", "#f99", "#f6f"];
  21. var wordColor = [];
  22. var colorIdx = 0;
  23. var matchRegex = "";
  24. var matchingSlides = [];
  25. this.setRegex = function(input)
  26. {
  27. input = input.replace(/^[^\w]+|[^\w]+$/g, "").replace(/[^\w'-]+/g, "|");
  28. matchRegex = new RegExp("(" + input + ")","i");
  29. }
  30. this.getRegex = function()
  31. {
  32. return matchRegex.toString().replace(/^\/\\b\(|\)\\b\/i$/g, "").replace(/\|/g, " ");
  33. }
  34. // recursively apply word highlighting
  35. this.hiliteWords = function(node)
  36. {
  37. if(node == undefined || !node) return;
  38. if(!matchRegex) return;
  39. if(skipTags.test(node.nodeName)) return;
  40. if(node.hasChildNodes()) {
  41. for(var i=0; i < node.childNodes.length; i++)
  42. this.hiliteWords(node.childNodes[i]);
  43. }
  44. if(node.nodeType == 3) { // NODE_TEXT
  45. if((nv = node.nodeValue) && (regs = matchRegex.exec(nv))) {
  46. //find the slide's section element and save it in our list of matching slides
  47. var secnode = node.parentNode;
  48. while (secnode.nodeName != 'SECTION') {
  49. secnode = secnode.parentNode;
  50. }
  51. var slideIndex = Reveal.getIndices(secnode);
  52. var slidelen = matchingSlides.length;
  53. var alreadyAdded = false;
  54. for (var i=0; i < slidelen; i++) {
  55. if ( (matchingSlides[i].h === slideIndex.h) && (matchingSlides[i].v === slideIndex.v) ) {
  56. alreadyAdded = true;
  57. }
  58. }
  59. if (! alreadyAdded) {
  60. matchingSlides.push(slideIndex);
  61. }
  62. if(!wordColor[regs[0].toLowerCase()]) {
  63. wordColor[regs[0].toLowerCase()] = colors[colorIdx++ % colors.length];
  64. }
  65. var match = document.createElement(hiliteTag);
  66. match.appendChild(document.createTextNode(regs[0]));
  67. match.style.backgroundColor = wordColor[regs[0].toLowerCase()];
  68. match.style.fontStyle = "inherit";
  69. match.style.color = "#000";
  70. var after = node.splitText(regs.index);
  71. after.nodeValue = after.nodeValue.substring(regs[0].length);
  72. node.parentNode.insertBefore(match, after);
  73. }
  74. }
  75. };
  76. // remove highlighting
  77. this.remove = function()
  78. {
  79. var arr = document.getElementsByTagName(hiliteTag);
  80. while(arr.length && (el = arr[0])) {
  81. el.parentNode.replaceChild(el.firstChild, el);
  82. }
  83. };
  84. // start highlighting at target node
  85. this.apply = function(input)
  86. {
  87. if(input == undefined || !input) return;
  88. this.remove();
  89. this.setRegex(input);
  90. this.hiliteWords(targetNode);
  91. return matchingSlides;
  92. };
  93. }
  94. function openSearch() {
  95. //ensure the search term input dialog is visible and has focus:
  96. var inputbox = document.getElementById("searchinput");
  97. inputbox.style.display = "inline";
  98. inputbox.focus();
  99. inputbox.select();
  100. }
  101. function toggleSearch() {
  102. var inputbox = document.getElementById("searchinput");
  103. if (inputbox.style.display !== "inline") {
  104. openSearch();
  105. }
  106. else {
  107. inputbox.style.display = "none";
  108. myHilitor.remove();
  109. }
  110. }
  111. function doSearch() {
  112. //if there's been a change in the search term, perform a new search:
  113. if (searchboxDirty) {
  114. var searchstring = document.getElementById("searchinput").value;
  115. //find the keyword amongst the slides
  116. myHilitor = new Hilitor("slidecontent");
  117. matchedSlides = myHilitor.apply(searchstring);
  118. currentMatchedIndex = 0;
  119. }
  120. //navigate to the next slide that has the keyword, wrapping to the first if necessary
  121. if (matchedSlides.length && (matchedSlides.length <= currentMatchedIndex)) {
  122. currentMatchedIndex = 0;
  123. }
  124. if (matchedSlides.length > currentMatchedIndex) {
  125. Reveal.slide(matchedSlides[currentMatchedIndex].h, matchedSlides[currentMatchedIndex].v);
  126. currentMatchedIndex++;
  127. }
  128. }
  129. var dom = {};
  130. dom.wrapper = document.querySelector( '.reveal' );
  131. if( !dom.wrapper.querySelector( '.searchbox' ) ) {
  132. var searchElement = document.createElement( 'div' );
  133. searchElement.id = "searchinputdiv";
  134. searchElement.classList.add( 'searchdiv' );
  135. searchElement.style.position = 'absolute';
  136. searchElement.style.top = '10px';
  137. searchElement.style.left = '10px';
  138. //embedded base64 search icon Designed by Sketchdock - http://www.sketchdock.com/:
  139. searchElement.innerHTML = '<span><input type="search" id="searchinput" class="searchinput" style="vertical-align: top;"/><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAYAAADgdz34AAAABGdBTUEAAK/INwWK6QAAABl0RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAAJiSURBVHjatFZNaxNBGH5md+Mmu92NVdKDRipSAyqCghgQD4L4cRe86UUtAQ+eFCxoa4/25EXBFi8eBE+eRPoDhB6KgiiixdAPCEkx2pjvTXadd9yNsflwuyUDD/O+u8PzzDPvzOwyx3EwyCZhwG3gAkp7MnpjgbopjsltcD4gjuXZZKeAR348MYLYTm3LzOs/y3j3JTfZxgXWXmTuwPHIc4VmoOmv5IrI53+AO2DdHLjkDWQ3GoEEVFXtXQOvkSnPWcyUceviLhwbDYv8/XIVj97kse7TodLvZXxYxrPUHkQ1ufXs3FEdybEIxucySOesoNvUgWU1cP3MkCBfTFdw9fGaAMVmRELq7LBw2Q3/FaAxxWIRpw+ZIr/7IouPqzUBiqmdHAv7EuhRAwf1er2Vy4x1jW3b2d5Jfvu5IPp7l2LYbcgCFFNb+FoJ7oBqEAqFMPNqFcmEgVMJDfMT+1tvN0pNjERlMS6QA5pFOKxiKVPFhakPeL3It+WGJUDxt2wFR+JhzI7v5ctkd8DXOZAkCYYxhO+lKm4+Xfqz/rIixBuNBl7eOYzkQQNzqX249mRl6zUgEcYkaJrGhUwBinVdh6IouPzwE6/DL5w4oLkH8y981aDf+uq6hlKpJESiUdNfDZi7/ehG9K6KfiA3pml0PLcsq+cSMTj2NL9ukc4UOmz7AZ3+crkC4mHujFvXNaMFB3bEr8xPS6p5O+jXxq4VZtaen7/PwzrntjcLUE0iHPS1Ud1cdiEJl/8WivZk0wXd7zWOMkeF8s0CcAmkNrC2nvXZDbbbN73ccYnZoH9bfgswAFzAe9/h3dbKAAAAAElFTkSuQmCC" id="searchbutton" class="searchicon" style="vertical-align: top; margin-top: -1px;"/></span>';
  140. dom.wrapper.appendChild( searchElement );
  141. }
  142. document.getElementById("searchbutton").addEventListener( 'click', function(event) {
  143. doSearch();
  144. }, false );
  145. document.getElementById("searchinput").addEventListener( 'keyup', function( event ) {
  146. switch (event.keyCode) {
  147. case 13:
  148. event.preventDefault();
  149. doSearch();
  150. searchboxDirty = false;
  151. break;
  152. default:
  153. searchboxDirty = true;
  154. }
  155. }, false );
  156. // Open the search when the 's' key is hit (yes, this conflicts with the notes plugin, disabling for now)
  157. /*
  158. document.addEventListener( 'keydown', function( event ) {
  159. // Disregard the event if the target is editable or a
  160. // modifier is present
  161. if ( document.querySelector( ':focus' ) !== null || event.shiftKey || event.altKey || event.ctrlKey || event.metaKey ) return;
  162. if( event.keyCode === 83 ) {
  163. event.preventDefault();
  164. openSearch();
  165. }
  166. }, false );
  167. */
  168. return { open: openSearch };
  169. })();