codecombat/app/views/play/level/tome/SpellListView.coffee

97 lines
3.1 KiB
CoffeeScript
Raw Normal View History

2014-01-03 13:32:13 -05:00
# The SpellListView has SpellListEntryViews, which have ThangAvatarViews.
# The SpellListView serves as a dropdown triggered from a SpellListTabEntryView, which actually isn't in a list, just had a lot of similar parts.
# There is only one SpellListView, and it belongs to the TomeView.
# TODO: showTopDivider should change when we reorder
2014-07-17 20:20:11 -04:00
CocoView = require 'views/kinds/CocoView'
2014-01-03 13:32:13 -05:00
template = require 'templates/play/level/tome/spell_list'
{me} = require 'lib/auth'
SpellListEntryView = require './SpellListEntryView'
2014-01-03 13:32:13 -05:00
2014-07-17 20:20:11 -04:00
module.exports = class SpellListView extends CocoView
2014-01-03 13:32:13 -05:00
className: 'spell-list-view'
id: 'spell-list-view'
template: template
subscriptions:
'god:new-world-created': 'onNewWorld'
2014-01-03 13:32:13 -05:00
constructor: (options) ->
super options
2014-02-11 15:02:49 -05:00
@entries = []
2014-01-03 13:32:13 -05:00
@sortSpells()
sortSpells: ->
# Keep only spells for which we have permissions
spells = _.filter @options.spells, (s) -> s.canRead()
2014-01-03 13:32:13 -05:00
@spells = _.sortBy spells, @sortScoreForSpell
2014-06-30 22:16:26 -04:00
#console.log 'Kept sorted spells', @spells
2014-01-03 13:32:13 -05:00
sortScoreForSpell: (s) =>
# Sort by most spells per fewest Thangs
# Lower comes first
score = 0
# Selected spell at the top
score -= 9001900190019001 if s is @spell
# Spells for selected thang at the top
score -= 900190019001 if @thang and @thang.id of s.thangs
# Read-only spells at the bottom
score += 90019001 unless s.canWrite()
# The more Thangs sharing a spell, the lower
score += 9001 * _.size(s.thangs)
# The more spells per Thang, the higher
score -= _.filter(@spells, (s2) -> thangID of s2.thangs).length for thangID of s.thangs
score
sortEntries: ->
# Call sortSpells before this
@entries = _.sortBy @entries, (entry) => _.indexOf @spells, entry.spell
@$el.append entry.$el for entry in @entries
afterRender: ->
super()
@addSpellListEntries()
addSpellListEntries: ->
newEntries = []
2014-01-03 13:32:13 -05:00
lastThangs = null
for spell, index in @spells
continue if _.find @entries, spell: spell
2014-01-03 13:32:13 -05:00
theseThangs = _.keys(spell.thangs)
changedThangs = not lastThangs or not _.isEqual theseThangs, lastThangs
lastThangs = theseThangs
newEntries.push entry = new SpellListEntryView spell: spell, showTopDivider: changedThangs, supermodel: @supermodel, includeSpellList: @spells.length > 1
@entries.push entry
for entry in newEntries
2014-01-03 13:32:13 -05:00
@$el.append entry.el
entry.render() # Render after appending so that we can access parent container for popover
2014-06-30 22:16:26 -04:00
rerenderEntries: ->
entry.render() for entry in @entries
2014-01-03 13:32:13 -05:00
onNewWorld: (e) ->
@thang = e.world.thangMap[@thang.id] if @thang
2014-01-03 13:32:13 -05:00
setThangAndSpell: (@thang, @spell) ->
@entries[0]?.setSelected false
@sortSpells()
@sortEntries()
@entries[0].setSelected true, @thang
addThang: (thang) ->
@sortSpells()
@addSpellListEntries()
adjustSpells: (spells) ->
for entry in @entries when _.isEmpty entry.spell.thangs
entry.$el.remove()
entry.destroy()
@spells = @options.spells = spells
@sortSpells()
@addSpellListEntries()
2014-02-11 15:02:49 -05:00
destroy: ->
2014-02-11 17:58:45 -05:00
entry.destroy() for entry in @entries
2014-06-30 22:16:26 -04:00
super()