Extended search result not shown after clicking “search details” on quick search.

This commit is contained in:
Martin Edenhofer 2016-11-30 21:22:33 +01:00
parent dfbe15cb0e
commit 8e8cc4555b
4 changed files with 43 additions and 46 deletions

View file

@ -25,6 +25,10 @@ class App.Navigation extends App.ControllerWidgetPermanent
@throttledSearch = _.throttle @search, 200 @throttledSearch = _.throttle @search, 200
@globalSearch = new App.GlobalSearch(
render: @renderResult
)
# rerender view, e. g. on langauge change # rerender view, e. g. on langauge change
@bind 'ui:rerender', => @bind 'ui:rerender', =>
@renderMenu() @renderMenu()
@ -127,15 +131,15 @@ class App.Navigation extends App.ControllerWidgetPermanent
items = @getItems(navbar: @Config.get('NavBarRight')) items = @getItems(navbar: @Config.get('NavBarRight'))
# get open tabs to repopen on rerender # get open tabs to repopen on rerender
open_tab = {} openTab = {}
@$('.open').children('a').each( (i,d) -> @$('.open').children('a').each( (i,d) ->
href = $(d).attr('href') href = $(d).attr('href')
open_tab[href] = true openTab[href] = true
) )
@$('.navbar-items-personal').html App.view('navigation/personal')( @$('.navbar-items-personal').html App.view('navigation/personal')(
items: items items: items
open_tab: open_tab openTab: openTab
) )
# only start avatar widget on existing session # only start avatar widget on existing session
@ -151,6 +155,7 @@ class App.Navigation extends App.ControllerWidgetPermanent
# remove result if not result exists # remove result if not result exists
if _.isEmpty(result) if _.isEmpty(result)
@searchContainer.removeClass('open') @searchContainer.removeClass('open')
@globalSearch.close()
@searchResult.html('') @searchResult.html('')
return return
@ -275,6 +280,7 @@ class App.Navigation extends App.ControllerWidgetPermanent
emptyAndClose: => emptyAndClose: =>
@searchInput.val('') @searchInput.val('')
@searchContainer.removeClass('filled').removeClass('open').removeClass('focused') @searchContainer.removeClass('filled').removeClass('open').removeClass('focused')
@globalSearch.close()
# remove not needed popovers # remove not needed popovers
@delay(@anyPopoversDestroy, 100, 'removePopovers') @delay(@anyPopoversDestroy, 100, 'removePopovers')
@ -282,6 +288,7 @@ class App.Navigation extends App.ControllerWidgetPermanent
andClose: => andClose: =>
@searchInput.blur() @searchInput.blur()
@searchContainer.removeClass('open') @searchContainer.removeClass('open')
@globalSearch.close()
@delay(@anyPopoversDestroy, 100, 'removePopovers') @delay(@anyPopoversDestroy, 100, 'removePopovers')
search: => search: =>
@ -290,11 +297,7 @@ class App.Navigation extends App.ControllerWidgetPermanent
return if query is @query return if query is @query
@query = query @query = query
@searchContainer.toggleClass('filled', !!@query) @searchContainer.toggleClass('filled', !!@query)
@globalSearch.search(query: @query)
App.GlobalSearch.execute(
query: @query
render: @renderResult
)
getItems: (data) -> getItems: (data) ->
navbar = _.values(data.navbar) navbar = _.values(data.navbar)

View file

@ -1,5 +1,4 @@
class App.Search extends App.Controller class App.Search extends App.Controller
searchResultCache: {}
elements: elements:
'.js-search': 'searchInput' '.js-search': 'searchInput'
@ -25,6 +24,11 @@ class App.Search extends App.Controller
@throttledSearch = _.throttle @search, 200 @throttledSearch = _.throttle @search, 200
@globalSearch = new App.GlobalSearch(
render: @renderResult
limit: 50
)
@render() @render()
# rerender view, e. g. on langauge change # rerender view, e. g. on langauge change
@ -52,10 +56,11 @@ class App.Search extends App.Controller
@navupdate(url: '#search', type: 'menu') @navupdate(url: '#search', type: 'menu')
return if _.isEmpty(params.query) return if _.isEmpty(params.query)
@$('.js-search').val(params.query).trigger('change') @$('.js-search').val(params.query).trigger('change')
return if @shown
@throttledSearch(true) @throttledSearch(true)
hide: -> hide: ->
# nothing @shown = false
changed: -> changed: ->
# nothing # nothing
@ -117,11 +122,7 @@ class App.Search extends App.Controller
@query = query @query = query
@updateTask() @updateTask()
App.GlobalSearch.execute( @globalSearch.search(query: @query)
query: @query
render: @renderResult
limit: 50
)
renderResult: (result = []) => renderResult: (result = []) =>
@result = result @result = result

