Browse Source

Improve behavior of search scope when hitting escape

Closes #639.
Thibaut Courouble 8 years ago
parent
commit
48d85f1124

+ 2 - 0
assets/javascripts/lib/events.coffee

@@ -15,8 +15,10 @@
     @
 
   trigger: (event, args...) ->
+    @eventInProgress = { name: event, args: args }
     if callbacks = @_callbacks?[event]
       callback? args... for callback in callbacks.slice(0)
+    @eventInProgress = null
     @trigger 'all', event, args... unless event is 'all'
     @
 

+ 7 - 2
assets/javascripts/views/search/search.coffee

@@ -44,7 +44,11 @@ class app.views.Search extends app.View
       @input.focus() unless document.activeElement?.tagName is 'INPUT'
     return
 
-  reset: ->
+  getScopeDoc: ->
+    @scope.getScope() if @scope.isActive()
+
+  reset: (force) ->
+    @scope.reset() if force or not @input.value
     @el.reset()
     @onInput()
     @autoFocus()
@@ -129,7 +133,8 @@ class app.views.Search extends app.View
     return
 
   afterRoute: (name, context) =>
-    @reset() if not context.init and app.router.isIndex()
+    return if app.shortcuts.eventInProgress?.name is 'escape'
+    @reset(true) if not context.init and app.router.isIndex()
     @delay @searchUrl if context.hash
     @delay @autoFocus
     return

+ 3 - 3
assets/javascripts/views/search/search_scope.coffee

@@ -11,9 +11,6 @@ class app.views.SearchScope extends app.View
   @routes:
     after: 'afterRoute'
 
-  @shortcuts:
-    escape: 'reset'
-
   constructor: (@el) -> super
 
   init: ->
@@ -29,6 +26,9 @@ class app.views.SearchScope extends app.View
   getScope: ->
     @doc or app
 
+  isActive: ->
+    !!@doc
+
   name: ->
     @doc?.name
 

+ 5 - 2
assets/javascripts/views/sidebar/sidebar.coffee

@@ -129,7 +129,8 @@ class app.views.Sidebar extends app.View
 
   onEscape: =>
     @reset()
-    @scrollToTop()
+    @resetDisplay()
+    if doc = @search.getScopeDoc() then @docList.reveal(doc.toEntry()) else @scrollToTop()
     return
 
   onDocEnabled: ->
@@ -137,6 +138,8 @@ class app.views.Sidebar extends app.View
     @reset()
     return
 
-  afterRoute: =>
+  afterRoute: (name, context) =>
+    return if app.shortcuts.eventInProgress?.name is 'escape'
+    @reset() if not context.init and app.router.isIndex()
     @resetDisplay()
     return