Closes #434.
Unified · Split
| @@ -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 path | 109 | # Remove superfluous forward slashes at the beginning of the path |
| 107 | if (path = location.pathname.replace /^\/{2,}/g, '/') isnt location.pathname | 110 | if (path = location.pathname.replace /^\/{2,}/g, '/') isnt location.pathname |
| @@ -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 @el | 26 | @addSubview @scope = new app.views.SearchScope @el |
| @@ -84,8 +84,10 @@ class app.views.Search extends app.View · 57 unmodified lines | |||
| 84 | return | 84 | 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 = value | 93 | @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.init | 140 | @reset() unless context.init |
| 139 | @delay @searchUrl if context.hash | ||
| 140 | return | 141 | 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 | value | 150 | 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] catch | 155 | try HASH_RGX.exec($.urlDecode location.hash)?[1] catch |
| @@ -32,19 +32,23 @@ class app.views.SearchScope extends app.View · 31 unmodified lines | |||
| 32 | name: -> | 32 | name: -> |
| 33 | @doc?.name | 33 | @doc?.name |
| 34 | 34 | ||
| 35 | search: (value) -> | 35 | search: (value, searchDisabled = false) -> |
| 36 | unless @doc | 36 | return if @doc |
| 37 | @searcher.find app.docs.all(), 'text', value | 37 | @searcher.find app.docs.all(), 'text', value |
| 38 | @searcher.find app.disabledDocs.all(), 'text', value if not @doc and searchDisabled | ||
| 38 | return | 39 | return |
| 39 | 40 | ||
| 40 | searchUrl: -> | 41 | searchUrl: -> |
| 41 | if value = @extractHashValue() | 42 | if value = @extractHashValue() |
| 42 | @search value | 43 | @search value, true |
| 43 | return | 44 | return |
| 44 | 45 | ||
| 45 | onResults: (results) => | 46 | onResults: (results) => |
| 46 | if results.length | 47 | return unless doc = results[0] |
| 47 | @selectDoc results[0] | 48 | if app.docs.contains(doc) |
| 49 | @selectDoc(doc) | ||
| 50 | else | ||
| 51 | @redirectToDoc(doc) | ||
| 48 | return | 52 | 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, previousDoc | 67 | @trigger 'change', @doc, previousDoc |
| 64 | return | 68 | 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 @doc | 77 | return unless @doc |
| 68 | previousDoc = @doc | 78 | 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 | value | 108 | 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] catch | 113 | 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.doc | 116 | if !app.isSingleDoc() and context.init and context.doc |