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

103 lines
3.3 KiB
CoffeeScript
Raw Normal View History

2014-01-03 13:32:13 -05:00
View = require 'views/kinds/CocoView'
template = require 'templates/play/level/tome/spell_palette_entry'
popoverTemplate = require 'templates/play/level/tome/spell_palette_entry_popover'
2014-01-03 13:32:13 -05:00
{me} = require 'lib/auth'
filters = require 'lib/image_filter'
{downTheChain} = require 'lib/world/world_utils'
# If we use marked somewhere else, we'll have to make sure to preserve options
marked.setOptions {gfm: true, sanitize: false, smartLists: true, breaks: true}
2014-01-03 13:32:13 -05:00
module.exports = class SpellPaletteEntryView extends View
tagName: 'div' # Could also try <code> instead of <div>, but would need to adjust colors
className: 'spell-palette-entry-view'
template: template
subscriptions:
'surface:frame-changed': "onFrameChanged"
events:
'mouseover': 'onMouseOver'
'click': 'onClick'
constructor: (options) ->
super options
@thang = options.thang
@doc = options.doc
if _.isString @doc
@doc = name: @doc, type: typeof @thang[@doc]
@doc.owner ?= 'this'
if options.isSnippet
@doc.type = 'snippet'
@doc.shortName = @doc.shorterName = @doc.title = @doc.name
else
suffix = if @doc.type is 'function' then '()' else ''
@doc.shortName = "#{@doc.owner}.#{@doc.name}#{suffix};"
if @doc.owner is 'this'
@doc.shorterName = "#{@doc.name}#{suffix}"
else
@doc.shorterName = @doc.shortName.replace ';', ''
@doc.title = if options.shortenize then @doc.shorterName else @doc.shortName
getRenderData: ->
c = super()
c.doc = @doc
c
2014-01-03 13:32:13 -05:00
afterRender: ->
super()
@$el.addClass(@doc.type)
@$el.popover(
2014-01-03 13:32:13 -05:00
animation: true
html: true
placement: 'top'
trigger: 'hover'
content: @formatPopover()
2014-01-03 13:32:13 -05:00
container: @$el.parent().parent().parent()
)
2014-01-30 19:36:36 -05:00
@$el.on 'show.bs.popover', =>
Backbone.Mediator.publish 'tome:palette-hovered', thang: @thang, prop: @doc.name
formatPopover: ->
content = popoverTemplate doc: @doc, value: @formatValue(), marked: marked, argumentExamples: (arg.example or arg.default or arg.name for arg in @doc.args ? [])
owner = if @doc.owner is 'this' then @thang else window[@doc.owner]
content.replace /\#\{(.*?)\}/g, (s, properties) => @formatValue downTheChain(owner, properties.split('.'))
formatValue: (v) ->
return '[Function]' if not v and @doc.type is 'function'
unless v?
if @doc.owner is 'this'
v = @thang[@doc.name]
else
v = window[@doc.owner][@doc.name]
if @type is 'number' and not isNaN v
if v == Math.round v
return v
return v.toFixed 2
if _.isString v
return "\"#{v}\""
if v?.id
return v.id
if v?.name
return v.name
if _.isArray v
return '[' + (@formatValue v2 for v2 in v).join(', ') + ']'
v
2014-01-03 13:32:13 -05:00
onMouseOver: (e) ->
# Make sure the doc has the updated Thang so it can regenerate its prop value
@$el.data('bs.popover').options.content = @formatPopover()
2014-01-03 13:32:13 -05:00
@$el.popover('setContent')
onClick: (e) ->
Backbone.Mediator.publish 'tome:palette-clicked', thang: @thang, prop: @doc.name
2014-01-03 13:32:13 -05:00
onFrameChanged: (e) ->
return unless e.selectedThang?.id is @thang.id
@options.thang = @thang = e.selectedThang # Update our thang to the current version
@$el.find("code.current-value").text(@formatValue())
2014-02-11 14:31:02 -05:00
destroy: ->
@$el.off()
super()