diff options
author | Jörn Friedrich Dreyer <jfd@butonic.de> | 2014-12-10 17:11:02 +0100 |
---|---|---|
committer | Jörn Friedrich Dreyer <jfd@butonic.de> | 2015-01-02 10:28:41 +0100 |
commit | 088323aad5de7b2fadd3c26a10cf4bdf51b5ae73 (patch) | |
tree | e2d531409d9514b61319d1fae0b8e741813b4b0b | |
parent | d3662722f66eb5b97593922a11e5357eff0fb042 (diff) | |
download | nextcloud-server-088323aad5de7b2fadd3c26a10cf4bdf51b5ae73.tar.gz nextcloud-server-088323aad5de7b2fadd3c26a10cf4bdf51b5ae73.zip |
move code from js.js to search.js in the search app
-rw-r--r-- | core/js/js.js | 61 | ||||
-rw-r--r-- | lib/base.php | 1 | ||||
-rw-r--r-- | search/js/result.js | 49 | ||||
-rw-r--r-- | search/js/search.js | 116 |
4 files changed, 145 insertions, 82 deletions
diff --git a/core/js/js.js b/core/js/js.js index 9d62b7cac1e..60237a7fe83 100644 --- a/core/js/js.js +++ b/core/js/js.js @@ -308,22 +308,9 @@ var OC={ * Do a search query and display the results * @param {string} query the search query */ - search: _.debounce(function(query){ - if(query){ - OC.addStyle('search','results'); - var classList = document.getElementById('content').className.split(/\s+/); - var inApps = []; - for (var i = 0; i < classList.length; i++) { - if (classList[i].indexOf('app-') === 0) { - var inApps = [classList[i].substr(4)]; - } - } - $.getJSON(OC.generateUrl('search/ajax/search.php'), {inApps:inApps, query:query}, function(results){ - OC.Search.lastResults=results; - OC.Search.showResults(results); - }); - } - }, 500), + search: function (query) { + OC.Search.search(query) + }, /** * Dialog helper for jquery dialogs. * @@ -1075,48 +1062,6 @@ function initCore() { }else{ SVGSupport.checkMimeType(); } - $('form.searchbox').submit(function(event){ - event.preventDefault(); - }); - $('#searchbox').keyup(function(event){ - if(event.keyCode===13){//enter - if(OC.Search.currentResult>-1){ - var result=$('#searchresults tr.result a')[OC.Search.currentResult]; - window.location = $(result).attr('href'); - } - }else if(event.keyCode===38){//up - if(OC.Search.currentResult>0){ - OC.Search.currentResult--; - OC.Search.renderCurrent(); - } - }else if(event.keyCode===40){//down - if(OC.Search.lastResults.length>OC.Search.currentResult+1){ - OC.Search.currentResult++; - OC.Search.renderCurrent(); - } - }else if(event.keyCode===27){//esc - OC.Search.hide(); - if (FileList && typeof FileList.unfilter === 'function') { //TODO add hook system - FileList.unfilter(); - } - }else{ - var query=$('#searchbox').val(); - if(OC.Search.lastQuery!==query){ - OC.Search.lastQuery=query; - OC.Search.currentResult=-1; - if (FileList && typeof FileList.filter === 'function') { //TODO add hook system - FileList.filter(query); - } - if(query.length>2){ - OC.search(query); - }else{ - if(OC.Search.hide){ - OC.Search.hide(); - } - } - } - } - }); // user menu $('#settings #expand').keydown(function(event) { diff --git a/lib/base.php b/lib/base.php index 009732ead7b..551d0afbd78 100644 --- a/lib/base.php +++ b/lib/base.php @@ -362,6 +362,7 @@ class OC { OC_Util::addScript("eventsource"); OC_Util::addScript("config"); //OC_Util::addScript( "multiselect" ); + OC_Util::addScript('search', 'search'); OC_Util::addScript('search', 'result'); OC_Util::addScript("oc-requesttoken"); OC_Util::addScript("apps"); diff --git a/search/js/result.js b/search/js/result.js index 3d3cbd56aea..95526749c53 100644 --- a/search/js/result.js +++ b/search/js/result.js @@ -8,14 +8,7 @@ * */ -//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') -}; -OC.Search.hide=function(){ +OC.Search.hide = function(){ $('#searchresults').hide(); if($('#searchbox').val().length>2){ $('#searchbox').val(''); @@ -29,15 +22,15 @@ OC.Search.hide=function(){ } } }; -OC.Search.showResults=function(results){ +OC.Search.showResults = function(results){ if(results.length === 0){ return; } - if(!OC.Search.showResults.loaded){ - var parent=$('<div class="searchresults-wrapper"/>'); + if (!OC.Search.showResults.loaded){ + var parent = $('<div class="searchresults-wrapper"/>'); $('#app-content').append(parent); parent.load(OC.filePath('search','templates','part.results.php'),function(){ - OC.Search.showResults.loaded=true; + OC.Search.showResults.loaded = true; $('#searchresults').click(function(event){ OC.Search.hide(); event.stopPropagation(); @@ -68,19 +61,18 @@ OC.Search.showResults=function(results){ $row.find('td.icon').css('background-image', 'url(' + OC.imagePath('core', 'places/link') + ')'); /** * Give plugins the ability to customize the search results. For example: - * OC.search.customResults.file = function (row, item){ + * OC.search.customResults.file = function (row, item){ FIXME * if(item.name.search('.json') >= 0) ... * }; */ - if(OC.Search.hasFormatter(result.type)){ + if (OC.Search.hasFormatter(result.type)) { OC.Search.getFormatter(result.type)($row, result); - } else - { + } 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[result.type]){ + if(OC.search.customResults && OC.search.customResults[result.type]) { OC.search.customResults[result.type]($row, result); } } @@ -99,13 +91,22 @@ OC.Search.showResults=function(results){ }); } }; -OC.Search.showResults.loaded=false; +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'); +OC.Search.renderCurrent = function(){ + var $resultsContainer = $('#searchresults'); + var result = $resultsContainer.find('tr.result')[OC.Search.currentResult] + if (result) { + var $result = $(result); + var currentOffset = $resultsContainer.scrollTop(); + $resultsContainer.animate({ + // Scrolling to the top of the new result + scrollTop: currentOffset + $result.offset().top - $result.height() * 2 + }, { + duration: 100 + }); + $resultsContainer.find('tr.result.current').removeClass('current'); + $result.addClass('current'); } }; @@ -117,7 +118,7 @@ OC.Search.setFormatter('file', function ($row, result) { result.mime = result.mime_type; } - $pathDiv = $('<div class="path"></div>').text(result.path) + $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); diff --git a/search/js/search.js b/search/js/search.js new file mode 100644 index 00000000000..372d1d7a055 --- /dev/null +++ b/search/js/search.js @@ -0,0 +1,116 @@ +/** + * ownCloud - core + * + * This file is licensed under the Affero General Public License version 3 or + * later. See the COPYING file. + * + * @author Bernhard Posselt <dev@bernhard-posselt.com> + * @copyright Bernhard Posselt 2014 + */ + +(function (exports) { + + 'use strict'; + + exports.Search = { + /** + * contains closures that are called to format search results + */ + formatter:{}, + setFormatter: function(type, formatter) { + this.formatter[type] = formatter; + }, + hasFormatter: function(type) { + return typeof this.formatter[type] !== 'undefined'; + }, + getFormatter: function(type) { + return this.formatter[type]; + }, + /** + * contains closures that are called when a search result has been clicked + */ + handler:{}, + setHandler: function(type, handler) { + this.handler[type] = handler; + }, + hasHandler: function(type) { + return typeof this.handler[type] !== 'undefined'; + }, + getHandler: function(type) { + return this.handler[type]; + }, + currentResult:-1, + lastQuery:'', + lastResults:{}, + /** + * Do a search query and display the results + * @param {string} query the search query + */ + search: _.debounce(function(query) { + if(query) { + exports.addStyle('search','results'); + $.getJSON(exports.filePath('search','ajax','search.php')+'?query=' + encodeURIComponent(query), function(results) { + exports.Search.lastResults = results; + exports.Search.showResults(results); + }); + } + }, 500) + }; + + + $(document).ready(function () { + $('form.searchbox').submit(function(event) { + event.preventDefault(); + }); + $('#searchbox').keyup(function(event) { + if (event.keyCode === 13) { //enter + if(exports.Search.currentResult > -1) { + var result = $('#searchresults tr.result a')[exports.Search.currentResult]; + window.location = $(result).attr('href'); + } + } else if(event.keyCode === 38) { //up + if(exports.Search.currentResult > 0) { + exports.Search.currentResult--; + exports.Search.renderCurrent(); + + } + } else if(event.keyCode === 40) { //down + if(exports.Search.lastResults.length > exports.Search.currentResult + 1){ + exports.Search.currentResult++; + exports.Search.renderCurrent(); + } + } else if(event.keyCode === 27) { //esc + exports.Search.hide(); + if (FileList && typeof FileList.unfilter === 'function') { //TODO add hook system + FileList.unfilter(); + } + } else { + var query = $('#searchbox').val(); + if (exports.Search.lastQuery !== query) { + exports.Search.lastQuery = query; + exports.Search.currentResult = -1; + if (FileList && typeof FileList.filter === 'function') { //TODO add hook system + FileList.filter(query); + } + if (query.length > 2) { + exports.Search.search(query); + } else { + if (exports.Search.hide) { + exports.Search.hide(); + } + } + } + } + }); + }); + +}(OC)); + +/** + * @deprecated use get/setFormatter() instead + */ +OC.search.customResults = {}; +/** + * @deprecated use get/setFormatter() instead + */ +OC.search.resultTypes = {};
\ No newline at end of file |