nextcloud/search/js/result.js

139 lines
3.8 KiB
JavaScript
Raw Normal View History

/*
* Copyright (c) 2014
*
* This file is licensed under the Affero General Public License version 3
* or later.
*
* See the COPYING-README file.
*
*/
//translations for result type ids, can be extended by apps
OC.search.resultTypes={
file: t('core','File'),
folder: t('core','Folder'),
image: t('core','Image'),
audio: t('core','Audio')
};
2011-07-30 23:18:54 +04:00
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;
};
2011-07-30 23:18:54 +04:00
OC.search.hide=function(){
$('#searchresults').hide();
if($('#searchbox').val().length>2){
$('#searchbox').val('');
if (FileList && typeof FileList.unfilter === 'function') { //TODO add hook system
FileList.unfilter();
}
2011-07-30 23:18:54 +04:00
};
if ($('#searchbox').val().length === 0) {
if (FileList && typeof FileList.unfilter === 'function') { //TODO add hook system
FileList.unfilter();
}
}
};
2011-07-30 23:18:54 +04:00
OC.search.showResults=function(results){
if(results.length === 0){
return;
}
2011-07-30 23:18:54 +04:00
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();
2011-07-30 23:18:54 +04:00
event.stopPropagation();
});
2013-02-21 20:38:25 +04:00
$(document).click(function(event){
2011-07-30 23:18:54 +04:00
OC.search.hide();
if (FileList && typeof FileList.unfilter === 'function') { //TODO add hook system
FileList.unfilter();
}
2011-07-30 23:18:54 +04:00
});
2011-07-31 06:03:48 +04:00
OC.search.lastResults=results;
2011-07-30 23:18:54 +04:00
OC.search.showResults(results);
});
}else{
var types=OC.search.catagorizeResults(results);
$('#searchresults').show();
$('#searchresults tr.result').remove();
2011-07-31 06:03:48 +04:00
var index=0;
for(var typeid in types){
var type=types[typeid];
2011-07-30 23:18:54 +04:00
if(type.length>0){
2012-06-22 19:43:57 +04:00
for(var i=0;i<type.length;i++){
2011-07-30 23:18:54 +04:00
var row=$('#searchresults tr.template').clone();
row.removeClass('template');
row.addClass('result');
2013-09-19 18:15:17 +04:00
row.data('type', typeid);
row.data('name', type[i].name);
row.data('text', type[i].text);
2013-09-19 18:15:17 +04:00
row.data('index',index);
if (i === 0){
var typeName = OC.search.resultTypes[typeid];
row.children('td.type').text(t('lib', typeName));
2012-06-22 19:43:57 +04:00
}
2011-07-30 23:18:54 +04:00
row.find('td.result div.name').text(type[i].name);
row.find('td.result div.text').text(type[i].text);
2013-09-19 18:15:17 +04:00
if (type[i].path) {
var parent = OC.dirname(type[i].path);
if (parent === '') {
parent = '/';
}
2013-09-19 18:15:17 +04:00
var containerName = OC.basename(parent);
if (containerName === '') {
containerName = '/';
}
var containerLink = OC.linkTo('files', 'index.php')
2014-09-04 15:58:37 +04:00
+'/?dir='+encodeURIComponent(parent)
+'&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);
}
2013-09-19 18:15:17 +04:00
2011-07-31 06:03:48 +04:00
index++;
/**
* Give plugins the ability to customize the search results. For example:
* OC.search.customResults.file = function (row, item){
* if(item.name.search('.json') >= 0) ...
* };
*/
2013-09-19 18:15:17 +04:00
if(OC.search.customResults[typeid]){
OC.search.customResults[typeid](row, type[i]);
}
2011-07-30 23:18:54 +04:00
$('#searchresults tbody').append(row);
}
}
}
$('#searchresults').on('click', 'result', function () {
if ($(this).data('type') === 'Files') {
2013-09-17 20:45:38 +04:00
//FIXME use ajax to navigate to folder & highlight file
}
});
2011-07-30 23:18:54 +04:00
}
};
2011-07-30 23:18:54 +04:00
OC.search.showResults.loaded=false;
2011-07-31 06:03:48 +04:00
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');
}
};