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

167 lines
5.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'
2014-07-23 08:38:12 -04:00
filters = require 'lib/image_filter'
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'
2014-01-03 13:32:13 -05:00
constructor: (options) ->
super options
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'}"
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: ->
@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
2014-07-15 22:25:53 -04:00
docFormatter = new DocFormatter doc: doc, thang: @thang, language: @options.language, selectedMethod: true
@$el.find('code').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-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
onDropdownClick: (e) ->
2014-02-24 14:21:51 -05:00
return unless @controlsEnabled
Backbone.Mediator.publish 'tome:toggle-spell-list', {}
Backbone.Mediator.publish 'audio-player:play-sound', trigger: 'spell-list-open', volume: 1
2014-01-03 13:32:13 -05:00
2014-08-27 20:26:56 -04:00
onCodeReload: (e) ->
2014-02-24 14:21:51 -05:00
return unless @controlsEnabled
2014-06-30 22:16:26 -04:00
Backbone.Mediator.publish 'tome:reload-code', spell: @spell
2014-01-03 13:32:13 -05:00
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.language = e.language
@$el.find('code').popover 'destroy'
@render()
@docsBuilt = false
@buildDocs() if @thang
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-07-23 08:38:12 -04:00
@toggleBackground()
toggleBackground: =>
# TODO: make the palette background an actual background and do the CSS trick
# used in spell_list_entry.sass for disabling
background = @$el.find('img.spell-tab-image-hidden')[0]
if background.naturalWidth is 0 # not loaded yet
return _.delay @toggleBackground, 100
filters.revertImage background, '.spell-list-entry-view.spell-tab' if @controlsEnabled
filters.darkenImage background, '.spell-list-entry-view.spell-tab', 0.8 unless @controlsEnabled
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', 1 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('code').popover 'destroy'
super()