Orbit/app/helpers/attribute_fields_helper.rb

242 lines
9.2 KiB
Ruby
Raw Normal View History

#encoding: utf-8
module AttributeFieldsHelper
2012-10-08 08:58:33 +00:00
include ActionView::Helpers::FormTagHelper
include ActionView::Helpers::FormOptionsHelper
include ActionView::Helpers::DateHelper
include ActionView::Helpers::TagHelper
include ActionView::Helpers::RenderingHelper
2012-11-12 02:41:41 +00:00
include OrbitBasis::RenderAnywhere
2012-10-18 10:36:21 +00:00
def block_helper(user,index,disable = false)
unless self.disabled
@index = index
@markup_options = markup_options.merge(:disabled=>disable,:func=>"input_unit")
@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.value
@panel_setting = self.get_data
return instance_eval("render_#{markup}") #rescue ""
end
2012-10-08 08:58:33 +00:00
end
def lang_tab(str,lang)
content_tag(:div,str,:class=>"tab-pane fade",:id=>(get_field_name_base+"tab_#{lang}"))
2012-10-08 08:58:33 +00:00
end
def render_address
control_group_wrapper do |key,value|
2012-11-12 02:41:41 +00:00
value = (can_muti_lang_input? ? @prefiled_value[key] : @prefiled_value) rescue nil
key_field = can_muti_lang_input? ? "[#{key}]" : ""
place_holder= @panel_setting["placeholder"][key] rescue ''
result = text_area_tag(get_field_name_base + key_field, value,@markup_options.merge(:placeholder=>place_holder))
result << hidden_field_tag(get_basic_field_name_base+"[temp][street_address][#{key}]",nil,:class=>"street_address_#{key}")
result << hidden_field_tag(get_basic_field_name_base+"[temp][city][#{key}]",nil,:class=>"city_#{key}")
result << hidden_field_tag(get_basic_field_name_base+"[temp][zip][#{key}]",nil,:class=>"zip_#{key}")
result << hidden_field_tag(get_basic_field_name_base+"[temp][country][#{key}]",nil,:class=>"country_#{key}")
result << hidden_field_tag(get_basic_field_name_base+"[temp][indicator][#{key}]",nil,:class=>"indicator_#{key}")
2012-10-08 08:58:33 +00:00
end
end
def render_checkbox
@prefiled_value ||=[]
2012-10-08 08:58:33 +00:00
control_group_wrapper do
a = self[:option_list].collect do |key,value|
label_tag(key,check_box_tag(get_field_name_base+"[#{key}]", true , (@prefiled_value.include?(key) ? true : false), {})+value[I18n.locale.to_s],@markup_options.merge(:class=>"control-label"))
end.join rescue ""
2012-10-08 08:58:33 +00:00
end
end
def render_date
control_group_wrapper{date_select(get_field_name_base,nil,@markup_options.merge(:default=>@prefiled_value),:class=>"input-small")}
2012-10-08 08:58:33 +00:00
end
def render_date_durnation #Need re-write low priority
2012-10-08 08:58:33 +00:00
end
def render_radio_button
@prefiled_value ||=[]
control_group_wrapper do
self[:option_list].collect do |key,value|
label_tag(key,radio_button_tag(get_field_name_base, key , (@prefiled_value.include?(key) ? true : false), {})+value[I18n.locale.to_s],@markup_options.merge(:class=>"control-label"))
end.join
2012-10-08 08:58:33 +00:00
end
end
def render_select
2012-11-08 10:59:19 +00:00
prompt = @panel_setting["initial"][I18n.locale.to_s] rescue nil
@markup_options.merge!(:prompt => prompt) unless prompt.nil?
control_group_wrapper{select_tag( get_field_name_base,options_for_select(self.option_list.collect{|p| [p[1][I18n.locale.to_s],p[0]]},@prefiled_value),@markup_options)} rescue ""
2012-10-08 08:58:33 +00:00
end
def render_text_area
2012-10-18 10:36:21 +00:00
control_group_wrapper do |key,value|
2012-11-12 02:41:41 +00:00
value = can_muti_lang_input? ? @prefiled_value[key] : @prefiled_value
key = can_muti_lang_input? ? "[#{key}]" : ""
place_holder= @panel_setting["placeholder"][I18n.locale.to_s] rescue ''
text_area_tag(get_field_name_base + key, value,@markup_options.merge(:placeholder=>place_holder))
2012-10-18 10:36:21 +00:00
end
2012-10-08 08:58:33 +00:00
end
def render_text_field
2012-11-08 10:59:19 +00:00
control_group_wrapper do |key,value,add_more_counter|
if(add_more)
2012-11-08 10:59:19 +00:00
place_holder= @panel_setting["placeholder"][key]
2012-11-12 02:41:41 +00:00
text_field_tag(get_field_name_base + (key.nil? ? '' : "[][#{key}]"), value,@markup_options.merge(:placeholder=>place_holder))
else
2012-11-12 02:41:41 +00:00
value = (can_muti_lang_input? ? @prefiled_value[key] : @prefiled_value) rescue nil
key_field = can_muti_lang_input? ? "[#{key}]" : ""
2012-11-08 10:59:19 +00:00
place_holder= @panel_setting["placeholder"][key] rescue ''
text_field_tag(get_field_name_base + key_field, value,@markup_options.merge(:placeholder=>place_holder))
end
end
end
2012-10-08 08:58:33 +00:00
protected
def lang_panel_control_wrapper(&block)
2012-10-18 10:36:21 +00:00
result = '<div class="tabbable">'
result << '<div class="tab-content">'
result << controls_wrapper{yield}
result << '</div>'
result << '</div>'
result.html_safe
end
2012-10-08 08:58:33 +00:00
2012-11-08 10:59:19 +00:00
2012-10-08 08:58:33 +00:00
def controls_wrapper(&block)
2012-11-08 10:59:19 +00:00
loop_time = self.add_more ? @attribute_value.add_more_counter : 1
result = ''
2012-11-12 02:41:41 +00:00
multi_and_edit_more_class = (self.markup == 'address' ? "multipleInput" : "multipleInput editMore" )
result << "<div class='#{multi_and_edit_more_class}'>" if self.add_more
2012-11-08 10:59:19 +00:00
result << "<div class='controls'>"
loop_time.times do |loop_counter|
2012-11-12 02:41:41 +00:00
if can_muti_lang_input?
2012-10-18 10:36:21 +00:00
result << "<div class='tabbable'>"
2012-11-12 02:41:41 +00:00
tab_content_class = add_more ? "tab-content input-append" : "tab-content"
result << "<div class= '#{tab_content_class}'>"
VALID_LOCALES.collect do |key|
2012-11-08 10:59:19 +00:00
value = @prefiled_value[key.to_s] rescue nil
div_class = ["tab-pane" ,"fade"].join(" ")
div_class << (key == I18n.locale.to_s ? " active in" : '')
2012-11-08 10:59:19 +00:00
if self.add_more
2012-11-12 02:41:41 +00:00
div_class << " #{add_more_tab(:input_field,loop_counter,key)}"
2012-11-08 10:59:19 +00:00
add_more_field_value = @prefiled_value[loop_counter][key] rescue ''
result << content_tag(:div,yield(key,add_more_field_value,loop_counter),:class=>div_class)#,:id=>"tab"+id.to_s+"_#{key}_#{loop_counter}"
else
result << content_tag(:div,yield(key,value),:class=>div_class,:id=>"tab"+id.to_s+"_#{key}")
end
end # of VALID_LOCALES.collect for tabed input
2012-11-12 02:41:41 +00:00
result << '<a href=".address-edit.' + get_pairing_tab_class({}) + ' " class="btn edit-btn" type="button" data-toggle="modal"><i class="icon-edit"></i></a>' if self.markup == 'address'
result << '<a href="#" class="btn removeInput" type="button"><i class="icon-trash"></i></a>' if self.add_more
result << "</div>"
2012-11-08 10:59:19 +00:00
2012-11-12 02:41:41 +00:00
if can_muti_lang_input?
result << "<ul class='nav nav-pills'>"
VALID_LOCALES.reverse.each do |key|
link_entry = self.add_more ? "#{add_more_tab(:tab_btn,loop_counter,key)}" : "#tab"+id.to_s+"_#{key}"
result << content_tag(:li,link_to(I18n.t("langs."+key),link_entry,:data=>{:toggle=>"tab"}),:class=>(key == I18n.locale.to_s ? "active" : nil))
end # of VALID_LOCALES.collect for tabs
result << "</ul>"
end
2012-11-08 10:59:19 +00:00
result << "</div>"
2012-11-12 02:41:41 +00:00
result << gen_modal_dialog if self.markup == "address"
else
2012-11-12 02:41:41 +00:00
result << yield
end
2012-11-08 10:59:19 +00:00
if can_add_more and (loop_counter == loop_time-1)
temp_field_name = get_basic_field_name_base + '[temp]'
2012-11-12 02:41:41 +00:00
result << '<div class="controls">'
result << '<span class="help-block">'
2012-11-12 02:41:41 +00:00
result << '<a href="#" class="addinput"><i class="icon-plus-sign"></i>'+I18n.t("admin.infos.add")+' </a>'
2012-11-08 10:59:19 +00:00
result << hidden_field_tag("#{temp_field_name}[count]",loop_time,:class=>"list_count")
result << hidden_field_tag("#{temp_field_name}[count]",get_basic_field_name_base,:class=>"field_name")
result << '</span>'
2012-11-12 02:41:41 +00:00
result << '</div>'
end
2012-11-08 10:59:19 +00:00
end # of loop_time
2012-10-08 08:58:33 +00:00
result << "</div>"
2012-11-08 10:59:19 +00:00
result << "</div>" if self.add_more
2012-10-08 08:58:33 +00:00
result.html_safe
2012-11-08 10:59:19 +00:00
end # of def controls_wrapper(&block)
2012-10-08 08:58:33 +00:00
def control_group_wrapper(&block)
2012-11-12 02:41:41 +00:00
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
2012-10-08 08:58:33 +00:00
result.html_safe
end
def end_block
if @new_attribute
hidden_field_tag(get_basic_field_name_base+"[attribute_field_id]",id,:for=>"field_#{@index}")
2012-10-08 08:58:33 +00:00
else
hidden_field_tag(get_basic_field_name_base+"[id]",@attribute_value.id,:for=>"field_#{@index}")
2012-10-08 08:58:33 +00:00
end
end
2012-11-08 10:59:19 +00:00
def add_more_tab(mode,counter,key)
case mode
when :input_field
get_pairing_tab_class(:suffix=>['','tab'+counter.to_s,key].join('-'))
when :tab_btn
".#{get_pairing_tab_class(:suffix=>['','tab'+counter.to_s,key].join('-'))}"
end
end
def get_pairing_tab_class(opts)
prefix = opts[:prefix]
suffix = opts[:suffix]
str = get_basic_field_name_base.gsub("[","-").gsub("]",'')
str = prefix.nil? ? str : prefix+ str
suffix.nil? ? str : str + suffix
end
def get_basic_field_name_base
2012-10-08 08:58:33 +00:00
if @new_attribute
"user[new_attribute_values][#{@index}]"
else
"user[attribute_values][#{@index}]"
end
end
def get_field_name_base
get_basic_field_name_base + "[value]"
end
2012-10-08 08:58:33 +00:00
def label
label_tag(key,title,:class=>"control-label",:func => "field_label")
2012-10-08 08:58:33 +00:00
end
2012-11-12 02:41:41 +00:00
def can_muti_lang_input?
if self.markup == "address"
return true
else
LIST[:markups][markup]["muti_lang_input_supprt"] and !(get_data["cross_lang"] == "true")
end
end
def can_add_more
2012-11-12 02:41:41 +00:00
if self.markup == "address"
return false
else
locale and LIST[:markups][markup]["ext_support"] && add_more
end
end
2012-11-12 02:41:41 +00:00
def gen_modal_dialog
2012-11-12 02:41:41 +00:00
render_anywhere("shared/attribute_field/address_modal_dialog",{:field_name=>title,:btn_class => "#{get_pairing_tab_class({})}"})
end
2012-10-08 08:58:33 +00:00
end