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
102 isRoot: ->102 isRoot: ->
103 location.pathname is '/'103 location.pathname is '/'
104 104
105 isDocIndex: ->
106 @context.doc and @context.entry is @context.doc.toEntry()
107
105 setInitialPath: ->108 setInitialPath: ->
106 # Remove superfluous forward slashes at the beginning of the path109 # Remove superfluous forward slashes at the beginning of the path
107 if (path = location.pathname.replace /^\/{2,}/g, '/') isnt location.pathname110 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
20 20
21 @routes:21 @routes:
22 root: 'onRoot'22 root: 'onRoot'
23 after: 'autoFocus'23 after: 'afterRoute'
24 24
25 init: ->25 init: ->
26 @addSubview @scope = new app.views.SearchScope @el26 @addSubview @scope = new app.views.SearchScope @el
@@ -84,8 +84,10 @@ class app.views.Search extends app.View · 57 unmodified lines
84 return84 return
85 85
86 searchUrl: =>86 searchUrl: =>
87 return unless app.router.isRoot()87 if app.router.isRoot()
88 @scope.searchUrl()88 @scope.searchUrl()
89 else if not app.router.isDocIndex()
90 return
89 91
90 return unless value = @extractHashValue()92 return unless value = @extractHashValue()
91 @input.value = @value = value93 @input.value = @value = value
@@ -136,13 +138,18 @@ class app.views.Search extends app.View · 44 unmodified lines
136 138
137 onRoot: (context) =>139 onRoot: (context) =>
138 @reset() unless context.init140 @reset() unless context.init
139 @delay @searchUrl if context.hash
140 return141 return
141 142
143 afterRoute: (name, context) =>
144 @delay @searchUrl if context.hash
145 @autoFocus()
146
142 extractHashValue: ->147 extractHashValue: ->
143 if (value = @getHashValue())?148 if (value = @getHashValue())?
144 app.router.replaceHash()149 app.router.replaceHash()
145 value150 value
146 151
152 HASH_RGX = new RegExp "^##{SEARCH_PARAM}=(.*)"
153
147 getHashValue: ->154 getHashValue: ->
148 try (new RegExp "##{SEARCH_PARAM}=(.*)").exec($.urlDecode location.hash)?[1] catch155 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
32 name: ->32 name: ->
33 @doc?.name33 @doc?.name
34 34
35 search: (value) ->35 search: (value, searchDisabled = false) ->
36 unless @doc36 return if @doc
37 @searcher.find app.docs.all(), 'text', value37 @searcher.find app.docs.all(), 'text', value
38 @searcher.find app.disabledDocs.all(), 'text', value if not @doc and searchDisabled
38 return39 return
39 40
40 searchUrl: ->41 searchUrl: ->
41 if value = @extractHashValue()42 if value = @extractHashValue()
42 @search value43 @search value, true
43 return44 return
44 45
45 onResults: (results) =>46 onResults: (results) =>
46 if results.length47 return unless doc = results[0]
47 @selectDoc results[0]48 if app.docs.contains(doc)
49 @selectDoc(doc)
50 else
51 @redirectToDoc(doc)
48 return52 return
49 53
50 selectDoc: (doc) ->54 selectDoc: (doc) ->
@@ -63,6 +67,12 @@ class app.views.SearchScope extends app.View · 12 unmodified lines
63 @trigger 'change', @doc, previousDoc67 @trigger 'change', @doc, previousDoc
64 return68 return
65 69
70 redirectToDoc: (doc) ->
71 hash = location.hash
72 app.router.replaceHash('')
73 window.location = doc.fullPath() + hash
74 return
75
66 reset: =>76 reset: =>
67 return unless @doc77 return unless @doc
68 previousDoc = @doc78 previousDoc = @doc
@@ -97,8 +107,10 @@ class app.views.SearchScope extends app.View · 28 unmodified lines
97 app.router.replaceHash(newHash)107 app.router.replaceHash(newHash)
98 value108 value
99 109
110 HASH_RGX = new RegExp "^##{SEARCH_PARAM}=(.+?) ."
111
100 getHashValue: ->112 getHashValue: ->
101 try (new RegExp "^##{SEARCH_PARAM}=(.+?) .").exec($.urlDecode location.hash)?[1] catch113 try HASH_RGX.exec($.urlDecode location.hash)?[1] catch
102 114
103 afterRoute: (name, context) =>115 afterRoute: (name, context) =>
104 if !app.isSingleDoc() and context.init and context.doc116 if !app.isSingleDoc() and context.init and context.doc