2014-07-17 20:20:11 -04:00
|
|
|
CocoView = require 'views/kinds/CocoView'
|
2014-03-18 14:59:36 -04:00
|
|
|
add_thangs_template = require 'templates/editor/level/add_thangs'
|
|
|
|
ThangType = require 'models/ThangType'
|
2014-04-22 15:42:26 -04:00
|
|
|
CocoCollection = require 'collections/CocoCollection'
|
2014-03-18 14:59:36 -04:00
|
|
|
|
|
|
|
class ThangTypeSearchCollection extends CocoCollection
|
2014-08-18 18:25:22 -04:00
|
|
|
url: '/db/thang.type?project=original,name,version,description,slug,kind,rasterIcon'
|
2014-03-18 14:59:36 -04:00
|
|
|
model: ThangType
|
|
|
|
|
|
|
|
addTerm: (term) ->
|
|
|
|
@url += "&term=#{term}" if term
|
|
|
|
|
2014-07-17 20:20:11 -04:00
|
|
|
module.exports = class AddThangsView extends CocoView
|
2014-06-30 22:16:26 -04:00
|
|
|
id: 'add-thangs-column'
|
2014-03-18 14:59:36 -04:00
|
|
|
className: 'add-thangs-palette thangs-column'
|
|
|
|
template: add_thangs_template
|
|
|
|
startsLoading: false
|
|
|
|
|
|
|
|
events:
|
|
|
|
'keyup input#thang-search': 'runSearch'
|
|
|
|
|
|
|
|
constructor: (options) ->
|
|
|
|
super options
|
|
|
|
@world = options.world
|
2014-04-28 14:52:04 -04:00
|
|
|
|
|
|
|
# should load depended-on Components, too
|
|
|
|
@thangTypes = @supermodel.loadCollection(new ThangTypeSearchCollection(), 'thangs').model
|
2014-03-18 14:59:36 -04:00
|
|
|
|
|
|
|
getRenderData: (context={}) ->
|
|
|
|
context = super(context)
|
|
|
|
if @searchModels
|
|
|
|
models = @searchModels
|
|
|
|
else
|
|
|
|
models = @supermodel.getModels(ThangType)
|
|
|
|
|
2014-08-18 18:25:22 -04:00
|
|
|
thangTypes = _.uniq models, false, (thangType) -> thangType.get('original')
|
|
|
|
thangTypes = _.reject thangTypes, (thangType) -> thangType.get('kind') is 'Mark'
|
2014-03-18 14:59:36 -04:00
|
|
|
groupMap = {}
|
|
|
|
for thangType in thangTypes
|
2014-08-18 18:25:22 -04:00
|
|
|
kind = thangType.get('kind')
|
|
|
|
groupMap[kind] ?= []
|
|
|
|
groupMap[kind].push thangType
|
2014-03-18 14:59:36 -04:00
|
|
|
|
|
|
|
groups = []
|
|
|
|
for groupName in Object.keys(groupMap).sort()
|
|
|
|
someThangTypes = groupMap[groupName]
|
2014-08-18 18:25:22 -04:00
|
|
|
someThangTypes = _.sortBy someThangTypes, (thangType) -> thangType.get('name')
|
2014-03-18 14:59:36 -04:00
|
|
|
group =
|
|
|
|
name: groupName
|
|
|
|
thangs: someThangTypes
|
|
|
|
groups.push group
|
|
|
|
|
|
|
|
context.thangTypes = thangTypes
|
|
|
|
context.groups = groups
|
|
|
|
context
|
|
|
|
|
|
|
|
afterRender: ->
|
|
|
|
super()
|
|
|
|
|
|
|
|
runSearch: (e) =>
|
|
|
|
if e?.which is 27
|
|
|
|
@onEscapePressed()
|
|
|
|
term = @$el.find('input#thang-search').val()
|
|
|
|
return unless term isnt @lastSearch
|
|
|
|
|
|
|
|
@searchModels = @thangTypes.filter (model) ->
|
|
|
|
return true unless term
|
|
|
|
regExp = new RegExp term, 'i'
|
|
|
|
return model.get('name').match regExp
|
|
|
|
@render()
|
|
|
|
@$el.find('input#thang-search').focus().val(term)
|
|
|
|
@lastSearch = term
|
|
|
|
|
|
|
|
onEscapePressed: ->
|
2014-06-30 22:16:26 -04:00
|
|
|
@$el.find('input#thang-search').val('')
|
2014-06-09 10:18:26 -04:00
|
|
|
@runSearch
|