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

171 lines
6 KiB
CoffeeScript
Raw Normal View History

SpellListEntryView = require './SpellListEntryView'
ThangAvatarView = require 'views/play/level/ThangAvatarView'
2014-01-03 13:32:13 -05:00
template = require 'templates/play/level/tome/spell_list_tab_entry'
2014-02-13 19:09:32 -05:00
LevelComponent = require 'models/LevelComponent'
DocFormatter = require './DocFormatter'
ReloadLevelModal = require 'views/play/level/modal/ReloadLevelModal'
2014-01-03 13:32:13 -05:00
module.exports = class SpellListTabEntryView extends SpellListEntryView
template: template
id: 'spell-list-tab-entry-view'
subscriptions:
'level:disable-controls': 'onDisableControls'
'level:enable-controls': 'onEnableControls'
2014-06-30 22:16:26 -04:00
'tome:spell-loaded': 'onSpellLoaded'
'tome:spell-changed': 'onSpellChanged'
'god:new-world-created': 'onNewWorld'
'tome:spell-changed-language': 'onSpellChangedLanguage'
'tome:toggle-maximize': 'onToggleMaximize'
2014-01-03 13:32:13 -05:00
events:
'click .spell-list-button': 'onDropdownClick'
'click .reload-code': 'onCodeReload'
'click .beautify-code': 'onBeautifyClick'
'click .fullscreen-code': 'onToggleMaximize'
'click .hints-button': 'onClickHintsButton'
2014-01-03 13:32:13 -05:00
constructor: (options) ->
@hintsState = options.hintsState
super(options)
2014-01-03 13:32:13 -05:00
2014-02-11 17:58:45 -05:00
getRenderData: (context={}) ->
2014-01-03 13:32:13 -05:00
context = super context
2014-08-27 20:26:56 -04:00
ctrl = if @isMac() then 'Cmd' else 'Ctrl'
shift = $.i18n.t 'keyboard_shortcuts.shift'
context.beautifyShortcutVerbose = "#{ctrl}+#{shift}+B: #{$.i18n.t 'keyboard_shortcuts.beautify'}"
context.maximizeShortcutVerbose = "#{ctrl}+#{shift}+M: #{$.i18n.t 'keyboard_shortcuts.maximize_editor'}"
context.includeSpellList = @options.level.get('slug') in ['break-the-prison', 'zone-of-danger', 'k-means-cluster-wars', 'brawlwood', 'dungeon-arena', 'sky-span', 'minimax-tic-tac-toe']
context.codeLanguage = @options.codeLanguage
context.levelType = @options.level.get 'type', true
2014-01-03 13:32:13 -05:00
context
afterRender: ->
super()
@$el.addClass 'spell-tab'
2014-07-23 09:50:47 -04:00
@attachTransitionEventListener()
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
setThang: (thang) ->
return if thang.id is @thang?.id
@thang = thang
@spellThang = @spell.thangs[@thang.id]
@buildAvatar()
@buildDocs() unless @docsBuilt
buildAvatar: ->
avatar = new ThangAvatarView thang: @thang, includeName: false, supermodel: @supermodel
2014-01-03 13:32:13 -05:00
if @avatar
@avatar.$el.replaceWith avatar.$el
@avatar.destroy()
else
@$el.find('.thang-avatar-placeholder').replaceWith avatar.$el
@avatar = avatar
@avatar.render()
buildDocs: ->
return if @spell.name is 'plan' # Too confusing for beginners
2014-01-03 13:32:13 -05:00
@docsBuilt = true
2014-02-13 19:09:32 -05:00
lcs = @supermodel.getModels LevelComponent
found = false
for lc in lcs when not found
for doc in lc.get('propertyDocumentation') ? []
if doc.name is @spell.name
found = true
break
return unless found
docFormatter = new DocFormatter doc: doc, thang: @thang, language: @options.codeLanguage, selectedMethod: true
@$el.find('.method-signature').popover(
2014-02-13 19:09:32 -05:00
animation: true
html: true
2014-02-20 13:39:16 -05:00
placement: 'bottom'
2014-02-13 19:09:32 -05:00
trigger: 'hover'
content: docFormatter.formatPopover()
2014-02-13 19:09:32 -05:00
container: @$el.parent()
2014-09-11 11:38:30 -04:00
).on 'show.bs.popover', =>
2015-09-09 17:36:05 -04:00
@playSound 'spell-tab-entry-open', 0.75
2014-02-13 19:09:32 -05:00
2014-01-03 13:32:13 -05:00
onMouseEnterAvatar: (e) -> # Don't call super
onMouseLeaveAvatar: (e) -> # Don't call super
onClick: (e) -> # Don't call super
2014-07-23 08:38:12 -04:00
onDisableControls: (e) -> @toggleControls e, false
onEnableControls: (e) -> @toggleControls e, true
2014-01-03 13:32:13 -05:00
onClickHintsButton: ->
return unless @hintsState?
@hintsState.set('hidden', not @hintsState.get('hidden'))
window.tracker?.trackEvent 'Hints Clicked', category: 'Students', levelSlug: @options.level.get('slug'), hintCount: @hintsState.get('hints')?.length ? 0, ['Mixpanel']
2014-01-03 13:32:13 -05:00
onDropdownClick: (e) ->
2014-02-24 14:21:51 -05:00
return unless @controlsEnabled
Backbone.Mediator.publish 'tome:toggle-spell-list', {}
2015-09-09 17:36:05 -04:00
@playSound 'spell-list-open'
2014-01-03 13:32:13 -05:00
2014-08-27 20:26:56 -04:00
onCodeReload: (e) ->
#return unless @controlsEnabled
#Backbone.Mediator.publish 'tome:reload-code', spell: @spell # Old: just reload the current code
@openModalView new ReloadLevelModal() # New: prompt them to restart the level
2014-08-27 20:26:56 -04:00
onBeautifyClick: (e) ->
2014-02-24 14:21:51 -05:00
return unless @controlsEnabled
Backbone.Mediator.publish 'tome:spell-beautify', spell: @spell
onToggleMaximize: (e) ->
$codearea = $('html')
$('#code-area').css 'z-index', 20 unless $codearea.hasClass 'fullscreen-editor'
$('html').toggleClass 'fullscreen-editor'
2014-07-23 08:38:12 -04:00
$('.fullscreen-code').toggleClass 'maximized'
Backbone.Mediator.publish 'tome:maximize-toggled', {}
2014-01-03 13:32:13 -05:00
updateReloadButton: ->
changed = @spell.hasChanged null, @spell.getSource()
@$el.find('.reload-code').css('display', if changed then 'inline-block' else 'none')
onSpellLoaded: (e) ->
return unless e.spell is @spell
@updateReloadButton()
onSpellChanged: (e) ->
return unless e.spell is @spell
@updateReloadButton()
onSpellChangedLanguage: (e) ->
return unless e.spell is @spell
@options.codeLanguage = e.language
@$el.find('.method-signature').popover 'destroy'
@render()
@docsBuilt = false
@buildDocs() if @thang
@updateReloadButton()
2014-01-03 13:32:13 -05:00
toggleControls: (e, enabled) ->
# Don't call super; do it differently
return if e.controls and not ('editor' in e.controls)
return if enabled is @controlsEnabled
@controlsEnabled = enabled
@$el.toggleClass 'read-only', not enabled
2014-02-11 15:02:49 -05:00
2014-07-23 09:50:47 -04:00
attachTransitionEventListener: =>
transitionListener = ''
testEl = document.createElement 'fakeelement'
transitions =
2014-07-23 09:50:47 -04:00
'transition':'transitionend'
'OTransition':'oTransitionEnd'
'MozTransition':'transitionend'
'WebkitTransition':'webkitTransitionEnd'
for transition, transitionEvent of transitions
unless testEl.style[transition] is undefined
transitionListener = transitionEvent
break
$codearea = $('#code-area')
$codearea.on transitionListener, =>
$codearea.css 'z-index', 2 unless $('html').hasClass 'fullscreen-editor'
2014-07-23 09:50:47 -04:00
2014-02-11 15:02:49 -05:00
destroy: ->
@avatar?.destroy()
@$el.find('.method-signature').popover 'destroy'
super()