Initial Commit

This commit is contained in:
Jason Katz
2011-06-14 18:08:28 -07:00
commit 5a34ac406c
172 changed files with 18457 additions and 0 deletions

View File

@@ -0,0 +1,33 @@
require "calendar_date_select/calendar_date_select.rb"
require "calendar_date_select/form_helpers.rb"
require "calendar_date_select/includes_helper.rb"
if Object.const_defined?(:Rails) && File.directory?(Rails.root.to_s + "/public")
ActionView::Helpers::FormHelper.send(:include, CalendarDateSelect::FormHelpers)
ActionView::Base.send(:include, CalendarDateSelect::FormHelpers)
ActionView::Base.send(:include, CalendarDateSelect::IncludesHelper)
# Filthy backwards compatibility hooks... grumble
if ([Rails::VERSION::MAJOR, Rails::VERSION::MINOR] <=> [2, 2]) == -1
ActionView::Helpers::InstanceTag.class_eval do
def self.new_with_backwards_compatibility(object_name, method_name, template_object, object = nil)
new(object_name, method_name, template_object, nil, object)
end
end
else
ActionView::Helpers::InstanceTag.class_eval do
class << self; alias new_with_backwards_compatibility new; end
end
end
# install files
unless File.exists?(RAILS_ROOT + '/public/javascripts/calendar_date_select/calendar_date_select.js')
['/public', '/public/javascripts/calendar_date_select', '/public/stylesheets/calendar_date_select', '/public/images/calendar_date_select', '/public/javascripts/calendar_date_select/locale'].each do |dir|
source = File.dirname(__FILE__) + "/../#{dir}"
dest = RAILS_ROOT + dir
FileUtils.mkdir_p(dest)
FileUtils.cp(Dir.glob(source+'/*.*'), dest)
end
end
end

View File

@@ -0,0 +1,122 @@
module CalendarDateSelect
VERSION = '1.16.1'
FORMATS = {
:natural => {
:date => "%B %d, %Y",
:time => " %I:%M %p"
},
:hyphen_ampm => {
:date => "%Y-%m-%d",
:time => " %I:%M %p",
:javascript_include => "format_hyphen_ampm"
},
:iso_date => {
:date => "%Y-%m-%d",
:time => " %H:%M",
:javascript_include => "format_iso_date"
},
:finnish => {
:date => "%d.%m.%Y",
:time => " %H:%M",
:javascript_include => "format_finnish"
},
:danish => {
:date => "%d/%m/%Y",
:time => " %H:%M",
:javascript_include => "format_danish"
},
:american => {
:date => "%m/%d/%Y",
:time => " %I:%M %p",
:javascript_include => "format_american"
},
:euro_24hr => {
:date => "%d %B %Y",
:time => " %H:%M",
:javascript_include => "format_euro_24hr"
},
:euro_24hr_ymd => {
:date => "%Y.%m.%d",
:time => " %H:%M",
:javascript_include => "format_euro_24hr_ymd"
},
:italian => {
:date => "%d/%m/%Y",
:time => " %H:%M",
:javascript_include => "format_italian"
},
:db => {
:date => "%Y-%m-%d",
:time => " %H:%M",
:javascript_include => "format_db"
}
}
# Returns the default_options hash. These options are by default provided to every calendar_date_select control, unless otherwise overrided.
#
# Example:
# # At the bottom of config/environment.rb:
# CalendarDateSelect.default_options.update(
# :popup => "force",
# :month_year => "label",
# :image => "custom_calendar_picker.png"
# )
def self.default_options
@calendar_date_select_default_options ||= { :image => "calendar_date_select/calendar.gif" }
end
# Set the picker image. Provide the image url the same way you would provide it to image_tag
def self.image=(value)
default_options[:image] = value
end
# Returns the options for the given format
#
# Example:
# CalendarDateSelect.format = :italian
# puts CalendarDateSelect.format[:date]
# => "%d/%m/%Y"
def self.format
@calendar_date_select_format ||= FORMATS[:natural]
end
# Set the format. To see a list of available formats, CalendarDateSelect::FORMATS.keys, or open lib/calendar_date_select/calendar_date_select.rb
#
# (e.g. CalendarDateSelect.format = :italian)
def self.format=(format)
raise "CalendarDateSelect: Unrecognized format specification: #{format}" unless FORMATS.has_key?(format)
@calendar_date_select_format = FORMATS[format]
end
def self.date_format_string(time = false)
format[:date] + (time ? format[:time] : "")
end
def self.format_date(date)
if date.is_a?(Date)
date.strftime(date_format_string(false))
else
date.strftime(date_format_string(true))
end
end
def self.format_time(value, options = {})
return value unless value.respond_to?("strftime")
if options[:time]
format_date(value)
else
format_date(value.to_date)
end
end
# Detects the presence of time in a date, string
def self.has_time?(value)
case value
when DateTime, Time then true
when Date then false
else
/[0-9]:[0-9]{2}/.match(value.to_s) ? true : false
end
end
end

