#encoding: utf-8
# require ActionView::Helpers::FormTagHelper
module AttributeFieldsHelper
include ActionView::Helpers::FormTagHelper
include ActionView::Helpers::FormOptionsHelper
include ActionView::Helpers::DateHelper
include ActionView::Helpers::TagHelper
include ActionView::Helpers::RenderingHelper
def block_helper(user,index,disable = false)
@index = index
@markup_options = markup_options.merge(:disabled=>disable)
@user = user
@attribute_value = @user.get_value_from_field_id(id)
@new_attribute = @attribute_value.nil?
@attribute_value = @attribute_value || @user.attribute_values.build(attribute_field_id: id)
@prefiled_value = @attribute_value.get_values
return instance_eval("render_#{markup}") #rescue ""
end
def lang_tab(str,lang)
content_tag(:div,str,:class=>"tab-pane fade",:id=>(get_field_name_base+"tab_#{lang}"))
end
def render_address
control_group_wrapper do |key,value|
result = '
'.html_safe
result << gen_modal_dialog
end
end
def render_checkbox
@prefiled_value ||=[]
markup_value = eval(self.markup_value) rescue {}
control_group_wrapper do
markup_value.collect do |key,value|
label_tag(key,check_box_tag(get_field_name_base+"[value][#{key}]", value[I18n.locale.to_s], (@prefiled_value.include?(key) ? true : false), {})+value[I18n.locale.to_s],@markup_options.merge(:class=>"control-label"))
end.join rescue ""
end
end
def render_date
control_group_wrapper{date_select(get_field_name_base+"[value]",nil,@markup_options.merge(:default=>@prefiled_value),:class=>"input-small")}
end
def render_date_durnation #Need re-write low priority
end
def render_radio_button
@prefiled_value ||=[]
markup_value = eval(self.markup_value) rescue {}
control_group_wrapper do
markup_value.collect do |key,value|
label_tag(key,radio_button_tag(get_field_name_base+"[value][#{key}]", value[I18n.locale.to_s], (@prefiled_value.include?(key) ? true : false), {})+value[I18n.locale.to_s],@markup_options.merge(:class=>"control-label"))
end.join rescue ""
end
end
def render_select
markup_value = (self.markup_value.is_a?(Hash) ? self.markup_value : eval(self.markup_value) )rescue {}
control_group_wrapper{select_tag( get_field_name_base+"[value]",options_for_select(markup_value.collect{|p| [p[1][I18n.locale.to_s],p[0]]},@prefiled_value),@markup_options)} rescue ""
end
def render_text_area
control_group_wrapper do |key,value|
if(add_more and value.is_a?(Hash))
values = value
values.each_with_index.collect do |value,index|
text_area_tag(get_field_name_base + (key.nil? ? '' : "[#{key}][#{index}]"), value.last,@markup_options)
end.join.html_safe
else
value = can_muti_lang_input ? @prefiled_value[key] : @prefiled_value
key = can_muti_lang_input ? "[#{key}]" : "[value]"
text_area_tag(get_field_name_base + key, value,@markup_options)
end
end
end
def render_text_field
control_group_wrapper do |key,value|
if(add_more and value.is_a?(Hash))
values = value
values.each_with_index.collect do |value,index|
text_field_tag(get_field_name_base + (key.nil? ? '' : "[#{key}][#{index}]"), value.last,@markup_options)
end.join.html_safe
else
value = can_muti_lang_input ? @prefiled_value[key] : @prefiled_value
key = can_muti_lang_input ? "[#{key}]" : "[value]"
text_field_tag(get_field_name_base + key, value,@markup_options)
end
end
end
protected
def lang_panel_control_wrapper(&block)
result = ''
result << '
'
result << controls_wrapper{yield}
result << '
'
result << '
'
result.html_safe
end
def controls_wrapper(&block)
result = ""
if can_muti_lang_input
result << "
"
result << "
"
VALID_LOCALES.collect do |key|
value = @prefiled_value[key.to_s] rescue nil
div_class = ["tab-pane" ,"fade"].join(" ")
div_class << (key == I18n.locale.to_s ? " active in" : '')
result << content_tag(:div,yield(key,value),:class=>div_class,:id=>"tab"+id.to_s+"_#{key}")
end
result << "
"
result << "
"
VALID_LOCALES.each do |key|
result << content_tag(:li,link_to(I18n.t("langs."+key),"#tab"+id.to_s+"_#{key}",:data=>{:toggle=>"tab"}),:class=>(key == I18n.locale.to_s ? "active" : nil))
end
result << "
"
result << "
"
# @prefiled_value.collect do |key,value|
# result << yield(key,value)
# end
else
result << yield
end
if can_add_more
result << '
'
result << ' Add'
result << ''
end
result << "
"
result.html_safe
end
def control_group_wrapper(&block)
div_class = can_muti_lang_input ? "control-group language-swich" : "control-group"
temp = label + controls_wrapper(&block)
result = content_tag(:div,temp,:class=>div_class)
result << end_block
result.html_safe
end
def end_block
if @new_attribute
hidden_field_tag(get_field_name_base+"[attribute_field_id]",id,:for=>"field_#{@index}")
else
hidden_field_tag(get_field_name_base+"[id]",@attribute_value.id,:for=>"field_#{@index}")
end
end
def get_field_name_base
# "user[#{self.attribute._type.downcase.pluralize}][#{self.attribute._id.to_s}][attribute_values][#{attribute_value.id}]"
# binding.pry if @attribute_value[:key] == 'status'
if @new_attribute
"user[new_attribute_values][#{@index}]"
else
"user[attribute_values][#{@index}]"
end
end
def label
label_tag(key,title,:class=>"control-label")
end
def can_muti_lang_input
locale and LIST[:markups][markup]["muti_lang_input_supprt"]
end
def can_add_more
locale and LIST[:markups][markup]["ext_support"] && add_more
end
def render_anywhere(partial, assigns)
view = ActionView::Base.new(Rails::Configuration.new.view_path, assigns)
ActionView::Base.helper_modules.each { |helper| view.extend helper }
view.extend ApplicationHelper
view.render(:partial => partial)
end
def gen_modal_dialog
result = ''
result << ''
result << '
'
result << '
One fine body…
'
result << '
'
result << ''
result << '
'
result.html_safe
end
end