class DateTimePickerInput < SimpleForm::Inputs::StringInput include ActionView::Helpers::FormTagHelper include ActionView::Context def input label_from_options = options[:label] l = if label_from_options == false "" else label_from_options || object.class.human_attribute_name(attribute_name) end field_set_tag(l, class: (input_html_classes + ["uc-form-fieldset-datetime", "inline-inputs", "legend-as-label"]).map(&:to_s).join(" ")) do (date_field + time_field + timezone_label) end end def label "" end private def date_value value = @builder.object.public_send("#{attribute_name}_date_part") value.blank? ? value : format_date(value) end def time_value @builder.object.public_send("#{attribute_name}_time_part") end def format_date(date) user_agent.mobile? ? Date.parse(date).iso8601 : date end def date_aria_label aria_label = "Date (format: MM/DD/YYYY)" if options[:input_html] && options[:input_html]["disable-future-dates"] aria_label += ". You cannot select a future date." end aria_label end def user_agent @user_agent ||= UserAgent.parse(@builder.template.controller.request.user_agent) end def input_for_mobile @builder.text_field(attribute_name, input_html_options.merge( type: 'date' )) end def input_for_desktop data_presents = ['datepicker'] if input_html_options['data-presents'] data_presents << input_html_options['data-presents'] end @builder.text_field(attribute_name, input_html_options.merge( 'type' => 'text', 'data-presents' => data_presents.join(' '), )) end def date_field input_html_options.merge!(date_name_and_id) input_html_options.merge!( placeholder: "MM/DD/YYYY", class: "#{input_html_classes.join(' ')} date_part", value: date_value, label: false, "aria-label" => date_aria_label ) content_tag :div, class: 'uc-form-field-datetime-date uc-form-field-date' do user_agent.mobile? ? input_for_mobile : input_for_desktop end end def time_field content_tag :div, class: 'uc-form-field-datetime-time uc-form-field-time' do @builder.text_field(attribute_name, input_html_options.merge(time_name_and_id).merge( "data-presents" => "timepicker", placeholder: "HH:MM AM", class: "#{input_html_classes.join(' ')} time_part", value: time_value, label: false, "aria-label" => "Time (format: HH:MM AM)" )) end end def timezone_label content_tag :div, "", class: "js-timezone uc-form-field-datetime-timezone" end def date_name_and_id { name: input_name_from_type("date_part"), id: input_id_from_type("date_part") } end def time_name_and_id { name: input_name_from_type("time_part"), id: input_id_from_type("time_part") } end def index @builder.instance_variable_get('@default_options')[:index] end def prefix @builder.instance_variable_get('@object_name') end def field_name attribute_name.to_s end # Returns the name attribute for the input tag # => post[written_on_date_part] def input_name_from_type(suffix) "#{prefix}#{"[#{index}]" if index}[#{field_name}_#{suffix}]" end # Returns the id attribute for the input tag # => "post_written_on_date_part" def input_id_from_type(suffix) remove_brackets("#{prefix}#{"[#{index}]" if index}_#{field_name}_#{suffix}") end def remove_brackets(input_string) input_string.tr('[]', '_').gsub(/_+/, "_") end end