application_helper.rb
4.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# Methods added to this helper will be available to all templates in the application.
module ApplicationHelper
REJECTED_DIRS = %w[
.
..
.svn
]
# This method expect an array of boxes and the content retuned of a controller action
# It will generate the boxes div according the yaml definition
def display_boxes(boxes, main_content = "")
# If no boxes is passed return the main content
return main_content if boxes.nil?
#Generate all boxes of the current profile and considering the defined on template.
content = boxes.map do |box|
content_tag(:div, edit_mode? ? edit_blocks(box, main_content) : show_blocks(box, main_content) , :id=>"box_#{box.number}")
end
#In case of edit mode add a new div with a class named 'edit_mode' covering all div boxes.
content = content_tag(:div, content, :class => 'edit_mode') if edit_mode?
content
end
# Load all the css files of a existing template with the template_name passed as argument.
#
# The files loaded are in the path:
#
# 'public/templates/#{template_name}/stylesheets/*'
#TODO I think that implements this idea describe above it's good. Let's discuss about it.
# OBS: If no files are found in path the default template is used
def stylesheet_link_tag_template(template_name)
d = Dir.new("public/templates/#{template_name}/stylesheets/")
d.map do |filename|
stylesheet_link_tag("/templates/#{template_name}/stylesheets/#{filename}") unless REJECTED_DIRS.include?(filename.gsub(/.css/,""))
end
end
# Load all the javascript files of a existing template with the template_name passed as argument.
#
# The files loaded are in the path:
#
# 'public/templates/#{template_name}/javascripts/*'
#
#TODO I think that implements this idea describe above it's good. Let's discuss about it.
# OBS: If no files are found in path the default template is used
def javascript_include_tag_template(template_name)
d = Dir.new("public/templates/#{template_name}/javascripts/")
d.map do |filename|
javascript_include_tag("/templates/#{template_name}/javascripts/#{filename}") unless REJECTED_DIRS.include?(filename.gsub(/.js/,""))
end
end
private
# Check if the current controller is the controller that allows layout editing
def edit_mode?
controller.controller_name == 'edit_template' ? true : false
end
# Shows the block as the struture bellow
# <ul id="sort#{number of the box}">
# <li class="block_item_box_#{number of the box}" id="block_#{id of block}">
# </li>
# </ul>
#
def show_blocks(box, main_content = "")
blocks = box.blocks_sort_by_position
content_tag(:ul,
blocks.map {|b|
content_tag(:li, b.main? ? main_content : b.to_html, :class =>"block_item_box_#{box.number}" , :id => "block_#{b.id}" )
}, :id => "sort_#{box.number}"
)
end
# Shows the blocks as defined in <tt>show_blocks</tt> 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,
blocks.map {|b|
content_tag(:li, b.main? ? main_content : b.to_html , :class =>"block_item_box_#{box.number}" , :id => "block_#{b.id}" ) + draggable("block_#{b.id}")
}, :id => "sort_#{box.number}"
) + drag_drop_items(box) + sortable_block(box.number)
end
# Allows the biven box to have sortable elements
def sortable_block(box_number)
sortable_element "sort_#{box_number}",
:url => {:action => '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 = Box.find_not_box(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=>:change_box, :box_id => box.id})
}.to_s
end
end