2010-08-31 21:53:30 +00:00
|
|
|
module Locomotive
|
|
|
|
module Liquid
|
|
|
|
module Tags
|
|
|
|
module Editable
|
|
|
|
class Base < ::Liquid::Block
|
|
|
|
|
|
|
|
Syntax = /(#{::Liquid::QuotedFragment})(\s*,\s*#{::Liquid::Expression}+)?/
|
|
|
|
|
|
|
|
def initialize(tag_name, markup, tokens, context)
|
|
|
|
if markup =~ Syntax
|
|
|
|
@slug = $1.gsub('\'', '')
|
2011-03-02 01:13:45 +00:00
|
|
|
@options = { :assignable => true }
|
2010-08-31 21:53:30 +00:00
|
|
|
markup.scan(::Liquid::TagAttributes) { |key, value| @options[key.to_sym] = value.gsub(/^'/, '').gsub(/'$/, '') }
|
|
|
|
else
|
|
|
|
raise ::Liquid::SyntaxError.new("Syntax Error in 'editable_xxx' - Valid syntax: editable_xxx <slug>(, <options>)")
|
|
|
|
end
|
|
|
|
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
|
|
|
def end_tag
|
2011-01-14 07:27:58 +00:00
|
|
|
super
|
2011-07-06 23:34:11 +00:00
|
|
|
|
2011-01-14 07:27:58 +00:00
|
|
|
if @context[:page].present?
|
|
|
|
@context[:page].add_or_update_editable_element({
|
|
|
|
:block => @context[:current_block].try(:name),
|
|
|
|
:slug => @slug,
|
|
|
|
:hint => @options[:hint],
|
2011-03-02 06:38:32 +00:00
|
|
|
:default_attribute => @options[:default],
|
2011-07-06 16:47:28 +00:00
|
|
|
:default_content => default_content_option,
|
2011-03-02 01:13:45 +00:00
|
|
|
:assignable => @options[:assignable],
|
2011-01-14 07:27:58 +00:00
|
|
|
:disabled => false,
|
|
|
|
:from_parent => false
|
|
|
|
}, document_type)
|
|
|
|
end
|
2010-08-31 21:53:30 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def render(context)
|
|
|
|
current_page = context.registers[:page]
|
|
|
|
|
|
|
|
element = current_page.find_editable_element(context['block'].try(:name), @slug)
|
2011-07-06 23:34:11 +00:00
|
|
|
|
2011-03-02 02:05:50 +00:00
|
|
|
if element.present?
|
2011-07-06 16:47:28 +00:00
|
|
|
unless element.default_content.present?
|
|
|
|
element.default_content = render_default_content(context)
|
|
|
|
end
|
2010-09-02 23:03:20 +00:00
|
|
|
render_element(context, element)
|
2010-08-31 21:53:30 +00:00
|
|
|
else
|
2011-07-26 19:20:03 +00:00
|
|
|
Locomotive.log :error, "[editable element] missing element `#{context['block'].try(:name)}` / #{@slug} on #{current_page.fullpath}"
|
2010-08-31 21:53:30 +00:00
|
|
|
''
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def render_element(element)
|
|
|
|
raise 'FIXME: has to be overidden'
|
|
|
|
end
|
|
|
|
|
|
|
|
def document_type
|
|
|
|
raise 'FIXME: has to be overidden'
|
|
|
|
end
|
2011-07-06 23:34:11 +00:00
|
|
|
|
2011-07-06 16:47:28 +00:00
|
|
|
def default_content_option
|
|
|
|
result = nil
|
2011-03-02 03:28:54 +00:00
|
|
|
if @options[:default].present?
|
2011-07-06 16:47:28 +00:00
|
|
|
result = @context[:page].send(@options[:default])
|
2011-03-02 03:28:54 +00:00
|
|
|
end
|
2011-07-06 16:47:28 +00:00
|
|
|
result
|
|
|
|
end
|
2011-07-06 23:34:11 +00:00
|
|
|
|
2011-07-06 16:47:28 +00:00
|
|
|
def render_default_content(context)
|
2011-07-13 09:14:44 +00:00
|
|
|
render_all(@nodelist, context).join(' ')
|
2011-03-02 03:28:54 +00:00
|
|
|
end
|
2010-08-31 21:53:30 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|