|
| 1 | +/**************************************************************************** |
| 2 | + * Copyright (C) 2012-2016 Woboq GmbH |
| 3 | + * Olivier Goffart <contact at woboq.com> |
| 4 | + * https://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 https://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 | + |
| 22 | + |
| 23 | +$(function() { |
| 24 | + // ATTENTION: Keep in sync with C++ function of the same name in filesystem.cpp and `Generator::escapeAttrForFilename` |
| 25 | + var replace_invalid_filename_chars = function (str) { |
| 26 | + if(window.ecma_script_api_version && window.ecma_script_api_version >= 2) { |
| 27 | + return str.replace(new RegExp(':', 'g'), '.'); |
| 28 | + } |
| 29 | + |
| 30 | + return str; |
| 31 | + } |
| 32 | + |
| 33 | + // remove trailing slash |
| 34 | + root_path = root_path.replace(/\/$/, ""); |
| 35 | + if(!root_path) root_path = "."; |
| 36 | + |
| 37 | + |
| 38 | + //compute the length of the common prefix between two strings |
| 39 | + // (copied from codebrowser.js) |
| 40 | + var prefixLen = function( s1 , s2) { |
| 41 | + var maxMatchLen = Math.min(s1.length, s2.length); |
| 42 | + var res = -1; |
| 43 | + while (++res < maxMatchLen) { |
| 44 | + if (s1.charAt(res) != s2.charAt(res)) |
| 45 | + break; |
| 46 | + } |
| 47 | + return res * 256 + 256 - s1.length; |
| 48 | + } |
| 49 | + |
| 50 | + // Google text search (different than codebrowser.js) |
| 51 | + var text_search = function(text) { |
| 52 | + var location = "" + (window.location); |
| 53 | + window.location = "http://google.com/search?sitesearch=" + encodeURIComponent(location) + "&q=" + encodeURIComponent(text); |
| 54 | + } |
| 55 | + |
| 56 | + var fileIndex = []; |
| 57 | + var searchTerms = {} |
| 58 | + var functionDict = {}; |
| 59 | + var file = path; |
| 60 | + |
| 61 | + var searchline = $("input#searchline"); |
| 62 | + |
| 63 | + //BEGIN copied from codebrowser.js |
| 64 | + |
| 65 | + // callback for jqueryui's autocomple activate |
| 66 | + var activate = function(event,ui) { |
| 67 | + var val = ui.item.value; |
| 68 | + var type = searchTerms[val] && searchTerms[val].type; |
| 69 | + if (type == "file") { |
| 70 | + window.location = root_path + '/' + searchTerms[val].file + ".html"; |
| 71 | + } else if (type == "ref") { |
| 72 | + var ref = searchTerms[val].ref; |
| 73 | + var url = root_path + "/refs/" + replace_invalid_filename_chars(ref); |
| 74 | + $.get(url, function(data) { |
| 75 | + var res = $("<data>"+data+"</data>"); |
| 76 | + var def = res.find("def"); |
| 77 | + var result = { len: -1 }; |
| 78 | + def.each( function() { |
| 79 | + var cur = { len : -1, |
| 80 | + f : $(this).attr("f"), |
| 81 | + l : $(this).attr("l") } |
| 82 | + |
| 83 | + cur.len = prefixLen(cur.f, file) |
| 84 | + if (cur.len >= result.len) { |
| 85 | + result = cur; |
| 86 | + result.isMarcro = ($(this).attr("macro")); |
| 87 | + } |
| 88 | + }); |
| 89 | + |
| 90 | + if (result.len >= 0) { |
| 91 | + var newloc = root_path + "/" + result.f + ".html#" + |
| 92 | + (result.isMarcro ? result.l : ref ); |
| 93 | + window.location = newloc; |
| 94 | + } |
| 95 | + }); |
| 96 | + } else { |
| 97 | + text_search(val); |
| 98 | + } |
| 99 | + }; |
| 100 | + |
| 101 | + var getFnNameKey = function (request) { |
| 102 | + if (request.indexOf('/') != -1 || request.indexOf('.') != -1) |
| 103 | + return false; |
| 104 | + var mx = request.match(/::([^:]{2})[^:]*$/); |
| 105 | + if (mx) |
| 106 | + return mx[1].toLowerCase().replace(/[^a-z]/, '_'); |
| 107 | + request = request.replace(/^:*/, ""); |
| 108 | + if (request.length < 2) |
| 109 | + return false; |
| 110 | + var k = request.substr(0, 2).toLowerCase(); |
| 111 | + return k.replace(/[^a-z]/, '_') |
| 112 | + } |
| 113 | + |
| 114 | + var autocomplete = function(request, response) { |
| 115 | + var term = $.ui.autocomplete.escapeRegex(request.term); |
| 116 | + var rx1 = new RegExp(term, 'i'); |
| 117 | + var rx2 = new RegExp("(^|::)"+term.replace(/^:*/, ''), 'i'); |
| 118 | + var functionList = []; |
| 119 | + var k = getFnNameKey(request.term) |
| 120 | + if (k && Object.prototype.hasOwnProperty.call(functionDict,k)) { |
| 121 | + functionList = functionDict[k].filter( |
| 122 | + function(word) { return word.match(rx2) }); |
| 123 | + } |
| 124 | + var l = fileIndex.filter( function(word) { return word.match(rx1); }); |
| 125 | + l = l.concat(functionList); |
| 126 | + l = l.slice(0,1000); // too big lists are too slow |
| 127 | + response(l); |
| 128 | + }; |
| 129 | + |
| 130 | + searchline.autocomplete( {source: autocomplete, select: activate, minLength: 4 } ); |
| 131 | + |
| 132 | + searchline.keypress(function(e) { |
| 133 | + var value = searchline.val(); |
| 134 | + if(e.which == 13) { |
| 135 | + activate({}, { item: { value: value } }); |
| 136 | + } |
| 137 | + }); |
| 138 | + |
| 139 | + // When the content changes, fetch the list of function that starts with ... |
| 140 | + searchline.on('input', function() { |
| 141 | + var value = $(this).val(); |
| 142 | + var k = getFnNameKey(value); |
| 143 | + if (k && !Object.prototype.hasOwnProperty.call(functionDict, k)) { |
| 144 | + functionDict[k] = [] |
| 145 | + $.get(root_path + '/fnSearch/' + k, function(data) { |
| 146 | + var list = data.split("\n"); |
| 147 | + for (var i = 0; i < list.length; ++i) { |
| 148 | + var sep = list[i].indexOf('|'); |
| 149 | + var ref = list[i].slice(0, sep); |
| 150 | + var name = list[i].slice(sep+1); |
| 151 | + searchTerms[name] = { type:"ref", ref: ref }; |
| 152 | + functionDict[k].push(name); |
| 153 | + } |
| 154 | + if (searchline.is(":focus")) { |
| 155 | + searchline.autocomplete("search", searchline.val()); |
| 156 | + } |
| 157 | + }); |
| 158 | + } |
| 159 | + }); |
| 160 | + |
| 161 | + // Pasting should show the autocompletion |
| 162 | + searchline.on("paste", function() { setTimeout(function() { |
| 163 | + searchline.autocomplete("search", searchline.val()); |
| 164 | + }, 0); |
| 165 | + }); |
| 166 | + |
| 167 | + //END copied from codebrowser.js |
| 168 | + |
| 169 | + |
| 170 | + $.get(root_path + '/fileIndex', function(data) { |
| 171 | + var list = data.split("\n"); |
| 172 | + list.sort(); |
| 173 | + |
| 174 | + fileIndex = list; |
| 175 | + for (var i = 0; i < list.length; ++i) { |
| 176 | + searchTerms[list[i]] = { type:"file", file: list[i] }; |
| 177 | + } |
| 178 | + |
| 179 | + |
| 180 | + function openFolder() { |
| 181 | + var t = $(this); |
| 182 | + var state = {}; |
| 183 | + if (history) |
| 184 | + state = history.state || state; |
| 185 | + if (!this._opened) { |
| 186 | + this._opened = true; |
| 187 | + var p = t.attr("data-path") + "/"; |
| 188 | + var subPath = path=="" ? p : p.substr(path.length); |
| 189 | + t.text("[-]"); |
| 190 | + var content = $("<table/>"); |
| 191 | + var dict = {}; |
| 192 | + var toOpenNow = []; |
| 193 | + for (var i=0; i < fileIndex.length; ++i) { |
| 194 | + var f = fileIndex[i]; |
| 195 | + if (f.indexOf(p) == 0) { |
| 196 | + var sl = f.indexOf('/', p.length + 1); |
| 197 | + |
| 198 | + if (sl !== -1) { |
| 199 | + var name = f.substr( p.length, sl - p.length); |
| 200 | + if (dict[name]) |
| 201 | + continue; |
| 202 | + dict[name] = true; |
| 203 | + content.append("<tr><td class='folder'><a class='opener' data-path='" + p + name + "' href='"+subPath + name+"'>[+]</a> " + |
| 204 | + "<a href='" + subPath + name + "/'>" + name + "/</a></td></tr>\n"); |
| 205 | + if (state[p+name]) |
| 206 | + toOpenNow.push(p+name); |
| 207 | + } else { |
| 208 | + var name = f.substr(p.length); |
| 209 | + content.append("<tr><td class='file'> <a href='" + subPath + name + ".html'>" + name + "</a></td></tr>\n"); |
| 210 | + } |
| 211 | + } |
| 212 | + } |
| 213 | + content.find(".opener").click(openFolder); |
| 214 | + t.parent().append(content); |
| 215 | + state[t.attr("data-path")]=true; |
| 216 | + toOpenNow.forEach(function(toOpen) { |
| 217 | + var e = $("a[data-path='"+toOpen+"']").get(0) |
| 218 | + if (e) |
| 219 | + openFolder.call(e); |
| 220 | + }); |
| 221 | + } else { |
| 222 | + t.parent().find("> table").empty(); |
| 223 | + t.text("[+]"); |
| 224 | + this._opened = false; |
| 225 | + state[t.attr("data-path")]=false; |
| 226 | + } |
| 227 | + if (history && history.replaceState) |
| 228 | + history.replaceState(state, undefined); |
| 229 | + return false; |
| 230 | + } |
| 231 | + |
| 232 | + $(".opener").click(openFolder); |
| 233 | + var state; |
| 234 | + if (history) |
| 235 | + state = history.state; |
| 236 | + if (state) { |
| 237 | + $(".opener").each(function(e) { |
| 238 | + if (state[$(this).attr("data-path")]) |
| 239 | + openFolder.call(this); |
| 240 | + }); |
| 241 | + } |
| 242 | + }); |
| 243 | + |
| 244 | + $("#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, " |
| 245 | + + "with <b>semantic highlighting</b> and contextual <b>tooltips</b> that show you the usages and cross references.<br/>" |
| 246 | + + "Open a C or C++ file and try it by hovering over the symbols!<br />" |
| 247 | + + "Or take the <a href='https://woboq.com/codebrowser-features.html'>feature tour</a>." |
| 248 | + + "</p></div>") |
| 249 | +}); |
| 250 | + |
0 commit comments