##// END OF EJS Templates
files: fixed icons in file tree search
ergo -
r3472:bca1cba9 default
parent child Browse files
Show More
@@ -1,309 +1,310 b''
1 1 // # Copyright (C) 2010-2019 RhodeCode GmbH
2 2 // #
3 3 // # This program is free software: you can redistribute it and/or modify
4 4 // # it under the terms of the GNU Affero General Public License, version 3
5 5 // # (only), as published by the Free Software Foundation.
6 6 // #
7 7 // # This program is distributed in the hope that it will be useful,
8 8 // # but WITHOUT ANY WARRANTY; without even the implied warranty of
9 9 // # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 10 // # GNU General Public License for more details.
11 11 // #
12 12 // # You should have received a copy of the GNU Affero General Public License
13 13 // # along with this program. If not, see <http://www.gnu.org/licenses/>.
14 14 // #
15 15 // # This program is dual-licensed. If you wish to learn more about the
16 16 // # RhodeCode Enterprise Edition, including its added features, Support services,
17 17 // # and proprietary license terms, please see https://rhodecode.com/licenses/
18 18
19 19 /**
20 20 * Search file list
21 21 */
22 22 // global reference to file-node filter
23 23 var _NODEFILTER = {};
24 24
25 25 var fileBrowserListeners = function(node_list_url, url_base){
26 26 var n_filter = $('#node_filter').get(0);
27 27
28 28 _NODEFILTER.filterTimeout = null;
29 29 var nodes = null;
30 30
31 31 _NODEFILTER.fetchNodes = function(callback) {
32 32 $.ajax({url: node_list_url, headers: {'X-PARTIAL-XHR': true}})
33 33 .done(function(data){
34 34 nodes = data.nodes;
35 35 if (callback) {
36 36 callback();
37 37 }
38 38 })
39 39 .fail(function(data){
40 40 console.log('failed to load');
41 41 });
42 42 };
43 43
44 44 _NODEFILTER.fetchNodesCallback = function() {
45 45 $('#node_filter_box_loading').hide();
46 46 $('#node_filter_box').removeClass('hidden').show();
47 47 n_filter.focus();
48 48 if ($('#node_filter').hasClass('init')){
49 49 n_filter.value = '';
50 50 $('#node_filter').removeClass('init');
51 51 }
52 52 };
53 53
54 54 _NODEFILTER.initFilter = function(){
55 55 $('#node_filter_box_loading').removeClass('hidden').show();
56 56 $('#search_activate_id').hide();
57 57 $('#search_deactivate_id').removeClass('hidden').show();
58 58 $('#add_node_id').hide();
59 59 _NODEFILTER.fetchNodes(_NODEFILTER.fetchNodesCallback);
60 60 };
61 61
62 62 _NODEFILTER.resetFilter = function(){
63 63 $('#node_filter_box_loading').hide();
64 64 $('#node_filter_box').hide();
65 65 $('#search_activate_id').show();
66 66 $('#search_deactivate_id').hide();
67 67 $('#add_node_id').show();
68 68 $('#tbody').show();
69 69 $('#tbody_filtered').hide();
70 70 $('#node_filter').val('');
71 71 };
72 72
73 73 _NODEFILTER.fuzzy_match = function(filepath, query) {
74 74 var highlight = [];
75 75 var order = 0;
76 76 for (var i = 0; i < query.length; i++) {
77 77 var match_position = filepath.indexOf(query[i]);
78 78 if (match_position !== -1) {
79 79 var prev_match_position = highlight[highlight.length-1];
80 80 if (prev_match_position === undefined) {
81 81 highlight.push(match_position);
82 82 } else {
83 83 var current_match_position = prev_match_position + match_position + 1;
84 84 highlight.push(current_match_position);
85 85 order = order + current_match_position - prev_match_position;
86 86 }
87 87 filepath = filepath.substring(match_position+1);
88 88 } else {
89 89 return false;
90 90 }
91 91 }
92 92 return {'order': order,
93 93 'highlight': highlight};
94 94 };
95 95
96 96 _NODEFILTER.sortPredicate = function(a, b) {
97 97 if (a.order < b.order) return -1;
98 98 if (a.order > b.order) return 1;
99 99 if (a.filepath < b.filepath) return -1;
100 100 if (a.filepath > b.filepath) return 1;
101 101 return 0;
102 102 };
103 103
104 104 _NODEFILTER.updateFilter = function(elem, e) {
105 105 return function(){
106 106 // Reset timeout
107 107 _NODEFILTER.filterTimeout = null;
108 108 var query = elem.value.toLowerCase();
109 109 var match = [];
110 110 var matches_max = 20;
111 111 if (query !== ""){
112 112 var results = [];
113 113 for(var k=0;k<nodes.length;k++){
114 114 var result = _NODEFILTER.fuzzy_match(
115 115 nodes[k].name.toLowerCase(), query);
116 116 if (result) {
117 117 result.type = nodes[k].type;
118 118 result.filepath = nodes[k].name;
119 119 results.push(result);
120 120 }
121 121 }
122 122 results = results.sort(_NODEFILTER.sortPredicate);
123 123 var limit = matches_max;
124 124 if (results.length < matches_max) {
125 125 limit = results.length;
126 126 }
127 127 for (var i=0; i<limit; i++){
128 128 if(query && results.length > 0){
129 129 var n = results[i].filepath;
130 130 var t = results[i].type;
131 131 var n_hl = n.split("");
132 132 var pos = results[i].highlight;
133 133 for (var j = 0; j < pos.length; j++) {
134 134 n_hl[pos[j]] = "<em>" + n_hl[pos[j]] + "</em>";
135 135 }
136 136 n_hl = n_hl.join("");
137 137 var new_url = url_base.replace('__FPATH__',n);
138 138
139 139 var typeObj = {
140 dir: 'icon-folder browser-dir',
141 file: 'icon-file browser-file'
140 dir: 'icon-directory browser-dir',
141 file: 'icon-file-text browser-file'
142 142 };
143
143 144 var typeIcon = '<i class="{0}"></i>'.format(typeObj[t]);
144 match.push('<tr class="browser-result"><td><a class="browser-{0} pjax-link" href="{1}">{2}{3}</a></td><td colspan="5"></td></tr>'.format(t,new_url,typeIcon, n_hl));
145 match.push('<tr class="browser-result"><td><a class="pjax-link" href="{0}">{1}{2}</a></td><td colspan="5"></td></tr>'.format(new_url,typeIcon, n_hl));
145 146 }
146 147 }
147 148 if(results.length > limit){
148 149 var truncated_count = results.length - matches_max;
149 150 if (truncated_count === 1) {
150 151 match.push('<tr><td>{0} {1}</td><td colspan="5"></td></tr>'.format(truncated_count, _gettext('truncated result')));
151 152 } else {
152 153 match.push('<tr><td>{0} {1}</td><td colspan="5"></td></tr>'.format(truncated_count, _gettext('truncated results')));
153 154 }
154 155 }
155 156 }
156 157 if (query !== ""){
157 158 $('#tbody').hide();
158 159 $('#tbody_filtered').show();
159 160
160 161 if (match.length === 0){
161 162 match.push('<tr><td>{0}</td><td colspan="5"></td></tr>'.format(_gettext('No matching files')));
162 163 }
163 164 $('#tbody_filtered').html(match.join(""));
164 165 }
165 166 else{
166 167 $('#tbody').show();
167 168 $('#tbody_filtered').hide();
168 169 }
169 170
170 171 };
171 172 };
172 173
173 174 var scrollDown = function(element){
174 175 var elementBottom = element.offset().top + $(element).outerHeight();
175 176 var windowBottom = window.innerHeight + $(window).scrollTop();
176 177 if (elementBottom > windowBottom) {
177 178 var offset = elementBottom - window.innerHeight;
178 179 $('html,body').scrollTop(offset);
179 180 return false;
180 181 }
181 182 return true;
182 183 };
183 184
184 185 var scrollUp = function(element){
185 186 if (element.offset().top < $(window).scrollTop()) {
186 187 $('html,body').scrollTop(element.offset().top);
187 188 return false;
188 189 }
189 190 return true;
190 191 };
191 192
192 193 $('#filter_activate').click(function() {
193 194 _NODEFILTER.initFilter();
194 195 });
195 196
196 197 $('#filter_deactivate').click(function() {
197 198 _NODEFILTER.resetFilter();
198 199 });
199 200
200 201 $(n_filter).click(function() {
201 202 if ($('#node_filter').hasClass('init')){
202 203 n_filter.value = '';
203 204 $('#node_filter').removeClass('init');
204 205 }
205 206 });
206 207
207 208 $(n_filter).keydown(function(e) {
208 209 if (e.keyCode === 40){ // Down
209 210 if ($('.browser-highlight').length === 0){
210 211 $('.browser-result').first().addClass('browser-highlight');
211 212 } else {
212 213 var next = $('.browser-highlight').next();
213 214 if (next.length !== 0) {
214 215 $('.browser-highlight').removeClass('browser-highlight');
215 216 next.addClass('browser-highlight');
216 217 }
217 218 }
218 219 scrollDown($('.browser-highlight'));
219 220 }
220 221 if (e.keyCode === 38){ // Up
221 222 e.preventDefault();
222 223 if ($('.browser-highlight').length !== 0){
223 224 var next = $('.browser-highlight').prev();
224 225 if (next.length !== 0) {
225 226 $('.browser-highlight').removeClass('browser-highlight');
226 227 next.addClass('browser-highlight');
227 228 }
228 229 }
229 230 scrollUp($('.browser-highlight'));
230 231 }
231 232 if (e.keyCode === 13){ // Enter
232 233 if ($('.browser-highlight').length !== 0){
233 234 var url = $('.browser-highlight').find('.pjax-link').attr('href');
234 235 $.pjax({url: url, container: '#pjax-container', timeout: pjaxTimeout});
235 236 }
236 237 }
237 238 if (e.keyCode === 27){ // Esc
238 239 _NODEFILTER.resetFilter();
239 240 $('html,body').scrollTop(0);
240 241 }
241 242 });
242 243 var capture_keys = [40, 38, 39, 37, 13, 27];
243 244 $(n_filter).keyup(function(e) {
244 245 if ($.inArray(e.keyCode, capture_keys) === -1){
245 246 clearTimeout(_NODEFILTER.filterTimeout);
246 247 _NODEFILTER.filterTimeout = setTimeout(_NODEFILTER.updateFilter(n_filter, e),200);
247 248 }
248 249 });
249 250 };
250 251
251 252 var getIdentNode = function(n){
252 253 // iterate through nodes until matched interesting node
253 254 if (typeof n === 'undefined'){
254 255 return -1;
255 256 }
256 257 if(typeof n.id !== "undefined" && n.id.match('L[0-9]+')){
257 258 return n;
258 259 }
259 260 else{
260 261 return getIdentNode(n.parentNode);
261 262 }
262 263 };
263 264
264 265 var getSelectionLink = function(e) {
265 266 // get selection from start/to nodes
266 267 if (typeof window.getSelection !== "undefined") {
267 268 s = window.getSelection();
268 269
269 270 from = getIdentNode(s.anchorNode);
270 271 till = getIdentNode(s.focusNode);
271 272
272 273 f_int = parseInt(from.id.replace('L',''));
273 274 t_int = parseInt(till.id.replace('L',''));
274 275
275 276 if (f_int > t_int){
276 277 // highlight from bottom
277 278 offset = -35;
278 279 ranges = [t_int,f_int];
279 280 }
280 281 else{
281 282 // highligth from top
282 283 offset = 35;
283 284 ranges = [f_int,t_int];
284 285 }
285 286 // if we select more than 2 lines
286 287 if (ranges[0] !== ranges[1]){
287 288 if($('#linktt').length === 0){
288 289 hl_div = document.createElement('div');
289 290 hl_div.id = 'linktt';
290 291 }
291 292 hl_div.innerHTML = '';
292 293
293 294 anchor = '#L'+ranges[0]+'-'+ranges[1];
294 295 var link = document.createElement('a');
295 296 link.href = location.href.substring(0,location.href.indexOf('#'))+anchor;
296 297 link.innerHTML = _gettext('Selection link');
297 298 hl_div.appendChild(link);
298 299 $('#codeblock').append(hl_div);
299 300
300 301 var xy = $(till).offset();
301 302 $('#linktt').addClass('hl-tip-box tip-box');
302 303 $('#linktt').offset({top: xy.top + offset, left: xy.left});
303 304 $('#linktt').css('visibility','visible');
304 305 }
305 306 else{
306 307 $('#linktt').css('visibility','hidden');
307 308 }
308 309 }
309 310 };
General Comments 0
You need to be logged in to leave comments. Login now