View File

@@ -0,0 +1,237 @@
# Various helpers available for use in your view
module CalendarDateSelect::FormHelpers
# Similar to text_field_tag, but adds a calendar picker, naturally.
#
# == Arguments
#
# +name+ - the html name of the tag
# +value+ - When specified as a string, uses value verbatim. When Date, DateTime, Time, it converts it to a string basd off the format set by CalendarDateSelect#format=
# +options+ - ...
#
# == Options
#
# === :embedded
#
# Put the calendar straight into the form, rather than using a popup type of form.
#
# <%= calendar_date_select_tag "name", "2007-01-01", :embedded => true %>
#
# === :hidden
#
# Use a hidden element instead of a text box for a pop up calendar. Not compatible with :embedded => true. You'll probably want to use an onchange callback to do something with the value.
#
# <span id='cds_value' />
# <%= calendar_date_select_tag "hidden_date_selector", "", :hidden => "true", :onchange => "$('cds_value').update($F(this));" %>
#
# === :image
#
# Specify an alternative icon to use for the date picker.
#
# To use /images/groovy.png:
#
# <%= calendar_date_select_tag "altered_image", "", :image => "groovy.png" %>
#
# === :minute_interval
#
# Specifies the minute interval used in the hour/minute selector. Default is 5.
#
# <%= calendar_date_select_tag "month_year_selector_label", "", :minute_interval => 15 %>
#
# === :month_year
#
# Customize the month and year selectors at the top of the control.
#
# Valid values:
# * "dropdowns" (default) - Use a separate dropdown control for both the month and year
# * "label" - Use static text to show the month and the year.
#
# <%= calendar_date_select_tag "month_year_selector_label", "", :month_year => "label" %>
#
# === :popup => 'force'
#
# Forces the user to use the popup calendar by making it's text-box read-only and causing calendar_date_select to override it's default behavior of not allowing selection of a date on a target element that is read-only.
#
# <%= calendar_date_select_tag "name", "2007-01-01", :popup => "force" %>
#
# === :time
#
# Show time in the controls. There's three options:
#
# * +true+ - show an hour/minute selector.
# * +false+ - don't show an hour/minute selector.
# * +"mixed"+ - Show an hour/minute selector, but include a "all day" option - allowing them to choose whether or not to specify a time.
#
# === :year_range
#
# Limit the year range. You can pass in an array or range of ruby Date/Time objects or FixNum's.
#
# <%= calendar_date_select_tag "e_date", nil, :year_range => 10.years.ago..0.years.from_now %>
# <%= calendar_date_select_tag "e_date", nil, :year_range => [0.years.ago, 10.years.from_now] %>
# <%= calendar_date_select_tag "e_date", nil, :year_range => 2000..2007 %>
# <%= calendar_date_select_tag "e_date", nil, :year_range => [2000, 2007] %>
#
# == CALLBACKS
#
# The following callbacks are available:
#
# * before_show / after_show
# * before_close / after_close
# * after_navigate - Called when navigating to a different month. Passes first parameter as a date object refering to the current month viewed
# * onchange - Called when the form input value changes
#
# <%= calendar_date_select_tag "event_demo", "",
# :before_show => "log('Calendar Showing');" ,
# :after_show => "log('Calendar Shown');" ,
# :before_close => "log('Calendar closing');" ,
# :after_close => "log('Calendar closed');",
# :after_navigate => "log('Current month is ' + (param.getMonth()+1) + '/' + (param.getFullYear()));",
# :onchange => "log('value changed to - ' + $F(this));" %>
#
# }}}
#
# All callbacks are executed within the context of the target input element. If you'd like to access the CalendarDateSelect object itself, you can access it via "this.calendar_date_select".
#
# For example:
#
# <%= calendar_date_select_tag "event_demo", "", :after_navigate => "alert('The current selected month is ' + this.calendar_date_select.selected_date.getMonth());" ,
def calendar_date_select_tag( name, value = nil, options = {})
image, options, javascript_options = calendar_date_select_process_options(options)
value = CalendarDateSelect.format_time(value, javascript_options)
javascript_options.delete(:format)
options[:id] ||= name
tag = javascript_options[:hidden] || javascript_options[:embedded] ?
hidden_field_tag(name, value, options) :
text_field_tag(name, value, options)
calendar_date_select_output(tag, image, options, javascript_options)
end
# Similar to the difference between +text_field_tag+ and +text_field+, this method behaves like +text_field+
#
# It receives the same options as +calendar_date_select_tag+. Need for time selection is automatically detected by checking the corresponding column meta information of Model#columns_hash
def calendar_date_select(object, method, options={})
obj = options[:object] || instance_variable_get("@#{object}")
if !options.include?(:time) && obj.class.respond_to?("columns_hash")
column_type = obj.class.columns_hash[method.to_s].type if obj.class.columns_hash.include?(method.to_s)
options[:time] = true if column_type == :datetime
end
use_time = options[:time]
if options[:time].to_s=="mixed"
use_time = false if Date===(obj.respond_to?(method) && obj.send(method))
end
image, options, javascript_options = calendar_date_select_process_options(options)
options[:value] ||=
if(obj.respond_to?(method) && obj.send(method).respond_to?(:strftime))
obj.send(method).strftime(CalendarDateSelect.date_format_string(use_time))
elsif obj.respond_to?("#{method}_before_type_cast")
obj.send("#{method}_before_type_cast")
elsif obj.respond_to?(method)
obj.send(method).to_s
else
begin
obj.send(method).strftime(CalendarDateSelect.date_format_string(use_time))
rescue
nil
end
end
tag = ActionView::Helpers::InstanceTag.new_with_backwards_compatibility(object, method, self, options.delete(:object))
calendar_date_select_output(
tag.to_input_field_tag( (javascript_options[:hidden] || javascript_options[:embedded]) ? "hidden" : "text", options),
image,
options,
javascript_options
)
end
private
# extracts any options passed into calendar date select, appropriating them to either the Javascript call or the html tag.
def calendar_date_select_process_options(options)
options, javascript_options = CalendarDateSelect.default_options.merge(options), {}
image = options.delete(:image)
callbacks = [:before_show, :before_close, :after_show, :after_close, :after_navigate]
for key in [:default_time, :time, :valid_date_check, :embedded, :buttons, :clear_button, :format, :year_range, :month_year, :popup, :hidden, :minute_interval] + callbacks
javascript_options[key] = options.delete(key) if options.has_key?(key)
end
if (default_time = javascript_options[:default_time])
if default_time.respond_to?(:strftime)
javascript_options[:default_time] = "new Date('#{default_time.strftime(CalendarDateSelect.date_format_string(true))}')"
else
javascript_options[:default_time] = "function() { return #{default_time} }"
end
end
# if passing in mixed, pad it with single quotes
javascript_options[:time] = "'mixed'" if javascript_options[:time].to_s=="mixed"
javascript_options[:month_year] = "'#{javascript_options[:month_year]}'" if javascript_options[:month_year]
# if we are forcing the popup, automatically set the readonly property on the input control.
if javascript_options[:popup].to_s == "force"
javascript_options[:popup] = "'force'"
options[:readonly] = true
end
if (vdc=javascript_options.delete(:valid_date_check))
if vdc.include?(";") || vdc.include?("function")
raise ArgumentError, ":valid_date_check function is missing a 'return' statement. Try something like: :valid_date_check => 'if (date > new(Date)) return true; else return false;'" unless vdc.include?("return");
end
vdc = "return(#{vdc})" unless vdc.include?("return")
vdc = "function(date) { #{vdc} }" unless vdc.include?("function")
javascript_options[:valid_date_check] = vdc
end
javascript_options[:popup_by] ||= "this" if javascript_options[:hidden]
# surround any callbacks with a function, if not already done so
for key in callbacks
javascript_options[key] = "function(param) { #{javascript_options[key]} }" unless javascript_options[key].include?("function") if javascript_options[key]
end
javascript_options[:year_range] = format_year_range(javascript_options[:year_range] || 10)
[image, options, javascript_options]
end
def calendar_date_select_output(input, image, options = {}, javascript_options = {})
out = input
if javascript_options[:embedded]
uniq_id = "cds_placeholder_#{(rand*100000).to_i}"
# we need to be able to locate the target input element, so lets stick an invisible span tag here we can easily locate
out << content_tag(:span, nil, :style => "display: none; position: absolute;", :id => uniq_id)
out << javascript_tag("new CalendarDateSelect( $('#{uniq_id}').previous(), #{options_for_javascript(javascript_options)} ); ")
else
out << " "
out << image_tag(image,
:onclick => "new CalendarDateSelect( $(this).previous(), #{options_for_javascript(javascript_options)} );",
:style => 'border:0px; cursor:pointer;',
:class=>'calendar_date_select_popup_icon')
end
out
end
def format_year_range(year) # nodoc
return year unless year.respond_to?(:first)
return "[#{year.first}, #{year.last}]" unless year.first.respond_to?(:strftime)
return "[#{year.first.year}, #{year.last.year}]"
end
end
# Helper method for form builders
module ActionView
module Helpers
class FormBuilder
def calendar_date_select(method, options = {})
@template.calendar_date_select(@object_name, method, options.merge(:object => @object))
end
end
end
end

View File

@@ -0,0 +1,29 @@
module CalendarDateSelect::IncludesHelper
# returns the selected calendar_date_select stylesheet (not an array)
def calendar_date_select_stylesheets(options = {})
options.assert_valid_keys(:style)
"calendar_date_select/#{options[:style] || "default"}"
end
# returns an array of javascripts needed for the selected locale, date_format, and calendar control itself.
def calendar_date_select_javascripts(options = {})
options.assert_valid_keys(:locale)
files = ["calendar_date_select/calendar_date_select"]
files << "calendar_date_select/locale/#{options[:locale]}" if options[:locale]
files << "calendar_date_select/#{CalendarDateSelect.format[:javascript_include]}" if CalendarDateSelect.format[:javascript_include]
files
end
# returns html necessary to load javascript and css to make calendar_date_select work
def calendar_date_select_includes(*args)
return "" if @cds_already_included
@cds_already_included=true
options = (Hash === args.last) ? args.pop : {}
options.assert_valid_keys(:style, :locale)
options[:style] ||= args.shift
javascript_include_tag(*calendar_date_select_javascripts(:locale => options[:locale])) + "\n" +
stylesheet_link_tag(*calendar_date_select_stylesheets(:style => options[:style])) + "\n"
end
end