1bb6e20666f41b89baa352f5bf33937e3af7f24c
[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 define([
25 "jquery",
26 'jQueryUI',
27 "utils"
28 ], function($, UI, Utils) {
29
30 var Filtering = {
31
32 // Allow user to filter sidebar box entries by name
33 enableSidebarTextFilters: function(filterSelector) {
34 var div = $(document.createElement("div"))
35 .addClass("nitdoc-ui-filter")
36 .append(
37 $(document.createElement("input"))
38 .addClass("nitdoc-ui-filter-field")
39 .addClass("nitdoc-ui-filter-field-notused")
40 .attr("type", "text")
41 .attr("value", "filter...")
42 .keyup(function() {
43 var box = $(this).parents("nav.filterable");
44 var value = $(this).val();
45 box.find("ul li:not(:icontains('" + value + "'))").hide();
46 box.find("ul li:icontains('" + value + "')").show();
47 })
48 .focusout(function() {
49 if($(this).val() == "") {
50 $(this).addClass("nitdoc-ui-filter-field-notused");
51 $(this).val("filter...");
52 }
53 })
54 .focusin(function() {
55 if($(this).val() == "filter...") {
56 $(this).removeClass("nitdoc-ui-filter-field-notused");
57 $(this).val("");
58 }
59 })
60 );
61 $(filterSelector).after(div);
62 this.preloadSidebarTextFilters();
63 },
64
65 // Prealod filters using search query
66 preloadSidebarTextFilters: function() {
67 var anchor = Utils.extractAnchor(document.location.hash);
68 if(!anchor || anchor.indexOf("q=") == -1) return;
69
70 var query = anchor.substring(2);
71 if(!query) return;
72
73 $(".nitdoc-ui-filter input:text")
74 .val(query)
75 .removeClass("nitdoc-ui-notused")
76 .trigger("keyup");
77 },
78
79 // Allow user to filter side bar box entries by Introduced/Refined/inHerited type
80 enableSidebarTypeFilters: function(filterSelector) {
81 var box = $(filterSelector);
82 var types = {};
83
84 box.find("li").each(function() {
85 var span = $(this).find("span:first");
86 if(!types[span.html()]) types[span.html()] = {
87 title: span.attr("title"),
88 class: $(this).attr("class")
89 }
90 });
91
92 for(var type in types) {
93 var a = $(document.createElement("a"))
94 .addClass("nitdoc-ui-filter-link")
95 .html(type)
96 .attr("title", "Hide " + types[type].title)
97 .attr("data-filter-class", types[type].class)
98 .toggle(
99 function() {
100 var hclass = $(this).attr("data-filter-class");
101 $(this).parents(filterSelector).find("li." + hclass).hide();
102 $(this).addClass("nitdoc-ui-filter-hidden")
103 },
104 function() {
105 var hclass = $(this).attr("data-filter-class");
106 $(this).parents(filterSelector).find("li." + hclass).show();
107 $(this).removeClass("nitdoc-ui-filter-hidden")
108 }
109 )
110 $(filterSelector).find(".nitdoc-ui-filter").append(a);
111 }
112 },
113
114 // Allow user to filter sidebar box entries by name
115 enableSearchPageField: function(filterSelector) {
116 var div = $(document.createElement("div"))
117 .addClass("nitdoc-ui-searchpage-filter")
118 .append(
119 $(document.createElement("input"))
120 .addClass("nitdoc-ui-searchpage-field")
121 .addClass("nitdoc-ui-filter-field-notused")
122 .attr("type", "text")
123 .attr("value", "filter...")
124 .keyup(function() {
125 var box = $(this).parents(".content.fullpage").find("article.filterable");
126 var value = $(this).val();
127 box.find("ul li:not(:icontains('" + value + "'))").hide();
128 box.find("ul li:icontains('" + value + "')").show();
129 })
130 .focusout(function() {
131 if($(this).val() == "") {
132 $(this).addClass("nitdoc-ui-filter-field-notused");
133 $(this).val("filter...");
134 }
135 })
136 .focusin(function() {
137 if($(this).val() == "filter...") {
138 $(this).removeClass("nitdoc-ui-filter-field-notused");
139 $(this).val("");
140 }
141 })
142 );
143 $(filterSelector).after(div);
144 this.preloadSearchPageField();
145 },
146
147 // Prealod filter using search query
148 preloadSearchPageField: function() {
149 var anchor = Utils.extractAnchor(document.location.hash);
150 if(!anchor || anchor.indexOf("q=") == -1) return;
151
152 var query = anchor.substring(2);
153 if(!query) return;
154
155 $(".nitdoc-ui-searchpage-field")
156 .val(query)
157 .removeClass("nitdoc-ui-notused")
158 .trigger("keyup");
159 }
160 };
161
162 Filtering.enableSidebarTextFilters("nav.filterable h3");
163 Filtering.enableSidebarTypeFilters("nav.filterable");
164 Filtering.enableSearchPageField(".content.fullpage h1:contains('Search')");
165
166 return Filtering;
167 });