codecombat/app/views/editor/ThangTasksView.coffee

48 lines
1.7 KiB
CoffeeScript
Raw Normal View History

2015-12-21 15:44:22 -05:00
RootView = require 'views/core/RootView'
template = require 'templates/editor/thangTasksView'
ThangType = require 'models/ThangType'
CocoCollection = require 'collections/CocoCollection'
module.exports = class ThangTasksView extends RootView
template: template
id: 'thang-tasks-view'
events:
'input input': 'searchUpdate'
'change input': 'searchUpdate'
constructor: (options) ->
super options
@thangs = new CocoCollection([],
url: '/db/thang.type?project=name,tasks,slug'
model: ThangType
comparator: @sortThangs
)
@lastLoad = (new Date()).getTime()
2015-12-21 18:42:46 -05:00
@listenTo(@thangs, 'sync', @onThangsLoaded)
2015-12-21 15:44:22 -05:00
@supermodel.loadCollection(@thangs, 'thangs')
searchUpdate: ->
2015-12-21 18:42:46 -05:00
if not @lastLoad? or (new Date()).getTime() - @lastLoad > 60 * 1000 * 1 # Update only after a minute from last update.
2015-12-21 15:44:22 -05:00
@thangs.fetch()
2015-12-21 18:42:46 -05:00
@listenTo(@thangs, 'sync', @onThangsLoaded)
2015-12-21 15:44:22 -05:00
@supermodel.loadCollection(@thangs, 'thangs')
@lastLoad = (new Date()).getTime()
else
2015-12-21 18:42:46 -05:00
@onThangsLoaded()
onThangsLoaded: ->
@processedThangs = @thangs.filter (_elem) ->
# Case-insensitive search of input vs name.
return ///#{$('#nameSearch')[0].value}///i.test _elem.get('name')
2015-12-21 15:44:22 -05:00
for thang in @processedThangs
2015-12-21 18:42:46 -05:00
thang.tasks = _.filter thang.attributes.tasks, (_elem) ->
# Similar case-insensitive search of input vs description (name).
return ///#{$('#descSearch')[0].value}///i.test _elem.name
@renderSelectors '#thangTable'
2015-12-21 15:44:22 -05:00
sortThangs: (a, b) ->
2015-12-21 18:42:46 -05:00
a.get('name').localeCompare(b.get('name'))
# Jade helper
hasIncompleteTasks: (thang) ->
return thang.tasks and thang.tasks.filter((_elem) -> return not _elem.complete).length > 0