Improve URL search

Closes #434.
pull/438/head
Thibaut Courouble 9 years ago
parent 73fa07a595
commit ba4bcbf7ee

@ -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

@ -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

@ -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

Loading…
Cancel
Save