Blame view

sources/search/js/result.js 3.46 KB
03e52840d   Kload   Init
1
2
3
4
5
6
7
8
9
10
  OC.search.catagorizeResults=function(results){
  	var types={};
  	for(var i=0;i<results.length;i++){
  		var type=results[i].type;
  		if(!types[type]){
  			types[type]=[];
  		}
  		types[type].push(results[i]);
  	}
  	return types;
31b7f2792   Kload   Upgrade to ownclo...
11
  };
03e52840d   Kload   Init
12
13
14
15
  OC.search.hide=function(){
  	$('#searchresults').hide();
  	if($('#searchbox').val().length>2){
  		$('#searchbox').val('');
31b7f2792   Kload   Upgrade to ownclo...
16
17
18
  		if (FileList && typeof FileList.unfilter === 'function') { //TODO add hook system
  			FileList.unfilter();
  		}
03e52840d   Kload   Init
19
  	};
31b7f2792   Kload   Upgrade to ownclo...
20
21
22
23
24
25
  	if ($('#searchbox').val().length === 0) {
  		if (FileList && typeof FileList.unfilter === 'function') { //TODO add hook system
  			FileList.unfilter();
  		}
  	}
  };
03e52840d   Kload   Init
26
  OC.search.showResults=function(results){
31b7f2792   Kload   Upgrade to ownclo...
27
  	if(results.length === 0){
03e52840d   Kload   Init
28
29
30
31
32
33
34
35
36
37
38
39
40
  		return;
  	}
  	if(!OC.search.showResults.loaded){
  		var parent=$('<div/>');
  		$('body').append(parent);
  		parent.load(OC.filePath('search','templates','part.results.php'),function(){
  			OC.search.showResults.loaded=true;
  			$('#searchresults').click(function(event){
  				OC.search.hide();
  				event.stopPropagation();
  			});
  			$(document).click(function(event){
  				OC.search.hide();
31b7f2792   Kload   Upgrade to ownclo...
41
42
43
  				if (FileList && typeof FileList.unfilter === 'function') { //TODO add hook system
  					FileList.unfilter();
  				}
03e52840d   Kload   Init
44
45
46
47
48
49
50
51
52
  			});
  			OC.search.lastResults=results;
  			OC.search.showResults(results);
  		});
  	}else{
  		var types=OC.search.catagorizeResults(results);
  		$('#searchresults').show();
  		$('#searchresults tr.result').remove();
  		var index=0;
31b7f2792   Kload   Upgrade to ownclo...
53
54
  		for(var typeid in types){
  			var type=types[typeid];
03e52840d   Kload   Init
55
56
57
58
59
  			if(type.length>0){
  				for(var i=0;i<type.length;i++){
  					var row=$('#searchresults tr.template').clone();
  					row.removeClass('template');
  					row.addClass('result');
6d9380f96   Cédric Dupont   Update sources OC...
60
  					
31b7f2792   Kload   Upgrade to ownclo...
61
62
63
  					row.data('type', typeid);
  					row.data('name', type[i].name);
  					row.data('text', type[i].text);
6d9380f96   Cédric Dupont   Update sources OC...
64
65
  					row.data('index',index);
  					
31b7f2792   Kload   Upgrade to ownclo...
66
  					if (i === 0){
6d9380f96   Cédric Dupont   Update sources OC...
67
68
  						var typeName = OC.search.resultTypes[typeid];
  						row.children('td.type').text(t('lib', typeName));
03e52840d   Kload   Init
69
  					}
03e52840d   Kload   Init
70
71
  					row.find('td.result div.name').text(type[i].name);
  					row.find('td.result div.text').text(type[i].text);
6d9380f96   Cédric Dupont   Update sources OC...
72
73
74
75
76
77
78
  					
  					if (type[i].path) {
  						var parent = OC.dirname(type[i].path);
  						if (parent === '') {
  							parent = '/';
  						}
  						var containerName = OC.basename(parent);
31b7f2792   Kload   Upgrade to ownclo...
79
80
81
82
  						if (containerName === '') {
  							containerName = '/';
  						}
  						var containerLink = OC.linkTo('files', 'index.php')
6d9380f96   Cédric Dupont   Update sources OC...
83
  							+'?dir='+encodeURIComponent(parent)
31b7f2792   Kload   Upgrade to ownclo...
84
85
86
87
88
89
90
  							+'&scrollto='+encodeURIComponent(type[i].name);
  						row.find('td.result a')
  							.attr('href', containerLink)
  							.attr('title', t('core', 'Show in {folder}', {folder: containerName}));
  					} else {
  						row.find('td.result a').attr('href', type[i].link);
  					}
6d9380f96   Cédric Dupont   Update sources OC...
91
  					
03e52840d   Kload   Init
92
  					index++;
6d9380f96   Cédric Dupont   Update sources OC...
93
94
95
96
97
98
99
100
  					/** 
  					 * Give plugins the ability to customize the search results. For example:
  					 * OC.search.customResults.file = function (row, item){
  				 	 *  if(item.name.search('.json') >= 0) ...
  					 * };
  					 */
  					if(OC.search.customResults[typeid]){
  						OC.search.customResults[typeid](row, type[i]);
03e52840d   Kload   Init
101
102
103
104
105
  					}
  					$('#searchresults tbody').append(row);
  				}
  			}
  		}
31b7f2792   Kload   Upgrade to ownclo...
106
107
108
109
110
  		$('#searchresults').on('click', 'result', function () {
  			if ($(this).data('type') === 'Files') {
  				//FIXME use ajax to navigate to folder & highlight file
  			}
  		});
03e52840d   Kload   Init
111
  	}
31b7f2792   Kload   Upgrade to ownclo...
112
  };
03e52840d   Kload   Init
113
114
115
116
117
118
119
120
  OC.search.showResults.loaded=false;
  
  OC.search.renderCurrent=function(){
  	if($('#searchresults tr.result')[OC.search.currentResult]){
  		var result=$('#searchresults tr.result')[OC.search.currentResult];
  		$('#searchresults tr.result').removeClass('current');
  		$(result).addClass('current');
  	}
6d9380f96   Cédric Dupont   Update sources OC...
121
  };