History

Improve URL search

Closes #434.

Thibaut Courouble committed 10 years ago

Commit ba4bcbf1 parent 73fa07a3 files · +34 −12Browse files

Unified · Split

3 files
  1. assets/javascripts/app/router.coffee +3 −0
  2. assets/javascripts/views/search/search.coffee +12 −5
  3. assets/javascripts/views/search/search_scope.coffee +19 −7
assets/javascripts/app/router.coffee+3−0
@@ -102,6 +102,9 @@ class app.Router · 101 unmodified lines
102102 isRoot: ->
103103 location.pathname is '/'
104104
105+ isDocIndex: ->
106+ @context.doc and @context.entry is @context.doc.toEntry()
107+
105108 setInitialPath: ->
106109 # Remove superfluous forward slashes at the beginning of the path
107110 if (path = location.pathname.replace /^\/{2,}/g, '/') isnt location.pathname
assets/javascripts/views/search/search.coffee+12−5
@@ -20,7 +20,7 @@ class app.views.Search extends app.View · 19 unmodified lines
2020
2121 @routes:
2222 root: 'onRoot'
23− after: 'autoFocus'
23+ after: 'afterRoute'
2424
2525 init: ->
2626 @addSubview @scope = new app.views.SearchScope @el
@@ -84,8 +84,10 @@ class app.views.Search extends app.View · 57 unmodified lines
8484 return
8585
8686 searchUrl: =>
87− return unless app.router.isRoot()
88− @scope.searchUrl()
87+ if app.router.isRoot()
88+ @scope.searchUrl()
89+ else if not app.router.isDocIndex()
90+ return
8991
9092 return unless value = @extractHashValue()
9193 @input.value = @value = value
@@ -136,13 +138,18 @@ class app.views.Search extends app.View · 44 unmodified lines
136138
137139 onRoot: (context) =>
138140 @reset() unless context.init
139− @delay @searchUrl if context.hash
140141 return
141142
143+ afterRoute: (name, context) =>
144+ @delay @searchUrl if context.hash
145+ @autoFocus()
146+
142147 extractHashValue: ->
143148 if (value = @getHashValue())?
144149 app.router.replaceHash()
145150 value
146151
152+ HASH_RGX = new RegExp "^##{SEARCH_PARAM}=(.*)"
153+
147154 getHashValue: ->
148− try (new RegExp "##{SEARCH_PARAM}=(.*)").exec($.urlDecode location.hash)?[1] catch
155+ try HASH_RGX.exec($.urlDecode location.hash)?[1] catch
assets/javascripts/views/search/search_scope.coffee+19−7
@@ -32,19 +32,23 @@ class app.views.SearchScope extends app.View · 31 unmodified lines
3232 name: ->
3333 @doc?.name
3434
35− search: (value) ->
36− unless @doc
37− @searcher.find app.docs.all(), 'text', value
35+ search: (value, searchDisabled = false) ->
36+ return if @doc
37+ @searcher.find app.docs.all(), 'text', value
38+ @searcher.find app.disabledDocs.all(), 'text', value if not @doc and searchDisabled
3839 return
3940
4041 searchUrl: ->
4142 if value = @extractHashValue()
42− @search value
43+ @search value, true
4344 return
4445
4546 onResults: (results) =>
46− if results.length
47− @selectDoc results[0]
47+ return unless doc = results[0]
48+ if app.docs.contains(doc)
49+ @selectDoc(doc)
50+ else
51+ @redirectToDoc(doc)
4852 return
4953
5054 selectDoc: (doc) ->
@@ -63,6 +67,12 @@ class app.views.SearchScope extends app.View · 12 unmodified lines
6367 @trigger 'change', @doc, previousDoc
6468 return
6569
70+ redirectToDoc: (doc) ->
71+ hash = location.hash
72+ app.router.replaceHash('')
73+ window.location = doc.fullPath() + hash
74+ return
75+
6676 reset: =>
6777 return unless @doc
6878 previousDoc = @doc
@@ -97,8 +107,10 @@ class app.views.SearchScope extends app.View · 28 unmodified lines
97107 app.router.replaceHash(newHash)
98108 value
99109
110+ HASH_RGX = new RegExp "^##{SEARCH_PARAM}=(.+?) ."
111+
100112 getHashValue: ->
101− try (new RegExp "^##{SEARCH_PARAM}=(.+?) .").exec($.urlDecode location.hash)?[1] catch
113+ try HASH_RGX.exec($.urlDecode location.hash)?[1] catch
102114
103115 afterRoute: (name, context) =>
104116 if !app.isSingleDoc() and context.init and context.doc