From ba4bcbf7eee088e619e082fc8a01f589e14ee689 Mon Sep 17 00:00:00 2001 From: Thibaut Courouble Date: Sat, 9 Jul 2016 12:19:17 -0400 Subject: [PATCH] Improve URL search Closes #434. --- assets/javascripts/app/router.coffee | 3 +++ assets/javascripts/views/search/search.coffee | 17 ++++++++---- .../views/search/search_scope.coffee | 26 ++++++++++++++----- 3 files changed, 34 insertions(+), 12 deletions(-) diff --git a/assets/javascripts/app/router.coffee b/assets/javascripts/app/router.coffee index aa1fc6b7..d53484c4 100644 --- a/assets/javascripts/app/router.coffee +++ b/assets/javascripts/app/router.coffee @@ -102,6 +102,9 @@ class app.Router isRoot: -> location.pathname is '/' + isDocIndex: -> + @context.doc and @context.entry is @context.doc.toEntry() + setInitialPath: -> # Remove superfluous forward slashes at the beginning of the path if (path = location.pathname.replace /^\/{2,}/g, '/') isnt location.pathname diff --git a/assets/javascripts/views/search/search.coffee b/assets/javascripts/views/search/search.coffee index fdf37957..4cdefd67 100644 --- a/assets/javascripts/views/search/search.coffee +++ b/assets/javascripts/views/search/search.coffee @@ -20,7 +20,7 @@ class app.views.Search extends app.View @routes: root: 'onRoot' - after: 'autoFocus' + after: 'afterRoute' init: -> @addSubview @scope = new app.views.SearchScope @el @@ -84,8 +84,10 @@ class app.views.Search extends app.View return searchUrl: => - return unless app.router.isRoot() - @scope.searchUrl() + if app.router.isRoot() + @scope.searchUrl() + else if not app.router.isDocIndex() + return return unless value = @extractHashValue() @input.value = @value = value @@ -136,13 +138,18 @@ class app.views.Search extends app.View onRoot: (context) => @reset() unless context.init - @delay @searchUrl if context.hash return + afterRoute: (name, context) => + @delay @searchUrl if context.hash + @autoFocus() + extractHashValue: -> if (value = @getHashValue())? app.router.replaceHash() value + HASH_RGX = new RegExp "^##{SEARCH_PARAM}=(.*)" + getHashValue: -> - try (new RegExp "##{SEARCH_PARAM}=(.*)").exec($.urlDecode location.hash)?[1] catch + try HASH_RGX.exec($.urlDecode location.hash)?[1] catch diff --git a/assets/javascripts/views/search/search_scope.coffee b/assets/javascripts/views/search/search_scope.coffee index 8337c136..f5d3f8dc 100644 --- a/assets/javascripts/views/search/search_scope.coffee +++ b/assets/javascripts/views/search/search_scope.coffee @@ -32,19 +32,23 @@ class app.views.SearchScope extends app.View name: -> @doc?.name - search: (value) -> - unless @doc - @searcher.find app.docs.all(), 'text', value + search: (value, searchDisabled = false) -> + return if @doc + @searcher.find app.docs.all(), 'text', value + @searcher.find app.disabledDocs.all(), 'text', value if not @doc and searchDisabled return searchUrl: -> if value = @extractHashValue() - @search value + @search value, true return onResults: (results) => - if results.length - @selectDoc results[0] + return unless doc = results[0] + if app.docs.contains(doc) + @selectDoc(doc) + else + @redirectToDoc(doc) return selectDoc: (doc) -> @@ -63,6 +67,12 @@ class app.views.SearchScope extends app.View @trigger 'change', @doc, previousDoc return + redirectToDoc: (doc) -> + hash = location.hash + app.router.replaceHash('') + window.location = doc.fullPath() + hash + return + reset: => return unless @doc previousDoc = @doc @@ -97,8 +107,10 @@ class app.views.SearchScope extends app.View app.router.replaceHash(newHash) value + HASH_RGX = new RegExp "^##{SEARCH_PARAM}=(.+?) ." + getHashValue: -> - try (new RegExp "^##{SEARCH_PARAM}=(.+?) .").exec($.urlDecode location.hash)?[1] catch + try HASH_RGX.exec($.urlDecode location.hash)?[1] catch afterRoute: (name, context) => if !app.isSingleDoc() and context.init and context.doc