fe479e2c43c928599462450a058dc56872620bda
[nit.git] / share / nitdoc / js / plugins / ui.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 UI module
21 *
22 * Enhance nitdoc usability with JS features
23 */
24 define([
25 "jquery",
26 "ZeroClipboard",
27 "plugins/utils"
28 ], function($, ZeroClipboard, utils) {
29
30 var UI = {
31
32 // Allow user to fold sidebar nav elements on click
33 enableFolding: function(containerSelector) {
34 var container = $(containerSelector);
35 var foldLink = $(document.createElement("a"))
36 .addClass("nitdoc-ui-fold")
37 .html("-");
38
39 container.find("nav h3")
40 .prepend(foldLink)
41 .css("cursor", "pointer")
42 .toggle(
43 function() {
44 $(this).find("a.nitdoc-ui-fold").html("+");
45 $(this).nextAll().toggle();
46 },
47 function() {
48 $(this).find("a.nitdoc-ui-fold").html("-");
49 $(this).nextAll().toggle();
50 }
51 );
52 },
53
54 // Allow user to copy signatures to clipboard with ZeroClipboard flahs plugin
55 // See: https://github.com/zeroclipboard/ZeroClipboard
56 enableCopyToClipboard: function(copySelector) {
57 $(copySelector).each(function() {
58 var btn = $(document.createElement("button"))
59 .addClass("nitdoc-ui-copy")
60 .attr("data-clipboard-text", $(this).attr("data-untyped-signature"))
61 .append(
62 $(document.createElement("img"))
63 .attr("src", './resources/icons/copy.png')
64 );
65 $(this).append(btn);
66 });
67
68 var clip = new ZeroClipboard($("button.nitdoc-ui-copy"), {
69 moviePath: "./ZeroClipboard.swf"
70 });
71 },
72
73 // Allow user to filter sidebar box entries by name
74 enableSidebarTextFilters: function(filterSelector) {
75 var div = $(document.createElement("div"))
76 .addClass("nitdoc-ui-filter")
77 .append(
78 $(document.createElement("input"))
79 .addClass("nitdoc-ui-filter-field")
80 .addClass("nitdoc-ui-filter-field-notused")
81 .attr("type", "text")
82 .attr("value", "filter...")
83 .keyup(function() {
84 var box = $(this).parents("nav.filterable");
85 var value = $(this).val();
86 box.find("ul li:not(:icontains('" + value + "'))").hide();
87 box.find("ul li:icontains('" + value + "')").show();
88 })
89 .focusout(function() {
90 if($(this).val() == "") {
91 $(this).addClass("nitdoc-ui-filter-field-notused");
92 $(this).val("filter...");
93 }
94 })
95 .focusin(function() {
96 if($(this).val() == "filter...") {
97 $(this).removeClass("nitdoc-ui-filter-field-notused");
98 $(this).val("");
99 }
100 })
101 );
102 $(filterSelector).after(div);
103 this.preloadSidebarTextFilters();
104 },
105
106 // Prealod filters using search query
107 preloadSidebarTextFilters: function() {
108 var anchor = utils.extractAnchor(document.location.hash);
109 if(!anchor || anchor.indexOf("q=") == -1) return;
110
111 var query = anchor.substring(2);
112 if(!query) return;
113
114 $(".nitdoc-ui-filter input:text")
115 .val(query)
116 .removeClass("nitdoc-ui-notused")
117 .trigger("keyup");
118 },
119
120 // Allow user to filter side bar box entries by Introduced/Refined/inHerited type
121 enableSidebarTypeFilters: function(filterSelector) {
122 var box = $(filterSelector);
123 var types = {};
124
125 box.find("li").each(function() {
126 var span = $(this).find("span:first");
127 if(!types[span.html()]) types[span.html()] = {
128 title: span.attr("title"),
129 class: $(this).attr("class")
130 }
131 });
132
133 for(var type in types) {
134 var a = $(document.createElement("a"))
135 .addClass("nitdoc-ui-filter-link")
136 .html(type)
137 .attr("title", "Hide " + types[type].title)
138 .attr("data-filter-class", types[type].class)
139 .toggle(
140 function() {
141 var hclass = $(this).attr("data-filter-class");
142 $(this).parents(filterSelector).find("li." + hclass).hide();
143 $(this).addClass("nitdoc-ui-filter-hidden")
144 },
145 function() {
146 var hclass = $(this).attr("data-filter-class");
147 $(this).parents(filterSelector).find("li." + hclass).show();
148 $(this).removeClass("nitdoc-ui-filter-hidden")
149 }
150 )
151 $(filterSelector).find(".nitdoc-ui-filter").append(a);
152 }
153 },
154
155 // Allow user to filter sidebar box entries by name
156 enableSearchPageField: function(filterSelector) {
157 var div = $(document.createElement("div"))
158 .addClass("nitdoc-ui-searchpage-filter")
159 .append(
160 $(document.createElement("input"))
161 .addClass("nitdoc-ui-searchpage-field")
162 .addClass("nitdoc-ui-filter-field-notused")
163 .attr("type", "text")
164 .attr("value", "filter...")
165 .keyup(function() {
166 var box = $(this).parents(".content.fullpage").find("article.filterable");
167 var value = $(this).val();
168 box.find("ul li:not(:icontains('" + value + "'))").hide();
169 box.find("ul li:icontains('" + value + "')").show();
170 })
171 .focusout(function() {
172 if($(this).val() == "") {
173 $(this).addClass("nitdoc-ui-filter-field-notused");
174 $(this).val("filter...");
175 }
176 })
177 .focusin(function() {
178 if($(this).val() == "filter...") {
179 $(this).removeClass("nitdoc-ui-filter-field-notused");
180 $(this).val("");
181 }
182 })
183 );
184 $(filterSelector).after(div);
185 this.preloadSearchPageField();
186 },
187
188 // Prealod filter using search query
189 preloadSearchPageField: function() {
190 var anchor = utils.extractAnchor(document.location.hash);
191 if(!anchor || anchor.indexOf("q=") == -1) return;
192
193 var query = anchor.substring(2);
194 if(!query) return;
195
196 $(".nitdoc-ui-searchpage-field")
197 .val(query)
198 .removeClass("nitdoc-ui-notused")
199 .trigger("keyup");
200 }
201 };
202
203 UI.enableFolding(".sidebar");
204 UI.enableCopyToClipboard(".signature");
205 UI.enableSidebarTextFilters("nav.filterable h3");
206 UI.enableSidebarTypeFilters("nav.filterable");
207 UI.enableSearchPageField(".content.fullpage h1:contains('Search')");
208 });