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.coffee

157 lines
3.1 KiB

11 years ago
class app.views.Search extends app.View
SEARCH_PARAM = app.config.search_param
@el: '._search'
@activeClass: '_search-active'
@elements:
input: '._search-input'
resetLink: '._search-clear'
@events:
input: 'onInput'
click: 'onClick'
submit: 'onSubmit'
@shortcuts:
typing: 'autoFocus'
altG: 'google'
altS: 'stackoverflow'
11 years ago
@routes:
root: 'onRoot'
after: 'afterRoute'
11 years ago
init: ->
@addSubview @scope = new app.views.SearchScope @el
@searcher = new app.Searcher
@searcher
.on 'results', @onResults
.on 'end', @onEnd
11 years ago
app.on 'ready', @onReady
$.on window, 'hashchange', @searchUrl
$.on window, 'focus', @autoFocus
return
focus: ->
@delay =>
@input.focus() unless document.activeElement is @input
11 years ago
return
autoFocus: =>
unless $.isTouchScreen()
@input.focus() unless document.activeElement is @input
11 years ago
return
reset: ->
11 years ago
@el.reset()
@onInput()
@autoFocus()
return
disable: ->
@input.setAttribute('disabled', 'disabled')
return
enable: ->
@input.removeAttribute('disabled')
return
11 years ago
onReady: =>
@value = ''
@delay @onInput
return
onInput: =>
return if not @value? or # ignore events pre-"ready"
@value is @input.value
@value = @input.value
if @value.length
@search()
else
@clear()
return
search: (url = false) ->
@addClass @constructor.activeClass
@trigger 'searching'
@hasResults = null
11 years ago
@flags = urlSearch: url, initialResults: true
@searcher.find @scope.getScope().entries.all(), 'text', @value
return
searchUrl: =>
if app.router.isRoot()
@scope.searchUrl()
else if not app.router.isDocIndex()
return
11 years ago
return unless value = @extractHashValue()
@input.value = @value = value
@input.setSelectionRange(value.length, value.length)
11 years ago
@search true
true
clear: ->
@removeClass @constructor.activeClass
@trigger 'clear'
return
11 years ago
externalSearch: (url) ->
if value = @value
value = "#{@scope.name()} #{value}" if @scope.name()
$.popup "#{url}#{encodeURIComponent value}"
@reset()
return
google: =>
@externalSearch "https://www.google.com/search?q="
return
stackoverflow: =>
@externalSearch "https://stackoverflow.com/search?q="
return
11 years ago
onResults: (results) =>
@hasResults = true if results.length
11 years ago
@trigger 'results', results, @flags
@flags.initialResults = false
return
onEnd: =>
@trigger 'noresults' unless @hasResults
return
11 years ago
onClick: (event) =>
if event.target is @resetLink
$.stopEvent(event)
@reset()
@focus()
return
onSubmit: (event) ->
$.stopEvent(event)
return
onRoot: (context) =>
@reset() unless context.init
return
afterRoute: (name, context) =>
@delay @searchUrl if context.hash
@autoFocus()
11 years ago
extractHashValue: ->
if (value = @getHashValue())?
app.router.replaceHash()
value
HASH_RGX = new RegExp "^##{SEARCH_PARAM}=(.*)"
11 years ago
getHashValue: ->
try HASH_RGX.exec($.urlDecode location.hash)?[1] catch