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