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

69 lines
2.7 KiB
CoffeeScript
Raw Normal View History

ProblemAlertView = require './ProblemAlertView'
2014-06-30 22:16:26 -04:00
Range = ace.require('ace/range').Range
2014-08-14 14:55:43 -04:00
UserCodeProblem = require 'models/UserCodeProblem'
2014-01-03 13:32:13 -05:00
module.exports = class Problem
annotation: null
alertView: null
markerRange: null
2014-08-14 14:55:43 -04:00
constructor: (@aether, @aetherProblem, @ace, withAlert=false, isCast=false, @levelID) ->
2014-01-03 13:32:13 -05:00
@buildAnnotation()
@buildAlertView() if withAlert
2014-08-14 14:55:43 -04:00
@buildMarkerRange() if isCast
@saveUserCodeProblem() if isCast
2014-01-03 13:32:13 -05:00
destroy: ->
unless @alertView?.destroyed
@alertView?.$el?.remove()
@alertView?.destroy()
2014-01-03 13:32:13 -05:00
@removeMarkerRange()
2014-08-14 14:55:43 -04:00
@userCodeProblem.off() if @userCodeProblem
2014-01-03 13:32:13 -05:00
buildAnnotation: ->
2014-05-08 14:43:00 -04:00
return unless @aetherProblem.range
2014-01-03 13:32:13 -05:00
text = @aetherProblem.message.replace /^Line \d+: /, ''
2014-05-08 14:43:00 -04:00
start = @aetherProblem.range[0]
2014-01-03 13:32:13 -05:00
@annotation =
row: start.row,
column: start.col,
2014-01-03 13:32:13 -05:00
raw: text,
text: text,
2014-06-30 22:16:26 -04:00
type: @aetherProblem.level ? 'error'
2014-01-03 13:32:13 -05:00
buildAlertView: ->
@alertView = new ProblemAlertView problem: @
@alertView.render()
$(@ace.container).append @alertView.el
buildMarkerRange: ->
2014-05-08 14:43:00 -04:00
return unless @aetherProblem.range
[start, end] = @aetherProblem.range
2014-01-03 13:32:13 -05:00
clazz = "problem-marker-#{@aetherProblem.level}"
@markerRange = new Range start.row, start.col, end.row, end.col
2014-01-03 13:32:13 -05:00
@markerRange.start = @ace.getSession().getDocument().createAnchor @markerRange.start
@markerRange.end = @ace.getSession().getDocument().createAnchor @markerRange.end
2014-06-30 22:16:26 -04:00
@markerRange.id = @ace.getSession().addMarker @markerRange, clazz, 'text'
2014-01-03 13:32:13 -05:00
removeMarkerRange: ->
return unless @markerRange
@ace.getSession().removeMarker @markerRange.id
@markerRange.start.detach()
@markerRange.end.detach()
2014-08-14 14:55:43 -04:00
saveUserCodeProblem: () ->
@userCodeProblem = new UserCodeProblem()
@userCodeProblem.set 'code', @aether.raw
if @aetherProblem.range
rawLines = @aether.raw.split '\n'
errorLines = rawLines.slice @aetherProblem.range[0].row, @aetherProblem.range[1].row + 1
@userCodeProblem.set 'codeSnippet', errorLines.join '\n'
@userCodeProblem.set 'errHint', @aetherProblem.hint if @aetherProblem.hint
@userCodeProblem.set 'errId', @aetherProblem.id if @aetherProblem.id
@userCodeProblem.set 'errLevel', @aetherProblem.level if @aetherProblem.level
@userCodeProblem.set 'errMessage', @aetherProblem.message if @aetherProblem.message
@userCodeProblem.set 'errRange', @aetherProblem.range if @aetherProblem.range
@userCodeProblem.set 'errType', @aetherProblem.type if @aetherProblem.type
@userCodeProblem.set 'language', @aether.language.id if @aether.language?.id
@userCodeProblem.set 'levelID', @levelID if @levelID
@userCodeProblem.save()
null