2014-01-03 13:32:13 -05:00
|
|
|
View = require 'views/kinds/RootView'
|
|
|
|
template = require 'templates/kinds/search'
|
|
|
|
forms = require('lib/forms')
|
|
|
|
app = require('application')
|
|
|
|
|
|
|
|
class SearchCollection extends Backbone.Collection
|
|
|
|
initialize: (modelURL, @model, @term) ->
|
2014-01-21 01:38:36 -05:00
|
|
|
@url = "#{modelURL}/search?project=yes"
|
|
|
|
@url += "&term=#{term}" if @term
|
2014-01-03 13:32:13 -05:00
|
|
|
|
|
|
|
module.exports = class ThangTypeHomeView extends View
|
|
|
|
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'
|
|
|
|
|
|
|
|
events:
|
|
|
|
'change input#search': 'runSearch'
|
|
|
|
'keydown input#search': 'runSearch'
|
|
|
|
'click button.new-model-submit': 'makeNewModel'
|
2014-02-27 12:17:19 -05:00
|
|
|
'submit form': 'makeNewModel'
|
2014-02-27 02:32:18 -05:00
|
|
|
'shown.bs.modal #new-model-modal': 'focusOnName'
|
2014-02-28 12:55:30 -05:00
|
|
|
'hidden.bs.modal #new-model-modal': 'onModalHidden'
|
2014-01-03 13:32:13 -05:00
|
|
|
|
|
|
|
getRenderData: ->
|
2014-03-01 01:47:31 -05:00
|
|
|
context = super()
|
|
|
|
switch @modelLabel
|
2014-03-01 01:52:51 -05:00
|
|
|
when 'Level'
|
|
|
|
context.currentEditor = 'editor.level_title'
|
2014-03-03 06:45:04 -05:00
|
|
|
context.currentNew = 'editor.new_level_title'
|
|
|
|
context.currentSearch = 'editor.level_search_title'
|
2014-03-01 01:47:31 -05:00
|
|
|
when 'Thang Type'
|
|
|
|
context.currentEditor = 'editor.thang_title'
|
2014-03-03 06:45:04 -05:00
|
|
|
context.currentNew = 'editor.new_thang_title'
|
|
|
|
context.currentSearch = 'editor.thang_search_title'
|
2014-03-01 01:47:31 -05:00
|
|
|
when 'Article'
|
|
|
|
context.currentEditor = 'editor.article_title'
|
2014-03-03 06:45:04 -05:00
|
|
|
context.currentNew = 'editor.new_article_title'
|
|
|
|
context.currentSearch = 'editor.article_search_title'
|
|
|
|
@$el.i18n()
|
2014-03-01 01:47:31 -05:00
|
|
|
context
|
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?
|
2014-03-21 20:21:44 -04:00
|
|
|
delete @collection?.term
|
2014-01-03 13:32:13 -05:00
|
|
|
searchInput.trigger('change')
|
|
|
|
searchInput.focus()
|
|
|
|
|
|
|
|
runSearch: =>
|
|
|
|
term = @$el.find('input#search').val()
|
|
|
|
return if @sameSearch(term)
|
|
|
|
@removeOldSearch()
|
|
|
|
|
|
|
|
@collection = new SearchCollection(@modelURL, @model, term)
|
|
|
|
@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) ->
|
|
|
|
newPath = document.location.pathname + (if term then "#" + term else "")
|
|
|
|
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-01-06 15:37:35 -05:00
|
|
|
documents = @collection.models
|
2014-01-03 13:32:13 -05:00
|
|
|
table = $(@tableTemplate(documents:documents))
|
|
|
|
@$el.find('table').replaceWith(table)
|
2014-03-02 18:31:49 -05:00
|
|
|
@$el.find('table').i18n()
|
2014-01-03 13:32:13 -05:00
|
|
|
|
|
|
|
removeOldSearch: ->
|
|
|
|
return unless @collection?
|
|
|
|
@collection = null
|
|
|
|
|
|
|
|
makeNewModel: (e) ->
|
|
|
|
e.preventDefault()
|
|
|
|
name = @$el.find('#name').val()
|
|
|
|
model = new @model()
|
|
|
|
model.set('name', name)
|
|
|
|
if @model.schema.get('properties').permissions
|
|
|
|
model.set 'permissions', [{access: 'owner', target: me.id}]
|
|
|
|
res = model.save()
|
|
|
|
return unless res
|
|
|
|
|
2014-02-28 12:55:30 -05:00
|
|
|
modal = @$el.find('#new-model-modal')
|
2014-01-27 14:30:22 -05:00
|
|
|
forms.clearFormAlerts(modal)
|
2014-01-03 13:32:13 -05:00
|
|
|
@showLoading(modal.find('.modal-body'))
|
|
|
|
res.error =>
|
|
|
|
@hideLoading()
|
|
|
|
forms.applyErrorsToForm(modal, JSON.parse(res.responseText))
|
2014-02-28 12:55:30 -05:00
|
|
|
that = @
|
2014-01-03 13:32:13 -05:00
|
|
|
res.success ->
|
2014-02-28 12:55:30 -05:00
|
|
|
that.model = model
|
|
|
|
modal.modal('hide')
|
|
|
|
|
|
|
|
onModalHidden: ->
|
|
|
|
# Can only redirect after the modal hidden event has triggered
|
|
|
|
base = document.location.pathname[1..] + '/'
|
|
|
|
app.router.navigate(base + (@model.get('slug') or @model.id), {trigger:true})
|
2014-02-27 02:32:18 -05:00
|
|
|
|
|
|
|
focusOnName: ->
|
2014-02-27 02:38:03 -05:00
|
|
|
@$el.find('#name').focus()
|