fa0f12ec0137b6c8f06c0da663a0023e21b21774
[nit.git] / share / nitdoc / js / plugins / filtering.js
1 /* This file is part of NIT ( http://www.nitlanguage.org ).
2
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14
15 Documentation generator for the nit language.
16 Generate API documentation in HTML format from nit source code.
17 */
18
19 /*
20 * Nitdoc Filtering
21 *
22 * Allow user to filter sidebar entries and search page
23 */
24 var Filtering = {
25
26 // Allow user to filter sidebar box entries by name
27 enableSidebarTextFilters: function(filterSelector) {
28 var div = $(document.createElement("div"))
29 .addClass("nitdoc-ui-filter")
30 .append(
31 $(document.createElement("input"))
32 .addClass("nitdoc-ui-filter-field")
33 .addClass("nitdoc-ui-filter-field-notused")
34 .attr("type", "text")
35 .attr("value", "filter...")
36 .keyup(function() {
37 var box = $(this).parents("nav.filterable");
38 var value = $(this).val();
39 box.find("ul li:not(:icontains('" + value + "'))").hide();
40 box.find("ul li:icontains('" + value + "')").show();
41 })
42 .focusout(function() {
43 if($(this).val() == "") {
44 $(this).addClass("nitdoc-ui-filter-field-notused");
45 $(this).val("filter...");
46 }
47 })
48 .focusin(function() {
49 if($(this).val() == "filter...") {
50 $(this).removeClass("nitdoc-ui-filter-field-notused");
51 $(this).val("");
52 }
53 })
54 );
55 $(filterSelector).after(div);
56 this.preloadSidebarTextFilters();
57 },
58
59 // Prealod filters using search query
60 preloadSidebarTextFilters: function() {
61 var anchor = Utils.extractAnchor(document.location.hash);
62 if(!anchor || anchor.indexOf("q=") == -1) return;
63
64 var query = anchor.substring(2);
65 if(!query) return;
66
67 $(".nitdoc-ui-filter input:text")
68 .val(query)
69 .removeClass("nitdoc-ui-notused")
70 .trigger("keyup");
71 },
72
73 // Allow user to filter side bar box entries by Introduced/Refined/inHerited type
74 enableSidebarTypeFilters: function(filterSelector) {
75 var box = $(filterSelector);
76 var types = {};
77
78 box.find("li").each(function() {
79 var span = $(this).find("span:first");
80 if(!types[span.html()]) types[span.html()] = {
81 title: span.attr("title"),
82 class: $(this).attr("class")
83 }
84 });
85
86 for(var type in types) {
87 var a = $(document.createElement("a"))
88 .addClass("nitdoc-ui-filter-link")
89 .html(type)
90 .attr("title", "Hide " + types[type].title)
91 .attr("data-filter-class", types[type].class)
92 .toggle(
93 function() {
94 var hclass = $(this).attr("data-filter-class");
95 $(this).parents(filterSelector).find("li." + hclass).hide();
96 $(this).addClass("nitdoc-ui-filter-hidden")
97 },
98 function() {
99 var hclass = $(this).attr("data-filter-class");
100 $(this).parents(filterSelector).find("li." + hclass).show();
101 $(this).removeClass("nitdoc-ui-filter-hidden")
102 }
103 )
104 $(filterSelector).find(".nitdoc-ui-filter").append(a);
105 }
106 },
107
108 // Allow user to filter sidebar box entries by name
109 enableSearchPageField: function(filterSelector) {
110 var div = $(document.createElement("div"))
111 .addClass("nitdoc-ui-searchpage-filter")
112 .append(
113 $(document.createElement("input"))
114 .addClass("nitdoc-ui-searchpage-field")
115 .addClass("nitdoc-ui-filter-field-notused")
116 .attr("type", "text")
117 .attr("value", "filter...")
118 .keyup(function() {
119 var box = $(this).parents(".content.fullpage").find("article.filterable");
120 var value = $(this).val();
121 box.find("ul li:not(:icontains('" + value + "'))").hide();
122 box.find("ul li:icontains('" + value + "')").show();
123 })
124 .focusout(function() {
125 if($(this).val() == "") {
126 $(this).addClass("nitdoc-ui-filter-field-notused");
127 $(this).val("filter...");
128 }
129 })
130 .focusin(function() {
131 if($(this).val() == "filter...") {
132 $(this).removeClass("nitdoc-ui-filter-field-notused");
133 $(this).val("");
134 }
135 })
136 );
137 $(filterSelector).after(div);
138 this.preloadSearchPageField();
139 },
140
141 // Prealod filter using search query
142 preloadSearchPageField: function() {
143 var anchor = Utils.extractAnchor(document.location.hash);
144 if(!anchor || anchor.indexOf("q=") == -1) return;
145
146 var query = anchor.substring(2);
147 if(!query) return;
148
149 $(".nitdoc-ui-searchpage-field")
150 .val(query)
151 .removeClass("nitdoc-ui-notused")
152 .trigger("keyup");
153 }
154 };
155
156 Filtering.enableSidebarTextFilters("nav.filterable h3");
157 Filtering.enableSidebarTypeFilters("nav.filterable");
158 Filtering.enableSearchPageField(".content.fullpage h1:contains('Search')");