server/apps/files/js/search.js

150 lines
4.3 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.
*
*/
(function() {
/**
* Construct a new FileActions instance
* @constructs Files
*/
var Files = function() {
this.initialize();
};
/**
* @memberof OCA.Search
*/
Files.prototype = {
/**
* Initialize the file search
*/
initialize: function() {
OC.Plugins.register('OCA.Search', this);
},
attach: function(search) {
2014-12-31 13:28:01 +00:00
var self = this;
search.setFilter('files', function (query) {
2014-12-31 13:28:01 +00:00
if (self.fileAppLoaded()) {
OCA.Files.App.fileList.setFilter(query);
}
});
2014-12-31 13:28:01 +00:00
search.setRenderer('folder', this.renderFolderResult);
search.setRenderer('file', this.renderFileResult);
search.setRenderer('audio', this.renderAudioResult);
search.setRenderer('image', this.renderImageResult);
2014-12-31 13:28:01 +00:00
search.setHandler('folder', this.handleFolderClick);
search.setHandler(['file', 'audio', 'image'], this.handleFileClick);
},
renderFolderResult: function($row, result) {
2014-12-31 13:28:01 +00:00
if (this.inFileList($row, result)) {
2014-12-18 09:26:41 +00:00
return null;
}
/*render folder icon, show path beneath filename,
show size and last modified date on the right */
2014-12-31 13:28:01 +00:00
this.updateLegacyMimetype(result);
2014-12-18 09:26:41 +00:00
var $pathDiv = $('<div class="path"></div>').text(result.path);
$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') + ')');
2014-12-18 09:26:41 +00:00
return $row;
},
renderFileResult: function($row, result) {
2014-12-31 13:28:01 +00:00
if (this.inFileList($row, result)) {
2014-12-18 09:26:41 +00:00
return null;
}
/*render preview icon, show path beneath filename,
show size and last modified date on the right */
2014-12-31 13:28:01 +00:00
this.updateLegacyMimetype(result);
2014-12-04 12:43:38 +00:00
var $pathDiv = $('<div class="path"></div>').text(result.path);
2014-12-11 15:23:39 +00:00
$row.find('td.info div.name').after($pathDiv).text(result.name);
2014-12-04 12:43:38 +00:00
2014-12-11 15:23:39 +00:00
$row.find('td.result a').attr('href', result.link);
2014-12-04 12:43:38 +00:00
2014-12-31 13:28:01 +00:00
if (this.fileAppLoaded()) {
2014-12-11 15:23:39 +00:00
OCA.Files.App.fileList.lazyLoadPreview({
path: result.path,
mime: result.mime,
callback: function (url) {
$row.find('td.icon').css('background-image', 'url(' + url + ')');
}
});
} else {
2014-12-11 15:23:39 +00:00
// 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 = '/';
2011-07-30 19:18:54 +00:00
}
2014-12-11 15:23:39 +00:00
$row.find('td.info a').attr('href',
OC.generateUrl('/apps/files/?dir={dir}&scrollto={scrollto}', {dir: dir, scrollto: result.name})
);
2011-07-30 19:18:54 +00:00
}
2014-12-18 09:26:41 +00:00
return $row;
},
renderAudioResult: function($row, result) {
/*render preview icon, show path beneath filename,
show size and last modified date on the right
show Artist and Album */
2014-12-31 13:28:01 +00:00
$row = this.renderFileResult($row, result);
2014-12-18 23:24:46 +00:00
if ($row) {
$row.find('td.icon').css('background-image', 'url(' + OC.imagePath('core', 'filetypes/audio') + ')');
}
2014-12-18 09:26:41 +00:00
return $row;
},
renderImageResult: function($row, result) {
/*render preview icon, show path beneath filename,
show size and last modified date on the right
show width and height */
2014-12-31 13:28:01 +00:00
$row = this.renderFileResult($row, result);
if ($row && !this.fileAppLoaded()) {
2014-12-18 23:24:46 +00:00
$row.find('td.icon').css('background-image', 'url(' + OC.imagePath('core', 'filetypes/image') + ')');
}
2014-12-18 09:26:41 +00:00
return $row;
},
inFileList: function($row, result){
2014-12-31 13:28:01 +00:00
return this.fileAppLoaded() && OCA.Files.App.fileList.inList(result.name);
},
2014-12-18 23:24:46 +00:00
updateLegacyMimetype: function(result){
// backward compatibility:
if (!result.mime && result.mime_type) {
result.mime = result.mime_type;
}
},
handleFolderClick: function($row, result, event) {
// open folder
2014-12-31 13:28:01 +00:00
if (this.fileAppLoaded()) {
OCA.Files.App.fileList.changeDirectory(result.path);
2014-12-11 15:23:39 +00:00
return false;
} else {
return true;
}
},
handleFileClick: function($row, result, event) {
2014-12-31 13:28:01 +00:00
if (this.fileAppLoaded()) {
OCA.Files.App.fileList.changeDirectory(OC.dirname(result.path));
OCA.Files.App.fileList.scrollTo(result.name);
2014-12-11 15:23:39 +00:00
return false;
} else {
return true;
}
2014-12-18 09:26:41 +00:00
},
fileAppLoaded: function() {
2014-12-19 15:37:44 +00:00
return !!OCA.Files && !!OCA.Files.App;
}
};
2014-12-31 13:28:01 +00:00
new Files();
})();