nitdoc: Adds the github authenfication
[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 * Quick Search global vars
10 */
11
12 // Current search results preview table
13 var currentTable = null;
14
15 //Hightlighted index in search result preview table
16 var currentIndex = -1;
17
18
19 /*
20 * Add folding and filtering facilities to class description page.
21 */
22 $(document).ready(function() {
23
24 /*
25 * Highlight the spoted element
26 */
27 highlightBlock(currentAnchor());
28
29 /*
30 * Nav block folding
31 */
32
33 // Menu nav folding
34 $(".menu nav h3")
35 .prepend(
36 $(document.createElement("a"))
37 .html("-")
38 .addClass("fold")
39 )
40 .css("cursor", "pointer")
41 .click( function() {
42 if($(this).find("a.fold").html() == "+") {
43 $(this).find("a.fold").html("-");
44 } else {
45 $(this).find("a.fold").html("+");
46 }
47 $(this).nextAll().toggle();
48 })
49
50 // Insert search field
51 $("nav.main ul")
52 .append(
53 $(document.createElement("li"))
54 .append(
55 $(document.createElement("form"))
56 .append(
57 $(document.createElement("input"))
58 .attr({
59 id: "search",
60 type: "text",
61 autocomplete: "off",
62 value: "quick search..."
63 })
64 .addClass("notUsed")
65
66 // Key management
67 .keyup(function(e) {
68 switch(e.keyCode) {
69
70 // Select previous result on "Up"
71 case 38:
72 // If already on first result, focus search input
73 if(currentIndex == 0) {
74 $("#search").val($(currentTable.find("tr")[currentIndex]).data("searchDetails").name);
75 $("#search").focus();
76 // Else select previous result
77 } else if(currentIndex > 0) {
78 $(currentTable.find("tr")[currentIndex]).removeClass("activeSearchResult");
79 currentIndex--;
80 $(currentTable.find("tr")[currentIndex]).addClass("activeSearchResult");
81 $("#search").val($(currentTable.find("tr")[currentIndex]).data("searchDetails").name);
82 $("#search").focus();
83 }
84 break;
85
86 // Select next result on "Down"
87 case 40:
88 if(currentIndex < currentTable.find("tr").length - 1) {
89 $(currentTable.find("tr")[currentIndex]).removeClass("activeSearchResult");
90 currentIndex++;
91 $(currentTable.find("tr")[currentIndex]).addClass("activeSearchResult");
92 $("#search").val($(currentTable.find("tr")[currentIndex]).data("searchDetails").name);
93 $("#search").focus();
94 }
95 break;
96 // Go to url on "Enter"
97 case 13:
98 if(currentIndex > -1) {
99 window.location = $(currentTable.find("tr")[currentIndex]).data("searchDetails").url;
100 return false;
101 }
102 if($("#search").val().length == 0)
103 return false
104
105 window.location = "full-index.html#q=" + $("#search").val();
106 if(window.location.href.indexOf("full-index.html") > -1) {
107 location.reload();
108 }
109 return false;
110 break;
111
112 // Hide results preview on "Escape"
113 case 27:
114 $(this).blur();
115 if(currentTable != null) {
116 currentTable.remove();
117 currentTable = null;
118 }
119 break;
120
121 default:
122 if($("#search").val().length == 0) {
123 return false;
124 }
125
126 // Remove previous table
127 if(currentTable != null) {
128 currentTable.remove();
129 }
130
131 // Build results table
132 currentIndex = -1;
133 currentTable = $(document.createElement("table"));
134
135 // Escape regexp related characters in query
136 var query = $("#search").val();
137 query = query.replace(/\[/gi, "\\[");
138 query = query.replace(/\|/gi, "\\|");
139 query = query.replace(/\*/gi, "\\*");
140 query = query.replace(/\+/gi, "\\+");
141 query = query.replace(/\\/gi, "\\\\");
142 query = query.replace(/\?/gi, "\\?");
143 query = query.replace(/\(/gi, "\\(");
144 query = query.replace(/\)/gi, "\\)");
145
146 var index = 0;
147 var regexp = new RegExp("^" + query, "i");
148 for(var entry in entries) {
149 if(index > 10) {
150 break;
151 }
152 var result = entry.match(regexp);
153 if(result != null && result.toString().toUpperCase() == $("#search").val().toUpperCase()) {
154 for(var i = 0; i < entries[entry].length; i++) {
155 if(index > 10) {
156 break;
157 }
158 currentTable.append(
159 $(document.createElement("tr"))
160 .data("searchDetails", {name: entry, url: entries[entry][i]["url"]})
161 .data("index", index)
162 .append($(document.createElement("td")).html(entry))
163 .append(
164 $(document.createElement("td"))
165 .addClass("entryInfo")
166 .html(entries[entry][i]["txt"] + "&nbsp;&raquo;"))
167 .mouseover( function() {
168 $(currentTable.find("tr")[currentIndex]).removeClass("activeSearchResult");
169 $(this).addClass("activeSearchResult");
170 currentIndex = $(this).data("index");
171 })
172 .mouseout( function() {
173 $(this).removeClass("activeSearchResult");
174 })
175 .click( function() {
176 window.location = $(this).data("searchDetails")["url"];
177 })
178 );
179 index++;
180 }
181 }
182 }
183
184 // Initialize table properties
185 currentTable.attr("id", "searchTable");
186 currentTable.css("position", "absolute");
187 currentTable.width($("#search").outerWidth());
188 $("header").append(currentTable);
189 currentTable.offset({left: $("#search").offset().left + ($("#search").outerWidth() - currentTable.outerWidth()), top: $("#search").offset().top + $("#search").outerHeight()});
190
191 // Preselect first entry
192 if(currentTable.find("tr").length > 0) {
193 currentIndex = 0;
194 $(currentTable.find("tr")[currentIndex]).addClass("activeSearchResult");
195 $("#search").focus();
196 }
197 break;
198 }
199 })
200 .focusout(function() {
201 if($(this).val() == "") {
202 $(this).addClass("notUsed");
203 $(this).val("quick search...");
204 }
205 })
206 .focusin(function() {
207 if($(this).val() == "quick search...") {
208 $(this).removeClass("notUsed");
209 $(this).val("");
210 }
211 })
212 )
213 .submit( function() {
214 return false;
215 })
216 )
217 );
218
219 // Close quicksearch list on click
220 $(document).click(function(e) {
221 if(e.target != $("#search")[0] && e.target != $("#searchTable")[0]) {
222 if(currentTable != null) {
223 currentTable.remove();
224 currentTable = null;
225 }
226 }
227 });
228
229 // Insert filter field
230 $("article.filterable h2, nav.filterable h3")
231 .after(
232 $(document.createElement("div"))
233 .addClass("filter")
234 .append(
235 $(document.createElement("input"))
236 .attr({
237 type: "text",
238 value: "filter..."
239 })
240 .addClass("notUsed")
241 .keyup(function() {
242 $(this).parent().parent().find("ul li:not(:icontains('" + $(this).val() + "'))").addClass("hide");
243 $(this).parent().parent().find("ul li:icontains('" + $(this).val() + "')").removeClass("hide");
244 })
245 .focusout(function() {
246 if($(this).val() == "") {
247 $(this).addClass("notUsed");
248 $(this).val("filter...");
249 }
250 })
251 .focusin(function() {
252 if($(this).val() == "filter...") {
253 $(this).removeClass("notUsed");
254 $(this).val("");
255 }
256 })
257 )
258 );
259
260 // Filter toggle between H I R in nav porperties list
261 $("nav.properties.filterable .filter")
262 .append(
263 $(document.createElement("a"))
264 .html("H")
265 .attr({
266 title: "hide inherited properties"
267 })
268 .click( function() {
269 if($(this).hasClass("hidden")) {
270 $(this).parent().parent().find("li.inherit").show();
271 } else {
272 $(this).parent().parent().find("li.inherit").hide();
273 }
274
275 $(this).toggleClass("hidden");
276 })
277 )
278 .append(
279 $(document.createElement("a"))
280 .html("R")
281 .attr({
282 title: "hide redefined properties"
283 })
284 .click( function() {
285 if($(this).hasClass("hidden")) {
286 $(this).parent().parent().find("li.redef").show();
287 } else {
288 $(this).parent().parent().find("li.redef").hide();
289 }
290
291 $(this).toggleClass("hidden");
292 })
293 )
294 .append(
295 $(document.createElement("a"))
296 .html("I")
297 .attr({
298 title: "hide introduced properties"
299 })
300 .click( function() {
301 if($(this).hasClass("hidden")) {
302 $(this).parent().parent().find("li.intro").show();
303 } else {
304 $(this).parent().parent().find("li.intro").hide();
305 }
306
307 $(this).toggleClass("hidden");
308 })
309 );
310
311 // Filter toggle between I R in
312 $("article.properties.filterable .filter, article.classes.filterable .filter")
313 .append(
314 $(document.createElement("a"))
315 .html("I")
316 .attr({
317 title: "hide introduced properties"
318 })
319 .click( function() {
320 if($(this).hasClass("hidden")) {
321 $(this).parent().parent().find("li.intro").show();
322 } else {
323 $(this).parent().parent().find("li.intro").hide();
324 }
325
326 $(this).toggleClass("hidden");
327 })
328 )
329 .append(
330 $(document.createElement("a"))
331 .html("R")
332 .attr({
333 title: "hide redefined properties"
334 })
335 .click( function() {
336 if($(this).hasClass("hidden")) {
337 $(this).parent().parent().find("li.redef").show();
338 } else {
339 $(this).parent().parent().find("li.redef").hide();
340 }
341
342 $(this).toggleClass("hidden");
343 })
344 );
345
346 /*
347 * Anchors jumps
348 */
349 $("a[href*='#']").click( function() {
350 highlightBlock($(this).attr("href").split(/#/)[1]);
351 });
352
353 //Preload filter fields with query string
354 preloadFilters();
355 // Hide Authenfication form
356 $(".popover").hide();
357 // Display Login modal
358 $("#logGitHub").click(function(){ displayLogginModal(); });
359 });
360
361 /* Parse current URL and return anchor name */
362 function currentAnchor() {
363 var index = document.location.hash.indexOf("#");
364 if (index >= 0) {
365 return document.location.hash.substring(index + 1);
366 }
367 return null;
368 }
369
370 /* Prealod filters field using search query */
371 function preloadFilters() {
372 // Parse URL and get query string
373 var search = currentAnchor();
374
375 if(search == null || search.indexOf("q=") == -1)
376 return;
377
378 search = search.substring(2, search.length);
379
380 if(search == "" || search == "undefined")
381 return;
382
383 $(":text").val(search);
384 $(".filter :text")
385 .removeClass("notUsed")
386 .trigger("keyup");
387
388 }
389
390 /* Hightlight the spoted block */
391 function highlightBlock(a) {
392 if(a == undefined) {
393 return;
394 }
395
396 $(".highlighted").removeClass("highlighted");
397
398 var target = $("#" + a);
399
400 if(target.is("article")) {
401 target.parent().addClass("highlighted");
402 }
403
404 target.addClass("highlighted");
405 target.show();
406 }
407
408 function displayLogginModal(){
409 if ($('.popover').is(':hidden')) { $('.popover').show(); }
410 else { $('.popover').hide(); }
411 updateDisplaying();
412 }
413
414 function updateDisplaying(){
415 $('#logginMessage').css({'display' : 'none'});
416 $("#liGitHub").attr("class", "");
417 $("#imgGitHub").attr("src", "resources/icons/github-icon.png");
418 $('#loginGit').val("");
419 $('#passwordGit').val("");
420 $('#nickName').text("");
421 $('.popover').css({'height' : '280px'});
422 $('#logginMessage').css({'display' : 'none'});
423 $('#repositoryGit').val($('#repoName').attr('name'));
424 $('#branchGit').val('wikidoc');
425 $('#signIn').text("Sign In");
426 $('#loginGit').show();
427 $('#passwordGit').show();
428 $('#lbpasswordGit').show();
429 $('#lbloginGit').show();
430 $('#repositoryGit').show();
431 $('#lbrepositoryGit').show();
432 $('#lbbranchGit').show();
433 $('#branchGit').show();
434 }