editable long text implemented + refactoring + ui

This commit is contained in:
dinedine 2010-08-30 01:41:44 +02:00
parent af5fe62d89
commit d7aba616f6
11 changed files with 88 additions and 76 deletions

View File

@ -3,6 +3,7 @@ class EditableElement
include Mongoid::Document include Mongoid::Document
## fields ## ## fields ##
field :kind
field :slug field :slug
field :block field :block
field :content field :content
@ -23,4 +24,8 @@ class EditableElement
self.read_attribute(:content).blank? ? self.default_content : self.read_attribute(:content) self.read_attribute(:content).blank? ? self.default_content : self.read_attribute(:content)
end end
def short_text?; self.kind == 'ShortText'; end
def long_text?; self.kind == 'LongText'; end
end end

View File

@ -17,4 +17,7 @@
%ol %ol
- elements.each_with_index do |el, index| - elements.each_with_index do |el, index|
= f.fields_for 'editable_elements', el, :child_index => el._index do |g| = f.fields_for 'editable_elements', el, :child_index => el._index do |g|
- if el.short_text?
= g.input :content, :label => el.slug.humanize, :hint => el.hint = g.input :content, :label => el.slug.humanize, :hint => el.hint
- elsif el.long_text?
= g.input :content, :label => el.slug.humanize, :hint => el.hint, :as => :text, :input_html => { :class => 'html' }

View File

@ -1,5 +1,5 @@
- content_for :head do - content_for :head do
= javascript_include_tag 'admin/plugins/codemirror/codemirror', 'admin/pages', 'admin/editable_elements' = javascript_include_tag 'admin/plugins/tiny_mce/tinymce', 'admin/plugins/codemirror/codemirror', 'admin/pages', 'admin/editable_elements'
= stylesheet_link_tag 'admin/editable_elements' = stylesheet_link_tag 'admin/editable_elements'
= image_picker_include_tags = image_picker_include_tags

View File

@ -6,7 +6,8 @@ x bug editable_xxx disabled for nil block
x display liquid errors x display liquid errors
x theme assets selector in page editor x theme assets selector in page editor
x saving page in ajax x saving page in ajax
- editable_long_text tag x editable_long_text tag
- blocking issue when modifying the parent of 2 templates => one of the 2 children has reference of the first child
- editable_file tag - editable_file tag
- refactor slugify method (use parameterize + create a module) - refactor slugify method (use parameterize + create a module)

View File

@ -0,0 +1,2 @@
require 'locomotive/liquid/tags/editable/short_text'
require 'locomotive/liquid/tags/editable/long_text'

View File

@ -0,0 +1,12 @@
module Locomotive
module Liquid
module Tags
module Editable
class LongText < ShortText
end
::Liquid::Template.register_tag('editable_long_text', LongText)
end
end
end
end

View File

@ -0,0 +1,58 @@
module Locomotive
module Liquid
module Tags
module Editable
class ShortText < ::Liquid::Block
Syntax = /(#{::Liquid::QuotedFragment})(\s*,\s*#{::Liquid::Expression}+)?/
def initialize(tag_name, markup, tokens, context)
if markup =~ Syntax
@slug = $1.gsub('\'', '')
@options = {}
markup.scan(::Liquid::TagAttributes) { |key, value| @options[key.to_sym] = value.gsub(/^'/, '').gsub(/'$/, '') }
else
raise ::Liquid::SyntaxError.new("Syntax Error in 'editable_short_text' - Valid syntax: editable_short_text <slug>(, <options>)")
end
super
end
def end_tag
@context[:page].add_or_update_editable_element({
:kind => self.kind,
:block => @context[:current_block].try(:name),
:slug => @slug,
:hint => @options[:hint],
:default_content => @nodelist.first.to_s,
:disabled => false,
:from_parent => false
})
end
def render(context)
current_page = context.registers[:page]
element = current_page.find_editable_element(context['block'].try(:name), @slug)
if element
element.content
else
Locomotive.logger "[editable short text] missing editable short text #{context[:block].name} / #{@slug}"
''
end
end
protected
def kind
self.class.name.demodulize
end
end
::Liquid::Template.register_tag('editable_short_text', ShortText)
end
end
end
end

View File

@ -1,50 +0,0 @@
module Locomotive
module Liquid
module Tags
class EditableShortText < ::Liquid::Block
Syntax = /(#{::Liquid::QuotedFragment})(\s*,\s*#{::Liquid::Expression}+)?/
def initialize(tag_name, markup, tokens, context)
if markup =~ Syntax
@slug = $1.gsub('\'', '')
@options = {}
markup.scan(::Liquid::TagAttributes) { |key, value| @options[key.to_sym] = value.gsub(/^'/, '').gsub(/'$/, '') }
else
raise ::Liquid::SyntaxError.new("Syntax Error in 'editable_short_text' - Valid syntax: editable_short_text <slug>(, <options>)")
end
super
# puts "context = #{context.object_id} / #{@context[:page]}"
end
def end_tag
@context[:page].add_or_update_editable_element({
:block => @context[:current_block].try(:name),
:slug => @slug,
:hint => @options[:hint],
:default_content => @nodelist.first.to_s,
:disabled => false,
:from_parent => false
})
end
def render(context)
current_page = context.registers[:page]
# puts "[EditableShortText] rendering #{current_page.editable_elements.inspect}"
element = current_page.find_editable_element(context['block'].try(:name), @slug)
if element
element.content
else
Locomotive.logger "[editable short text] missing editable short text #{context[:block].name} / #{@slug}"
''
end
end
::Liquid::Template.register_tag('editable_short_text', EditableShortText)
end
end
end
end

View File

@ -32,30 +32,9 @@ module Locomotive
protected protected
# def register_current_block
# @context[:blocks] ||= {}
#
# block = @context[:blocks][@name]
#
# if block
# # needed for the block.super statement
# # puts "[BLOCK #{@name}|end_tag] nodelist #{@nodelist.inspect}"
# block.add_parent(@nodelist)
#
# @parent = block.parent
# @nodelist = block.nodelist
#
# # puts "[BLOCK #{@name}|end_tag] direct parent #{block.parent.inspect}"
# else
# # register it
# # puts "[BLOCK #{@name}|end_tag] register it"
# @context[:blocks][@name] = self
# end
# end
def contains_super?(nodelist) def contains_super?(nodelist)
nodelist.any? do |node| nodelist.any? do |node|
if node.is_a?(String) && node =~ /\{\{\s*block.super\s*\}\}/ if node.is_a?(::Liquid::Variable) && node.name == 'block.super'
true true
elsif node.respond_to?(:nodelist) && !node.is_a?(Locomotive::Liquid::Tags::InheritedBlock) elsif node.respond_to?(:nodelist) && !node.is_a?(Locomotive::Liquid::Tags::InheritedBlock)
contains_super?(node.nodelist) contains_super?(node.nodelist)

View File

@ -23,4 +23,6 @@ $(document).ready(function() {
} }
}, []); }, []);
$('textarea.html').tinymce(TinyMceDefaultSettings);
}); });