2012-10-12 10:09:02 +00:00
#encoding: utf-8
2012-10-09 06:47:16 +00:00
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
2012-10-12 10:09:02 +00:00
include ActionView :: Helpers :: RenderingHelper
2012-11-15 09:23:39 +00:00
include ActionView :: Context
2012-11-12 02:41:41 +00:00
include OrbitBasis :: RenderAnywhere
2012-11-15 09:23:39 +00:00
2012-10-18 10:36:21 +00:00
def block_helper ( user , index , disable = false )
2012-11-03 21:56:29 +00:00
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
2012-10-12 10:09:02 +00:00
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
2012-10-12 10:09:02 +00:00
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 ''
2012-11-15 09:23:39 +00:00
result = text_area_tag ( get_field_name_base + key_field , value , @markup_options . merge ( { :placeholder = > place_holder , :for = > key } ) )
2012-11-12 02:41:41 +00:00
2012-11-16 07:36:08 +00:00
add_ext = @attribute_value . address_ext [ key ] rescue { }
result << hidden_field_tag ( get_basic_field_name_base + " [address_ext][ #{ key } ][county] " , add_ext [ " county " ] , :class = > " county_ #{ key } " )
result << hidden_field_tag ( get_basic_field_name_base + " [address_ext][ #{ key } ][street_address] " , add_ext [ " street_address " ] , :class = > " street_address_ #{ key } " )
result << hidden_field_tag ( get_basic_field_name_base + " [address_ext][ #{ key } ][city] " , add_ext [ " city " ] , :class = > " city_ #{ key } " )
result << hidden_field_tag ( get_basic_field_name_base + " [address_ext][ #{ key } ][zip] " , add_ext [ " zip " ] , :class = > " zip_ #{ key } " )
result << hidden_field_tag ( get_basic_field_name_base + " [address_ext][ #{ key } ][country] " , add_ext [ " country " ] , :class = > " country_ #{ key } " )
result << hidden_field_tag ( get_basic_field_name_base + " [address_ext][ #{ key } ][indicator] " , add_ext [ " indicator " ] , :class = > " indicator_ #{ key } " )
2012-10-08 08:58:33 +00:00
end
end
2012-10-12 10:09:02 +00:00
def render_checkbox
2012-10-24 06:46:25 +00:00
@prefiled_value || = [ ]
2012-10-08 08:58:33 +00:00
control_group_wrapper do
2012-11-03 21:56:29 +00:00
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 " ) )
2012-10-12 10:09:02 +00:00
end . join rescue " "
2012-10-08 08:58:33 +00:00
end
end
2012-10-12 10:09:02 +00:00
def render_date
2012-11-22 09:49:44 +00:00
if date_is_range?
fill_from = @attribute_value . get_date ( :from ) rescue nil
fill_to = @attribute_value . get_date ( :to ) rescue nil
control_group_wrapper do
buf = date_select ( get_field_name_base + '[from]' , nil , @markup_options . merge ( :default = > fill_from ) , :class = > " input-small " )
buf << ' ~ '
buf << date_select ( get_field_name_base + '[to]' , nil , @markup_options . merge ( :default = > fill_to ) , :class = > " input-small " )
buf
end
else
@prefiled_value = @attribute_value . get_date
control_group_wrapper { date_select ( get_field_name_base , nil , @markup_options . merge ( :default = > @prefiled_value ) , :class = > " input-small " ) }
end
2012-10-08 08:58:33 +00:00
end
2012-10-12 10:09:02 +00:00
def render_date_durnation #Need re-write low priority
2012-11-15 09:23:39 +00:00
2012-10-08 08:58:33 +00:00
end
2012-10-12 10:09:02 +00:00
def render_radio_button
2012-10-24 06:46:25 +00:00
@prefiled_value || = [ ]
control_group_wrapper do
2012-11-03 21:56:29 +00:00
self [ :option_list ] . collect do | key , value |
2012-11-22 09:49:44 +00:00
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 = > " radio inline " ) )
2012-11-03 21:56:29 +00:00
end . join
2012-10-08 08:58:33 +00:00
end
end
2012-10-12 10:09:02 +00:00
def render_select
2012-11-08 10:59:19 +00:00
prompt = @panel_setting [ " initial " ] [ I18n . locale . to_s ] rescue nil
2012-11-03 21:56:29 +00:00
@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
2012-10-12 10:09:02 +00:00
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 } ] " : " "
2012-11-03 21:56:29 +00:00
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
2012-10-12 10:09:02 +00:00
def render_text_field
2012-11-22 09:49:44 +00:00
a = control_group_wrapper do | key , value |
2012-11-15 09:23:39 +00:00
add_more_blank = can_add_more ? " [] " : " "
2012-11-12 02:41:41 +00:00
key_field = can_muti_lang_input? ? " [ #{ key } ] " : " "
2012-11-08 10:59:19 +00:00
place_holder = @panel_setting [ " placeholder " ] [ key ] rescue ''
2012-11-15 09:23:39 +00:00
text_field_tag ( [ get_field_name_base , add_more_blank , key_field ] . join , value , @markup_options . merge ( :placeholder = > place_holder ) )
end
2012-10-12 10:09:02 +00:00
end
2012-11-22 09:49:44 +00:00
def date_is_range?
is_range = " false "
data = get_data
if ! data . nil?
is_range = data [ 'is_range' ] if data . has_key? " is_range "
end
is_range == " true "
end
2012-10-08 08:58:33 +00:00
protected
2012-11-15 09:23:39 +00:00
def lang_panel_tabbable_wrapper ( add_more_params , & block )
add_more_counter = ''
content_tag :div , :class = > " tabbable " do
tmp = content_tag :div , :class = > ( add_more || self . markup == 'address' ) ? " tab-content input-append " : " tab-content " do
buff = VALID_LOCALES . collect do | key |
value = @prefiled_value [ key ] rescue nil
div_class_ary = [ " tab-pane " , " fade " , key , get_pairing_tab_class ( { } ) ]
if can_add_more
add_more_value = add_more_params [ 0 ] [ :value ]
add_more_counter = add_more_params [ 0 ] [ :counter ]
value = add_more_value [ key ] rescue nil
div_class_ary << " add_more_item_ #{ add_more_counter } "
end
div_class = div_class_ary . join ( " " )
div_class << ( key == I18n . locale . to_s ? " active in " : '' )
content_tag ( :div , yield ( key , value ) , :class = > div_class )
end # of VALID_LOCALES.collect for tabed input
buff << link_to ( ( content_tag :i , '' , :class = > 'icon-edit' ) , " # #{ get_pairing_tab_class ( { } ) } _m_window " , :class = > 'btn edit-btn' , :type = > 'button' , :data = > { :toggle = > " modal " } ) if self . markup == 'address'
2012-11-22 09:49:44 +00:00
buff << link_to ( ( content_tag :i , '' , :class = > 'icon-trash' ) , " # " , :class = > " btn removeInput " ) if self . add_more
2012-11-15 09:23:39 +00:00
2012-11-22 09:49:44 +00:00
buff . join ( '' ) . html_safe
2012-11-15 09:23:39 +00:00
end
tmp << content_tag ( :ul , :class = > 'nav nav-pills' ) do
VALID_LOCALES . each . collect do | key |
# link_entry = self.add_more ? "#{add_more_tab(:tab_btn,loop_counter,key)}" : "#tab"+id.to_s+"_#{key}"
link_entry_ary = [ " . #{ get_pairing_tab_class ( { } ) } " , " . #{ key } " ]
2012-11-22 09:49:44 +00:00
link_entry_ary << " .add_more_item_ #{ add_more_counter } " if can_add_more
2012-11-15 09:23:39 +00:00
link_entry = link_entry_ary . join
2013-01-08 09:30:36 +00:00
content_tag ( :li , link_to ( I18nVariable . from_locale ( key ) , link_entry , :data = > { :toggle = > " tab " } ) , :class = > ( key == I18n . locale . to_s ? " active " : nil ) , :for = > key )
2012-11-15 09:23:39 +00:00
end . join . html_safe # of VALID_LOCALES.collect for tabs
end # of content ul
end
2012-10-12 10:09:02 +00:00
end
2012-10-08 08:58:33 +00:00
2012-11-08 10:59:19 +00:00
2012-11-15 09:23:39 +00:00
def controls_wrapper ( * add_more_params , & block )
content_tag :div , :class = > 'controls' do
result = ''
add_more_counter = " "
if can_add_more
add_more_counter = add_more_params [ 0 ] [ :counter ]
add_more_value = add_more_params [ 0 ] [ :value ]
2012-11-12 02:41:41 +00:00
end
2012-11-15 09:23:39 +00:00
2012-11-08 10:59:19 +00:00
2012-11-15 09:23:39 +00:00
if can_muti_lang_input?
result << lang_panel_tabbable_wrapper ( add_more_params , & block )
2012-11-12 02:41:41 +00:00
result << gen_modal_dialog if self . markup == " address "
2012-11-22 09:49:44 +00:00
else #cross lang field
2012-11-15 09:23:39 +00:00
2012-11-22 09:49:44 +00:00
case can_add_more
when true
value = add_more_params [ 0 ] [ :value ]
result << content_tag ( :div , :class = > " input-append " ) { yield ( nil , value ) + link_to ( ( content_tag :i , '' , :class = > 'icon-trash' ) , " # " , :class = > " btn removeInput " ) }
else
value = @prefiled_value
result << yield ( nil , value )
2012-11-15 09:23:39 +00:00
end
2012-11-12 02:41:41 +00:00
2012-11-22 09:49:44 +00:00
2012-10-12 10:09:02 +00:00
end
2012-11-15 09:23:39 +00:00
result . html_safe
end # of controls div
2012-11-08 10:59:19 +00:00
end # of def controls_wrapper(&block)
2012-10-08 08:58:33 +00:00
2012-10-12 10:09:02 +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 "
2012-11-15 09:23:39 +00:00
result = " "
case self . markup
when " text_field "
if can_add_more
2012-11-22 09:49:44 +00:00
multipleInput_ary = %w{ multipleInput }
2012-11-16 07:36:08 +00:00
add_more_seri = " add_more_group_ #{ id } "
multipleInputs = content_tag :div , :class = > multipleInput_ary . join ( ' ' ) , :ext_class = > get_pairing_tab_class ( { } ) do
2012-11-15 09:23:39 +00:00
@attribute_value . add_more_counter . times . collect do | t |
2012-11-16 07:36:08 +00:00
controls_wrapper ( :value = > ( @prefiled_value [ t ] rescue nil ) , :counter = > t , & block )
2012-11-22 09:49:44 +00:00
end . join ( '' ) . html_safe # of add_more fields
2012-11-15 09:23:39 +00:00
end # of div multipleInput editMore
result = label + multipleInputs + add_more_unt
# result = label + 一堆的輸入框(要用 multipleInput editMore 包起來) + add_more btn + hidden_fields
else
temp = label + controls_wrapper ( & block )
result = content_tag ( :div , temp , :class = > div_class )
end
when " address "
address = content_tag :div , :class = > " multipleInput editMore " do
controls_wrapper ( & block )
end # of div multipleInput editMore
result = label + address
else
temp = label + controls_wrapper ( & block )
result = content_tag ( :div , temp , :class = > div_class )
end
2012-10-12 10:09:02 +00:00
2012-11-15 09:23:39 +00:00
result << end_block
2012-10-08 08:58:33 +00:00
result . html_safe
end
2012-11-15 09:23:39 +00:00
def add_more_unt
temp_field_name = get_basic_field_name_base + '[temp]'
add_more = content_tag :div , :class = > 'controls' do
content_tag :span , :class = > 'help-block' do
2012-11-27 03:15:15 +00:00
content = link_to ( content_tag :i , I18n . t ( :add ) , :class = > " icon-plus-sign " ) , " # " , :class = > 'addinput'
2012-11-15 09:23:39 +00:00
content << hidden_field_tag ( " #{ temp_field_name } [count] " , @attribute_value . add_more_counter , :class = > " list_count " )
content << hidden_field_tag ( " #{ temp_field_name } [count] " , get_basic_field_name_base , :class = > " field_name " )
content
end # of span
end # of div
end
2012-10-08 08:58:33 +00:00
def end_block
if @new_attribute
2012-11-03 21:56:29 +00:00
hidden_field_tag ( get_basic_field_name_base + " [attribute_field_id] " , id , :for = > " field_ #{ @index } " )
2012-10-08 08:58:33 +00:00
else
2012-11-03 21:56:29 +00:00
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
2012-11-03 21:56:29 +00:00
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
2012-11-03 21:56:29 +00:00
def get_field_name_base
get_basic_field_name_base + " [value] "
end
2012-10-08 08:58:33 +00:00
def label
2012-11-03 21:56:29 +00:00
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
2012-10-12 10:09:02 +00:00
end
def can_add_more
2012-11-12 02:41:41 +00:00
if self . markup == " address "
return false
else
2012-11-22 09:49:44 +00:00
add_more
2012-11-12 02:41:41 +00:00
end
2012-10-12 10:09:02 +00:00
end
2012-11-15 09:23:39 +00:00
def gen_modal_dialog
2012-11-16 07:36:08 +00:00
render_anywhere ( " shared/attribute_field/address_modal_dialog " , {
:field_name = > title ,
:html_id = > " #{ get_pairing_tab_class ( { } ) } _m_window " ,
:btn_class = > " #{ get_pairing_tab_class ( { } ) } " ,
:field_name_basic = > get_basic_field_name_base
}
)
2012-11-15 09:23:39 +00:00
end
2012-10-12 10:09:02 +00:00
2012-10-08 08:58:33 +00:00
end