Commit 11461cf62d27825fd0449a5f86779e44a0b5b9fd

Authored by AntonioTerceiro
1 parent f4d329de

r238@sede: terceiro | 2007-07-28 19:40:52 -0300

ActionItem0: adding code from flexible template to be reworked later
 


git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@242 3f533792-8f58-4932-b0fe-aaf55b0a4547
vendor/plugins/design/lib/design/editor.rb
1 1 module Design
2 2  
3 3 # This module contains the functionality for e design editor.
  4 + #
  5 + # FIXME: this helper module is still deeply broken, with code just copied
  6 + # from flexible_template
4 7 module Editor
  8 +
  9 + #FIXME: move extraction of these values elsewhere
  10 + def start
  11 + @ft_config[:available_templates] = parse_path(Dir.glob("#{RAILS_ROOT}/public/templates/*"), 'templates/')
  12 + @ft_config[:available_themes] = parse_path(Dir.glob("#{RAILS_ROOT}/public/themes/*"), 'themes/')
  13 + @ft_config[:available_icon_themes] = parse_path(Dir.glob("#{RAILS_ROOT}/public/icons/*"), 'icons/')
  14 + end
  15 +
  16 + def flexible_template_edit_template?
  17 + @ft_config[:edit_mode]
  18 + end
  19 +
  20 + # Set the default template to the profile
  21 + def set_default_template
  22 + set_template(@ft_config[:owner],params[:template_name]) if exist_template? params[:template_name]
  23 + end
  24 +
  25 + # Set the default theme to the profile
  26 + def set_default_theme
  27 + set_theme(@ft_config[:owner],params[:theme_name]) if exist_theme? params[:theme_name]
  28 + end
  29 +
  30 + # Set the default icons theme to the profile
  31 + def set_default_icon_theme
  32 + set_icon_theme(@ft_config[:owner],params[:icon_theme_name]) if exist_icon_theme? params[:icon_theme_name]
  33 + end
  34 +
  35 + def flexible_template_set_sort_mode
  36 + box = @ft_config[:owner].boxes.find(params[:box_id])
  37 + render :update do |page|
  38 + page.replace_html "box_#{box.number}", edit_blocks(box)
  39 + page.sortable "sort_#{box.number}", :url => {:action => 'flexible_template_sort_box', :box_number => box.number}
  40 + end
  41 + end
  42 +
  43 + def flexible_template_sort_box
  44 + box_number = params[:box_number]
  45 + pos = 0
  46 + params["sort_#{box_number}"].each do |block_id|
  47 + pos = pos + 1
  48 + #TO this is a security fail we have to limit the researh only in valid blocks of owners
  49 + b = Block.find(block_id)
  50 + b.position = pos
  51 + b.save
  52 + end
  53 + end
  54 +
  55 + # This method changes a block content to a different box place and
  56 + # updates all boxes at the ends
  57 + def flexible_template_change_box
  58 + #TODO fix it i tried the source code comment but i have no success
  59 + #b = @ft_config[:owner].blocks.detect{|b| b.id.to_s == params[:block].to_s }
  60 + b = Block.find(params[:block])
  61 + b.box = @ft_config[:owner].boxes.find(params[:box_id])
  62 + b.save
  63 + render :update do |page|
  64 + @ft_config[:owner].boxes.each do |box|
  65 + page.replace_html "box_#{box.number}", edit_blocks(box)
  66 + end
  67 + end
  68 + end
  69 +
  70 + def flexible_template_new_block
  71 + box = @ft_config[:owner].boxes.find(params[:box_id])
  72 + render :update do |page|
  73 + page.replace_html "box_#{box.number}", new_block_form(box)
  74 + end
  75 + end
  76 +
  77 + def flexible_template_create_block
  78 + block = Block.new(params[:block])
  79 + block.box = nil if !@ft_config[:boxes].include? block.box
  80 + block.position = block.box.blocks.count + 1 if !block.box.nil?
  81 + if block.save
  82 + render :update do |page|
  83 + page.replace_html "box_#{block.box.number}", edit_blocks(block.box)
  84 + end
  85 + else
  86 + render :update do |page|
  87 + page.replace_html "flexible_template_edit_mode", _('Block cannot be saved')
  88 + end
  89 + end
  90 + end
  91 +
  92 + def flexible_template_destroy_block
  93 + block = Block.find(params[:block_id])
  94 + box = block.box
  95 + #TO check if the block is of the owner
  96 + block.destroy
  97 + render :update do |page|
  98 + page.replace_html "box_#{box.number}", edit_blocks(box)
  99 + end
  100 + end
  101 +
  102 + private
  103 +
  104 + # Set to the owner the theme choosed
  105 + def set_theme(object, theme_name)
  106 + object.flexible_template_theme = theme_name
  107 + object.save
  108 + end
  109 +
  110 + # Set to the owner the icons theme choosed
  111 + def set_icon_theme(object,icon_theme_name)
  112 + object.flexible_template_icon_theme = icon_theme_name
  113 + object.save
  114 + end
  115 +
  116 + # Set to the owner the template choosed
  117 + def set_template(object, template_name)
  118 + object.flexible_template_template = template_name
  119 + object.save
  120 + end
  121 +
  122 + #Load a set of boxes belongs to a owner. We have to situations.
  123 + # 1 - The owner has equal or more boxes that boxes defined in template.
  124 + # The system limit the max number of boxes to the number permited in template
  125 + # 2 - The owner there isn't enough box that defined in template
  126 + # The system create the boxes needed to use the current template
  127 + #If no chosen_template was selected the default template is set
  128 + def ft_load_boxes
  129 + n = boxes_by_template(@ft_config[:template])
  130 + boxes = @ft_config[:owner].boxes
  131 +
  132 + if boxes.length >= n
  133 + boxes = boxes.first(n)
  134 + else
  135 + while boxes.length < n do
  136 + b = Box.new
  137 + b.owner = @ft_config[:owner]
  138 + raise _('The box cannot be saved becaus of erros %s') % b.errors if !b.save
  139 + boxes.push(b)
  140 + end
  141 + end
  142 + boxes
  143 + end
  144 +
  145 + # Return the number of boxes defined in template file.
  146 + # If the template file didn't exist the default template is loaded
  147 + def boxes_by_template(template)
  148 + template_def_file = Dir.glob("#{RAILS_ROOT}/public/templates/#{template}/*.yml")
  149 +
  150 + if template_def_file.length != 1
  151 + flash[:notice] = _("The template %s is not a valid template. You don't have the %s.yml file or there are more than one yml file to define your template") % [template, template]
  152 + return
  153 + end
  154 +
  155 + f = YAML.load_file(template_def_file.to_s)
  156 + number_of_boxes = f[template.to_s]["number_of_boxes"]
  157 + flash[:notice] = _("The file #{@ft_config[:template]}.yml it's not a valid template filename") if number_of_boxes.nil?
  158 + number_of_boxes
  159 + end
  160 +
  161 + def exist_template? template
  162 + @ft_config[:available_templates].include?(template)
  163 + end
  164 +
  165 + def exist_theme? theme
  166 + @ft_config[:available_themes].include?(theme)
  167 + end
  168 +
  169 + def exist_icon_theme? icon_theme
  170 + @ft_config[:available_icon_themes].include?(icon_theme)
  171 + end
  172 +
  173 + def parse_path(files_path = [], remove_until = 'public')
  174 + remove_until = remove_until.gsub(/\//, '\/')
  175 + files_path.map{|f| f.gsub(/.*#{remove_until}/, '')}
  176 + end
  177 +
5 178 end
6 179  
7 180 end
... ...
vendor/plugins/design/lib/design/editor/helper.rb
... ... @@ -3,6 +3,9 @@ module Design
3 3 module Editor
4 4  
5 5 # defines helper methods for controllers that use +design_editor+
  6 + #
  7 + # FIXME: this Helper module is still deeply broken, just copied code from
  8 + # flexible_template
6 9 module Helper
7 10  
8 11 # draws the user interface for the design editor.
... ... @@ -37,6 +40,238 @@ module Design
37 40 content = content_tag(:div, content, :id => 'flexible_template_edit_mode')
38 41 end
39 42  
  43 + # Symbol dictionary used on select when we add or edit a block. This
  44 + # method has the responsability of translate a Block class in a humam
  45 + # name By default the class "MainBlock" has the human name "Main Block".
  46 + # Other classes defined by user are not going to display in a human name
  47 + # format until de method flexible_template_block_dict be redefined in a
  48 + # controller by user
  49 + # #TODO define the method
  50 + # flexible_template_block_dict if not defined by helper
  51 + # if !self.public_instance_methods.include? "flexible_template_block_dict"
  52 + # define_method('flexible_template_block_dict') do |str|
  53 + # {
  54 + # 'MainBlock' => _("Main Block")
  55 + # }[str] || str
  56 + # end
  57 + # end
  58 +
  59 +
  60 + def flexible_template_block_helper_dict(str)
  61 + {
  62 + 'plain_content' => _('Plain Content') ,
  63 + 'list_content' => _('List Content')
  64 + }[str] || str
  65 + end
  66 +
  67 +
  68 + private
  69 +
  70 + #################################################
  71 + # TEMPLATES METHODS RELATED
  72 + #################################################
  73 +
  74 + # Shows the blocks as defined in <tt>show_blocks</tt> adding the sortable and draggable elements.
  75 + # In this case the layout can be manipulated
  76 + def edit_blocks(box, main_content = "")
  77 + blocks = box.blocks_sort_by_position
  78 + [
  79 + content_tag(
  80 + :ul,[
  81 + box.name,
  82 + link_to_active_sort(box),
  83 + link_to_add_block(box),
  84 + blocks.map {|b|
  85 + [content_tag(
  86 + :li,
  87 + b.name + link_to_destroy_block(b),
  88 + :class =>"block_item_box_#{box.number}" , :id => "block_#{b.id}"
  89 + ),
  90 + draggable("block_#{b.id}")].join("\n")
  91 + }.join("\n")].join("\n"), :id => "sort_#{box.number}"
  92 + ),
  93 + drag_drop_items(box)].join("\n")
  94 + end
  95 +
  96 + def link_to_active_sort(box)
  97 + link_to_remote(_('Sort'),
  98 + {:update => "sort_#{box.number}", :url => {:action => 'flexible_template_set_sort_mode', :box_id => box.id }},
  99 + :class => 'sort_button')
  100 + end
  101 +
  102 + def link_to_add_block(box)
  103 + link_to_remote(_('Add Block'),
  104 + {:update => "sort_#{box.number}", :url => {:action => 'flexible_template_new_block', :box_id => box.id }},
  105 + :class => 'add_block_button')
  106 + end
  107 +
  108 + def link_to_destroy_block(block)
  109 + link_to_remote(_('Remove'),
  110 + {:update => "sort_#{block.box.number}", :url => {:action => 'flexible_template_destroy_block', :block_id => block.id }},
  111 + :class => 'destroy_block_button')
  112 + end
  113 +
  114 +
  115 + # Allows the biven box to have sortable elements
  116 + def sortable_block(box_number)
  117 + sortable_element "sort_#{box_number}",
  118 + :url => {:action => 'flexible_template_sort_box', :box_number => box_number },
  119 + :complete => visual_effect(:highlight, "sort_#{box_number}")
  120 + end
  121 +
  122 + # Allows an element item to be draggable
  123 + def draggable(item)
  124 + draggable_element(item, :ghosting => true, :revert => true)
  125 + end
  126 +
  127 + # Allows an draggable element change between diferrents boxes
  128 + def drag_drop_items(box)
  129 + boxes = @ft_config[:boxes].reject{|b| b.id == box.id}
  130 +
  131 + boxes.map{ |b|
  132 + drop_receiving_element("box_#{box.number}",
  133 + :accept => "block_item_box_#{b.number}",
  134 + :complete => "$('spinner').hide();",
  135 + :before => "$('spinner').show();",
  136 + :hoverclass => 'hover',
  137 + :with => "'block=' + encodeURIComponent(element.id.split('_').last())",
  138 + :url => {:action=>:flexible_template_change_box, :box_id => box.id})
  139 + }.to_s
  140 + end
  141 +
  142 +
  143 + # Generate a select option to choose one of the available templates.
  144 + # The available templates are those in 'public/templates'
  145 + def select_template
  146 + available_templates = @ft_config[:available_templates]
  147 +
  148 + template_options = options_for_select(available_templates.map{|template| [template, template] }, @ft_config[:template])
  149 + [ select_tag('template_name', template_options ),
  150 + change_template].join("\n")
  151 + end
  152 +
  153 + # Generate a observer to reload a page when a template is selected
  154 + def change_template
  155 + observe_field( 'template_name',
  156 + :url => {:action => 'set_default_template'},
  157 + :with =>"'template_name=' + escape(value) + '&object_id=' + escape(#{@ft_config[:owner].id})",
  158 + :complete => "document.location.reload();"
  159 + )
  160 + end
  161 +
  162 + def available_blocks
  163 +#TOD O check if are valids blocks
  164 + h = {
  165 + 'MainBlock' => _("Main Block"),
  166 + }
  167 + h.merge!(controller.class::FLEXIBLE_TEMPLATE_AVAILABLE_BLOCKS) if controller.class.constants.include? "FLEXIBLE_TEMPLATE_AVAILABLE_BLOCKS"
  168 + h
  169 + end
  170 +
  171 + def block_helpers
  172 +#TOD O check if are valids helpers
  173 + h = {
  174 + 'plain_content' => _("Plain Content"),
  175 + 'list_content' => _("Simple List Content"),
  176 + }
  177 + h.merge!(controller.class::FLEXIBLE_TEMPLATE_BLOCK_HELPER) if controller.class.constants.include? "FLEXIBLE_TEMPLATE_BLOCK_HELPER"
  178 + h
  179 + end
  180 +
  181 + def new_block_form(box)
  182 + type_block_options = options_for_select(available_blocks.collect{|k,v| [v,k] })
  183 + type_block_helper_options = options_for_select(block_helpers.collect{|k,v| [v,k] })
  184 + @block = Block.new
  185 + @block.box = box
  186 +
  187 + _("Adding block on %s") % box.name +
  188 + [
  189 + form_remote_tag(:url => {:action => 'flexible_template_create_block'}, :update => "sort_#{box.number}"),
  190 + hidden_field('block', 'box_id'),
  191 + content_tag(
  192 + :p,
  193 + [
  194 + content_tag(
  195 + :label, _('Name:')
  196 + ),
  197 + text_field('block', 'name')
  198 + ].join("\n")
  199 + ),
  200 + content_tag(
  201 + :p,
  202 + [
  203 + content_tag(
  204 + :label, _('Title:')
  205 + ),
  206 + text_field('block', 'title')
  207 + ].join("\n")
  208 + ),
  209 + content_tag(
  210 + :p,
  211 + [
  212 + content_tag(
  213 + :label, _('Type:')
  214 + ),
  215 + select_tag('block[type]', type_block_options)
  216 + ].join("\n")
  217 + ),
  218 + content_tag(
  219 + :p,
  220 + [
  221 + content_tag(
  222 + :label, _('Visualization Mode:')
  223 + ),
  224 + select_tag('block[helper]', type_block_helper_options)
  225 + ].join("\n")
  226 + ),
  227 + submit_tag( _('Submit')),
  228 + end_form_tag
  229 + ].join("\n")
  230 +
  231 + end
  232 +
  233 + # Generate a select option to choose one of the available themes.
  234 + # The available themes are those in 'public/themes'
  235 + def select_theme
  236 + available_themes = @ft_config[:available_themes]
  237 + theme_options = options_for_select(available_themes.map{|theme| [theme, theme] }, @ft_config[:theme])
  238 + [ select_tag('theme_name', theme_options ),
  239 + change_theme].join("\n")
  240 + end
  241 +
  242 + # Generate a observer to reload a page when a theme is selected
  243 + def change_theme
  244 + observe_field( 'theme_name',
  245 + :url => {:action => 'set_default_theme'},
  246 + :with =>"'theme_name=' + escape(value) + '&object_id=' + escape(#{@ft_config[:owner].id})",
  247 + :complete => "document.location.reload();"
  248 + )
  249 + end
  250 +
  251 +
  252 + #################################################
  253 + #ICONS THEMES RELATED
  254 + #################################################
  255 +
  256 + # Generate a select option to choose one of the available icons themes.
  257 + # The available icons themes are those in 'public/icons'
  258 + def select_icon_theme
  259 + available_icon_themes = @ft_config[:available_icon_themes]
  260 + icon_theme_options = options_for_select(available_icon_themes.map{|icon_theme| [icon_theme, icon_theme] }, @ft_config[:icon_theme])
  261 + [ select_tag('icon_theme_name', icon_theme_options ),
  262 + change_icon_theme].join("\n")
  263 + end
  264 +
  265 + # Generate a observer to reload a page when a icons theme is selected
  266 + def change_icon_theme
  267 + observe_field( 'icon_theme_name',
  268 + :url => {:action => 'set_default_icon_theme'},
  269 + :with =>"'icon_theme_name=' + escape(value) + '&object_id=' + escape(#{@ft_config[:owner].id})",
  270 + :complete => "document.location.reload();"
  271 + )
  272 + end
  273 +
  274 +
40 275 end # END OF module Helper
41 276  
42 277 end # END OF module Editor
... ...