codecombat/app/views/common/SearchView.coffee

97 lines
3 KiB
CoffeeScript
Raw Normal View History

RootView = require 'views/core/RootView'
NewModelModal = require 'views/modal/NewModelModal'
template = require 'templates/common/search'
app = require 'core/application'
2014-01-03 13:32:13 -05:00
class SearchCollection extends Backbone.Collection
initialize: (modelURL, @model, @term, @projection) ->
@url = "#{modelURL}?project="
if @projection? and not (@projection == [])
2014-08-28 13:50:20 -04:00
@url += 'created,permissions'
@url += ',' + projected for projected in projection
2014-06-30 22:16:26 -04:00
else @url += 'true'
@url += "&term=#{term}" if @term
2014-01-03 13:32:13 -05:00
2014-08-28 13:50:20 -04:00
comparator: (a, b) ->
score = 0
score -= 9001900190019001 if a.getOwner() is me.id
score += 9001900190019001 if b.getOwner() is me.id
score -= new Date(a.get 'created')
score -= -(new Date(b.get 'created'))
if score < 0 then -1 else (if score > 0 then 1 else 0)
module.exports = class SearchView extends RootView
2014-01-03 13:32:13 -05:00
template: template
className: 'search-view'
# to overwrite in subclasses
modelLabel: '' # 'Article'
model: null # Article
modelURL: null # '/db/article'
tableTemplate: null # require 'templates/editor/article/table'
projected: null # ['name', 'description', 'version'] or null for default
2014-01-03 13:32:13 -05:00
events:
'change input#search': 'runSearch'
'keydown input#search': 'runSearch'
'click #new-model-button': 'newModel'
'hidden.bs.modal #new-model-modal': 'onModalHidden'
2014-01-03 13:32:13 -05:00
constructor: (options) ->
@runSearch = _.debounce(@runSearch, 500)
super options
afterRender: ->
super()
hash = document.location.hash[1..]
searchInput = @$el.find('#search')
searchInput.val(hash) if hash?
delete @collection?.term
2014-01-03 13:32:13 -05:00
searchInput.trigger('change')
searchInput.focus()
runSearch: =>
2014-07-04 09:13:06 -04:00
return if @destroyed
2014-01-03 13:32:13 -05:00
term = @$el.find('input#search').val()
return if @sameSearch(term)
@removeOldSearch()
@collection = new SearchCollection(@modelURL, @model, term, @projection)
2014-01-03 13:32:13 -05:00
@collection.term = term # needed?
2014-03-24 02:53:41 -04:00
@listenTo(@collection, 'sync', @onSearchChange)
2014-01-03 13:32:13 -05:00
@showLoading(@$el.find('.results'))
@updateHash(term)
@collection.fetch()
updateHash: (term) ->
2014-06-30 22:16:26 -04:00
newPath = document.location.pathname + (if term then '#' + term else '')
2014-01-03 13:32:13 -05:00
currentPath = document.location.pathname + document.location.hash
app.router.navigate(newPath) if newPath isnt currentPath
sameSearch: (term) ->
return false unless @collection
return term is @collection.term
2014-03-24 02:53:41 -04:00
onSearchChange: ->
2014-01-03 13:32:13 -05:00
@hideLoading()
2014-08-28 13:50:20 -04:00
@collection.sort()
2014-01-06 15:37:35 -05:00
documents = @collection.models
2014-08-28 13:50:20 -04:00
table = $(@tableTemplate(documents: documents, me: me, page: @page))
2014-01-03 13:32:13 -05:00
@$el.find('table').replaceWith(table)
@$el.find('table').i18n()
2014-01-03 13:32:13 -05:00
removeOldSearch: ->
return unless @collection?
2014-03-24 12:07:09 -04:00
@collection.off()
2014-01-03 13:32:13 -05:00
@collection = null
onNewModelSaved: (@model) ->
base = document.location.pathname[1..] + '/'
2014-06-30 22:16:26 -04:00
app.router.navigate(base + (@model.get('slug') or @model.id), {trigger: true})
newModel: (e) ->
modal = new NewModelModal model: @model, modelLabel: @modelLabel
modal.once 'model-created', @onNewModelSaved
@openModalView modal