module ActiveScaffold::Bridges::DatePickerBridge

Constants

DATE_FORMAT_CONVERSION

Public Class Methods

date_options(locale) click to toggle source
# File lib/active_scaffold/bridges/date_picker/lib/datepicker_bridge.rb, line 78
def self.date_options(locale)
  begin
    date_options = I18n.translate! 'date', :locale => locale
    date_picker_options = { :closeText => as_(:close),
      :prevText => as_(:previous),
      :nextText => as_(:next),
      :currentText => as_(:today),
      :monthNames => date_options[:month_names][1, (date_options[:month_names].length - 1)],
      :monthNamesShort => date_options[:abbr_month_names][1, (date_options[:abbr_month_names].length - 1)],
      :dayNames => date_options[:day_names],
      :dayNamesShort => date_options[:abbr_day_names],
      :dayNamesMin => date_options[:abbr_day_names],
      :changeYear => true,
      :changeMonth => true,
    }

    begin
      as_date_picker_options = I18n.translate! 'active_scaffold.date_picker_options'
      date_picker_options.merge!(as_date_picker_options) if as_date_picker_options.is_a? Hash
    rescue
      Rails.logger.warn "ActiveScaffold: Missing date picker localization for your locale: #{locale}"
    end

    unless date_picker_options[:buttonImage].nil?
      date_picker_options[:buttonImage] = asset_path(date_picker_options[:buttonImage])
    end

    js_format = self.to_datepicker_format(date_options[:formats][:default])
    date_picker_options[:dateFormat] = js_format unless js_format.nil?
    date_picker_options
  rescue
    if locale == I18n.locale
      raise
    else
      nil
    end
  end
end
date_options_for_locales() click to toggle source
# File lib/active_scaffold/bridges/date_picker/lib/datepicker_bridge.rb, line 67
def self.date_options_for_locales
  I18n.available_locales.collect do |locale|
    locale_date_options = date_options(locale)
    if locale_date_options
      "$.datepicker.regional['#{locale}'] = #{locale_date_options.to_json};"
    else
      nil
    end
  end.compact.join('')
end
datetime_options(locale) click to toggle source
# File lib/active_scaffold/bridges/date_picker/lib/datepicker_bridge.rb, line 128
def self.datetime_options(locale)
  begin
    rails_time_format = I18n.translate! 'time.formats.default', :locale => locale
    datetime_options = I18n.translate! 'datetime.prompts', :locale => locale
    datetime_picker_options = {:ampm => false,
      :hourText => datetime_options[:hour],
      :minuteText => datetime_options[:minute],
      :secondText => datetime_options[:second],
    }

    begin
      as_datetime_picker_options = I18n.translate! 'active_scaffold.datetime_picker_options'
      datetime_picker_options.merge!(as_datetime_picker_options) if as_datetime_picker_options.is_a? Hash
    rescue
      Rails.logger.warn "ActiveScaffold: Missing datetime picker localization for your locale: #{locale}"
    end

    date_format, time_format = self.split_datetime_format(self.to_datepicker_format(rails_time_format))
    datetime_picker_options[:dateFormat] = date_format unless date_format.nil?
    unless time_format.nil?
      datetime_picker_options[:timeFormat] = time_format
      datetime_picker_options[:ampm] = true if rails_time_format.include?('%I')
    end
    datetime_picker_options
  rescue
    if locale == I18n.locale
      raise
    else
      nil
    end
  end
end
datetime_options_for_locales() click to toggle source
# File lib/active_scaffold/bridges/date_picker/lib/datepicker_bridge.rb, line 117
def self.datetime_options_for_locales
  I18n.available_locales.collect do |locale|
    locale_datetime_options = datetime_options(locale)
    if locale_datetime_options
      "$.timepicker.regional['#{locale}'] = #{locale_datetime_options.to_json};"
    else
      nil
    end
  end.compact.join('')
end
localization() click to toggle source
# File lib/active_scaffold/bridges/date_picker/lib/datepicker_bridge.rb, line 54
      def self.localization
        "jQuery(function($){
  if (typeof($.datepicker) === 'object') {
    #{date_options_for_locales}
    $.datepicker.setDefaults($.datepicker.regional['#{I18n.locale}']);
  }
  if (typeof($.timepicker) === 'object') {
    #{datetime_options_for_locales}
    $.timepicker.setDefaults($.timepicker.regional['#{I18n.locale}']);
  }
});\n"        
      end
split_datetime_format(datetime_format) click to toggle source
# File lib/active_scaffold/bridges/date_picker/lib/datepicker_bridge.rb, line 175
def self.split_datetime_format(datetime_format)
  date_format = datetime_format
  time_format = nil
  time_start_indicators = %w{HH hh mm tt ss}
  unless datetime_format.nil?
    start_indicator = time_start_indicators.detect {|indicator| datetime_format.include?(indicator)}
    unless start_indicator.nil?
      pos_time_format = datetime_format.index(start_indicator)
      date_format = datetime_format.to(pos_time_format - 1)
      date_format.strip!
      time_format = datetime_format.from(pos_time_format)
    end
  end
  return date_format, time_format
end
to_datepicker_format(rails_format) click to toggle source
# File lib/active_scaffold/bridges/date_picker/lib/datepicker_bridge.rb, line 161
def self.to_datepicker_format(rails_format)
  return nil if rails_format.nil?
  if rails_format =~ /%[cUWwxXZz]/
    Rails.logger.warn("AS DatePickerBridge: Can t convert rails date format: #{rails_format} to jquery datepicker format. Options %c, %U, %W, %w, %x %X, %z, %Z are not supported by datepicker]")
    nil
  else
    js_format = rails_format.dup
    DATE_FORMAT_CONVERSION.each do |key, value|
      js_format.gsub!(Regexp.new("#{key}"), value)
    end
    js_format
  end
end