ginkel-calendar_date_select 1.16.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (57) hide show
  1. data/.gitignore +3 -0
  2. data/History.txt +270 -0
  3. data/MIT-LICENSE +20 -0
  4. data/Manifest.txt +42 -0
  5. data/README.txt +16 -0
  6. data/Rakefile +38 -0
  7. data/VERSION +1 -0
  8. data/calendar_date_select.gemspec +99 -0
  9. data/init.rb +1 -0
  10. data/js_test/functional/.tmp_cds_test.html +306 -0
  11. data/js_test/functional/cds_test.html +365 -0
  12. data/js_test/functional/format_iso_date_test.html +52 -0
  13. data/js_test/prototype.js +4184 -0
  14. data/js_test/test.css +40 -0
  15. data/js_test/unit/cds_helper_methods.html +46 -0
  16. data/js_test/unittest.js +564 -0
  17. data/lib/calendar_date_select.rb +33 -0
  18. data/lib/calendar_date_select/calendar_date_select.rb +122 -0
  19. data/lib/calendar_date_select/form_helpers.rb +240 -0
  20. data/lib/calendar_date_select/includes_helper.rb +29 -0
  21. data/public/blank_iframe.html +2 -0
  22. data/public/images/calendar_date_select/calendar.gif +0 -0
  23. data/public/javascripts/calendar_date_select/calendar_date_select.js +448 -0
  24. data/public/javascripts/calendar_date_select/format_american.js +35 -0
  25. data/public/javascripts/calendar_date_select/format_danish.js +31 -0
  26. data/public/javascripts/calendar_date_select/format_db.js +27 -0
  27. data/public/javascripts/calendar_date_select/format_euro_24hr.js +7 -0
  28. data/public/javascripts/calendar_date_select/format_euro_24hr_ymd.js +7 -0
  29. data/public/javascripts/calendar_date_select/format_finnish.js +32 -0
  30. data/public/javascripts/calendar_date_select/format_hyphen_ampm.js +37 -0
  31. data/public/javascripts/calendar_date_select/format_iso_date.js +29 -0
  32. data/public/javascripts/calendar_date_select/format_italian.js +24 -0
  33. data/public/javascripts/calendar_date_select/locale/ar.js +10 -0
  34. data/public/javascripts/calendar_date_select/locale/da.js +11 -0
  35. data/public/javascripts/calendar_date_select/locale/de.js +11 -0
  36. data/public/javascripts/calendar_date_select/locale/es.js +11 -0
  37. data/public/javascripts/calendar_date_select/locale/fi.js +10 -0
  38. data/public/javascripts/calendar_date_select/locale/fr.js +11 -0
  39. data/public/javascripts/calendar_date_select/locale/it.js +9 -0
  40. data/public/javascripts/calendar_date_select/locale/ja.js +11 -0
  41. data/public/javascripts/calendar_date_select/locale/nl.js +11 -0
  42. data/public/javascripts/calendar_date_select/locale/pl.js +11 -0
  43. data/public/javascripts/calendar_date_select/locale/pt.js +11 -0
  44. data/public/javascripts/calendar_date_select/locale/ru.js +10 -0
  45. data/public/javascripts/calendar_date_select/locale/sl.js +11 -0
  46. data/public/javascripts/calendar_date_select/locale/sv.js +9 -0
  47. data/public/stylesheets/calendar_date_select/blue.css +130 -0
  48. data/public/stylesheets/calendar_date_select/default.css +135 -0
  49. data/public/stylesheets/calendar_date_select/green.css +142 -0
  50. data/public/stylesheets/calendar_date_select/plain.css +128 -0
  51. data/public/stylesheets/calendar_date_select/red.css +135 -0
  52. data/public/stylesheets/calendar_date_select/silver.css +133 -0
  53. data/spec/calendar_date_select/calendar_date_select_spec.rb +14 -0
  54. data/spec/calendar_date_select/form_helpers_spec.rb +189 -0
  55. data/spec/calendar_date_select/includes_helper_spec.rb +46 -0
  56. data/spec/spec_helper.rb +26 -0
  57. metadata +130 -0
