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