# -*- encoding : utf-8 -*- module KirguduBase class FormBuilder < ActionView::Helpers::FormBuilder include ActionView::Helpers::AssetTagHelper include ActionView::Helpers::TagHelper include ::KirguduBase::GuiHelper def control(property, data, html_options = {}) if property.read_only == :true self.read_only_field(property, data, html_options) else if property.input_type == :chosen_select self.chosen_select(property, data, html_options) elsif property.input_type == :autocomplete_with_id self.autocomplete_with_id(property, html_options) elsif property.input_type == :autocomplete #autocomplete(property, html_options) elsif property.input_type == :switch_button self.switch_button(property, html_options) elsif property.input_type == :date_picker self.date_picker(property, html_options) elsif property.input_type == :time_picker self.time_picker(property, html_options) elsif property.input_type == :image_selector self.image_selector(property, html_options) elsif property.input_type == :check_box self.check_box(property, html_options) elsif property.input_type == :radio_button #radio_button(property, html_options) elsif property.input_type == :link self.link(property, html_options) elsif property.input_type == :elastic_text_area self.elastic_textarea(property, html_options) elsif property.input_type == :password self.elastic_textarea(property, html_options) elsif property.input_type == :text_area self.text_area(property, html_options) elsif property.input_type == :file_input self.file_input(property, html_options) elsif property.input_type == :select #file_input(property, html_options) elsif property.input_type == :collection_check_boxes else self.text_field(property, html_options) end end end def error_label(property, html_options = {}) html_options.merge!(generated: "true", style: "display: block") html_options_add_class(html_options, "error") if @object.errors[property.name].present? errors_in_html = "" if @object.errors[property.name].present? @object.errors[property.name].each do |error_message| errors_in_html += @template.content_tag("label", error_message.capitalize.html_safe, html_options) end end errors_in_html.html_safe end def label_with_hint(label, hint, html_options = {}) @template.content_tag("label", (@object.class.human_attribute_name(label) + @template.content_tag("span", hint, class: 'help-block')).html_safe, html_options) end def label(property, html_options = {}) @template.content_tag("label", html_options) do @object.class.human_attribute_name(property.name) end end def read_only_field(property, data, html_options = {}) @template.content_tag("label", html_options) do @object.send(property.text_value_property || property.name) end end def text_field(property, html_options = {}) html_options ||= {} html_options.merge!(property.html_options) if property.html_options html_options.merge!( { id: "#{@object.class.for_form_params}_#{property.name}", name: "#{@object.class.for_form_params}[#{property.name}]", :"property-name" => property.name, :"property-value" => @object[property.name] } ) html_options_add_class(html_options, "error") if @object.errors[property.name].present? text_field_tag(property, @object, html_options) end def text_area(property, html_options = {}) #html_options ||= {} #html_options.merge!(property.html_options) if property.html_options #html_options.merge!( # { # id: "#{@object.class.for_form_params}_#{property.name.to_sym}", # name: "#{@object.class.for_form_params}[#{property.name.to_sym}]", # :"property-name" => property.name.to_s, # :"property-value" => @object[property.name] # } #) # #html_options_add_class(html_options, "error") if @object.errors[property.name].present? text_area_tag(property, @object, html_options) #super(property.name, html_options) end def password_field(property, html_options = {}) #html_options ||= {} # #html_options.merge!(property.html_options) if property.html_options #html_options.merge!( # { # id: "#{@object.class.for_form_params}_#{property.name.to_sym}", # name: "#{@object.class.for_form_params}[#{property.name.to_sym}]", # :"property-name" => property.name.to_s, # :"property-value" => @object[property.name] # } #) #html_options_add_class(html_options, "error") if @object.errors[property.name].present? # #super(property.name, html_options) password_field_tag(property, @object, html_options) end def autocomplete_field(property, html_options = {}) html_options ||= {} html_options_add_class(html_options, "error") if @object.errors[property.name].present? end def autocomplete_with_id(property, html_options = {}) html_options ||= {} #html_options.merge!(property.html_options) if property.html_options #html_options.merge!( # { # id: "#{@object.class.for_form_params}_#{property.name.to_sym}", # name: "#{@object.class.for_form_params}[#{property.name.to_sym}]", # :"property-name" => property.name.to_s, # :"property-value" => @object[property.name], # type: "hidden", # :"value-property" => property.data_source.value_property, # :"label-property" => property.data_source.label_property, # url: property.data_source.url, # value: @object.send(property.name), # label: @object.send(property.text_value_property), # placeholder: property.use_i18n ? I18n.t(property.placeholder) : property.placeholder, # :"parent-id" => ("#{@object.class.for_form_params}_#{html_options[:parent]}" if property.parent) # } #) # #html_options_add_class(html_options, "autocomplete-with-id") #html_options_add_class(html_options, "error") if @object.errors[property.name].present? # #@template.content_tag("input", nil, html_options) autocomplete_with_id_tag(property, @object, html_options) end def date_picker(property, html_options = {}) html_options ||= {} #html_options.merge!(property.html_options) if property.html_options #html_options.merge!( # { # id: "#{@object.class.for_form_params}_#{property.name.to_sym}", # name: "#{@object.class.for_form_params}[#{property.name.to_sym}]", # :"property-name" => property.name.to_s, # :"property-value" => @object[property.name] # # } #) #html_options_add_class(html_options, "error") if @object.errors[property.name].present? date_picker_tag(property, @object, html_options) end def elastic_textarea(property, html_options = {}) html_options ||= {} html_options.merge!(property.html_options) if property.html_options html_options.merge!( { id: "#{@object.class.for_form_params}_#{property.name.to_sym}", name: "#{@object.class.for_form_params}[#{property.name.to_sym}]", :"property-name" => property.name.to_s, :"property-value" => @object[property.name], value: @object[property.name] } ) html_options_add_class(html_options, "error") if @object.errors[property.name].present? text_area(property.name, html_options) end def color_picker(property, html_options = {}) html_options ||= {} html_options.merge!(property.html_options) if property.html_options html_options.merge!( { id: "#{@object.class.for_form_params}_#{property.name.to_sym}", name: "#{@object.class.for_form_params}[#{property.name.to_sym}]", :"property-name" => property.name.to_s, :"property-value" => @object[property.name] } ) html_options_add_class(html_options, ["color-picker", "error"]) if @object.errors[property.name].present? text_field(property.name, html_options) end def masked_field(property, html_options = {}) html_options ||= {} html_options.merge!(property.html_options) if property.html_options html_options.merge!( { id: "#{@object.class.for_form_params}_#{property.name.to_sym}", name: "#{@object.class.for_form_params}[#{property.name.to_sym}]", :"property-name" => property.name.to_s, :"property-value" => @object[property.name] } ) html_options_add_class(html_options, ["error"]) if @object.errors[property.name].present? #TODO: Add Mask Implementation for Control text_field(property.name.to_sym, args) end def check_box(property, html_options = {}) html_options ||= {} html_options.merge!(property.html_options) if property.html_options html_options.merge!( { id: "#{@object.class.for_form_params}_#{property.name.to_sym}", name: "#{@object.class.for_form_params}[#{property.name.to_sym}]", :"property-name" => property.name.to_s, :"property-value" => @object[property.name], value: @object[property.name] } ) html_options_add_class(html_options, ["error"]) if @object.errors[property.name].present? #if html_options[:onchange] # onchange_actions = [] # if html_options[:onchange].is_a?(Hash) # onchange_actions.push(html_options[:onchange]) # elsif html_options[:onchange].is_a?(Array) # html_options[:onchange].each do |a| # onchange_actions.push(a) # end # end # # onchange_actions.each do |o| # o[:property_control] = "#{@object.class.for_form_params}_#{o[:property]}" # # if o[:source] && o[:source].is_a?(Hash) # o[:source] = Rails.application.routes.url_helpers.url_for(o[:source]) # end # end # # html_options[:onchange] = onchange_actions #end html_options[:parent_value] = @object[property.parent] if property.parent super(property.name, html_options) end def select(property, select_choices, html_options = {}) html_options ||= {} #html_options.merge!(property.html_options) if property.html_options #html_options.merge!( # { # id: "#{@object.class.for_form_params}_#{property.name.to_sym}", # name: "#{@object.class.for_form_params}[#{property.name.to_sym}]", # :"property-name" => property.name.to_s, # :"property-value" => @object[property.name], # value: @object[property.name], # parent_value: (@object[property.parent] if property.parent) # } #) #html_options_add_class(html_options, ["error"]) if @object.errors[property.name].present? #select_tag(html_options[:choices], html_options) select_tag(property, @object, select_choices, html_options) end def chosen_select(property, select_choices, html_options = {}) html_options ||= {} #html_options.merge!(property.html_options) if property.html_options #html_options.merge!( # { # id: "#{@object.class.for_form_params}_#{property.name.to_s}", # name: "#{@object.class.for_form_params}[#{property.name.to_s}]", # value: @object[property.name.to_sym], # :"property-name" => property.name.to_s, # :"property-value" => @object[property.name], # parent: (property.parent if property.parent), # parent_id: ("#{@object.class.for_form_params}_#{property.parent}" if property.parent), # parent_value: (@object[property.parent] if property.parent), # placeholder: property.placeholder_as_string # } #) #html_options_add_class(html_options, ["error"]) if @object.errors[property.name].present? # # #if property.data_source.type == :jquery || property.data_source.type == :jquery_db # html_options.merge!({ # :"ds-method" => property.data_source.http_method, # :"ds-url" => property.data_source.url, # :"ds-value-property" => property.data_source.value_property, # :"ds-label-property" => property.data_source.label_property, # #ds_type: property.data_source.type == :jquery ? :jquery : :local # :"ds-format" => property.data_source.data_format # }) #end #raise "CHOSEN SELECT HTML OPTIONS: #{html_options.to_json}" #chosen_select_tag(select_choices, html_options) chosen_select_tag(property, @object, select_choices, html_options) end def file_input(property, html_options = {}) file_input_tag(property, @object, html_options) end def image_selector(property, html_options = {}) html_options ||= {} control_html = "" # # #image_options = # { # src: thumb_url, # id: "#{@object.class.for_form_params}_#{label}_selected_image" # } #image_options[:style] ='display: none' unless @object[label] # #control_html = @template.content_tag('img', '', image_options) + '
'.html_safe # #select_image_options = # { # href: "javascript:showImageSelectDialog('##{@object.class.for_form_params}_#{label}_imagesModal'); return false;" # } # #control_html += @template.content_tag('a', select_image_options) do # "Select Image" #end # #hidden_options = { # type: 'hidden', # id: "#{@object.class.for_form_params}_#{label}", # name: "#{@object.class.for_form_params}[#{label}]" #} # #hidden_options[:value] = @object[label].to_s if @object[label] # #control_html += @template.content_tag('input', '', hidden_options) control_html end def radio_group(label, values, html_options = {}) label = property.name.to_sym html_options ||= {} control_html = "" values.each do |value_pair| control_html += @template.content_tag('label', class: "radio") do @template.content_tag('div', class: "radio") do @template.content_tag('span', class: '') do @template.tag('input', type: 'radio', name: "#{@object.class.for_form_params}[#{label}]", id: "#{@object.class.for_form_params}_#{label}", value: value_pair[1], style: 'opacity: 0;') end end + value_pair[0] end end control_html.html_safe end def switch_button(property, html_options = {}) html_options ||= {} html_options.merge!(property.html_options) if property.html_options html_options.merge!( { id: "#{@object.class.for_form_params}_#{property.name.to_sym}", name: "#{@object.class.for_form_params}[#{property.name.to_sym}]", value: @object[property.name.to_sym], :"property-name" => property.name.to_s, :"property-value" => @object[property.name], type: "checkbox" } ) html_options_add_class(html_options, "switch-button") @template.tag("input", html_options) #label = property.name.to_sym #html_options = args.extract_options! #html_options.stringify_keys! # #value_off = html_options['value-off'] || "0" #value_on = html_options['value-on'] || "1" # #off_input_options = { # id: "#{@object.class.for_form_params}-#{label}-off", # name: "#{@object.class.for_form_params}[#{label}]", # type: "radio", # value: value_off #} #on_input_options = { # id: "#{@object.class.for_form_params}-#{label}-on", # name: "#{@object.class.for_form_params}[#{label}]", # type: "radio", # value: value_on #} # #if @object[property.name] == true # on_input_options['checked'] = true #else # off_input_options['checked'] = true #end # #toggle_options = {} # #toggle_options[:class] = "toggle" + (@object[property.name] == value_on ? ' on' : ' off') ##toggle_options[:class] = "toggle on" # # #@template.content_tag('div', class: "row-fluid") do # @template.content_tag('div', class: "pull-left") do # @template.content_tag('label', class: 'radio off') do # @template.tag('input', off_input_options) # # end + # @template.content_tag('label', class: 'radio on') do # @template.tag('input', on_input_options) # end + # @template.content_tag('div', toggle_options) do # @template.content_tag('div', class: "yes") do # html_options['label-on'] || 'ON' # end + # @template.content_tag('div', class: "switch") do # end + # @template.content_tag('div', class: "no") do # html_options['label-off'] || 'OFF' # end # end # end #end end private def html_options_add_class(html_options, classes_to_add) classes = html_options[:class] ? html_options[:class].split(' ') : [] if classes_to_add.is_a?(Array) classes_to_add.each do |item| classes << item.to_s unless classes.include?(item.to_s) end else classes << classes_to_add.to_s unless classes.include?(classes_to_add.to_s) end html_options[:class] = classes.join(" ") if classes.length > 0 end end end