diff --git a/vendor/plugins/design/lib/design/editor.rb b/vendor/plugins/design/lib/design/editor.rb index 4f75d33..2e076ba 100644 --- a/vendor/plugins/design/lib/design/editor.rb +++ b/vendor/plugins/design/lib/design/editor.rb @@ -1,7 +1,180 @@ module Design # This module contains the functionality for e design editor. + # + # FIXME: this helper module is still deeply broken, with code just copied + # from flexible_template module Editor + + #FIXME: move extraction of these values elsewhere + def start + @ft_config[:available_templates] = parse_path(Dir.glob("#{RAILS_ROOT}/public/templates/*"), 'templates/') + @ft_config[:available_themes] = parse_path(Dir.glob("#{RAILS_ROOT}/public/themes/*"), 'themes/') + @ft_config[:available_icon_themes] = parse_path(Dir.glob("#{RAILS_ROOT}/public/icons/*"), 'icons/') + end + + def flexible_template_edit_template? + @ft_config[:edit_mode] + end + + # Set the default template to the profile + def set_default_template + set_template(@ft_config[:owner],params[:template_name]) if exist_template? params[:template_name] + end + + # Set the default theme to the profile + def set_default_theme + set_theme(@ft_config[:owner],params[:theme_name]) if exist_theme? params[:theme_name] + end + + # Set the default icons theme to the profile + def set_default_icon_theme + set_icon_theme(@ft_config[:owner],params[:icon_theme_name]) if exist_icon_theme? params[:icon_theme_name] + end + + def flexible_template_set_sort_mode + box = @ft_config[:owner].boxes.find(params[:box_id]) + render :update do |page| + page.replace_html "box_#{box.number}", edit_blocks(box) + page.sortable "sort_#{box.number}", :url => {:action => 'flexible_template_sort_box', :box_number => box.number} + end + end + + def flexible_template_sort_box + box_number = params[:box_number] + pos = 0 + params["sort_#{box_number}"].each do |block_id| + pos = pos + 1 + #TO this is a security fail we have to limit the researh only in valid blocks of owners + b = Block.find(block_id) + b.position = pos + b.save + end + end + + # This method changes a block content to a different box place and + # updates all boxes at the ends + def flexible_template_change_box + #TODO fix it i tried the source code comment but i have no success + #b = @ft_config[:owner].blocks.detect{|b| b.id.to_s == params[:block].to_s } + b = Block.find(params[:block]) + b.box = @ft_config[:owner].boxes.find(params[:box_id]) + b.save + render :update do |page| + @ft_config[:owner].boxes.each do |box| + page.replace_html "box_#{box.number}", edit_blocks(box) + end + end + end + + def flexible_template_new_block + box = @ft_config[:owner].boxes.find(params[:box_id]) + render :update do |page| + page.replace_html "box_#{box.number}", new_block_form(box) + end + end + + def flexible_template_create_block + block = Block.new(params[:block]) + block.box = nil if !@ft_config[:boxes].include? block.box + block.position = block.box.blocks.count + 1 if !block.box.nil? + if block.save + render :update do |page| + page.replace_html "box_#{block.box.number}", edit_blocks(block.box) + end + else + render :update do |page| + page.replace_html "flexible_template_edit_mode", _('Block cannot be saved') + end + end + end + + def flexible_template_destroy_block + block = Block.find(params[:block_id]) + box = block.box + #TO check if the block is of the owner + block.destroy + render :update do |page| + page.replace_html "box_#{box.number}", edit_blocks(box) + end + end + + private + + # Set to the owner the theme choosed + def set_theme(object, theme_name) + object.flexible_template_theme = theme_name + object.save + end + + # Set to the owner the icons theme choosed + def set_icon_theme(object,icon_theme_name) + object.flexible_template_icon_theme = icon_theme_name + object.save + end + + # Set to the owner the template choosed + def set_template(object, template_name) + object.flexible_template_template = template_name + object.save + end + + #Load a set of boxes belongs to a owner. We have to situations. + # 1 - The owner has equal or more boxes that boxes defined in template. + # The system limit the max number of boxes to the number permited in template + # 2 - The owner there isn't enough box that defined in template + # The system create the boxes needed to use the current template + #If no chosen_template was selected the default template is set + def ft_load_boxes + n = boxes_by_template(@ft_config[:template]) + boxes = @ft_config[:owner].boxes + + if boxes.length >= n + boxes = boxes.first(n) + else + while boxes.length < n do + b = Box.new + b.owner = @ft_config[:owner] + raise _('The box cannot be saved becaus of erros %s') % b.errors if !b.save + boxes.push(b) + end + end + boxes + end + + # Return the number of boxes defined in template file. + # If the template file didn't exist the default template is loaded + def boxes_by_template(template) + template_def_file = Dir.glob("#{RAILS_ROOT}/public/templates/#{template}/*.yml") + + if template_def_file.length != 1 + 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] + return + end + + f = YAML.load_file(template_def_file.to_s) + number_of_boxes = f[template.to_s]["number_of_boxes"] + flash[:notice] = _("The file #{@ft_config[:template]}.yml it's not a valid template filename") if number_of_boxes.nil? + number_of_boxes + end + + def exist_template? template + @ft_config[:available_templates].include?(template) + end + + def exist_theme? theme + @ft_config[:available_themes].include?(theme) + end + + def exist_icon_theme? icon_theme + @ft_config[:available_icon_themes].include?(icon_theme) + end + + def parse_path(files_path = [], remove_until = 'public') + remove_until = remove_until.gsub(/\//, '\/') + files_path.map{|f| f.gsub(/.*#{remove_until}/, '')} + end + end end diff --git a/vendor/plugins/design/lib/design/editor/helper.rb b/vendor/plugins/design/lib/design/editor/helper.rb index ddb027b..0a637f5 100644 --- a/vendor/plugins/design/lib/design/editor/helper.rb +++ b/vendor/plugins/design/lib/design/editor/helper.rb @@ -3,6 +3,9 @@ module Design module Editor # defines helper methods for controllers that use +design_editor+ + # + # FIXME: this Helper module is still deeply broken, just copied code from + # flexible_template module Helper # draws the user interface for the design editor. @@ -37,6 +40,238 @@ module Design content = content_tag(:div, content, :id => 'flexible_template_edit_mode') end + # Symbol dictionary used on select when we add or edit a block. This + # method has the responsability of translate a Block class in a humam + # name By default the class "MainBlock" has the human name "Main Block". + # Other classes defined by user are not going to display in a human name + # format until de method flexible_template_block_dict be redefined in a + # controller by user + # #TODO define the method + # flexible_template_block_dict if not defined by helper + # if !self.public_instance_methods.include? "flexible_template_block_dict" + # define_method('flexible_template_block_dict') do |str| + # { + # 'MainBlock' => _("Main Block") + # }[str] || str + # end + # end + + + def flexible_template_block_helper_dict(str) + { + 'plain_content' => _('Plain Content') , + 'list_content' => _('List Content') + }[str] || str + end + + + private + + ################################################# + # TEMPLATES METHODS RELATED + ################################################# + + # Shows the blocks as defined in show_blocks adding the sortable and draggable elements. + # In this case the layout can be manipulated + def edit_blocks(box, main_content = "") + blocks = box.blocks_sort_by_position + [ + content_tag( + :ul,[ + box.name, + link_to_active_sort(box), + link_to_add_block(box), + blocks.map {|b| + [content_tag( + :li, + b.name + link_to_destroy_block(b), + :class =>"block_item_box_#{box.number}" , :id => "block_#{b.id}" + ), + draggable("block_#{b.id}")].join("\n") + }.join("\n")].join("\n"), :id => "sort_#{box.number}" + ), + drag_drop_items(box)].join("\n") + end + + def link_to_active_sort(box) + link_to_remote(_('Sort'), + {:update => "sort_#{box.number}", :url => {:action => 'flexible_template_set_sort_mode', :box_id => box.id }}, + :class => 'sort_button') + end + + def link_to_add_block(box) + link_to_remote(_('Add Block'), + {:update => "sort_#{box.number}", :url => {:action => 'flexible_template_new_block', :box_id => box.id }}, + :class => 'add_block_button') + end + + def link_to_destroy_block(block) + link_to_remote(_('Remove'), + {:update => "sort_#{block.box.number}", :url => {:action => 'flexible_template_destroy_block', :block_id => block.id }}, + :class => 'destroy_block_button') + end + + + # Allows the biven box to have sortable elements + def sortable_block(box_number) + sortable_element "sort_#{box_number}", + :url => {:action => 'flexible_template_sort_box', :box_number => box_number }, + :complete => visual_effect(:highlight, "sort_#{box_number}") + end + + # Allows an element item to be draggable + def draggable(item) + draggable_element(item, :ghosting => true, :revert => true) + end + + # Allows an draggable element change between diferrents boxes + def drag_drop_items(box) + boxes = @ft_config[:boxes].reject{|b| b.id == box.id} + + boxes.map{ |b| + drop_receiving_element("box_#{box.number}", + :accept => "block_item_box_#{b.number}", + :complete => "$('spinner').hide();", + :before => "$('spinner').show();", + :hoverclass => 'hover', + :with => "'block=' + encodeURIComponent(element.id.split('_').last())", + :url => {:action=>:flexible_template_change_box, :box_id => box.id}) + }.to_s + end + + + # Generate a select option to choose one of the available templates. + # The available templates are those in 'public/templates' + def select_template + available_templates = @ft_config[:available_templates] + + template_options = options_for_select(available_templates.map{|template| [template, template] }, @ft_config[:template]) + [ select_tag('template_name', template_options ), + change_template].join("\n") + end + + # Generate a observer to reload a page when a template is selected + def change_template + observe_field( 'template_name', + :url => {:action => 'set_default_template'}, + :with =>"'template_name=' + escape(value) + '&object_id=' + escape(#{@ft_config[:owner].id})", + :complete => "document.location.reload();" + ) + end + + def available_blocks +#TOD O check if are valids blocks + h = { + 'MainBlock' => _("Main Block"), + } + h.merge!(controller.class::FLEXIBLE_TEMPLATE_AVAILABLE_BLOCKS) if controller.class.constants.include? "FLEXIBLE_TEMPLATE_AVAILABLE_BLOCKS" + h + end + + def block_helpers +#TOD O check if are valids helpers + h = { + 'plain_content' => _("Plain Content"), + 'list_content' => _("Simple List Content"), + } + h.merge!(controller.class::FLEXIBLE_TEMPLATE_BLOCK_HELPER) if controller.class.constants.include? "FLEXIBLE_TEMPLATE_BLOCK_HELPER" + h + end + + def new_block_form(box) + type_block_options = options_for_select(available_blocks.collect{|k,v| [v,k] }) + type_block_helper_options = options_for_select(block_helpers.collect{|k,v| [v,k] }) + @block = Block.new + @block.box = box + + _("Adding block on %s") % box.name + + [ + form_remote_tag(:url => {:action => 'flexible_template_create_block'}, :update => "sort_#{box.number}"), + hidden_field('block', 'box_id'), + content_tag( + :p, + [ + content_tag( + :label, _('Name:') + ), + text_field('block', 'name') + ].join("\n") + ), + content_tag( + :p, + [ + content_tag( + :label, _('Title:') + ), + text_field('block', 'title') + ].join("\n") + ), + content_tag( + :p, + [ + content_tag( + :label, _('Type:') + ), + select_tag('block[type]', type_block_options) + ].join("\n") + ), + content_tag( + :p, + [ + content_tag( + :label, _('Visualization Mode:') + ), + select_tag('block[helper]', type_block_helper_options) + ].join("\n") + ), + submit_tag( _('Submit')), + end_form_tag + ].join("\n") + + end + + # Generate a select option to choose one of the available themes. + # The available themes are those in 'public/themes' + def select_theme + available_themes = @ft_config[:available_themes] + theme_options = options_for_select(available_themes.map{|theme| [theme, theme] }, @ft_config[:theme]) + [ select_tag('theme_name', theme_options ), + change_theme].join("\n") + end + + # Generate a observer to reload a page when a theme is selected + def change_theme + observe_field( 'theme_name', + :url => {:action => 'set_default_theme'}, + :with =>"'theme_name=' + escape(value) + '&object_id=' + escape(#{@ft_config[:owner].id})", + :complete => "document.location.reload();" + ) + end + + + ################################################# + #ICONS THEMES RELATED + ################################################# + + # Generate a select option to choose one of the available icons themes. + # The available icons themes are those in 'public/icons' + def select_icon_theme + available_icon_themes = @ft_config[:available_icon_themes] + icon_theme_options = options_for_select(available_icon_themes.map{|icon_theme| [icon_theme, icon_theme] }, @ft_config[:icon_theme]) + [ select_tag('icon_theme_name', icon_theme_options ), + change_icon_theme].join("\n") + end + + # Generate a observer to reload a page when a icons theme is selected + def change_icon_theme + observe_field( 'icon_theme_name', + :url => {:action => 'set_default_icon_theme'}, + :with =>"'icon_theme_name=' + escape(value) + '&object_id=' + escape(#{@ft_config[:owner].id})", + :complete => "document.location.reload();" + ) + end + + end # END OF module Helper end # END OF module Editor -- libgit2 0.21.2