PageRenderTime 52ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/data/indexscript.js

https://github.com/TrinityCore/trinitycore.github.com
JavaScript | 242 lines | 185 code | 28 blank | 29 comment | 45 complexity | c335f8bf992b00938e93554dceac4492 MD5 | raw file
  1. /****************************************************************************
  2. * Copyright (C) 2012-2016 Woboq GmbH
  3. * Olivier Goffart <contact at woboq.com>
  4. * http://woboq.com/codebrowser.html
  5. *
  6. * This file is part of the Woboq Code Browser.
  7. *
  8. * Commercial License Usage:
  9. * Licensees holding valid commercial licenses provided by Woboq may use
  10. * this file in accordance with the terms contained in a written agreement
  11. * between the licensee and Woboq.
  12. * For further information see http://woboq.com/codebrowser.html
  13. *
  14. * Alternatively, this work may be used under a Creative Commons
  15. * Attribution-NonCommercial-ShareAlike 3.0 (CC-BY-NC-SA 3.0) License.
  16. * http://creativecommons.org/licenses/by-nc-sa/3.0/deed.en_US
  17. * This license does not allow you to use the code browser to assist the
  18. * development of your commercial software. If you intent to do so, consider
  19. * purchasing a commercial licence.
  20. ****************************************************************************/
  21. $(function() {
  22. // remove trailing slash
  23. root_path = root_path.replace(/\/$/, "");
  24. if(!root_path) root_path = ".";
  25. //compute the length of the common prefix between two strings
  26. // (copied from codebrowser.js)
  27. var prefixLen = function( s1 , s2) {
  28. var maxMatchLen = Math.min(s1.length, s2.length);
  29. var res = -1;
  30. while (++res < maxMatchLen) {
  31. if (s1.charAt(res) != s2.charAt(res))
  32. break;
  33. }
  34. return res * 256 + 256 - s1.length;
  35. }
  36. // Google text search (different than codebrowser.js)
  37. var text_search = function(text) {
  38. var location = "" + (window.location);
  39. window.location = "http://google.com/search?sitesearch=" + encodeURIComponent(location) + "&q=" + encodeURIComponent(text);
  40. }
  41. var fileIndex = [];
  42. var searchTerms = {}
  43. var functionDict = {};
  44. var file = path;
  45. var searchline = $("input#searchline");
  46. //BEGIN copied from codebrowser.js
  47. // callback for jqueryui's autocomple activate
  48. var activate = function(event,ui) {
  49. var val = ui.item.value;
  50. var type = searchTerms[val] && searchTerms[val].type;
  51. if (type == "file") {
  52. window.location = root_path + '/' + searchTerms[val].file + ".html";
  53. } else if (type == "ref") {
  54. var ref = searchTerms[val].ref;
  55. var url = root_path + "/refs/" + ref;
  56. $.get(url, function(data) {
  57. var res = $("<data>"+data+"</data>");
  58. var def = res.find("def");
  59. var result = { len: -1 };
  60. def.each( function() {
  61. var cur = { len : -1,
  62. f : $(this).attr("f"),
  63. l : $(this).attr("l") }
  64. cur.len = prefixLen(cur.f, file)
  65. if (cur.len >= result.len) {
  66. result = cur;
  67. result.isMarcro = ($(this).attr("macro"));
  68. }
  69. });
  70. if (result.len >= 0) {
  71. var newloc = root_path + "/" + result.f + ".html#" +
  72. (result.isMarcro ? result.l : ref );
  73. window.location = newloc;
  74. }
  75. });
  76. } else {
  77. text_search(val);
  78. }
  79. };
  80. var getFnNameKey = function (request) {
  81. if (request.indexOf('/') != -1 || request.indexOf('.') != -1)
  82. return false;
  83. var mx = request.match(/::([^:]{2})[^:]*$/);
  84. if (mx)
  85. return mx[1].toLowerCase().replace(/[^a-z]/, '_');
  86. request = request.replace(/^:*/, "");
  87. if (request.length < 2)
  88. return false;
  89. var k = request.substr(0, 2).toLowerCase();
  90. return k.replace(/[^a-z]/, '_')
  91. }
  92. var autocomplete = function(request, response) {
  93. var term = $.ui.autocomplete.escapeRegex(request.term);
  94. var rx1 = new RegExp(term, 'i');
  95. var rx2 = new RegExp("(^|::)"+term.replace(/^:*/, ''), 'i');
  96. var functionList = [];
  97. var k = getFnNameKey(request.term)
  98. if (k && Object.prototype.hasOwnProperty.call(functionDict,k)) {
  99. functionList = functionDict[k].filter(
  100. function(word) { return word.match(rx2) });
  101. }
  102. var l = fileIndex.filter( function(word) { return word.match(rx1); });
  103. l = l.concat(functionList);
  104. l = l.slice(0,1000); // too big lists are too slow
  105. response(l);
  106. };
  107. searchline.autocomplete( {source: autocomplete, select: activate, minLength: 4 } );
  108. searchline.keypress(function(e) {
  109. var value = searchline.val();
  110. if(e.which == 13) {
  111. activate({}, { item: { value: value } });
  112. }
  113. });
  114. // When the content changes, fetch the list of function that starts with ...
  115. searchline.on('input', function() {
  116. var value = $(this).val();
  117. var k = getFnNameKey(value);
  118. if (k && !Object.prototype.hasOwnProperty.call(functionDict, k)) {
  119. functionDict[k] = []
  120. $.get(root_path + '/fnSearch/' + k, function(data) {
  121. var list = data.split("\n");
  122. for (var i = 0; i < list.length; ++i) {
  123. var sep = list[i].indexOf('|');
  124. var ref = list[i].slice(0, sep);
  125. var name = list[i].slice(sep+1);
  126. searchTerms[name] = { type:"ref", ref: ref };
  127. functionDict[k].push(name);
  128. }
  129. if (searchline.is(":focus")) {
  130. searchline.autocomplete("search", searchline.val());
  131. }
  132. });
  133. }
  134. });
  135. // Pasting should show the autocompletion
  136. searchline.on("paste", function() { setTimeout(function() {
  137. searchline.autocomplete("search", searchline.val());
  138. }, 0);
  139. });
  140. //END copied from codebrowser.js
  141. $.get(root_path + '/fileIndex', function(data) {
  142. var list = data.split("\n");
  143. list.sort();
  144. fileIndex = list;
  145. for (var i = 0; i < list.length; ++i) {
  146. searchTerms[list[i]] = { type:"file", file: list[i] };
  147. }
  148. function openFolder() {
  149. var t = $(this);
  150. var state = {};
  151. if (history)
  152. state = history.state || state;
  153. if (!this._opened) {
  154. this._opened = true;
  155. var p = t.attr("data-path") + "/";
  156. var subPath = path=="" ? p : p.substr(path.length);
  157. t.text("[-]");
  158. var content = $("<table/>");
  159. var dict = {};
  160. var toOpenNow = [];
  161. for (var i=0; i < fileIndex.length; ++i) {
  162. var f = fileIndex[i];
  163. if (f.indexOf(p) == 0) {
  164. var sl = f.indexOf('/', p.length + 1);
  165. if (sl !== -1) {
  166. var name = f.substr( p.length, sl - p.length);
  167. if (dict[name])
  168. continue;
  169. dict[name] = true;
  170. content.append("<tr><td class='folder'><a class='opener' data-path='" + p + name + "' href='"+subPath + name+"'>[+]</a> " +
  171. "<a href='" + subPath + name + "/'>" + name + "/</a></td></tr>\n");
  172. if (state[p+name])
  173. toOpenNow.push(p+name);
  174. } else {
  175. var name = f.substr(p.length);
  176. content.append("<tr><td class='file'> <a href='" + subPath + name + ".html'>" + name + "</a></td></tr>\n");
  177. }
  178. }
  179. }
  180. content.find(".opener").click(openFolder);
  181. t.parent().append(content);
  182. state[t.attr("data-path")]=true;
  183. toOpenNow.forEach(function(toOpen) {
  184. var e = $("a[data-path='"+toOpen+"']").get(0)
  185. if (e)
  186. openFolder.call(e);
  187. });
  188. } else {
  189. t.parent().find("> table").empty();
  190. t.text("[+]");
  191. this._opened = false;
  192. state[t.attr("data-path")]=false;
  193. }
  194. if (history && history.replaceState)
  195. history.replaceState(state, undefined);
  196. return false;
  197. }
  198. $(".opener").click(openFolder);
  199. var state;
  200. if (history)
  201. state = history.state;
  202. if (state) {
  203. $(".opener").each(function(e) {
  204. if (state[$(this).attr("data-path")])
  205. openFolder.call(this);
  206. });
  207. }
  208. });
  209. $("#footer").before("<div id='whatisit'><h3>What is this ?</h3><p>This is an online code browser that allows you to browse C/C++ code just like in your IDE, "
  210. + "with <b>semantic highlighting</b> and contextual <b>tooltips</b> that show you the usages and cross references.<br/>"
  211. + "Open a C or C++ file and try it by hovering over the symbols!<br />"
  212. + "Or take the <a href='http://woboq.com/codebrowser-features.html'>feature tour</a>."
  213. + "</p></div>")
  214. });