]> git.r.bdr.sh - rbdr/lyricli.rb/blob - doc/js/full_list.js
Add a README
[rbdr/lyricli.rb] / doc / js / full_list.js
1 var inSearch = null;
2 var searchIndex = 0;
3 var searchCache = [];
4 var searchString = '';
5 var regexSearchString = '';
6 var caseSensitiveMatch = false;
7 var ignoreKeyCodeMin = 8;
8 var ignoreKeyCodeMax = 46;
9 var commandKey = 91;
10
11 RegExp.escape = function(text) {
12 return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
13 }
14
15 function fullListSearch() {
16 // generate cache
17 searchCache = [];
18 $('#full_list li').each(function() {
19 var link = $(this).find('.object_link a');
20 var fullName = link.attr('title').split(' ')[0];
21 searchCache.push({name:link.text(), fullName:fullName, node:$(this), link:link});
22 });
23
24 $('#search input').keyup(function(event) {
25 if ((event.keyCode > ignoreKeyCodeMin && event.keyCode < ignoreKeyCodeMax)
26 || event.keyCode == commandKey)
27 return;
28 searchString = this.value;
29 caseSensitiveMatch = searchString.match(/[A-Z]/) != null;
30 regexSearchString = RegExp.escape(searchString);
31 if (caseSensitiveMatch) {
32 regexSearchString += "|" +
33 $.map(searchString.split(''), function(e) { return RegExp.escape(e); }).
34 join('.+?');
35 }
36 if (searchString === "") {
37 clearTimeout(inSearch);
38 inSearch = null;
39 $('ul .search_uncollapsed').removeClass('search_uncollapsed');
40 $('#full_list, #content').removeClass('insearch');
41 $('#full_list li').removeClass('found').each(function() {
42
43 var link = $(this).find('.object_link a');
44 link.text(link.text());
45 });
46 if (clicked) {
47 clicked.parents('ul').each(function() {
48 $(this).removeClass('collapsed').prev().removeClass('collapsed');
49 });
50 }
51 highlight();
52 }
53 else {
54 if (inSearch) clearTimeout(inSearch);
55 searchIndex = 0;
56 lastRowClass = '';
57 $('#full_list, #content').addClass('insearch');
58 $('#noresults').text('');
59 searchItem();
60 }
61 });
62
63 $('#search input').focus();
64 $('#full_list').after("<div id='noresults'></div>");
65 }
66
67 var lastRowClass = '';
68 function searchItem() {
69 for (var i = 0; i < searchCache.length / 50; i++) {
70 var item = searchCache[searchIndex];
71 var searchName = (searchString.indexOf('::') != -1 ? item.fullName : item.name);
72 var matchString = regexSearchString;
73 var matchRegexp = new RegExp(matchString, caseSensitiveMatch ? "" : "i");
74 if (searchName.match(matchRegexp) == null) {
75 item.node.removeClass('found');
76 }
77 else {
78 item.node.css('padding-left', '10px').addClass('found');
79 item.node.parents().addClass('search_uncollapsed');
80 item.node.removeClass(lastRowClass).addClass(lastRowClass == 'r1' ? 'r2' : 'r1');
81 lastRowClass = item.node.hasClass('r1') ? 'r1' : 'r2';
82 item.link.html(item.name.replace(matchRegexp, "<strong>$&</strong>"));
83 }
84
85 if (searchCache.length === searchIndex + 1) {
86 searchDone();
87 return;
88 }
89 else {
90 searchIndex++;
91 }
92 }
93 inSearch = setTimeout('searchItem()', 0);
94 }
95
96 function searchDone() {
97 highlight(true);
98 if ($('#full_list li:visible').size() === 0) {
99 $('#noresults').text('No results were found.').hide().fadeIn();
100 }
101 else {
102 $('#noresults').text('');
103 }
104 $('#content').removeClass('insearch');
105 clearTimeout(inSearch);
106 inSearch = null;
107 }
108
109 clicked = null;
110 function linkList() {
111 $('#full_list li, #full_list li a:last').click(function(evt) {
112 if ($(this).hasClass('toggle')) return true;
113 if (this.tagName.toLowerCase() == "li") {
114 var toggle = $(this).children('a.toggle');
115 if (toggle.size() > 0 && evt.pageX < toggle.offset().left) {
116 toggle.click();
117 return false;
118 }
119 }
120 if (clicked) clicked.removeClass('clicked');
121 var win = window.top.frames.main ? window.top.frames.main : window.parent;
122 if (this.tagName.toLowerCase() == "a") {
123 clicked = $(this).parent('li').addClass('clicked');
124 win.location = this.href;
125 }
126 else {
127 clicked = $(this).addClass('clicked');
128 win.location = $(this).find('a:last').attr('href');
129 }
130 return false;
131 });
132 }
133
134 function collapse() {
135 if (!$('#full_list').hasClass('class')) return;
136 $('#full_list.class a.toggle').click(function() {
137 $(this).parent().toggleClass('collapsed').next().toggleClass('collapsed');
138 highlight();
139 return false;
140 });
141 $('#full_list.class ul').each(function() {
142 $(this).addClass('collapsed').prev().addClass('collapsed');
143 });
144 $('#full_list.class').children().removeClass('collapsed');
145 highlight();
146 }
147
148 function highlight(no_padding) {
149 var n = 1;
150 $('#full_list li:visible').each(function() {
151 var next = n == 1 ? 2 : 1;
152 $(this).removeClass("r" + next).addClass("r" + n);
153 if (!no_padding && $('#full_list').hasClass('class')) {
154 $(this).css('padding-left', (10 + $(this).parents('ul').size() * 15) + 'px');
155 }
156 n = next;
157 });
158 }
159
160 function escapeShortcut() {
161 $(document).keydown(function(evt) {
162 if (evt.which == 27) {
163 $('#search_frame', window.top.document).slideUp(100);
164 $('#search a', window.top.document).removeClass('active inactive');
165 $(window.top).focus();
166 }
167 });
168 }
169
170 $(escapeShortcut);
171 $(fullListSearch);
172 $(linkList);
173 $(collapse);