PageSourceSearch

https://www.onetonline.org/shared/libraries/autocomplete/autocomplete.js

js onetonline.org collected 2026-09-24 08:28:47 UTC 3,505 bytes, 119 lines download raw bytes

1+function ($) {
2  'use strict';
3  
4  $.fn.autocomplete = function (options) {
5    var defaults = {
6      titles: [],
7      codes: []
8    };
9    var settings = $.extend({}, defaults, options);
10
11    return this.each(function () {
12      var $input = $(this);
13      var $menu = $('#' + $input.data('acMenu'));
14      var links = [ $menu.find('.result1 a'), $menu.find('.result2 a'), $menu.find('.result3 a'), $menu.find('.result4 a'), $menu.find('.result5 a') ];
15      var dropdown = bootstrap.Dropdown.getInstance($input[0]);
16      if (!dropdown) {
17        dropdown = new bootstrap.Dropdown($input[0]);
18      }
19      
20      var has_results = false;
21      
22      var lookup = function (query) {
23        var srch = query.normalize("NFKD").replace(/[\u0300-\u036f]/g, '').trim().toLowerCase();
24        if (!srch.length) {
25          return null;
26        }
27        if (settings.codes.length && /^\d/.test(srch)) {
28          srch = srch.replaceAll(/[^\d]/g, '');
29          var res = [];
30          for (oref of settings.codes) {
31            if (oref[0].startsWith(srch)) {
32              res.push([ oref[1], oref[2] ]);
33            }
34            if (res.length >= 5) {
35              break;
36            }
37          }
38          return res;
39        } else if (settings.titles.length) {
40          srch = ' ' + srch.replaceAll(/[^a-z0-9 ]/g, '');
41          if (srch.length < 4) {
42            return null;
43          }
44          var founds = [];
45          for (var oref of settings.titles) {
46            var idx = oref[0].indexOf(srch);
47            if (idx < 0) {
48              continue;
49            }
50            if (!founds[idx]) {
51              founds[idx] = [];
52            }
53            founds[idx].push([ oref[1], oref[2] ]);
54          }
55          var res = [];
56          for (var idx in founds) {
57            for (var val of founds[idx]) {
58              res.push(val);
59              if (res.length >= 5) {
60                break;
61              }
62            }
63            if (res.length >= 5) {
64              break;
65            }
66          }
67          return res;
68        }
69        return null;
70      };
71    
72      var sync = function () {
73        $menu.removeClass('prompt');
74        $menu.removeClass('result0');
75        $menu.removeClass('result1');
76        $menu.removeClass('result2');
77        $menu.removeClass('result3');
78        $menu.removeClass('result4');
79        $menu.removeClass('result5');
80        
81        var res = lookup($input.val());
82        if (res === null) {
83          has_results = false;
84          $menu.addClass('prompt');
85        } else {
86          has_results = true;
87          $menu.addClass('result' + res.length);
88          for (var idx = 0; idx < res.length; idx++) {
89            links[idx].attr('href', links[idx].data('root') + res[idx][0]);
90            links[idx].text(res[idx][1]);
91          }
92        }
93        if (dropdown) {
94          dropdown.update();
95        }
96        if (!$menu.hasClass('show') && $input.is(':focus')) {
97          dropdown.show();
98        }
99      };
100      sync();
101      $input.on('input', sync);
102      $input.on('keydown', function (evt) {
103        if (!has_results) {
104          return;
105        }
106        if (evt.isComposing || evt.keyCode === 229) {
107          return;
108        }
109        if (evt.keyCode === 40) {
110          var el = $input[0];
111          if (el.selectionStart === el.selectionEnd && el.selectionStart == el.value.length) {
112            evt.preventDefault();
113            links[0].focus();
114          }
115        }
116      });
117    });
118  };
119}(jQuery);

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.