2014-12-10 16:11:02 +00:00
|
|
|
/**
|
|
|
|
* ownCloud - core
|
|
|
|
*
|
|
|
|
* This file is licensed under the Affero General Public License version 3 or
|
|
|
|
* later. See the COPYING file.
|
|
|
|
*
|
2014-12-11 15:23:39 +00:00
|
|
|
* @author Jörn Friedrich Dreyer <jfd@owncloud.com>
|
|
|
|
* @copyright Jörn Friedrich Dreyer 2014
|
2014-12-10 16:11:02 +00:00
|
|
|
*/
|
|
|
|
|
2014-12-11 15:23:39 +00:00
|
|
|
(function () {
|
|
|
|
/**
|
|
|
|
* @class OCA.Search
|
|
|
|
* @classdesc
|
|
|
|
*
|
|
|
|
* The Search class manages a search queries and their results
|
|
|
|
*
|
|
|
|
* @param $searchBox container element with existing markup for the #searchbox form
|
2015-01-02 13:22:48 +00:00
|
|
|
* @param $searchResults container element for results und status message
|
2014-12-11 15:23:39 +00:00
|
|
|
*/
|
2015-01-02 13:22:48 +00:00
|
|
|
var Search = function($searchBox, $searchResults) {
|
|
|
|
this.initialize($searchBox, $searchResults);
|
2014-12-11 15:23:39 +00:00
|
|
|
};
|
|
|
|
/**
|
|
|
|
* @memberof OC
|
|
|
|
*/
|
|
|
|
Search.prototype = {
|
2014-12-10 16:11:02 +00:00
|
|
|
|
|
|
|
/**
|
2014-12-18 09:26:41 +00:00
|
|
|
* Initialize the search box
|
2014-12-11 15:23:39 +00:00
|
|
|
*
|
|
|
|
* @param $searchBox container element with existing markup for the #searchbox form
|
2015-01-02 14:44:41 +00:00
|
|
|
* @param $searchResults container element for results und status message
|
2014-12-11 15:23:39 +00:00
|
|
|
* @private
|
2014-12-10 16:11:02 +00:00
|
|
|
*/
|
2015-01-02 13:22:48 +00:00
|
|
|
initialize: function($searchBox, $searchResults) {
|
2014-12-11 15:23:39 +00:00
|
|
|
|
2014-12-30 23:13:09 +00:00
|
|
|
var self = this;
|
2014-12-11 15:23:39 +00:00
|
|
|
|
|
|
|
/**
|
2014-12-17 17:49:39 +00:00
|
|
|
* contains closures that are called to filter the current content
|
2014-12-11 15:23:39 +00:00
|
|
|
*/
|
2014-12-17 17:49:39 +00:00
|
|
|
var filters = {};
|
|
|
|
this.setFilter = function(type, filter) {
|
|
|
|
filters[type] = filter;
|
2014-12-11 15:23:39 +00:00
|
|
|
};
|
2014-12-17 17:49:39 +00:00
|
|
|
this.hasFilter = function(type) {
|
|
|
|
return typeof filters[type] !== 'undefined';
|
2014-12-11 15:23:39 +00:00
|
|
|
};
|
2014-12-17 17:49:39 +00:00
|
|
|
this.getFilter = function(type) {
|
|
|
|
return filters[type];
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* contains closures that are called to render search results
|
|
|
|
*/
|
|
|
|
var renderers = {};
|
|
|
|
this.setRenderer = function(type, renderer) {
|
|
|
|
renderers[type] = renderer;
|
|
|
|
};
|
|
|
|
this.hasRenderer = function(type) {
|
|
|
|
return typeof renderers[type] !== 'undefined';
|
|
|
|
};
|
|
|
|
this.getRenderer = function(type) {
|
|
|
|
return renderers[type];
|
2014-12-11 15:23:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* contains closures that are called when a search result has been clicked
|
|
|
|
*/
|
|
|
|
var handlers = {};
|
|
|
|
this.setHandler = function(type, handler) {
|
|
|
|
handlers[type] = handler;
|
|
|
|
};
|
|
|
|
this.hasHandler = function(type) {
|
|
|
|
return typeof handlers[type] !== 'undefined';
|
|
|
|
};
|
|
|
|
this.getHandler = function(type) {
|
|
|
|
return handlers[type];
|
|
|
|
};
|
|
|
|
|
|
|
|
var currentResult = -1;
|
|
|
|
var lastQuery = '';
|
2015-01-05 12:53:56 +00:00
|
|
|
var lastInApps = [];
|
2014-12-11 15:23:39 +00:00
|
|
|
var lastPage = 0;
|
|
|
|
var lastSize = 30;
|
2015-01-06 14:18:41 +00:00
|
|
|
var lastResults = [];
|
|
|
|
var timeoutID = null;
|
2014-12-11 15:23:39 +00:00
|
|
|
|
2014-12-19 15:39:49 +00:00
|
|
|
this.getLastQuery = function() {
|
|
|
|
return lastQuery;
|
|
|
|
};
|
|
|
|
|
2014-12-11 15:23:39 +00:00
|
|
|
/**
|
|
|
|
* Do a search query and display the results
|
|
|
|
* @param {string} query the search query
|
2015-06-08 18:18:41 +00:00
|
|
|
* @param inApps
|
|
|
|
* @param page
|
|
|
|
* @param size
|
2014-12-11 15:23:39 +00:00
|
|
|
*/
|
2015-01-06 14:18:41 +00:00
|
|
|
this.search = function(query, inApps, page, size) {
|
|
|
|
if (query) {
|
2015-01-13 15:42:04 +00:00
|
|
|
OC.addStyle('core/search','results');
|
2014-12-11 15:23:39 +00:00
|
|
|
if (typeof page !== 'number') {
|
2014-12-17 17:49:39 +00:00
|
|
|
page = 1;
|
2014-12-11 15:23:39 +00:00
|
|
|
}
|
|
|
|
if (typeof size !== 'number') {
|
|
|
|
size = 30;
|
|
|
|
}
|
2014-12-17 17:49:39 +00:00
|
|
|
if (typeof inApps !== 'object') {
|
|
|
|
var currentApp = getCurrentApp();
|
|
|
|
if(currentApp) {
|
|
|
|
inApps = [currentApp];
|
|
|
|
} else {
|
|
|
|
inApps = [];
|
|
|
|
}
|
|
|
|
}
|
2014-12-11 15:23:39 +00:00
|
|
|
// prevent double pages
|
2015-01-02 11:50:21 +00:00
|
|
|
if ($searchResults && query === lastQuery && page === lastPage && size === lastSize) {
|
2014-12-11 15:23:39 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-01-06 14:18:41 +00:00
|
|
|
window.clearTimeout(timeoutID);
|
|
|
|
timeoutID = window.setTimeout(function() {
|
|
|
|
lastQuery = query;
|
|
|
|
lastInApps = inApps;
|
|
|
|
lastPage = page;
|
|
|
|
lastSize = size;
|
2015-01-02 13:22:48 +00:00
|
|
|
|
2015-01-06 14:18:41 +00:00
|
|
|
//show spinner
|
|
|
|
$searchResults.removeClass('hidden');
|
2015-03-24 09:56:44 +00:00
|
|
|
$status.addClass('status');
|
2015-01-06 14:18:41 +00:00
|
|
|
$status.html(t('core', 'Searching other places')+'<img class="spinner" alt="search in progress" src="'+OC.webroot+'/core/img/loading.gif" />');
|
2015-01-02 14:44:41 +00:00
|
|
|
|
2015-01-06 14:18:41 +00:00
|
|
|
// do the actual search query
|
2015-01-13 15:42:04 +00:00
|
|
|
$.getJSON(OC.generateUrl('core/search'), {query:query, inApps:inApps, page:page, size:size }, function(results) {
|
2015-01-06 14:18:41 +00:00
|
|
|
lastResults = results;
|
|
|
|
if (page === 1) {
|
|
|
|
showResults(results);
|
|
|
|
} else {
|
|
|
|
addResults(results);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}, 500);
|
2014-12-11 15:23:39 +00:00
|
|
|
}
|
2015-01-06 14:18:41 +00:00
|
|
|
};
|
2014-12-17 17:49:39 +00:00
|
|
|
|
2015-01-02 13:22:48 +00:00
|
|
|
//TODO should be a core method, see https://github.com/owncloud/core/issues/12557
|
2014-12-17 17:49:39 +00:00
|
|
|
function getCurrentApp() {
|
2015-01-02 13:22:48 +00:00
|
|
|
var content = document.getElementById('content');
|
|
|
|
if (content) {
|
|
|
|
var classList = document.getElementById('content').className.split(/\s+/);
|
|
|
|
for (var i = 0; i < classList.length; i++) {
|
|
|
|
if (classList[i].indexOf('app-') === 0) {
|
|
|
|
return classList[i].substr(4);
|
|
|
|
}
|
2014-12-17 17:49:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-01-02 13:22:48 +00:00
|
|
|
var $status = $searchResults.find('#status');
|
2015-01-13 07:20:37 +00:00
|
|
|
// summaryAndStatusHeight is a constant
|
|
|
|
var summaryAndStatusHeight = 118;
|
2014-12-11 15:23:39 +00:00
|
|
|
|
2014-12-17 17:49:39 +00:00
|
|
|
function isStatusOffScreen() {
|
2015-06-08 18:18:41 +00:00
|
|
|
return $searchResults.position() &&
|
|
|
|
($searchResults.position().top + summaryAndStatusHeight > window.innerHeight);
|
2014-12-17 17:49:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function placeStatus() {
|
|
|
|
if (isStatusOffScreen()) {
|
|
|
|
$status.addClass('fixed');
|
|
|
|
} else {
|
|
|
|
$status.removeClass('fixed');
|
|
|
|
}
|
|
|
|
}
|
2014-12-11 15:23:39 +00:00
|
|
|
function showResults(results) {
|
2015-01-02 13:22:48 +00:00
|
|
|
lastResults = results;
|
|
|
|
$searchResults.find('tr.result').remove();
|
2015-01-05 12:11:50 +00:00
|
|
|
$searchResults.removeClass('hidden');
|
2015-01-02 13:22:48 +00:00
|
|
|
addResults(results);
|
2014-12-10 16:11:02 +00:00
|
|
|
}
|
2014-12-11 15:23:39 +00:00
|
|
|
function addResults(results) {
|
|
|
|
var $template = $searchResults.find('tr.template');
|
|
|
|
jQuery.each(results, function (i, result) {
|
|
|
|
var $row = $template.clone();
|
|
|
|
$row.removeClass('template');
|
|
|
|
$row.addClass('result');
|
2014-12-10 16:11:02 +00:00
|
|
|
|
2014-12-11 15:23:39 +00:00
|
|
|
$row.data('result', result);
|
2014-12-10 16:11:02 +00:00
|
|
|
|
2014-12-11 15:23:39 +00:00
|
|
|
// generic results only have four attributes
|
|
|
|
$row.find('td.info div.name').text(result.name);
|
|
|
|
$row.find('td.info a').attr('href', result.link);
|
2014-12-10 16:11:02 +00:00
|
|
|
|
2014-12-11 15:23:39 +00:00
|
|
|
/**
|
|
|
|
* Give plugins the ability to customize the search results. see result.js for examples
|
|
|
|
*/
|
2014-12-30 23:13:09 +00:00
|
|
|
if (self.hasRenderer(result.type)) {
|
|
|
|
$row = self.getRenderer(result.type)($row, result);
|
2014-12-11 15:23:39 +00:00
|
|
|
} else {
|
|
|
|
// for backward compatibility add text div
|
|
|
|
$row.find('td.info div.name').addClass('result');
|
|
|
|
$row.find('td.result div.name').after('<div class="text"></div>');
|
|
|
|
$row.find('td.result div.text').text(result.name);
|
|
|
|
if (OC.search.customResults && OC.search.customResults[result.type]) {
|
|
|
|
OC.search.customResults[result.type]($row, result);
|
|
|
|
}
|
|
|
|
}
|
2014-12-18 09:26:41 +00:00
|
|
|
if ($row) {
|
|
|
|
$searchResults.find('tbody').append($row);
|
|
|
|
}
|
2014-12-11 15:23:39 +00:00
|
|
|
});
|
2015-01-02 11:50:21 +00:00
|
|
|
var count = $searchResults.find('tr.result').length;
|
2015-01-02 15:22:23 +00:00
|
|
|
$status.data('count', count);
|
|
|
|
if (count === 0) {
|
2015-03-24 09:56:44 +00:00
|
|
|
$status.addClass('emptycontent').removeClass('status');
|
|
|
|
$status.html('');
|
|
|
|
$status.append('<div class="icon-search"></div>');
|
2015-09-18 10:19:07 +00:00
|
|
|
$status.append('<h2>' + t('core', 'No search results in other folders') + '</h2>');
|
2015-01-02 15:22:23 +00:00
|
|
|
} else {
|
2015-03-24 09:56:44 +00:00
|
|
|
$status.removeClass('emptycontent').addClass('status');
|
2015-09-18 10:19:07 +00:00
|
|
|
$status.text(n('core', '{count} search result in another folder', '{count} search results in other folders', count, {count:count}));
|
2015-01-02 15:22:23 +00:00
|
|
|
}
|
2014-12-11 15:23:39 +00:00
|
|
|
}
|
|
|
|
function renderCurrent() {
|
|
|
|
var result = $searchResults.find('tr.result')[currentResult];
|
|
|
|
if (result) {
|
|
|
|
var $result = $(result);
|
2015-01-05 13:28:09 +00:00
|
|
|
var currentOffset = $('#app-content').scrollTop();
|
2014-12-17 17:49:39 +00:00
|
|
|
$('#app-content').animate({
|
2014-12-11 15:23:39 +00:00
|
|
|
// Scrolling to the top of the new result
|
|
|
|
scrollTop: currentOffset + $result.offset().top - $result.height() * 2
|
|
|
|
}, {
|
|
|
|
duration: 100
|
|
|
|
});
|
|
|
|
$searchResults.find('tr.result.current').removeClass('current');
|
|
|
|
$result.addClass('current');
|
2014-12-10 16:11:02 +00:00
|
|
|
}
|
2014-12-11 15:23:39 +00:00
|
|
|
}
|
|
|
|
this.hideResults = function() {
|
2015-01-02 13:22:48 +00:00
|
|
|
$searchResults.addClass('hidden');
|
|
|
|
$searchResults.find('tr.result').remove();
|
|
|
|
lastQuery = false;
|
|
|
|
};
|
2015-01-05 16:53:14 +00:00
|
|
|
this.clear = function() {
|
|
|
|
self.hideResults();
|
|
|
|
if(self.hasFilter(getCurrentApp())) {
|
|
|
|
self.getFilter(getCurrentApp())('');
|
|
|
|
}
|
|
|
|
$searchBox.val('');
|
|
|
|
$searchBox.blur();
|
|
|
|
};
|
2015-01-02 13:22:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Event handler for when scrolling the list container.
|
|
|
|
* This appends/renders the next page of entries when reaching the bottom.
|
|
|
|
*/
|
2015-06-08 18:18:41 +00:00
|
|
|
function onScroll() {
|
2015-01-06 14:18:41 +00:00
|
|
|
if ($searchResults && lastQuery !== false && lastResults.length > 0) {
|
2015-01-05 12:53:56 +00:00
|
|
|
var resultsBottom = $searchResults.offset().top + $searchResults.height();
|
2015-06-08 18:18:41 +00:00
|
|
|
var containerBottom = $searchResults.offsetParent().offset().top +
|
|
|
|
$searchResults.offsetParent().height();
|
2015-01-05 12:53:56 +00:00
|
|
|
if ( resultsBottom < containerBottom * 1.2 ) {
|
|
|
|
self.search(lastQuery, lastInApps, lastPage + 1);
|
|
|
|
}
|
2015-01-02 13:22:48 +00:00
|
|
|
placeStatus();
|
2014-12-10 16:11:02 +00:00
|
|
|
}
|
2015-01-02 13:22:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$('#app-content').on('scroll', _.bind(onScroll, this));
|
|
|
|
|
|
|
|
/**
|
|
|
|
* scrolls the search results to the top
|
|
|
|
*/
|
|
|
|
function scrollToResults() {
|
|
|
|
setTimeout(function() {
|
|
|
|
if (isStatusOffScreen()) {
|
|
|
|
var newScrollTop = $('#app-content').prop('scrollHeight') - $searchResults.height();
|
|
|
|
console.log('scrolling to ' + newScrollTop);
|
|
|
|
$('#app-content').animate({
|
|
|
|
scrollTop: newScrollTop
|
|
|
|
}, {
|
|
|
|
duration: 100,
|
|
|
|
complete: function () {
|
|
|
|
scrollToResults();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}, 150);
|
|
|
|
}
|
|
|
|
|
|
|
|
$('form.searchbox').submit(function(event) {
|
|
|
|
event.preventDefault();
|
|
|
|
});
|
2014-12-11 15:23:39 +00:00
|
|
|
|
2015-06-08 18:18:41 +00:00
|
|
|
$searchBox.on('search', function () {
|
2015-01-02 14:44:41 +00:00
|
|
|
if($searchBox.val() === '') {
|
|
|
|
if(self.hasFilter(getCurrentApp())) {
|
|
|
|
self.getFilter(getCurrentApp())('');
|
|
|
|
}
|
|
|
|
self.hideResults();
|
|
|
|
}
|
|
|
|
});
|
2014-12-11 15:23:39 +00:00
|
|
|
$searchBox.keyup(function(event) {
|
|
|
|
if (event.keyCode === 13) { //enter
|
|
|
|
if(currentResult > -1) {
|
|
|
|
var result = $searchResults.find('tr.result a')[currentResult];
|
|
|
|
window.location = $(result).attr('href');
|
2014-12-10 16:11:02 +00:00
|
|
|
}
|
2014-12-11 15:23:39 +00:00
|
|
|
} else if(event.keyCode === 38) { //up
|
|
|
|
if(currentResult > 0) {
|
|
|
|
currentResult--;
|
|
|
|
renderCurrent();
|
|
|
|
}
|
|
|
|
} else if(event.keyCode === 40) { //down
|
|
|
|
if(lastResults.length > currentResult + 1){
|
|
|
|
currentResult++;
|
|
|
|
renderCurrent();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
var query = $searchBox.val();
|
|
|
|
if (lastQuery !== query) {
|
|
|
|
currentResult = -1;
|
|
|
|
if (query.length > 2) {
|
2014-12-30 23:13:09 +00:00
|
|
|
self.search(query);
|
2014-12-11 15:23:39 +00:00
|
|
|
} else {
|
2014-12-30 23:13:09 +00:00
|
|
|
self.hideResults();
|
2014-12-10 16:11:02 +00:00
|
|
|
}
|
2015-01-02 11:50:21 +00:00
|
|
|
if(self.hasFilter(getCurrentApp())) {
|
|
|
|
self.getFilter(getCurrentApp())(query);
|
|
|
|
}
|
2014-12-10 16:11:02 +00:00
|
|
|
}
|
|
|
|
}
|
2014-12-11 15:23:39 +00:00
|
|
|
});
|
2015-01-02 13:46:35 +00:00
|
|
|
$(document).keyup(function(event) {
|
|
|
|
if(event.keyCode === 27) { //esc
|
|
|
|
$searchBox.val('');
|
|
|
|
if(self.hasFilter(getCurrentApp())) {
|
|
|
|
self.getFilter(getCurrentApp())('');
|
|
|
|
}
|
|
|
|
self.hideResults();
|
|
|
|
}
|
|
|
|
});
|
2014-12-11 15:23:39 +00:00
|
|
|
|
2015-01-02 13:22:48 +00:00
|
|
|
$searchResults.on('click', 'tr.result', function (event) {
|
|
|
|
var $row = $(this);
|
|
|
|
var item = $row.data('result');
|
|
|
|
if(self.hasHandler(item.type)){
|
2015-02-17 20:48:10 +00:00
|
|
|
var result = self.getHandler(item.type)($row, item, event);
|
2015-01-02 13:22:48 +00:00
|
|
|
$searchBox.val('');
|
|
|
|
if(self.hasFilter(getCurrentApp())) {
|
|
|
|
self.getFilter(getCurrentApp())('');
|
2014-12-17 17:49:39 +00:00
|
|
|
}
|
2015-01-02 13:22:48 +00:00
|
|
|
self.hideResults();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
$searchResults.on('click', '#status', function (event) {
|
2014-12-11 15:23:39 +00:00
|
|
|
event.preventDefault();
|
2015-01-02 13:22:48 +00:00
|
|
|
scrollToResults();
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
placeStatus();
|
2014-12-17 17:49:39 +00:00
|
|
|
|
|
|
|
OC.Plugins.attach('OCA.Search', this);
|
2015-06-08 18:18:41 +00:00
|
|
|
|
|
|
|
// hide search file if search is not enabled
|
|
|
|
if(self.hasFilter(getCurrentApp())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ($searchResults.length === 0) {
|
|
|
|
$searchBox.hide();
|
|
|
|
}
|
2014-12-11 15:23:39 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
OCA.Search = Search;
|
|
|
|
})();
|
|
|
|
|
|
|
|
$(document).ready(function() {
|
2015-02-08 21:42:30 +00:00
|
|
|
var $searchResults = $('#searchresults');
|
2015-06-09 10:39:40 +00:00
|
|
|
if ($searchResults.length > 0) {
|
2015-02-08 21:42:30 +00:00
|
|
|
$searchResults.addClass('hidden');
|
|
|
|
$('#app-content')
|
|
|
|
.find('.viewcontainer').css('min-height', 'initial');
|
2015-06-09 10:39:40 +00:00
|
|
|
$searchResults.load(OC.webroot + '/core/search/templates/part.results.html', function () {
|
|
|
|
OC.Search = new OCA.Search($('#searchbox'), $('#searchresults'));
|
|
|
|
});
|
2015-02-08 21:42:30 +00:00
|
|
|
} else {
|
2015-06-09 13:46:15 +00:00
|
|
|
_.defer(function() {
|
|
|
|
OC.Search = new OCA.Search($('#searchbox'), $('#searchresults'));
|
|
|
|
});
|
2015-06-09 10:39:40 +00:00
|
|
|
}
|
2014-12-11 15:23:39 +00:00
|
|
|
});
|
2014-12-10 16:11:02 +00:00
|
|
|
|
|
|
|
/**
|
2014-12-17 17:49:39 +00:00
|
|
|
* @deprecated use get/setRenderer() instead
|
2014-12-10 16:11:02 +00:00
|
|
|
*/
|
|
|
|
OC.search.customResults = {};
|
|
|
|
/**
|
2014-12-17 17:49:39 +00:00
|
|
|
* @deprecated use get/setRenderer() instead
|
2014-12-10 16:11:02 +00:00
|
|
|
*/
|
2015-03-24 09:56:44 +00:00
|
|
|
OC.search.resultTypes = {};
|