nitdoc: Add shared files (imgs, styles and scripts)
[nit.git] / share / nitdoc / scripts / js-facilities.js
1 /*
2 * JQuery Case Insensitive :icontains selector
3 */
4 $.expr[':'].icontains = function(obj, index, meta, stack){
5 return (obj.textContent.replace(/\[[0-9]+\]/g, "") || obj.innerText.replace(/\[[0-9]+\]/g, "") || jQuery(obj).text().replace(/\[[0-9]+\]/g, "") || '').toLowerCase().indexOf(meta[3].toLowerCase()) >= 0;
6 };
7
8
9 /*
10 * Add folding and filtering facilities to class description page.
11 */
12 $(document).ready(function() {
13
14 /*
15 * Highlight the spoted element
16 */
17 highlightBlock(currentAnchor());
18
19 /*
20 * Nav block folding
21 */
22
23 // Menu nav folding
24 $(".menu nav h3")
25 .prepend(
26 $(document.createElement("a"))
27 .html("-")
28 .addClass("fold")
29 )
30 .css("cursor", "pointer")
31 .click( function() {
32 if($(this).find("a.fold").html() == "+") {
33 $(this).find("a.fold").html("-");
34 } else {
35 $(this).find("a.fold").html("+");
36 }
37 $(this).nextAll().toggle();
38 })
39
40 // Instert search field
41 $("nav.main ul")
42 .append(
43 $(document.createElement("li"))
44 .append(
45 $(document.createElement("form"))
46 .append(
47 $(document.createElement("input"))
48 .attr({
49 id: "search",
50 type: "text",
51 value: "quick search..."
52 })
53 .addClass("notUsed")
54 .keyup(function() {
55 $(this).parent().parent().find("ul li:not(:icontains('" + $(this).val() + "'))").addClass("hide");
56 $(this).parent().parent().find("ul li:icontains('" + $(this).val() + "')").removeClass("hide");
57 })
58 .focusout(function() {
59 if($(this).val() == "") {
60 $(this).addClass("notUsed");
61 $(this).val("quick search...");
62 }
63 })
64 .focusin(function() {
65 if($(this).val() == "quick search...") {
66 $(this).removeClass("notUsed");
67 $(this).val("");
68 }
69 })
70 )
71 .submit( function() {
72 if($("#search").val().length == 0)
73 return false
74
75 window.location = "full-index.html#q=" + $("#search").val();
76 if(window.location.href.indexOf("full-index.html") > -1) {
77 location.reload();
78 }
79 return false;
80 })
81 )
82 );
83
84 /*
85 * Anchors jumps
86 */
87 $("a[href^='#']").click( function() {
88 var a = $(this).attr("href").replace(/#/, "");
89 highlightBlock(a);
90 });
91
92 // Insert filter field
93 $("article.filterable h2, nav.filterable h3")
94 .after(
95 $(document.createElement("div"))
96 .addClass("filter")
97 .append(
98 $(document.createElement("input"))
99 .attr({
100 type: "text",
101 value: "filter..."
102 })
103 .addClass("notUsed")
104 .keyup(function() {
105 $(this).parent().parent().find("ul li:not(:icontains('" + $(this).val() + "'))").addClass("hide");
106 $(this).parent().parent().find("ul li:icontains('" + $(this).val() + "')").removeClass("hide");
107 })
108 .focusout(function() {
109 if($(this).val() == "") {
110 $(this).addClass("notUsed");
111 $(this).val("filter...");
112 }
113 })
114 .focusin(function() {
115 if($(this).val() == "filter...") {
116 $(this).removeClass("notUsed");
117 $(this).val("");
118 }
119 })
120 )
121 );
122
123 // Filter toggle between H I R in nav porperties list
124 $("nav.properties.filterable .filter")
125 .append(
126 $(document.createElement("a"))
127 .html("H")
128 .attr({
129 title: "hide inherited properties"
130 })
131 .click( function() {
132 if($(this).hasClass("hidden")) {
133 $(this).parent().parent().find("li.inherit").show();
134 } else {
135 $(this).parent().parent().find("li.inherit").hide();
136 }
137
138 $(this).toggleClass("hidden");
139 })
140 )
141 .append(
142 $(document.createElement("a"))
143 .html("R")
144 .attr({
145 title: "hide redefined properties"
146 })
147 .click( function() {
148 if($(this).hasClass("hidden")) {
149 $(this).parent().parent().find("li.redef").show();
150 } else {
151 $(this).parent().parent().find("li.redef").hide();
152 }
153
154 $(this).toggleClass("hidden");
155 })
156 )
157 .append(
158 $(document.createElement("a"))
159 .html("I")
160 .attr({
161 title: "hide introduced properties"
162 })
163 .click( function() {
164 if($(this).hasClass("hidden")) {
165 $(this).parent().parent().find("li.intro").show();
166 } else {
167 $(this).parent().parent().find("li.intro").hide();
168 }
169
170 $(this).toggleClass("hidden");
171 })
172 );
173
174 // Filter toggle between I R in
175 $("article.properties.filterable .filter, article.classes.filterable .filter")
176 .append(
177 $(document.createElement("a"))
178 .html("I")
179 .attr({
180 title: "hide introduced properties"
181 })
182 .click( function() {
183 if($(this).hasClass("hidden")) {
184 $(this).parent().parent().find("li.intro").show();
185 } else {
186 $(this).parent().parent().find("li.intro").hide();
187 }
188
189 $(this).toggleClass("hidden");
190 })
191 )
192 .append(
193 $(document.createElement("a"))
194 .html("R")
195 .attr({
196 title: "hide redefined properties"
197 })
198 .click( function() {
199 if($(this).hasClass("hidden")) {
200 $(this).parent().parent().find("li.redef").show();
201 } else {
202 $(this).parent().parent().find("li.redef").hide();
203 }
204
205 $(this).toggleClass("hidden");
206 })
207 );
208
209 //Preload filter fields with query string
210 preloadFilters();
211 });
212
213 /* Parse current URL and return anchor name */
214 function currentAnchor() {
215 var index = document.location.hash.indexOf("#");
216 if (index >= 0) {
217 return document.location.hash.substring(index + 1);
218 }
219 return null;
220 }
221
222 /* Prealod filters field using search query */
223 function preloadFilters() {
224 // Parse URL and get query string
225 var search = currentAnchor();
226
227 if(search == null || search.indexOf("q=") == -1)
228 return;
229
230 search = search.substring(2, search.length);
231
232 if(search == "" || search == "undefined")
233 return;
234
235 $(":text").val(search);
236 $(".filter :text")
237 .removeClass("notUsed")
238 .trigger("keyup");
239
240 }
241
242 /* Hightlight the spoted block */
243 function highlightBlock(a) {
244 if(a == undefined) {
245 return;
246 }
247
248 $(".highlighted").removeClass("highlighted");
249
250 var target = $("#" + a);
251
252 if(target.is("article")) {
253 target.parent().addClass("highlighted");
254 }
255
256 target.addClass("highlighted");
257 target.show();
258 }