You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
devdocs/assets/javascripts/views/search/search_scope.coffee

136 lines
3.1 KiB

11 years ago
class app.views.SearchScope extends app.View
SEARCH_PARAM = app.config.search_param
@elements:
input: '._search-input'
tag: '._search-tag'
@events:
click: 'onClick'
11 years ago
keydown: 'onKeydown'
textInput: 'onTextInput'
11 years ago
@routes:
after: 'afterRoute'
11 years ago
constructor: (@el) -> super
init: ->
@placeholder = @input.getAttribute 'placeholder'
@searcher = new app.SynchronousSearcher
fuzzy_min_length: 2
max_results: 1
@searcher.on 'results', @onResults
return
getScope: ->
@doc or app
isActive: ->
!!@doc
name: ->
@doc?.name
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
11 years ago
return
searchUrl: ->
if value = @extractHashValue()
@search value, true
11 years ago
return
onResults: (results) =>
return unless doc = results[0]
if app.docs.contains(doc)
@selectDoc(doc)
else
@redirectToDoc(doc)
11 years ago
return
selectDoc: (doc) ->
previousDoc = @doc
return if doc is previousDoc
11 years ago
@doc = doc
@tag.textContent = doc.fullName
11 years ago
@tag.style.display = 'block'
@input.removeAttribute 'placeholder'
@input.value = @input.value[@input.selectionStart..]
10 years ago
@input.style.paddingLeft = @tag.offsetWidth + 10 + 'px'
11 years ago
$.trigger @input, 'input'
@trigger 'change', @doc, previousDoc
return
11 years ago
redirectToDoc: (doc) ->
hash = location.hash
app.router.replaceHash('')
location.assign doc.fullPath() + hash
return
11 years ago
reset: =>
return unless @doc
previousDoc = @doc
11 years ago
@doc = null
@tag.textContent = ''
@tag.style.display = 'none'
@input.setAttribute 'placeholder', @placeholder
@input.style.paddingLeft = ''
@trigger 'change', null, previousDoc
return
doScopeSearch: (event) =>
@search @input.value[0...@input.selectionStart]
$.stopEvent(event) if @doc
return
onClick: (event) =>
if event.target is @tag
@reset()
$.stopEvent(event)
return
11 years ago
onKeydown: (event) =>
if event.which is 8 # backspace
if @doc and @input.selectionEnd is 0
11 years ago
@reset()
$.stopEvent(event)
else if not @doc and @input.value and not $.isChromeForAndroid()
return if event.ctrlKey or event.metaKey or event.altKey or event.shiftKey
if event.which is 9 or # tab
(event.which is 32 and app.isMobile()) # space
@doScopeSearch(event)
11 years ago
return
onTextInput: (event) =>
return unless $.isChromeForAndroid()
if not @doc and @input.value and event.data == ' '
@doScopeSearch(event)
return
11 years ago
extractHashValue: ->
if value = @getHashValue()
newHash = $.urlDecode(location.hash).replace "##{SEARCH_PARAM}=#{value} ", "##{SEARCH_PARAM}="
11 years ago
app.router.replaceHash(newHash)
value
HASH_RGX = new RegExp "^##{SEARCH_PARAM}=(.+?) ."
11 years ago
getHashValue: ->
try HASH_RGX.exec($.urlDecode location.hash)?[1] catch
afterRoute: (name, context) =>
if !app.isSingleDoc() and context.init and context.doc
@selectDoc(context.doc)
return