@@ -0,0 +1,33 @@
1
+ require "calendar_date_select/calendar_date_select.rb"
2
+ require "calendar_date_select/form_helpers.rb"
3
+ require "calendar_date_select/includes_helper.rb"
4
+
5
+ if Object.const_defined?(:Rails) && File.directory?(Rails.root.to_s + "/public")
6
+ ActionView::Helpers::FormHelper.send(:include, CalendarDateSelect::FormHelpers)
7
+ ActionView::Base.send(:include, CalendarDateSelect::FormHelpers)
8
+ ActionView::Base.send(:include, CalendarDateSelect::IncludesHelper)
9
+
10
+ # Filthy backwards compatibility hooks... grumble
11
+ if ([Rails::VERSION::MAJOR, Rails::VERSION::MINOR] <=> [2, 2]) == -1
12
+ ActionView::Helpers::InstanceTag.class_eval do
13
+ def self.new_with_backwards_compatibility(object_name, method_name, template_object, object = nil)
14
+ new(object_name, method_name, template_object, nil, object)
15
+ end
16
+ end
17
+
18
+ else
19
+ ActionView::Helpers::InstanceTag.class_eval do
20
+ class << self; alias new_with_backwards_compatibility new; end
21
+ end
22
+ end
23
+
24
+ # install files
25
+ unless File.exists?(RAILS_ROOT + '/public/javascripts/calendar_date_select/calendar_date_select.js')
26
+ ['/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|
27
+ source = File.dirname(__FILE__) + "/../#{dir}"
28
+ dest = RAILS_ROOT + dir
29
+ FileUtils.mkdir_p(dest)
30
+ FileUtils.cp(Dir.glob(source+'/*.*'), dest)
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,122 @@
1
+ module CalendarDateSelect
2
+ VERSION = '1.16.1.1'
3
+
4
+ FORMATS = {
5
+ :natural => {
6
+ :date => "%B %d, %Y",
7
+ :time => " %I:%M %p"
8
+ },
9
+ :hyphen_ampm => {
10
+ :date => "%Y-%m-%d",
11
+ :time => " %I:%M %p",
12
+ :javascript_include => "format_hyphen_ampm"
13
+ },
14
+ :iso_date => {
15
+ :date => "%Y-%m-%d",
16
+ :time => " %H:%M",
17
+ :javascript_include => "format_iso_date"
18
+ },
19
+ :finnish => {
20
+ :date => "%d.%m.%Y",
21
+ :time => " %H:%M",
22
+ :javascript_include => "format_finnish"
23
+ },
24
+ :danish => {
25
+ :date => "%d/%m/%Y",
26
+ :time => " %H:%M",
27
+ :javascript_include => "format_danish"
28
+ },
29
+ :american => {
30
+ :date => "%m/%d/%Y",
31
+ :time => " %I:%M %p",
32
+ :javascript_include => "format_american"
33
+ },
34
+ :euro_24hr => {
35
+ :date => "%d %B %Y",
36
+ :time => " %H:%M",
37
+ :javascript_include => "format_euro_24hr"
38
+ },
39
+ :euro_24hr_ymd => {
40
+ :date => "%Y.%m.%d",
41
+ :time => " %H:%M",
42
+ :javascript_include => "format_euro_24hr_ymd"
43
+ },
44
+ :italian => {
45
+ :date => "%d/%m/%Y",
46
+ :time => " %H:%M",
47
+ :javascript_include => "format_italian"
48
+ },
49
+ :db => {
50
+ :date => "%Y-%m-%d",
51
+ :time => " %H:%M",
52
+ :javascript_include => "format_db"
53
+ }
54
+ }
55
+
56
+ # Returns the default_options hash. These options are by default provided to every calendar_date_select control, unless otherwise overrided.
57
+ #
58
+ # Example:
59
+ # # At the bottom of config/environment.rb:
60
+ # CalendarDateSelect.default_options.update(
61
+ # :popup => "force",
62
+ # :month_year => "label",
63
+ # :image => "custom_calendar_picker.png"
64
+ # )
65
+ def self.default_options
66
+ @calendar_date_select_default_options ||= { :image => "calendar_date_select/calendar.gif" }
67
+ end
68
+
69
+ # Set the picker image. Provide the image url the same way you would provide it to image_tag
70
+ def self.image=(value)
71
+ default_options[:image] = value
72
+ end
73
+
74
+ # Returns the options for the given format
75
+ #
76
+ # Example:
77
+ # CalendarDateSelect.format = :italian
78
+ # puts CalendarDateSelect.format[:date]
79
+ # => "%d/%m/%Y"
80
+ def self.format
81
+ @calendar_date_select_format ||= FORMATS[:natural]
82
+ end
83
+
84
+ # Set the format. To see a list of available formats, CalendarDateSelect::FORMATS.keys, or open lib/calendar_date_select/calendar_date_select.rb
85
+ #
86
+ # (e.g. CalendarDateSelect.format = :italian)
87
+ def self.format=(format)
88
+ raise "CalendarDateSelect: Unrecognized format specification: #{format}" unless FORMATS.has_key?(format)
89
+ @calendar_date_select_format = FORMATS[format]
90
+ end
91
+
92
+ def self.date_format_string(time = false)
93
+ format[:date] + (time ? format[:time] : "")
94
+ end
95
+
96
+ def self.format_date(date)
97
+ if date.is_a?(Date)
98
+ date.strftime(date_format_string(false))
99
+ else
100
+ date.strftime(date_format_string(true))
101
+ end
102
+ end
103
+
104
+ def self.format_time(value, options = {})
105
+ return value unless value.respond_to?("strftime")
106
+ if options[:time]
107
+ format_date(value)
108
+ else
109
+ format_date(value.to_date)
110
+ end
111
+ end
112
+
113
+ # Detects the presence of time in a date, string
114
+ def self.has_time?(value)
115
+ case value
116
+ when DateTime, Time then true
117
+ when Date then false
118
+ else
119
+ /[0-9]:[0-9]{2}/.match(value.to_s) ? true : false
120
+ end
121
+ end
122
+ end
@@ -0,0 +1,240 @@
1
+ # Various helpers available for use in your view
2
+ module CalendarDateSelect::FormHelpers
3
+
4
+ # Similar to text_field_tag, but adds a calendar picker, naturally.
5
+ #
6
+ # == Arguments
7
+ #
8
+ # +name+ - the html name of the tag
9
+ # +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=
10
+ # +options+ - ...
11
+ #
12
+ # == Options
13
+ #
14
+ # === :embedded
15
+ #
16
+ # Put the calendar straight into the form, rather than using a popup type of form.
17
+ #
18
+ # <%= calendar_date_select_tag "name", "2007-01-01", :embedded => true %>
19
+ #
20
+ # === :hidden
21
+ #
22
+ # 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.
23
+ #
24
+ # <span id='cds_value' />
25
+ # <%= calendar_date_select_tag "hidden_date_selector", "", :hidden => "true", :onchange => "$('cds_value').update($F(this));" %>
26
+ #
27
+ # === :image
28
+ #
29
+ # Specify an alternative icon to use for the date picker.
30
+ #
31
+ # To use /images/groovy.png:
32
+ #
33
+ # <%= calendar_date_select_tag "altered_image", "", :image => "groovy.png" %>
34
+ #
35
+ # === :minute_interval
36
+ #
37
+ # Specifies the minute interval used in the hour/minute selector. Default is 5.
38
+ #
39
+ # <%= calendar_date_select_tag "month_year_selector_label", "", :minute_interval => 15 %>
40
+ #
41
+ # === :month_year
42
+ #
43
+ # Customize the month and year selectors at the top of the control.
44
+ #
45
+ # Valid values:
46
+ # * "dropdowns" (default) - Use a separate dropdown control for both the month and year
47
+ # * "label" - Use static text to show the month and the year.
48
+ #
49
+ # <%= calendar_date_select_tag "month_year_selector_label", "", :month_year => "label" %>
50
+ #
51
+ # === :popup => 'force'
52
+ #
53
+ # 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.
54
+ #
55
+ # <%= calendar_date_select_tag "name", "2007-01-01", :popup => "force" %>
56
+ #
57
+ # === :time
58
+ #
59
+ # Show time in the controls. There's three options:
60
+ #
61
+ # * +true+ - show an hour/minute selector.
62
+ # * +false+ - don't show an hour/minute selector.
63
+ # * +"mixed"+ - Show an hour/minute selector, but include a "all day" option - allowing them to choose whether or not to specify a time.
64
+ #
65
+ # === :year_range
66
+ #
67
+ # Limit the year range. You can pass in an array or range of ruby Date/Time objects or FixNum's.
68
+ #
69
+ # <%= calendar_date_select_tag "e_date", nil, :year_range => 10.years.ago..0.years.from_now %>
70
+ # <%= calendar_date_select_tag "e_date", nil, :year_range => [0.years.ago, 10.years.from_now] %>
71
+ # <%= calendar_date_select_tag "e_date", nil, :year_range => 2000..2007 %>
72
+ # <%= calendar_date_select_tag "e_date", nil, :year_range => [2000, 2007] %>
73
+ #
74
+ # == CALLBACKS
75
+ #
76
+ # The following callbacks are available:
77
+ #
78
+ # * before_show / after_show
79
+ # * before_close / after_close
80
+ # * after_navigate - Called when navigating to a different month. Passes first parameter as a date object refering to the current month viewed
81
+ # * onchange - Called when the form input value changes
82
+ #
83
+ # <%= calendar_date_select_tag "event_demo", "",
84
+ # :before_show => "log('Calendar Showing');" ,
85
+ # :after_show => "log('Calendar Shown');" ,
86
+ # :before_close => "log('Calendar closing');" ,
87
+ # :after_close => "log('Calendar closed');",
88
+ # :after_navigate => "log('Current month is ' + (param.getMonth()+1) + '/' + (param.getFullYear()));",
89
+ # :onchange => "log('value changed to - ' + $F(this));" %>
90
+ #
91
+ # }}}
92
+ #
93
+ # 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".
94
+ #
95
+ # For example:
96
+ #
97
+ # <%= calendar_date_select_tag "event_demo", "", :after_navigate => "alert('The current selected month is ' + this.calendar_date_select.selected_date.getMonth());" ,
98
+ def calendar_date_select_tag( name, value = nil, options = {})
99
+ image, options, javascript_options = calendar_date_select_process_options(options)
100
+ value = CalendarDateSelect.format_time(value, javascript_options)
101
+
102
+ javascript_options.delete(:format)
103
+
104
+ options[:id] ||= name
105
+ tag = javascript_options[:hidden] || javascript_options[:embedded] ?
106
+ hidden_field_tag(name, value, options) :
107
+ text_field_tag(name, value, options)
108
+
109
+ calendar_date_select_output(tag, image, options, javascript_options)
110
+ end
111
+
112
+ # Similar to the difference between +text_field_tag+ and +text_field+, this method behaves like +text_field+
113
+ #
114
+ # 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
115
+ def calendar_date_select(object, method, options={})
116
+ obj = options[:object] || instance_variable_get("@#{object}")
117
+
118
+ if !options.include?(:time) && obj.class.respond_to?("columns_hash")
119
+ column_type = obj.class.columns_hash[method.to_s].type if obj.class.columns_hash.include?(method.to_s)
120
+ options[:time] = true if column_type == :datetime
121
+ end
122
+
123
+ use_time = options[:time]
124
+
125
+ if options[:time].to_s=="mixed"
126
+ use_time = false if Date===(obj.respond_to?(method) && obj.send(method))
127
+ end
128
+
129
+ image, options, javascript_options = calendar_date_select_process_options(options)
130
+
131
+ options[:value] ||=
132
+ if(obj.respond_to?(method) && obj.send(method).respond_to?(:strftime))
133
+ obj.send(method).strftime(CalendarDateSelect.date_format_string(use_time))
134
+ elsif obj.respond_to?("#{method}_before_type_cast")
135
+ obj.send("#{method}_before_type_cast")
136
+ elsif obj.respond_to?(method)
137
+ obj.send(method).to_s
138
+ else
139
+ begin
140
+ obj.send(method).strftime(CalendarDateSelect.date_format_string(use_time))
141
+ rescue
142
+ nil
143
+ end
144
+ end
145
+
146
+ tag = ActionView::Helpers::InstanceTag.new_with_backwards_compatibility(object, method, self, options.delete(:object))
147
+ calendar_date_select_output(
148
+ tag.to_input_field_tag( (javascript_options[:hidden] || javascript_options[:embedded]) ? "hidden" : "text", options),
149
+ image,
150
+ options,
151
+ javascript_options
152
+ )
153
+ end
154
+
155
+ private
156
+ # extracts any options passed into calendar date select, appropriating them to either the Javascript call or the html tag.
157
+ def calendar_date_select_process_options(options)
158
+ options, javascript_options = CalendarDateSelect.default_options.merge(options), {}
159
+ image = options.delete(:image)
160
+ callbacks = [:before_show, :before_close, :after_show, :after_close, :after_navigate]
161
+ for key in [:default_time, :time, :valid_date_check, :embedded, :buttons, :clear_button, :format, :year_range, :month_year, :popup, :hidden, :minute_interval, :readonly] + callbacks
162
+ javascript_options[key] = options.delete(key) if options.has_key?(key)
163
+ end
164
+
165
+ if (default_time = javascript_options[:default_time])
166
+ if default_time.respond_to?(:strftime)
167
+ javascript_options[:default_time] = "new Date('#{default_time.strftime(CalendarDateSelect.date_format_string(true))}')"
168
+ else
169
+ javascript_options[:default_time] = "function() { return #{default_time} }"
170
+ end
171
+ end
172
+
173
+ # if passing in mixed, pad it with single quotes
174
+ javascript_options[:time] = "'mixed'" if javascript_options[:time].to_s=="mixed"
175
+ javascript_options[:month_year] = "'#{javascript_options[:month_year]}'" if javascript_options[:month_year]
176
+
177
+ # if we are forcing the popup, automatically set the readonly property on the input control.
178
+ if javascript_options[:popup].to_s == "force"
179
+ javascript_options[:popup] = "'force'"
180
+ options[:readonly] = true
181
+ end
182
+
183
+ if (vdc=javascript_options.delete(:valid_date_check))
184
+ if vdc.include?(";") || vdc.include?("function")
185
+ 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");
186
+ end
187
+
188
+ vdc = "return(#{vdc})" unless vdc.include?("return")
189
+ vdc = "function(date) { #{vdc} }" unless vdc.include?("function")
190
+ javascript_options[:valid_date_check] = vdc
191
+ end
192
+
193
+ javascript_options[:popup_by] ||= "this" if javascript_options[:hidden]
194
+
195
+ # surround any callbacks with a function, if not already done so
196
+ for key in callbacks
197
+ javascript_options[key] = "function(param) { #{javascript_options[key]} }" unless javascript_options[key].include?("function") if javascript_options[key]
198
+ end
199
+
200
+ javascript_options[:year_range] = format_year_range(javascript_options[:year_range] || 10)
201
+ [image, options, javascript_options]
202
+ end
203
+
204
+ def calendar_date_select_output(input, image, options = {}, javascript_options = {})
205
+ out = input
206
+ if javascript_options[:embedded]
207
+ uniq_id = "cds_placeholder_#{(rand*100000).to_i}"
208
+ # we need to be able to locate the target input element, so lets stick an invisible span tag here we can easily locate
209
+ out << content_tag(:span, nil, :style => "display: none; position: absolute;", :id => uniq_id)
210
+ out << javascript_tag("new CalendarDateSelect( $('#{uniq_id}').previous(), #{options_for_javascript(javascript_options)} ); ")
211
+ else
212
+ unless javascript_options[:readonly]
213
+ out << " "
214
+ out << image_tag(image,
215
+ :onclick => "new CalendarDateSelect( $(this).previous(), #{options_for_javascript(javascript_options)} );",
216
+ :style => 'border:0px; cursor:pointer;',
217
+ :class => 'calendar_date_select_popup_icon',
218
+ :id => options[:id] + '_img')
219
+ end
220
+ end
221
+ out
222
+ end
223
+
224
+ def format_year_range(year) # nodoc
225
+ return year unless year.respond_to?(:first)
226
+ return "[#{year.first}, #{year.last}]" unless year.first.respond_to?(:strftime)
227
+ return "[#{year.first.year}, #{year.last.year}]"
228
+ end
229
+ end
230
+
231
+ # Helper method for form builders
232
+ module ActionView
233
+ module Helpers
234
+ class FormBuilder
235
+ def calendar_date_select(method, options = {})
236
+ @template.calendar_date_select(@object_name, method, options.merge(:object => @object))
237
+ end
238
+ end
239
+ end
240
+ end
@@ -0,0 +1,29 @@
1
+ module CalendarDateSelect::IncludesHelper
2
+ # returns the selected calendar_date_select stylesheet (not an array)
3
+ def calendar_date_select_stylesheets(options = {})
4
+ options.assert_valid_keys(:style)
5
+ "calendar_date_select/#{options[:style] || "default"}"
6
+ end
7
+
8
+ # returns an array of javascripts needed for the selected locale, date_format, and calendar control itself.
9
+ def calendar_date_select_javascripts(options = {})
10
+ options.assert_valid_keys(:locale)
11
+ files = ["calendar_date_select/calendar_date_select"]
12
+ files << "calendar_date_select/locale/#{options[:locale]}" if options[:locale]
13
+ files << "calendar_date_select/#{CalendarDateSelect.format[:javascript_include]}" if CalendarDateSelect.format[:javascript_include]
14
+ files
15
+ end
16
+
17
+ # returns html necessary to load javascript and css to make calendar_date_select work
18
+ def calendar_date_select_includes(*args)
19
+ return "" if @cds_already_included
20
+ @cds_already_included=true
21
+
22
+ options = (Hash === args.last) ? args.pop : {}
23
+ options.assert_valid_keys(:style, :locale)
24
+ options[:style] ||= args.shift
25
+
26
+ javascript_include_tag(*calendar_date_select_javascripts(:locale => options[:locale])) + "\n" +
27
+ stylesheet_link_tag(*calendar_date_select_stylesheets(:style => options[:style])) + "\n"
28
+ end
29
+ end
@@ -0,0 +1,2 @@
1
+ <!-- Nothing here; part of the calendar_date_select plugin -->
2
+ <html><head></head><body></body></html>
@@ -0,0 +1,448 @@
1
+ // CalendarDateSelect version 1.16.1.1 - a prototype based date picker
2
+ // Questions, comments, bugs? - see the project page: http://code.google.com/p/calendardateselect
3
+ if (typeof Prototype == 'undefined') alert("CalendarDateSelect Error: Prototype could not be found. Please make sure that your application's layout includes prototype.js (.g. <%= javascript_include_tag :defaults %>) *before* it includes calendar_date_select.js (.g. <%= calendar_date_select_includes %>).");
4
+ if (Prototype.Version < "1.6") alert("Prototype 1.6.0 is required. If using earlier version of prototype, please use calendar_date_select version 1.8.3");
5
+
6
+ Element.addMethods({
7
+ purgeChildren: function(element) { $A(element.childNodes).each(function(e){$(e).remove();}); },
8
+ build: function(element, type, options, style) {
9
+ var newElement = Element.buildAndAppend(type, options, style);
10
+ element.appendChild(newElement);
11
+ return newElement;
12
+ }
13
+ });
14
+
15
+ Element.buildAndAppend = function(type, options, style)
16
+ {
17
+ var e = $(document.createElement(type));
18
+ $H(options).each(function(pair) { e[pair.key] = pair.value });
19
+ if (style) e.setStyle(style);
20
+ return e;
21
+ };
22
+ nil = null;
23
+
24
+ Date.one_day = 24*60*60*1000;
25
+ Date.weekdays = $w("S M T W T F S");
26
+ Date.first_day_of_week = 0;
27
+ Date.months = $w("January February March April May June July August September October November December" );
28
+ Date.padded2 = function(hour) { var padded2 = parseInt(hour, 10); if (hour < 10) padded2 = "0" + padded2; return padded2; }
29
+ Date.prototype.getPaddedMinutes = function() { return Date.padded2(this.getMinutes()); }
30
+ Date.prototype.getAMPMHour = function() { var hour = this.getHours(); return (hour == 0) ? 12 : (hour > 12 ? hour - 12 : hour ) }
31
+ Date.prototype.getAMPM = function() { return (this.getHours() < 12) ? "AM" : "PM"; }
32
+ Date.prototype.stripTime = function() { return new Date(this.getFullYear(), this.getMonth(), this.getDate());};
33
+ Date.prototype.daysDistance = function(compare_date) { return Math.round((compare_date - this) / Date.one_day); };
34
+ Date.prototype.toFormattedString = function(include_time){
35
+ var hour, str;
36
+ str = Date.months[this.getMonth()] + " " + this.getDate() + ", " + this.getFullYear();
37
+
38
+ if (include_time) { hour = this.getHours(); str += " " + this.getAMPMHour() + ":" + this.getPaddedMinutes() + " " + this.getAMPM() }
39
+ return str;
40
+ }
41
+ Date.parseFormattedString = function(string) { return new Date(string);}
42
+ Math.floor_to_interval = function(n, i) { return Math.floor(n/i) * i;}
43
+ window.f_height = function() { return( [window.innerHeight ? window.innerHeight : null, document.documentElement ? document.documentElement.clientHeight : null, document.body ? document.body.clientHeight : null].select(function(x){return x>0}).first()||0); }
44
+ window.f_scrollTop = function() { return ([window.pageYOffset ? window.pageYOffset : null, document.documentElement ? document.documentElement.scrollTop : null, document.body ? document.body.scrollTop : null].select(function(x){return x>0}).first()||0 ); }
45
+
46
+ _translations = {
47
+ "OK": "OK",
48
+ "Now": "Now",
49
+ "Today": "Today",
50
+ "Clear": "Clear"
51
+ }
52
+ SelectBox = Class.create();
53
+ SelectBox.prototype = {
54
+ initialize: function(parent_element, values, html_options, style_options) {
55
+ this.element = $(parent_element).build("select", html_options, style_options);
56
+ this.populate(values);
57
+ },
58
+ populate: function(values) {
59
+ this.element.purgeChildren();
60
+ var that = this; $A(values).each(function(pair) { if (typeof(pair)!="object") {pair = [pair, pair]}; that.element.build("option", { value: pair[1], innerHTML: pair[0]}) });
61
+ },
62
+ setValue: function(value) {
63
+ var e = this.element;
64
+ var matched = false;
65
+ $R(0, e.options.length - 1 ).each(function(i) { if(e.options[i].value==value.toString()) {e.selectedIndex = i; matched = true;}; } );
66
+ return matched;
67
+ },
68
+ getValue: function() { return $F(this.element)}
69
+ }
70
+ CalendarDateSelect = Class.create();
71
+ CalendarDateSelect.prototype = {
72
+ initialize: function(target_element, options) {
73
+ this.target_element = $(target_element); // make sure it's an element, not a string
74
+ if (!this.target_element) { alert("Target element " + target_element + " not found!"); return false;}
75
+ if (this.target_element.tagName != "INPUT") this.target_element = this.target_element.down("INPUT")
76
+
77
+ this.target_element.calendar_date_select = this;
78
+ this.last_click_at = 0;
79
+ // initialize the date control
80
+ this.options = $H({
81
+ embedded: false,
82
+ popup: nil,
83
+ time: false,
84
+ buttons: true,
85
+ clear_button: true,
86
+ year_range: 10,
87
+ close_on_click: nil,
88
+ minute_interval: 5,
89
+ popup_by: this.target_element,
90
+ month_year: "dropdowns",
91
+ onchange: this.target_element.onchange,
92
+ valid_date_check: nil
93
+ }).merge(options || {});
94
+ this.use_time = this.options.get("time");
95
+ this.parseDate();
96
+ this.callback("before_show")
97
+ this.initCalendarDiv();
98
+ if(!this.options.get("embedded")) {
99
+ this.positionCalendarDiv()
100
+ // set the click handler to check if a user has clicked away from the document
101
+ Event.observe(document, "mousedown", this.closeIfClickedOut_handler = this.closeIfClickedOut.bindAsEventListener(this));
102
+ Event.observe(document, "keypress", this.keyPress_handler = this.keyPress.bindAsEventListener(this));
103
+ }
104
+ this.callback("after_show")
105
+ },
106
+ positionCalendarDiv: function() {
107
+ var above = false;
108
+ var c_pos = this.calendar_div.cumulativeOffset(), c_left = c_pos[0], c_top = c_pos[1], c_dim = this.calendar_div.getDimensions(), c_height = c_dim.height, c_width = c_dim.width;
109
+ var w_top = window.f_scrollTop(), w_height = window.f_height();
110
+ var e_dim = $(this.options.get("popup_by")).cumulativeOffset(), e_top = e_dim[1], e_left = e_dim[0], e_height = $(this.options.get("popup_by")).getDimensions().height, e_bottom = e_top + e_height;
111
+
112
+ if ( (( e_bottom + c_height ) > (w_top + w_height)) && ( e_bottom - c_height > w_top )) above = true;
113
+ var left_px = e_left.toString() + "px", top_px = (above ? (e_top - c_height ) : ( e_top + e_height )).toString() + "px";
114
+
115
+ this.calendar_div.style.left = left_px; this.calendar_div.style.top = top_px;
116
+
117
+ this.calendar_div.setStyle({visibility:""});
118
+
119
+ // draw an iframe behind the calendar -- ugly hack to make IE 6 happy
120
+ if(navigator.appName=="Microsoft Internet Explorer") this.iframe = $(document.body).build("iframe", {src: "javascript:false", className: "ie6_blocker"}, { left: left_px, top: top_px, height: c_height.toString()+"px", width: c_width.toString()+"px", border: "0px"})
121
+ },
122
+ initCalendarDiv: function() {
123
+ if (this.options.get("embedded")) {
124
+ var parent = this.target_element.parentNode;
125
+ var style = {}
126
+ } else {
127
+ var parent = document.body
128
+ var style = { position:"absolute", visibility: "hidden", left:0, top:0 }
129
+ }
130
+ this.calendar_div = $(parent).build('div', {className: "calendar_date_select"}, style);
131
+
132
+ var that = this;
133
+ // create the divs
134
+ $w("top header body buttons footer bottom").each(function(name) {
135
+ eval("var " + name + "_div = that." + name + "_div = that.calendar_div.build('div', { className: 'cds_"+name+"' }, { clear: 'left'} ); ");
136
+ });
137
+
138
+ this.initHeaderDiv();
139
+ this.initButtonsDiv();
140
+ this.initCalendarGrid();
141
+ this.updateFooter("&#160;");
142
+
143
+ this.refresh();
144
+ this.setUseTime(this.use_time);
145
+ },
146
+ initHeaderDiv: function() {
147
+ var header_div = this.header_div;
148
+ this.close_button = header_div.build("a", { innerHTML: "x", href:"#", onclick:function () { this.close(); return false; }.bindAsEventListener(this), className: "close" });
149
+ this.next_month_button = header_div.build("a", { innerHTML: "&gt;", href:"#", onclick:function () { this.navMonth(this.date.getMonth() + 1 ); return false; }.bindAsEventListener(this), className: "next" });
150
+ this.prev_month_button = header_div.build("a", { innerHTML: "&lt;", href:"#", onclick:function () { this.navMonth(this.date.getMonth() - 1 ); return false; }.bindAsEventListener(this), className: "prev" });
151
+
152
+ if (this.options.get("month_year")=="dropdowns") {
153
+ this.month_select = new SelectBox(header_div, $R(0,11).map(function(m){return [Date.months[m], m]}), {className: "month", onchange: function () { this.navMonth(this.month_select.getValue()) }.bindAsEventListener(this)});
154
+ this.year_select = new SelectBox(header_div, [], {className: "year", onchange: function () { this.navYear(this.year_select.getValue()) }.bindAsEventListener(this)});
155
+ this.populateYearRange();
156
+ } else {
157
+ this.month_year_label = header_div.build("span")
158
+ }
159
+ },
160
+ initCalendarGrid: function() {
161
+ var body_div = this.body_div;
162
+ this.calendar_day_grid = [];
163
+ var days_table = body_div.build("table", { cellPadding: "0px", cellSpacing: "0px", width: "100%" })
164
+ // make the weekdays!
165
+ var weekdays_row = days_table.build("thead").build("tr");
166
+ Date.weekdays.each( function(weekday) {
167
+ weekdays_row.build("th", {innerHTML: weekday});
168
+ });
169
+
170
+ var days_tbody = days_table.build("tbody")
171
+ // Make the days!
172
+ var row_number = 0, weekday;
173
+ for(var cell_index = 0; cell_index<42; cell_index++)
174
+ {
175
+ weekday = (cell_index+Date.first_day_of_week ) % 7;
176
+ if ( cell_index % 7==0 ) days_row = days_tbody.build("tr", {className: 'row_'+row_number++});
177
+ (this.calendar_day_grid[cell_index] = days_row.build("td", {
178
+ calendar_date_select: this,
179
+ onmouseover: function () { this.calendar_date_select.dayHover(this); },
180
+ onmouseout: function () { this.calendar_date_select.dayHoverOut(this) },
181
+ onclick: function() { this.calendar_date_select.updateSelectedDate(this, true); },
182
+ className: (weekday==0) || (weekday==6) ? " weekend" : "" //clear the class
183
+ },
184
+ { cursor: "pointer" }
185
+ )).build("div");
186
+ this.calendar_day_grid[cell_index];
187
+ }
188
+ },
189
+ initButtonsDiv: function()
190
+ {
191
+ var buttons_div = this.buttons_div;
192
+ if (this.options.get("time"))
193
+ {
194
+ var blank_time = $A(this.options.get("time")=="mixed" ? [[" - ", ""]] : []);
195
+ buttons_div.build("span", {innerHTML:"@", className: "at_sign"});
196
+
197
+ var t = new Date();
198
+ this.hour_select = new SelectBox(buttons_div,
199
+ blank_time.concat($R(0,23).map(function(x) {t.setHours(x); return $A([t.getAMPMHour()+ " " + t.getAMPM(),x])} )),
200
+ {
201
+ calendar_date_select: this,
202
+ onchange: function() { this.calendar_date_select.updateSelectedDate( { hour: this.value });},
203
+ className: "hour"
204
+ }
205
+ );
206
+ buttons_div.build("span", {innerHTML:":", className: "seperator"});
207
+ var that = this;
208
+ this.minute_select = new SelectBox(buttons_div,
209
+ blank_time.concat($R(0,59).select(function(x){return (x % that.options.get('minute_interval')==0)}).map(function(x){ return $A([ Date.padded2(x), x]); } ) ),
210
+ {
211
+ calendar_date_select: this,
212
+ onchange: function() { this.calendar_date_select.updateSelectedDate( {minute: this.value }) },
213
+ className: "minute"
214
+ }
215
+ );
216
+
217
+ } else if (! this.options.get("buttons")) buttons_div.remove();
218
+
219
+ if (this.options.get("buttons")) {
220
+ buttons_div.build("span", {innerHTML: "&#160;"});
221
+ if (this.options.get("time")=="mixed" || !this.options.get("time")) b = buttons_div.build("a", {
222
+ innerHTML: _translations["Today"],
223
+ href: "#",
224
+ onclick: function() {this.today(false); return false;}.bindAsEventListener(this)
225
+ });
226
+
227
+ if (this.options.get("time")=="mixed") buttons_div.build("span", {innerHTML: "&#160;|&#160;", className:"button_seperator"})
228
+
229
+ if (this.options.get("time")) b = buttons_div.build("a", {
230
+ innerHTML: _translations["Now"],
231
+ href: "#",
232
+ onclick: function() {this.today(true); return false}.bindAsEventListener(this)
233
+ });
234
+
235
+ if (!this.options.get("embedded") && !this.closeOnClick())
236
+ {
237
+ buttons_div.build("span", {innerHTML: "&#160;|&#160;", className:"button_seperator"})
238
+ buttons_div.build("a", { innerHTML: _translations["OK"], href: "#", onclick: function() {this.close(); return false;}.bindAsEventListener(this) });
239
+ }
240
+ if (this.options.get('clear_button')) {
241
+ buttons_div.build("span", {innerHTML: "&#160;|&#160;", className:"button_seperator"})
242
+ buttons_div.build("a", { innerHTML: _translations["Clear"], href: "#", onclick: function() {this.clearDate(); if (!this.options.get("embedded")) this.close(); return false;}.bindAsEventListener(this) });
243
+ }
244
+ }
245
+ },
246
+ refresh: function ()
247
+ {
248
+ this.refreshMonthYear();
249
+ this.refreshCalendarGrid();
250
+
251
+ this.setSelectedClass();
252
+ this.updateFooter();
253
+ },
254
+ refreshCalendarGrid: function () {
255
+ this.beginning_date = new Date(this.date).stripTime();
256
+ this.beginning_date.setDate(1);
257
+ this.beginning_date.setHours(12); // Prevent daylight savings time boundaries from showing a duplicate day
258
+ var pre_days = this.beginning_date.getDay() // draw some days before the fact
259
+ if (pre_days < 3) pre_days += 7;
260
+ this.beginning_date.setDate(1 - pre_days + Date.first_day_of_week);
261
+
262
+ var iterator = new Date(this.beginning_date);
263
+
264
+ var today = new Date().stripTime();
265
+ var this_month = this.date.getMonth();
266
+ vdc = this.options.get("valid_date_check");
267
+ for (var cell_index = 0;cell_index<42; cell_index++)
268
+ {
269
+ day = iterator.getDate(); month = iterator.getMonth();
270
+ cell = this.calendar_day_grid[cell_index];
271
+ Element.remove(cell.childNodes[0]); div = cell.build("div", {innerHTML:day});
272
+ if (month!=this_month) div.className = "other";
273
+ cell.day = day; cell.month = month; cell.year = iterator.getFullYear();
274
+ if (vdc) { if (vdc(iterator.stripTime())) cell.removeClassName("disabled"); else cell.addClassName("disabled") };
275
+ iterator.setDate( day + 1);
276
+ }
277
+
278
+ if (this.today_cell) this.today_cell.removeClassName("today");
279
+
280
+ if ( $R( 0, 41 ).include(days_until = this.beginning_date.stripTime().daysDistance(today)) ) {
281
+ this.today_cell = this.calendar_day_grid[days_until];
282
+ this.today_cell.addClassName("today");
283
+ }
284
+ },
285
+ refreshMonthYear: function() {
286
+ var m = this.date.getMonth();
287
+ var y = this.date.getFullYear();
288
+ // set the month
289
+ if (this.options.get("month_year") == "dropdowns")
290
+ {
291
+ this.month_select.setValue(m, false);
292
+
293
+ var e = this.year_select.element;
294
+ if (this.flexibleYearRange() && (!(this.year_select.setValue(y, false)) || e.selectedIndex <= 1 || e.selectedIndex >= e.options.length - 2 )) this.populateYearRange();
295
+
296
+ this.year_select.setValue(y);
297
+
298
+ } else {
299
+ this.month_year_label.update( Date.months[m] + " " + y.toString() );
300
+ }
301
+ },
302
+ populateYearRange: function() {
303
+ this.year_select.populate(this.yearRange().toArray());
304
+ },
305
+ yearRange: function() {
306
+ if (!this.flexibleYearRange())
307
+ return $R(this.options.get("year_range")[0], this.options.get("year_range")[1]);
308
+
309
+ var y = this.date.getFullYear();
310
+ return $R(y - this.options.get("year_range"), y + this.options.get("year_range"));
311
+ },
312
+ flexibleYearRange: function() { return (typeof(this.options.get("year_range")) == "number"); },
313
+ validYear: function(year) { if (this.flexibleYearRange()) { return true;} else { return this.yearRange().include(year);} },
314
+ dayHover: function(element) {
315
+ var hover_date = new Date(this.selected_date);
316
+ hover_date.setYear(element.year); hover_date.setMonth(element.month); hover_date.setDate(element.day);
317
+ this.updateFooter(hover_date.toFormattedString(this.use_time));
318
+ },
319
+ dayHoverOut: function(element) { this.updateFooter(); },
320
+ clearSelectedClass: function() {if (this.selected_cell) this.selected_cell.removeClassName("selected");},
321
+ setSelectedClass: function() {
322
+ if (!this.selection_made) return;
323
+ this.clearSelectedClass()
324
+ if ($R(0,42).include( days_until = this.beginning_date.stripTime().daysDistance(this.selected_date.stripTime()) )) {
325
+ this.selected_cell = this.calendar_day_grid[days_until];
326
+ this.selected_cell.addClassName("selected");
327
+ }
328
+ },
329
+ reparse: function() { this.parseDate(); this.refresh(); },
330
+ dateString: function() {
331
+ return (this.selection_made) ? this.selected_date.toFormattedString(this.use_time) : "&#160;";
332
+ },
333
+ parseDate: function()
334
+ {
335
+ var value = $F(this.target_element).strip()
336
+ var default_time = this.options.get("default_time");
337
+ this.selection_made = (value != "" || default_time);
338
+ this.date = value=="" ? NaN : Date.parseFormattedString(this.options.get("date") || value);
339
+ if (isNaN(this.date) && !default_time)
340
+ this.date = new Date();
341
+ else if (isNaN(this.date) && default_time)
342
+ this.date = (Object.prototype.toString.apply(default_time) === '[object Function]') ? default_time() : default_time;
343
+
344
+ if (!this.validYear(this.date.getFullYear())) this.date.setYear( (this.date.getFullYear() < this.yearRange().start) ? this.yearRange().start : this.yearRange().end);
345
+ this.selected_date = new Date(this.date);
346
+ this.use_time = /[0-9]:[0-9]{2}/.exec(value) ? true : false;
347
+ this.date.setDate(1);
348
+ },
349
+ updateFooter:function(text) { if (!text) text = this.dateString(); this.footer_div.purgeChildren(); this.footer_div.build("span", {innerHTML: text }); },
350
+ clearDate:function() {
351
+ if ((this.target_element.disabled || this.target_element.readOnly) && this.options.get("popup") != "force") return false;
352
+ var last_value = this.target_element.value;
353
+ this.target_element.value = "";
354
+ this.clearSelectedClass();
355
+ this.updateFooter('&#160;');
356
+ if (last_value!=this.target_element.value) this.callback("onchange");
357
+ },
358
+ updateSelectedDate:function(partsOrElement, via_click) {
359
+ var parts = $H(partsOrElement);
360
+ if ((this.target_element.disabled || this.target_element.readOnly) && this.options.get("popup") != "force") return false;
361
+ if (parts.get("day")) {
362
+ var t_selected_date = this.selected_date, vdc = this.options.get("valid_date_check");
363
+ t_selected_date.setYear(parts.get("year"));
364
+ t_selected_date.setMonth(parts.get("month"));
365
+ t_selected_date.setDate(parts.get("day"));
366
+
367
+ if (vdc && ! vdc(t_selected_date.stripTime())) { return false; }
368
+ this.selected_date = t_selected_date;
369
+ this.selection_made = true;
370
+ }
371
+
372
+ if (!isNaN(parts.get("hour"))) this.selected_date.setHours(parts.get("hour"));
373
+ if (!isNaN(parts.get("minute"))) this.selected_date.setMinutes( Math.floor_to_interval(parts.get("minute"), this.options.get("minute_interval")) );
374
+ if (parts.get("hour") === "" || parts.get("minute") === "")
375
+ this.setUseTime(false);
376
+ else if (!isNaN(parts.get("hour")) || !isNaN(parts.get("minute")))
377
+ this.setUseTime(true);
378
+
379
+ this.updateFooter();
380
+ this.setSelectedClass();
381
+
382
+ if (this.selection_made) this.updateValue();
383
+ if (this.closeOnClick()) { this.close(); }
384
+ if (via_click && !this.options.get("embedded")) {
385
+ if ((new Date() - this.last_click_at) < 333) this.close();
386
+ this.last_click_at = new Date();
387
+ }
388
+ },
389
+ closeOnClick: function() {
390
+ if (this.options.get("embedded")) return false;
391
+ if (this.options.get("close_on_click")===nil )
392
+ return (this.options.get("time")) ? false : true
393
+ else
394
+ return (this.options.get("close_on_click"))
395
+ },
396
+ navMonth: function(month) { (target_date = new Date(this.date)).setMonth(month); return (this.navTo(target_date)); },
397
+ navYear: function(year) { (target_date = new Date(this.date)).setYear(year); return (this.navTo(target_date)); },
398
+ navTo: function(date) {
399
+ if (!this.validYear(date.getFullYear())) return false;
400
+ this.date = date;
401
+ this.date.setDate(1);
402
+ this.refresh();
403
+ this.callback("after_navigate", this.date);
404
+ return true;
405
+ },
406
+ setUseTime: function(turn_on) {
407
+ this.use_time = this.options.get("time") && (this.options.get("time")=="mixed" ? turn_on : true) // force use_time to true if time==true && time!="mixed"
408
+ if (this.use_time && this.selected_date) { // only set hour/minute if a date is already selected
409
+ var minute = Math.floor_to_interval(this.selected_date.getMinutes(), this.options.get("minute_interval"));
410
+ var hour = this.selected_date.getHours();
411
+
412
+ this.hour_select.setValue(hour);
413
+ this.minute_select.setValue(minute)
414
+ } else if (this.options.get("time")=="mixed") {
415
+ this.hour_select.setValue(""); this.minute_select.setValue("");
416
+ }
417
+ },
418
+ updateValue: function() {
419
+ var last_value = this.target_element.value;
420
+ this.target_element.value = this.dateString();
421
+ if (last_value!=this.target_element.value) this.callback("onchange");
422
+ },
423
+ today: function(now) {
424
+ var d = new Date(); this.date = new Date();
425
+ var o = $H({ day: d.getDate(), month: d.getMonth(), year: d.getFullYear(), hour: d.getHours(), minute: d.getMinutes()});
426
+ if ( ! now ) o = o.merge({hour: "", minute:""});
427
+ this.updateSelectedDate(o, true);
428
+ this.refresh();
429
+ },
430
+ close: function() {
431
+ if (this.closed) return false;
432
+ this.callback("before_close");
433
+ this.target_element.calendar_date_select = nil;
434
+ Event.stopObserving(document, "mousedown", this.closeIfClickedOut_handler);
435
+ Event.stopObserving(document, "keypress", this.keyPress_handler);
436
+ this.calendar_div.remove(); this.closed = true;
437
+ if (this.iframe) this.iframe.remove();
438
+ if (this.target_element.type != "hidden" && ! this.target_element.disabled) this.target_element.focus();
439
+ this.callback("after_close");
440
+ },
441
+ closeIfClickedOut: function(e) {
442
+ if (! $(Event.element(e)).descendantOf(this.calendar_div) ) this.close();
443
+ },
444
+ keyPress: function(e) {
445
+ if (e.keyCode==Event.KEY_ESC) this.close();
446
+ },
447
+ callback: function(name, param) { if (this.options.get(name)) { this.options.get(name).bind(this.target_element)(param); } }
448
+ }