2014-10-24 10:50:54 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2014
|
|
|
|
*
|
|
|
|
* This file is licensed under the Affero General Public License version 3
|
|
|
|
* or later.
|
|
|
|
*
|
|
|
|
* See the COPYING-README file.
|
|
|
|
*
|
|
|
|
*/
|
2014-12-17 17:49:39 +00:00
|
|
|
(function() {
|
2014-12-30 23:14:02 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Construct a new FileActions instance
|
|
|
|
* @constructs Files
|
|
|
|
*/
|
|
|
|
var Files = function() {
|
|
|
|
this.initialize();
|
|
|
|
};
|
|
|
|
/**
|
|
|
|
* @memberof OCA.Search
|
|
|
|
*/
|
|
|
|
Files.prototype = {
|
|
|
|
|
2015-01-05 12:11:50 +00:00
|
|
|
fileList: null,
|
|
|
|
|
2014-12-30 23:14:02 +00:00
|
|
|
/**
|
|
|
|
* Initialize the file search
|
|
|
|
*/
|
|
|
|
initialize: function() {
|
2014-12-31 15:02:58 +00:00
|
|
|
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
this.fileAppLoaded = function() {
|
|
|
|
return !!OCA.Files && !!OCA.Files.App;
|
|
|
|
};
|
|
|
|
function inFileList($row, result) {
|
2015-01-05 13:28:09 +00:00
|
|
|
if (! self.fileAppLoaded()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
var dir = self.fileList.getCurrentDirectory().replace(/\/+$/,'');
|
|
|
|
var resultDir = OC.dirname(result.path);
|
|
|
|
return dir === resultDir && self.fileList.inList(result.name);
|
2014-12-31 15:02:58 +00:00
|
|
|
}
|
2015-01-02 13:22:48 +00:00
|
|
|
function updateLegacyMimetype(result) {
|
2014-12-31 15:02:58 +00:00
|
|
|
// backward compatibility:
|
|
|
|
if (!result.mime && result.mime_type) {
|
|
|
|
result.mime = result.mime_type;
|
|
|
|
}
|
|
|
|
}
|
2015-01-02 13:22:48 +00:00
|
|
|
function hideNoFilterResults() {
|
2015-01-06 13:34:35 +00:00
|
|
|
var $nofilterresults = $('.nofilterresults');
|
2015-01-02 11:50:21 +00:00
|
|
|
if ( ! $nofilterresults.hasClass('hidden') ) {
|
|
|
|
$nofilterresults.addClass('hidden');
|
|
|
|
}
|
|
|
|
}
|
2014-12-31 15:02:58 +00:00
|
|
|
|
|
|
|
this.renderFolderResult = function($row, result) {
|
|
|
|
if (inFileList($row, result)) {
|
|
|
|
return null;
|
|
|
|
}
|
2015-01-02 11:50:21 +00:00
|
|
|
hideNoFilterResults();
|
2014-12-31 15:02:58 +00:00
|
|
|
/*render folder icon, show path beneath filename,
|
|
|
|
show size and last modified date on the right */
|
|
|
|
this.updateLegacyMimetype(result);
|
|
|
|
|
2016-09-09 16:01:00 +00:00
|
|
|
var $pathDiv = $('<div class="path"></div>').text(result.path.substr(1));
|
2014-12-31 15:02:58 +00:00
|
|
|
$row.find('td.info div.name').after($pathDiv).text(result.name);
|
|
|
|
|
|
|
|
$row.find('td.result a').attr('href', result.link);
|
|
|
|
$row.find('td.icon').css('background-image', 'url(' + OC.imagePath('core', 'filetypes/folder') + ')');
|
|
|
|
return $row;
|
|
|
|
};
|
|
|
|
|
|
|
|
this.renderFileResult = function($row, result) {
|
|
|
|
if (inFileList($row, result)) {
|
|
|
|
return null;
|
|
|
|
}
|
2015-01-02 11:50:21 +00:00
|
|
|
hideNoFilterResults();
|
2014-12-31 15:02:58 +00:00
|
|
|
/*render preview icon, show path beneath filename,
|
|
|
|
show size and last modified date on the right */
|
|
|
|
this.updateLegacyMimetype(result);
|
|
|
|
|
2016-09-09 16:01:00 +00:00
|
|
|
var $pathDiv = $('<div class="path"></div>').text(result.path.substr(1));
|
2014-12-31 15:02:58 +00:00
|
|
|
$row.find('td.info div.name').after($pathDiv).text(result.name);
|
|
|
|
|
|
|
|
$row.find('td.result a').attr('href', result.link);
|
|
|
|
|
|
|
|
if (self.fileAppLoaded()) {
|
2015-01-05 12:11:50 +00:00
|
|
|
self.fileList.lazyLoadPreview({
|
2014-12-31 15:02:58 +00:00
|
|
|
path: result.path,
|
|
|
|
mime: result.mime,
|
|
|
|
callback: function (url) {
|
|
|
|
$row.find('td.icon').css('background-image', 'url(' + url + ')');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// FIXME how to get mime icon if not in files app
|
|
|
|
var mimeicon = result.mime.replace('/', '-');
|
|
|
|
$row.find('td.icon').css('background-image', 'url(' + OC.imagePath('core', 'filetypes/' + mimeicon) + ')');
|
|
|
|
var dir = OC.dirname(result.path);
|
|
|
|
if (dir === '') {
|
|
|
|
dir = '/';
|
|
|
|
}
|
|
|
|
$row.find('td.info a').attr('href',
|
|
|
|
OC.generateUrl('/apps/files/?dir={dir}&scrollto={scrollto}', {dir: dir, scrollto: result.name})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return $row;
|
|
|
|
};
|
|
|
|
|
|
|
|
this.renderAudioResult = function($row, result) {
|
|
|
|
/*render preview icon, show path beneath filename,
|
|
|
|
show size and last modified date on the right
|
|
|
|
show Artist and Album */
|
|
|
|
$row = this.renderFileResult($row, result);
|
|
|
|
if ($row) {
|
|
|
|
$row.find('td.icon').css('background-image', 'url(' + OC.imagePath('core', 'filetypes/audio') + ')');
|
|
|
|
}
|
|
|
|
return $row;
|
|
|
|
};
|
|
|
|
|
|
|
|
this.renderImageResult = function($row, result) {
|
|
|
|
/*render preview icon, show path beneath filename,
|
|
|
|
show size and last modified date on the right
|
|
|
|
show width and height */
|
|
|
|
$row = this.renderFileResult($row, result);
|
|
|
|
if ($row && !self.fileAppLoaded()) {
|
|
|
|
$row.find('td.icon').css('background-image', 'url(' + OC.imagePath('core', 'filetypes/image') + ')');
|
|
|
|
}
|
|
|
|
return $row;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
this.handleFolderClick = function($row, result, event) {
|
|
|
|
// open folder
|
2016-06-24 09:31:29 +00:00
|
|
|
if (self.fileAppLoaded() && self.fileList.id === 'files') {
|
2015-01-05 12:11:50 +00:00
|
|
|
self.fileList.changeDirectory(result.path);
|
2014-12-31 15:02:58 +00:00
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
this.handleFileClick = function($row, result, event) {
|
2016-06-24 09:31:29 +00:00
|
|
|
if (self.fileAppLoaded() && self.fileList.id === 'files') {
|
2015-01-05 12:11:50 +00:00
|
|
|
self.fileList.changeDirectory(OC.dirname(result.path));
|
|
|
|
self.fileList.scrollTo(result.name);
|
2014-12-31 15:02:58 +00:00
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-12-31 15:36:00 +00:00
|
|
|
this.updateLegacyMimetype = function (result) {
|
|
|
|
// backward compatibility:
|
|
|
|
if (!result.mime && result.mime_type) {
|
|
|
|
result.mime = result.mime_type;
|
|
|
|
}
|
|
|
|
};
|
2015-01-05 12:11:50 +00:00
|
|
|
this.setFileList = function (fileList) {
|
|
|
|
this.fileList = fileList;
|
|
|
|
};
|
2014-12-31 15:36:00 +00:00
|
|
|
|
2014-12-30 23:14:02 +00:00
|
|
|
OC.Plugins.register('OCA.Search', this);
|
|
|
|
},
|
2014-12-17 17:49:39 +00:00
|
|
|
attach: function(search) {
|
2014-12-31 13:28:01 +00:00
|
|
|
var self = this;
|
2014-12-17 17:49:39 +00:00
|
|
|
search.setFilter('files', function (query) {
|
2014-12-31 13:28:01 +00:00
|
|
|
if (self.fileAppLoaded()) {
|
2015-01-05 12:11:50 +00:00
|
|
|
self.fileList.setFilter(query);
|
2015-01-02 13:22:48 +00:00
|
|
|
if (query.length > 2) {
|
2015-01-02 15:13:15 +00:00
|
|
|
//search is not started until 500msec have passed
|
|
|
|
window.setTimeout(function() {
|
2015-01-06 13:34:35 +00:00
|
|
|
$('.nofilterresults').addClass('hidden');
|
2015-01-02 15:13:15 +00:00
|
|
|
}, 500);
|
2015-01-02 13:22:48 +00:00
|
|
|
}
|
2014-12-17 17:49:39 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-12-31 15:36:00 +00:00
|
|
|
search.setRenderer('folder', this.renderFolderResult.bind(this));
|
|
|
|
search.setRenderer('file', this.renderFileResult.bind(this));
|
|
|
|
search.setRenderer('audio', this.renderAudioResult.bind(this));
|
|
|
|
search.setRenderer('image', this.renderImageResult.bind(this));
|
2014-12-17 17:49:39 +00:00
|
|
|
|
2014-12-31 15:36:00 +00:00
|
|
|
search.setHandler('folder', this.handleFolderClick.bind(this));
|
|
|
|
search.setHandler(['file', 'audio', 'image'], this.handleFileClick.bind(this));
|
2016-06-24 09:32:14 +00:00
|
|
|
|
|
|
|
if (self.fileAppLoaded()) {
|
|
|
|
// hide results when switching directory outside of search results
|
|
|
|
$('#app-content').delegate('>div', 'changeDirectory', function() {
|
|
|
|
search.clear();
|
|
|
|
});
|
|
|
|
}
|
2014-12-17 17:49:39 +00:00
|
|
|
}
|
|
|
|
};
|
2015-01-02 09:57:25 +00:00
|
|
|
OCA.Search.Files = Files;
|
2015-01-05 12:11:50 +00:00
|
|
|
OCA.Search.files = new Files();
|
2014-12-17 17:49:39 +00:00
|
|
|
})();
|