codecombat/app/views/play/menu/GuideView.coffee

182 lines
6.9 KiB
CoffeeScript
Raw Normal View History

CocoView = require 'views/core/CocoView'
template = require 'templates/play/menu/guide-view'
Article = require 'models/Article'
SubscribeModal = require 'views/core/SubscribeModal'
utils = require 'core/utils'
module.exports = class LevelGuideView extends CocoView
template: template
id: 'guide-view'
className: 'tab-pane'
2014-12-18 01:26:23 -05:00
helpVideoHeight: '295'
helpVideoWidth: '471'
events:
'click .start-subscription-button': 'clickSubscribe'
constructor: (options) ->
super options
@levelSlug = options.level.get('slug')
@sessionID = options.session.get('_id')
@requiresSubscription = not me.isPremium()
@helpVideos = options.level.get('helpVideos') ? []
2014-12-18 01:26:23 -05:00
@trackedHelpVideoStart = @trackedHelpVideoFinish = false
2014-12-18 02:55:11 -05:00
# A/B Testing video tutorial styles
@helpVideosIndex = me.getVideoTutorialStylesIndex(@helpVideos.length)
@helpVideo = @helpVideos[@helpVideosIndex] if @helpVideos.length > 0
@videoLocked = not (@helpVideo?.free or options.level.get('type', true) in ['course', 'course-ladder']) and @requiresSubscription
@firstOnly = options.firstOnly
@docs = options?.docs ? options.level.get('documentation') ? {}
general = @docs.generalArticles or []
specific = @docs.specificArticles or []
articles = options.supermodel.getModels(Article)
articleMap = {}
articleMap[article.get('original')] = article for article in articles
general = (articleMap[ref.original] for ref in general)
general = (article.attributes for article in general when article)
@docs = specific.concat(general)
@docs = $.extend(true, [], @docs)
@docs = [@docs[0]] if @firstOnly and @docs[0]
@addPicoCTFProblem() if window.serverConfig.picoCTF
doc.html = marked(utils.filterMarkdownCodeLanguages(utils.i18n(doc, 'body'), options.session.get('codeLanguage'))) for doc in @docs
doc.slug = _.string.slugify(doc.name) for doc in @docs
doc.name = (utils.i18n doc, 'name') for doc in @docs
2014-12-18 02:55:11 -05:00
destroy: ->
if @vimeoListenerAttached
if window.addEventListener
window.removeEventListener('message', @onMessageReceived, false)
else
window.detachEvent('onmessage', @onMessageReceived, false)
2015-11-10 18:22:09 -05:00
oldEditor.destroy() for oldEditor in @aceEditors ? []
2014-12-18 02:55:11 -05:00
super()
getRenderData: ->
c = super()
c.docs = @docs
2014-12-18 01:26:23 -05:00
c.showVideo = @helpVideos.length > 0
c.videoLocked = @videoLocked
c
afterRender: ->
super()
@setupVideoPlayer() unless @videoLocked
if @docs.length + @helpVideos.length > 1
if @helpVideos.length
startingTab = 0
else
startingTab = _.findIndex @docs, slug: 'overview'
startingTab = 0 if startingTab is -1
# incredible hackiness. Getting bootstrap tabs to work shouldn't be this complex
@$el.find(".nav-tabs li:nth(#{startingTab})").addClass('active')
@$el.find(".tab-content .tab-pane:nth(#{startingTab})").addClass('active')
@$el.find('.nav-tabs a').click(@clickTab)
@$el.addClass 'has-tabs'
2015-11-10 18:22:09 -05:00
@configureACEEditors()
@playSound 'guide-open'
2015-11-10 18:22:09 -05:00
configureACEEditors: ->
oldEditor.destroy() for oldEditor in @aceEditors ? []
@aceEditors = []
aceEditors = @aceEditors
codeLanguage = @options.session.get('codeLanguage') or me.get('aceConfig')?.language or 'python'
2015-11-10 18:22:09 -05:00
@$el.find('pre').each ->
aceEditor = utils.initializeACE @, codeLanguage
aceEditors.push aceEditor
clickSubscribe: (e) ->
level = @levelSlug # Save ref to level slug
@openModalView new SubscribeModal()
# TODO: Added levelID on 2/9/16. Remove level property and associated AnalyticsLogEvent 'properties.level' index later.
window.tracker?.trackEvent 'Show subscription modal', category: 'Subscription', label: 'help video clicked', level: level, levelID: level
clickTab: (e) =>
@$el.find('li.active').removeClass('active')
@playSound 'guide-tab-switch'
afterInsert: ->
super()
Backbone.Mediator.publish 'level:docs-shown', {}
onHidden: ->
if @vimeoListenerAttached
2016-04-25 17:19:28 -04:00
player = @$('#help-video-player')[0]
player.contentWindow.postMessage JSON.stringify(method: 'pause'), '*'
2015-02-18 15:35:41 -05:00
createjs?.Sound?.setVolume?(@volume ? ( me.get('volume') ? 1.0))
Backbone.Mediator.publish 'level:docs-hidden', {}
2014-12-18 01:26:23 -05:00
onShown: ->
# TODO: Disable sound only when video is playing?
@volume ?= me.get('volume') ? 1.0
createjs?.Sound?.setVolume(0.0)
2014-12-18 02:55:11 -05:00
onStartHelpVideo: ->
unless @trackedHelpVideoStart
window.tracker?.trackEvent 'Start help video', level: @levelSlug, ls: @sessionID, style: @helpVideo?.style
2014-12-18 02:55:11 -05:00
@trackedHelpVideoStart = true
2014-12-18 02:55:11 -05:00
onFinishHelpVideo: ->
unless @trackedHelpVideoFinish
window.tracker?.trackEvent 'Finish help video', level: @levelSlug, ls: @sessionID, style: @helpVideo?.style
2014-12-18 02:55:11 -05:00
@trackedHelpVideoFinish = true
2014-12-18 01:26:23 -05:00
setupVideoPlayer: () ->
return unless @helpVideo
# Always use HTTPS
# TODO: Not specifying the protocol should work based on Vimeo docs, but breaks postMessage/eventing in practice.
url = "https:" + @helpVideo.url.substr @helpVideo.url.indexOf '/'
@setupVimeoVideoPlayer url
2014-12-18 01:26:23 -05:00
setupVimeoVideoPlayer: (helpVideoURL) ->
# Setup Vimeo player
# https://developer.vimeo.com/player/js-api#universal-with-postmessage
# Create Vimeo iframe player
tag = document.createElement('iframe')
tag.id = 'help-video-player'
2014-12-18 02:59:59 -05:00
tag.src = helpVideoURL + "?api=1&badge=0&byline=0&portrait=0&title=0"
2014-12-18 01:26:23 -05:00
tag.height = @helpVideoHeight
tag.width = @helpVideoWidth
tag.allowFullscreen = true
tag.mozAllowFullscreen = true
$tag = $(tag)
$tag.attr('webkitallowfullscreen', true) # strong arm Safari into working
@$el.find('#help-video-player').replaceWith($tag)
2014-12-18 01:26:23 -05:00
2014-12-18 02:55:11 -05:00
@onMessageReceived = (e) =>
2014-12-18 01:26:23 -05:00
data = JSON.parse(e.data)
if data.event is 'ready'
# Vimeo player is ready, can now hook up other events
# https://developer.vimeo.com/player/js-api#events
player = $('#help-video-player')[0]
player.contentWindow.postMessage JSON.stringify(method: 'addEventListener', value: 'play'), helpVideoURL
player.contentWindow.postMessage JSON.stringify(method: 'addEventListener', value: 'finish'), helpVideoURL
else if data.event is 'play'
2014-12-18 02:55:11 -05:00
@onStartHelpVideo?()
2014-12-18 01:26:23 -05:00
else if data.event is 'finish'
2014-12-18 02:55:11 -05:00
@onFinishHelpVideo?()
2014-12-18 01:26:23 -05:00
# Listen for Vimeo player 'ready'
if window.addEventListener
2014-12-18 02:55:11 -05:00
window.addEventListener('message', @onMessageReceived, false)
2014-12-18 01:26:23 -05:00
else
2014-12-18 02:55:11 -05:00
window.attachEvent('onmessage', @onMessageReceived, false)
@vimeoListenerAttached = true
addPicoCTFProblem: ->
return unless problem = @options.level.picoCTFProblem
@docs = [name: 'Intro', body: '', slug: 'intro'] unless @docs.length
for doc in @docs when doc.name in ['Overview', 'Intro']
doc.body += """
### #{problem.name}
#{problem.description}
#{problem.category} - #{problem.score} points
Hint: #{problem.hints}
""".replace /<p>(.*?)<\/p>/gi, '$1'