|
SirTrevor.Blocks.Panel = SirTrevor.Block.extend { |
|
|
|
type: 'panel' |
|
title: 'Panel' |
|
icon_name: 'panel' |
|
controllable: true |
|
|
|
styles: |
|
top_border: 'Top border', left_border: 'Left border', filled: 'Filled', exclamation: '!' |
|
colors: |
|
gray: 'Gray', orange: 'Orange', green: 'Green', red: 'Red', blue: 'Blue' |
|
|
|
controlsHTML: |
|
'<div class="controls" style="display: none"> |
|
<label>Style: <select name="style"/></label> |
|
<span class="sep"/> |
|
<label>Color: <select name="color"/></label> |
|
</div>' |
|
|
|
editorHTML: () -> |
|
_.result(this, 'controlsHTML') + |
|
'<div class="rc-panel"> |
|
<div class="rc-panel-body"></div> |
|
</div>' |
|
|
|
constructor: (data, instance_id, sirTrevor) -> |
|
SirTrevor.Block.apply(this, arguments) |
|
|
|
getControls: -> |
|
this.$inner.children('.controls') |
|
|
|
toggleControls: -> |
|
this.getControls().slideToggle() |
|
|
|
getPanel: -> |
|
this.$inner.children('.rc-panel') |
|
|
|
getBlocksContainer: -> |
|
this.$inner.children('.rc-panel').children('.rc-panel-body') |
|
|
|
beforeBlockRender: -> |
|
@controls = |
|
configure: @toggleControls |
|
|
|
_setBlockInner: -> |
|
SirTrevor.Block.prototype._setBlockInner.apply(this, arguments) |
|
|
|
$container = this.getBlocksContainer() |
|
plus = new SirTrevor.FloatingBlockControls($container, @instanceID) |
|
this.listenTo(plus, 'showBlockControls', @sirTrevor.showBlockControls) |
|
$container.prepend(plus.render().$el) |
|
|
|
$styles_select = this.findControl('[name=style]') |
|
for own s of @styles |
|
$styles_select.append $('<option>', value: s).text(@styles[s]) |
|
|
|
$colors_select = this.findControl('[name=color]') |
|
for own c of @colors |
|
$colors_select.append $('<option>', value: c).text(@colors[c]) |
|
|
|
onBlockRender: -> |
|
to_class = (name, value) -> "rc-panel--#{name}_#{value}" |
|
|
|
$styles_select = this.findControl('[name=style]') |
|
$styles_select.on 'change click', => |
|
for own s of @styles |
|
this.getPanel().toggleClass to_class('style', s), s == $styles_select.val() |
|
$styles_select.trigger 'change' |
|
|
|
$colors_select = this.findControl('[name=color]') |
|
$colors_select.on 'change click', => |
|
for own c of @colors |
|
this.getPanel().toggleClass to_class('color', c), c == $colors_select.val() |
|
$colors_select.trigger 'change' |
|
|
|
if this.getBlocksContainer().children('.st-block').length == 0 |
|
$parent = this.getBlocksContainer().children('.st-block-controls__top') |
|
@sirTrevor.createBlock('text', {}, $parent) |
|
|
|
findControl: (selector) -> |
|
this.getControls().find(selector) |
|
|
|
toData: -> |
|
data = { blocks: [] } |
|
|
|
this.getBlocksContainer().children('.st-block').each (i, el) => |
|
block = @sirTrevor.findBlockById el.getAttribute('id') |
|
data.blocks.push block.saveAndReturnData() |
|
|
|
this.findControl(':input').each -> |
|
if $(this).is('input[type=checkbox]') |
|
data[this.getAttribute('name')] = if $(this).is(':checked') then $(this).val() else null |
|
else |
|
data[this.getAttribute('name')] = $(this).val() |
|
return true # continue |
|
|
|
this.setData data |
|
|
|
loadData: (data) -> |
|
for own i, v of data |
|
$input = this.findControl('select, input').filter('[name=' + i + ']') |
|
if $input.is('input[type=checkbox]') |
|
if v then $input.attr('checked', 'true') else $input.removeAttr('checked') |
|
else |
|
$input.val(v) |
|
|
|
blocks_data = (data.blocks || []) |
|
$block = null |
|
|
|
for block in blocks_data |
|
$parent = if $block then $block.$el else this.getBlocksContainer().children('.st-block-controls__top') |
|
$block = @sirTrevor.createBlock(block.type, block.data, $parent) |
|
|
|
}; |