View file

@ -1,37 +1,27 @@
class App.GlobalSearch class App.GlobalSearch extends App.Controller
_instance = undefined
@execute: (args) ->
if _instance == undefined
_instance ?= new _globalSearchSingleton
_instance.execute(args)
class _globalSearchSingleton extends Spine.Module
constructor: -> constructor: ->
@searchResultCache = undefined super
@searchResultCacheByKey = {} @searchResultCache = {}
@lastQuery = undefined
@apiPath = App.Config.get('api_path') @apiPath = App.Config.get('api_path')
@ajaxId = "search-#{Math.floor( Math.random() * 999999 )}"
execute: (params) -> search: (params) =>
query = params.query query = params.query
render = params.render
limit = params.limit || 10
cacheKey = "#{query}_#{limit}"
# use cache for search result # use cache for search result
currentTime = new Date currentTime = new Date
if @searchResultCacheByKey[cacheKey] && @searchResultCacheByKey[cacheKey].time > currentTime.setSeconds(currentTime.getSeconds() - 20) if @searchResultCache[query] && @searchResultCache[query].time > currentTime.setSeconds(currentTime.getSeconds() - 20)
@renderTry(render, @searchResultCacheByKey[cacheKey].result, cacheKey) @renderTry(@searchResultCache[query].result, query)
return return
App.Ajax.request( App.Ajax.request(
id: 'search' id: @ajaxId
type: 'GET' type: 'GET'
url: "#{@apiPath}/search" url: "#{@apiPath}/search"
data: data:
query: query query: query
limit: limit limit: @limit || 10
processData: true processData: true
success: (data, status, xhr) => success: (data, status, xhr) =>
App.Collection.loadAssets(data.assets) App.Collection.loadAssets(data.assets)
@ -49,21 +39,24 @@ class _globalSearchSingleton extends Spine.Module
else else
App.Log.error('_globalSearchSingleton', "No such model App.#{item.type}") App.Log.error('_globalSearchSingleton', "No such model App.#{item.type}")
@renderTry(render, result, cacheKey) @renderTry(result, query)
) )
renderTry: (render, result, cacheKey) => renderTry: (result, query) =>
# if result hasn't changed, do not rerender # if result hasn't changed, do not rerender
diff = false diff = false
if @searchResultCache if @lastQuery is query && @searchResultCache[query]
diff = difference(@searchResultCache, result) diff = difference(@searchResultCache[query].result, result)
return if diff isnt false && _.isEmpty(diff) return if diff isnt false && _.isEmpty(diff)
@lastQuery = query
# cache search result # cache search result
@searchResultCache = result @searchResultCache[query] =
@searchResultCacheByKey[cacheKey] =
result: result result: result
time: new Date time: new Date
render(result) @render(result)
close: =>
@lastQuery = undefined

View file

@ -1,6 +1,6 @@
<% for item in @items: %> <% for item in @items: %>
<% if item.child: %> <% if item.child: %>
<li class="<% if item.class: %><%- item.class %><% end %> dropup <% if @open_tab[item.target] : %>open<% end %>"> <li class="<% if item.class: %><%- item.class %><% end %> dropup <% if @openTab[item.target] : %>open<% end %>">
<a class="list-button dropdown-toggle js-action" data-toggle="dropdown" href="<%= item.target %>" title="<% if item.translate: %><%- @Ti( item.name ) %><% else: %><%= item.name %><% end %>"> <a class="list-button dropdown-toggle js-action" data-toggle="dropdown" href="<%= item.target %>" title="<% if item.translate: %><%- @Ti( item.name ) %><% else: %><%= item.name %><% end %>">
<span class="dropdown-nose"></span> <span class="dropdown-nose"></span>
<% if item.class is 'user': %> <% if item.class is 'user': %>