diff --git a/app/assets/javascripts/app/controllers/navigation.coffee b/app/assets/javascripts/app/controllers/navigation.coffee index 705b97ddb..b4a76109e 100644 --- a/app/assets/javascripts/app/controllers/navigation.coffee +++ b/app/assets/javascripts/app/controllers/navigation.coffee @@ -3,24 +3,26 @@ class App.Navigation extends App.ControllerWidgetPermanent elements: '#global-search': 'searchInput' - '#global-search-result': 'searchResult' + '.js-global-search-result': 'searchResult' '.search': 'searchContainer' events: 'click .js-toggleNotifications': 'toggleNotifications' 'click .js-emptySearch': 'emptyAndClose' - 'dblclick .search-holder .icon-magnifier': 'openExtendedSearch' 'submit form.search-holder': 'preventDefault' 'focus #global-search': 'searchFocus' 'blur #global-search': 'searchBlur' 'keydown #global-search': 'listNavigate' - 'click #global-search-result': 'andClose' + 'click .js-global-search-result': 'andClose' 'change .js-menu .js-switch input': 'switch' + 'click .js-details-link': 'openExtendedSearch' constructor: -> super @render() + @throttledSearch = _.throttle @search, 200 + # rerender view, e. g. on langauge change @bind 'ui:rerender', => @renderMenu() @@ -195,7 +197,7 @@ class App.Navigation extends App.ControllerWidgetPermanent @query = '' # reset query cache @searchContainer.addClass('focused') @anyPopoversDestroy() - @searchFunction(0) + @search() searchBlur: (e) => @@ -220,7 +222,7 @@ class App.Navigation extends App.ControllerWidgetPermanent @nudge(e, 1) return else if e.keyCode is 13 # enter - href = @$('#global-search-result .nav-tab.is-hover').attr('href') + href = @$('.global-search-result .nav-tab.is-hover').attr('href') return if !href @locationExecute(href) @emptyAndClose() @@ -228,7 +230,7 @@ class App.Navigation extends App.ControllerWidgetPermanent return # on other keys, show result - @searchFunction(200) + @throttledSearch() nudge: (e, position) => @@ -266,60 +268,57 @@ class App.Navigation extends App.ControllerWidgetPermanent @searchContainer.removeClass('open') @delay(@anyPopoversDestroy, 100, 'removePopovers') - searchFunction: (delay) => + search: => + query = @searchInput.val().trim() + return if !query + return if query is @query + @query = query + @searchContainer.toggleClass('filled', !!@query) - search = => - query = @searchInput.val().trim() - return if !query - return if query is @query - @query = query - @searchContainer.toggleClass('filled', !!@query) + # use cache for search result + if @searchResultCache[@query] + @renderResult(@searchResultCache[@query].result) + currentTime = new Date + return if @searchResultCache[@query].time > currentTime.setSeconds(currentTime.getSeconds() - 20) - # use cache for search result - if @searchResultCache[@query] - @renderResult(@searchResultCache[@query].result) - currentTime = new Date - return if @searchResultCache[@query].time > currentTime.setSeconds(currentTime.getSeconds() - 20) - - App.Ajax.request( - id: 'search' - type: 'GET' - url: "#{@apiPath}/search" - data: - query: @query - processData: true, - success: (data, status, xhr) => - App.Collection.loadAssets(data.assets) - result = {} - for item in data.result - if App[item.type] && App[item.type].find - if !result[item.type] - result[item.type] = [] - item_object = App[item.type].find(item.id) - if item_object.searchResultAttributes - item_object_search_attributes = item_object.searchResultAttributes() - result[item.type].push item_object_search_attributes - else - @log 'error', "No such model #{item.type.toLocaleLowerCase()}.searchResultAttributes()" + App.Ajax.request( + id: 'search' + type: 'GET' + url: "#{@apiPath}/search" + data: + query: @query + processData: true, + success: (data, status, xhr) => + App.Collection.loadAssets(data.assets) + result = {} + for item in data.result + if App[item.type] && App[item.type].find + if !result[item.type] + result[item.type] = [] + item_object = App[item.type].find(item.id) + if item_object.searchResultAttributes + item_object_search_attributes = item_object.searchResultAttributes() + result[item.type].push item_object_search_attributes else - @log 'error', "No such model App.#{item.type}" + @log 'error', "No such model #{item.type.toLocaleLowerCase()}.searchResultAttributes()" + else + @log 'error', "No such model App.#{item.type}" - diff = false - if @searchResultCache[@query] - diff = difference(@searchResultCache[@query].resultRaw, data.result) + diff = false + if @searchResultCache[@query] + diff = difference(@searchResultCache[@query].resultRaw, data.result) - # cache search result - @searchResultCache[@query] = - result: result - resultRaw: data.result - time: new Date + # cache search result + @searchResultCache[@query] = + result: result + resultRaw: data.result + time: new Date - # if result hasn't changed, do not rerender - return if diff isnt false && _.isEmpty(diff) + # if result hasn't changed, do not rerender + return if diff isnt false && _.isEmpty(diff) - @renderResult(result) - ) - @delay(search, delay, 'search') + @renderResult(result) + ) getItems: (data) -> navbar = _.values(data.navbar) @@ -479,7 +478,8 @@ class App.Navigation extends App.ControllerWidgetPermanent e.stopPropagation() @notificationWidget.toggle() - openExtendedSearch: => + openExtendedSearch: (event) -> + event.preventDefault() query = @searchInput.val() @searchInput.val('').blur() if query @@ -487,4 +487,6 @@ class App.Navigation extends App.ControllerWidgetPermanent return @navigate('#search') + + App.Config.set('navigation', App.Navigation, 'Navigations') diff --git a/app/assets/javascripts/app/controllers/search.coffee b/app/assets/javascripts/app/controllers/search.coffee index 66c2222bf..47a2ee434 100644 --- a/app/assets/javascripts/app/controllers/search.coffee +++ b/app/assets/javascripts/app/controllers/search.coffee @@ -4,9 +4,11 @@ class App.Search extends App.Controller '.js-search': 'searchInput' events: + 'click .js-emptySearch': 'empty' 'submit form.search-holder': 'preventDefault' 'keydown .js-search': 'listNavigate' 'click .js-tab': 'showTab' + 'input .js-search': 'updateFilledClass' constructor: -> super @@ -19,18 +21,21 @@ class App.Search extends App.Controller # update taskbar with new meta data App.TaskManager.touch(@task_key) + @throttledSearch = _.throttle @search, 200 + @render() meta: => - title = App.i18n.translateInline('Extended Search') if @query - title += ": #{App.Utils.htmlEscape(@query)}" + title = App.Utils.htmlEscape(@query) + else + title = App.i18n.translateInline('Extended Search') meta = url: @url() id: '' head: title title: title - iconClass: 'magnifier' + iconClass: 'searchdetail' meta url: -> @@ -38,10 +43,9 @@ class App.Search extends App.Controller show: (params) => @navupdate(url: '#search', type: 'menu') - console.log('par', params) return if !params.query @$('.js-search').val(decodeURIComponent(params.query)).trigger('change') - @searchFunction(200, true) + @throttledSearch(true) hide: -> # nothing @@ -79,7 +83,7 @@ class App.Search extends App.Controller ) if @query - @searchFunction(200, true) + @throttledSearch(true) listNavigate: (e) => if e.keyCode is 27 # close on esc @@ -87,71 +91,69 @@ class App.Search extends App.Controller return # on other keys, show result - @searchFunction(200) + @throttledSearch(200) empty: => @searchInput.val('') + @updateFilledClass() # remove not needed popovers @delay(@anyPopoversDestroy, 100, 'removePopovers') - searchFunction: (delay, force = false) => + search: (force = false) => + query = @searchInput.val().trim() + if !force + return if !query + return if query is @query + @query = query - search = => - query = @searchInput.val().trim() - if !force - return if !query - return if query is @query - @query = query + # use cache for search result + if @searchResultCache[@query] + @renderResult(@searchResultCache[@query].result) + currentTime = new Date + return if @searchResultCache[@query].time > currentTime.setSeconds(currentTime.getSeconds() - 20) - # use cache for search result - if @searchResultCache[@query] - @renderResult(@searchResultCache[@query].result) - currentTime = new Date - return if @searchResultCache[@query].time > currentTime.setSeconds(currentTime.getSeconds() - 20) + @updateTask() - @updateTask() - - App.Ajax.request( - id: 'search' - type: 'GET' - url: "#{@apiPath}/search" - data: - query: @query - limit: 200 - processData: true, - success: (data, status, xhr) => - App.Collection.loadAssets(data.assets) - result = {} - for item in data.result - if App[item.type] && App[item.type].find - if !result[item.type] - result[item.type] = [] - item_object = App[item.type].find(item.id) - if item_object.searchResultAttributes - item_object_search_attributes = item_object.searchResultAttributes() - result[item.type].push item_object_search_attributes - else - @log 'error', "No such model #{item.type.toLocaleLowerCase()}.searchResultAttributes()" + App.Ajax.request( + id: 'search' + type: 'GET' + url: "#{@apiPath}/search" + data: + query: @query + limit: 200 + processData: true, + success: (data, status, xhr) => + App.Collection.loadAssets(data.assets) + result = {} + for item in data.result + if App[item.type] && App[item.type].find + if !result[item.type] + result[item.type] = [] + item_object = App[item.type].find(item.id) + if item_object.searchResultAttributes + item_object_search_attributes = item_object.searchResultAttributes() + result[item.type].push item_object_search_attributes else - @log 'error', "No such model App.#{item.type}" + @log 'error', "No such model #{item.type.toLocaleLowerCase()}.searchResultAttributes()" + else + @log 'error', "No such model App.#{item.type}" - diff = false - if @searchResultCache[@query] - diff = difference(@searchResultCache[@query].resultRaw, data.result) + diff = false + if @searchResultCache[@query] + diff = difference(@searchResultCache[@query].resultRaw, data.result) - # cache search result - @searchResultCache[@query] = - result: result - resultRaw: data.result - time: new Date + # cache search result + @searchResultCache[@query] = + result: result + resultRaw: data.result + time: new Date - # if result hasn't changed, do not rerender - return if diff isnt false && _.isEmpty(diff) + # if result hasn't changed, do not rerender + return if diff isnt false && _.isEmpty(diff) - @renderResult(result) - ) - @delay(search, delay, 'search') + @renderResult(result) + ) renderResult: (result = []) => @result = result @@ -161,7 +163,7 @@ class App.Search extends App.Controller count = result[tab.model].length if @model is tab.model @renderTab(tab.model, result[tab.model] || []) - @$(".js-tab#{tab.model} .js-counter").text("(#{count})") + @$(".js-tab#{tab.model} .js-counter").text(count) showTab: (e) => tabs = $(e.currentTarget).closest('.tabs') @@ -213,6 +215,9 @@ class App.Search extends App.Controller App.TaskManager.update(@task_key, { state: current }) App.TaskManager.touch(@task_key) + updateFilledClass: -> + @searchInput.toggleClass 'is-empty', !@searchInput.val() + class Router extends App.ControllerPermanent constructor: (params) -> super diff --git a/app/assets/javascripts/app/views/navigation.jst.eco b/app/assets/javascripts/app/views/navigation.jst.eco index fcf88fdf7..09c734560 100644 --- a/app/assets/javascripts/app/views/navigation.jst.eco +++ b/app/assets/javascripts/app/views/navigation.jst.eco @@ -10,7 +10,18 @@ <%- @Icon('logo') %>
- + diff --git a/app/assets/javascripts/app/views/search/index.jst.eco b/app/assets/javascripts/app/views/search/index.jst.eco index 972b2df11..504e997f3 100644 --- a/app/assets/javascripts/app/views/search/index.jst.eco +++ b/app/assets/javascripts/app/views/search/index.jst.eco @@ -1,19 +1,26 @@ -