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

111 lines
3.8 KiB
CoffeeScript
Raw Normal View History

template = require 'templates/play/level/tome/spell-top-bar-view'
ReloadLevelModal = require 'views/play/level/modal/ReloadLevelModal'
CocoView = require 'views/core/CocoView'
ImageGalleryModal = require 'views/play/level/modal/ImageGalleryModal'
2014-01-03 13:32:13 -05:00
module.exports = class SpellTopBarView extends CocoView
2014-01-03 13:32:13 -05:00
template: template
id: 'spell-top-bar-view'
controlsEnabled: true
2014-01-03 13:32:13 -05:00
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'
'tome:spell-changed-language': 'onSpellChangedLanguage'
'tome:toggle-maximize': 'onToggleMaximize'
2014-01-03 13:32:13 -05:00
events:
'click .reload-code': 'onCodeReload'
'click .beautify-code': 'onBeautifyClick'
'click .fullscreen-code': 'onToggleMaximize'
'click .hints-button': 'onClickHintsButton'
'click .image-gallery-button': 'onClickImageGalleryButton'
2014-01-03 13:32:13 -05:00
constructor: (options) ->
@hintsState = options.hintsState
@spell = options.spell
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.codeLanguage = @options.codeLanguage
2014-01-03 13:32:13 -05:00
context
afterRender: ->
super()
2014-07-23 09:50:47 -04:00
@attachTransitionEventListener()
2014-01-03 13:32:13 -05:00
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
onClickImageGalleryButton: (e) ->
@openModalView new ImageGalleryModal()
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-08-27 20:26:56 -04:00
onCodeReload: (e) ->
@openModalView new ReloadLevelModal()
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
@render()
@updateReloadButton()
2014-01-03 13:32:13 -05:00
toggleControls: (e, enabled) ->
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: ->
super()