diff options
author | Hiroshi SHIBATA <[email protected]> | 2025-01-15 11:52:40 +0900 |
---|---|---|
committer | Hiroshi SHIBATA <[email protected]> | 2025-01-15 16:52:56 +0900 |
commit | 86d871d29cda15810d9d60dc1b94a07e9530e0cb (patch) | |
tree | ae0fd977690197a4c82eed861527c109caade4f1 /lib/rdoc/generator/template/json_index/js | |
parent | e0be1b902549f80fcdc95e801d4d533b0fdec43b (diff) |
Migrate rdoc as bundled gems
Notes
Notes:
Merged: https://github.com/ruby/ruby/pull/12577
Diffstat (limited to 'lib/rdoc/generator/template/json_index/js')
-rw-r--r-- | lib/rdoc/generator/template/json_index/js/navigation.js | 105 | ||||
-rw-r--r-- | lib/rdoc/generator/template/json_index/js/searcher.js | 229 |
2 files changed, 0 insertions, 334 deletions
diff --git a/lib/rdoc/generator/template/json_index/js/navigation.js b/lib/rdoc/generator/template/json_index/js/navigation.js deleted file mode 100644 index 137e3a0038..0000000000 --- a/lib/rdoc/generator/template/json_index/js/navigation.js +++ /dev/null @@ -1,105 +0,0 @@ -/* - * Navigation allows movement using the arrow keys through the search results. - * - * When using this library you will need to set scrollIntoView to the - * appropriate function for your layout. Use scrollInWindow if the container - * is not scrollable and scrollInElement if the container is a separate - * scrolling region. - */ -Navigation = new function() { - this.initNavigation = function() { - var _this = this; - - document.addEventListener('keydown', function(e) { - _this.onkeydown(e); - }); - - this.navigationActive = true; - } - - this.setNavigationActive = function(state) { - this.navigationActive = state; - } - - this.onkeydown = function(e) { - if (!this.navigationActive) return; - switch(e.key) { - case 'ArrowLeft': - if (this.moveLeft()) e.preventDefault(); - break; - case 'ArrowUp': - if (e.key == 'ArrowUp' || e.ctrlKey) { - if (this.moveUp()) e.preventDefault(); - } - break; - case 'ArrowRight': - if (this.moveRight()) e.preventDefault(); - break; - case 'ArrowDown': - if (e.key == 'ArrowDown' || e.ctrlKey) { - if (this.moveDown()) e.preventDefault(); - } - break; - case 'Enter': - if (this.current) e.preventDefault(); - this.select(this.current); - break; - } - if (e.ctrlKey && e.shiftKey) this.select(this.current); - } - - this.moveRight = function() { - } - - this.moveLeft = function() { - } - - this.move = function(isDown) { - } - - this.moveUp = function() { - return this.move(false); - } - - this.moveDown = function() { - return this.move(true); - } - - /* - * Scrolls to the given element in the scrollable element view. - */ - this.scrollInElement = function(element, view) { - var offset, viewHeight, viewScroll, height; - offset = element.offsetTop; - height = element.offsetHeight; - viewHeight = view.offsetHeight; - viewScroll = view.scrollTop; - - if (offset - viewScroll + height > viewHeight) { - view.scrollTop = offset - viewHeight + height; - } - if (offset < viewScroll) { - view.scrollTop = offset; - } - } - - /* - * Scrolls to the given element in the window. The second argument is - * ignored - */ - this.scrollInWindow = function(element, ignored) { - var offset, viewHeight, viewScroll, height; - offset = element.offsetTop; - height = element.offsetHeight; - viewHeight = window.innerHeight; - viewScroll = window.scrollY; - - if (offset - viewScroll + height > viewHeight) { - window.scrollTo(window.scrollX, offset - viewHeight + height); - } - if (offset < viewScroll) { - window.scrollTo(window.scrollX, offset); - } - } -} - diff --git a/lib/rdoc/generator/template/json_index/js/searcher.js b/lib/rdoc/generator/template/json_index/js/searcher.js deleted file mode 100644 index e200a168b0..0000000000 --- a/lib/rdoc/generator/template/json_index/js/searcher.js +++ /dev/null @@ -1,229 +0,0 @@ -Searcher = function(data) { - this.data = data; - this.handlers = []; -} - -Searcher.prototype = new function() { - // search is performed in chunks of 1000 for non-blocking user input - var CHUNK_SIZE = 1000; - // do not try to find more than 100 results - var MAX_RESULTS = 100; - var huid = 1; - var suid = 1; - var runs = 0; - - this.find = function(query) { - var queries = splitQuery(query); - var regexps = buildRegexps(queries); - var highlighters = buildHilighters(queries); - var state = { from: 0, pass: 0, limit: MAX_RESULTS, n: suid++}; - var _this = this; - - this.currentSuid = state.n; - - if (!query) return; - - var run = function() { - // stop current search thread if new search started - if (state.n != _this.currentSuid) return; - - var results = - performSearch(_this.data, regexps, queries, highlighters, state); - var hasMore = (state.limit > 0 && state.pass < 4); - - triggerResults.call(_this, results, !hasMore); - if (hasMore) { - setTimeout(run, 2); - } - runs++; - }; - runs = 0; - - // start search thread - run(); - } - - /* ----- Events ------ */ - this.ready = function(fn) { - fn.huid = huid; - this.handlers.push(fn); - } - - /* ----- Utilities ------ */ - function splitQuery(query) { - return query.split(/(\s+|::?|\(\)?)/).filter(function(string) { - return string.match(/\S/); - }); - } - - function buildRegexps(queries) { - return queries.map(function(query) { - return new RegExp(query.replace(/(.)/g, '([$1])([^$1]*?)'), 'i'); - }); - } - - function buildHilighters(queries) { - return queries.map(function(query) { - return query.split('').map(function(l, i) { - return '\u0001$' + (i*2+1) + '\u0002$' + (i*2+2); - }).join(''); - }); - } - - // function longMatchRegexp(index, longIndex, regexps) { - // for (var i = regexps.length - 1; i >= 0; i--){ - // if (!index.match(regexps[i]) && !longIndex.match(regexps[i])) return false; - // }; - // return true; - // } - - - /* ----- Mathchers ------ */ - - /* - * This record matches if the index starts with queries[0] and the record - * matches all of the regexps - */ - function matchPassBeginning(index, longIndex, queries, regexps) { - if (index.indexOf(queries[0]) != 0) return false; - for (var i=1, l = regexps.length; i < l; i++) { - if (!index.match(regexps[i]) && !longIndex.match(regexps[i])) - return false; - }; - return true; - } - - /* - * This record matches if the longIndex starts with queries[0] and the - * longIndex matches all of the regexps - */ - function matchPassLongIndex(index, longIndex, queries, regexps) { - if (longIndex.indexOf(queries[0]) != 0) return false; - for (var i=1, l = regexps.length; i < l; i++) { - if (!longIndex.match(regexps[i])) - return false; - }; - return true; - } - - /* - * This record matches if the index contains queries[0] and the record - * matches all of the regexps - */ - function matchPassContains(index, longIndex, queries, regexps) { - if (index.indexOf(queries[0]) == -1) return false; - for (var i=1, l = regexps.length; i < l; i++) { - if (!index.match(regexps[i]) && !longIndex.match(regexps[i])) - return false; - }; - return true; - } - - /* - * This record matches if regexps[0] matches the index and the record - * matches all of the regexps - */ - function matchPassRegexp(index, longIndex, queries, regexps) { - if (!index.match(regexps[0])) return false; - for (var i=1, l = regexps.length; i < l; i++) { - if (!index.match(regexps[i]) && !longIndex.match(regexps[i])) - return false; - }; - return true; - } - - - /* ----- Highlighters ------ */ - function highlightRegexp(info, queries, regexps, highlighters) { - var result = createResult(info); - for (var i=0, l = regexps.length; i < l; i++) { - result.title = result.title.replace(regexps[i], highlighters[i]); - result.namespace = result.namespace.replace(regexps[i], highlighters[i]); - }; - return result; - } - - function hltSubstring(string, pos, length) { - return string.substring(0, pos) + '\u0001' + string.substring(pos, pos + length) + '\u0002' + string.substring(pos + length); - } - - function highlightQuery(info, queries, regexps, highlighters) { - var result = createResult(info); - var pos = 0; - var lcTitle = result.title.toLowerCase(); - - pos = lcTitle.indexOf(queries[0]); - if (pos != -1) { - result.title = hltSubstring(result.title, pos, queries[0].length); - } - - result.namespace = result.namespace.replace(regexps[0], highlighters[0]); - for (var i=1, l = regexps.length; i < l; i++) { - result.title = result.title.replace(regexps[i], highlighters[i]); - result.namespace = result.namespace.replace(regexps[i], highlighters[i]); - }; - return result; - } - - function createResult(info) { - var result = {}; - result.title = info[0]; - result.namespace = info[1]; - result.path = info[2]; - result.params = info[3]; - result.snippet = info[4]; - result.badge = info[6]; - return result; - } - - /* ----- Searching ------ */ - function performSearch(data, regexps, queries, highlighters, state) { - var searchIndex = data.searchIndex; - var longSearchIndex = data.longSearchIndex; - var info = data.info; - var result = []; - var i = state.from; - var l = searchIndex.length; - var togo = CHUNK_SIZE; - var matchFunc, hltFunc; - - while (state.pass < 4 && state.limit > 0 && togo > 0) { - if (state.pass == 0) { - matchFunc = matchPassBeginning; - hltFunc = highlightQuery; - } else if (state.pass == 1) { - matchFunc = matchPassLongIndex; - hltFunc = highlightQuery; - } else if (state.pass == 2) { - matchFunc = matchPassContains; - hltFunc = highlightQuery; - } else if (state.pass == 3) { - matchFunc = matchPassRegexp; - hltFunc = highlightRegexp; - } - - for (; togo > 0 && i < l && state.limit > 0; i++, togo--) { - if (info[i].n == state.n) continue; - if (matchFunc(searchIndex[i], longSearchIndex[i], queries, regexps)) { - info[i].n = state.n; - result.push(hltFunc(info[i], queries, regexps, highlighters)); - state.limit--; - } - }; - if (searchIndex.length <= i) { - state.pass++; - i = state.from = 0; - } else { - state.from = i; - } - } - return result; - } - - function triggerResults(results, isLast) { - this.handlers.forEach(function(fn) { - fn.call(this, results, isLast) - }); - } -} - |