harlan-calendar_date_select 1.13 → 1.13.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,231 @@
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
+ # === :on_modal
21
+ #
22
+ # If the calendar is being placed in a widget which is already a modal itself,
23
+ # it needs to be aware that it needs to appear on top of that modal. Not
24
+ # compatible with :embedded => true.
25
+ #
26
+ # <%= calendar_date_select_tag "name", "2007-01-01", :on_modal => true %>
27
+ #
28
+ # === :hidden
29
+ #
30
+ # 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.
31
+ #
32
+ # <span id='cds_value' />
33
+ # <%= calendar_date_select_tag "hidden_date_selector", "", :hidden => "true", :onchange => "$('cds_value').update($F(this));" %>
34
+ #
35
+ # === :image
36
+ #
37
+ # Specify an alternative icon to use for the date picker.
38
+ #
39
+ # To use /images/groovy.png:
40
+ #
41
+ # <%= calendar_date_select_tag "altered_image", "", :image => "groovy.png" %>
42
+ #
43
+ # === :minute_interval
44
+ #
45
+ # Specifies the minute interval used in the hour/minute selector. Default is 5.
46
+ #
47
+ # <%= calendar_date_select_tag "month_year_selector_label", "", :minute_interval => 15 %>
48
+ #
49
+ # === :month_year
50
+ #
51
+ # Customize the month and year selectors at the top of the control.
52
+ #
53
+ # Valid values:
54
+ # * "dropdowns" (default) - Use a separate dropdown control for both the month and year
55
+ # * "label" - Use static text to show the month and the year.
56
+ #
57
+ # <%= calendar_date_select_tag "month_year_selector_label", "", :month_year => "label" %>
58
+ #
59
+ # === :popup => 'force'
60
+ #
61
+ # 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.
62
+ #
63
+ # <%= calendar_date_select_tag "name", "2007-01-01", :popup => "force" %>
64
+ #
65
+ # === :time
66
+ #
67
+ # Show time in the controls. There's three options:
68
+ #
69
+ # * +true+ - show an hour/minute selector.
70
+ # * +false+ - don't show an hour/minute selector.
71
+ # * +"mixed"+ - Show an hour/minute selector, but include a "all day" option - allowing them to choose whether or not to specify a time.
72
+ #
73
+ # === :year_range
74
+ #
75
+ # Limit the year range. You can pass in an array or range of ruby Date/Time objects or FixNum's.
76
+ #
77
+ # <%= calendar_date_select_tag "e_date", nil, :year_range => 10.years.ago..0.years.from_now %>
78
+ # <%= calendar_date_select_tag "e_date", nil, :year_range => [0.years.ago, 10.years.from_now] %>
79
+ # <%= calendar_date_select_tag "e_date", nil, :year_range => 2000..2007 %>
80
+ # <%= calendar_date_select_tag "e_date", nil, :year_range => [2000, 2007] %>
81
+ #
82
+ # == CALLBACKS
83
+ #
84
+ # The following callbacks are available:
85
+ #
86
+ # * before_show / after_show
87
+ # * before_close / after_close
88
+ # * after_navigate - Called when navigating to a different month. Passes first parameter as a date object refering to the current month viewed
89
+ # * onchange - Called when the form input value changes
90
+ #
91
+ # <%= calendar_date_select_tag "event_demo", "",
92
+ # :before_show => "log('Calendar Showing');" ,
93
+ # :after_show => "log('Calendar Shown');" ,
94
+ # :before_close => "log('Calendar closing');" ,
95
+ # :after_close => "log('Calendar closed');",
96
+ # :after_navigate => "log('Current month is ' + (param.getMonth()+1) + '/' + (param.getFullYear()));",
97
+ # :onchange => "log('value changed to - ' + $F(this));"
98
+ #
99
+ # }}}
100
+ #
101
+ # 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".
102
+ #
103
+ # For example:
104
+ #
105
+ # <%= calendar_date_select_tag "event_demo", "", :after_navigate => "alert('The current selected month is ' + this.calendar_date_select.selected_date.getMonth());" ,
106
+ def calendar_date_select_tag( name, value = nil, options = {})
107
+ options, javascript_options = calendar_date_select_process_options(options)
108
+ value = CalendarDateSelect.format_time(value, javascript_options)
109
+
110
+ javascript_options.delete(:format)
111
+
112
+ options[:id] ||= name
113
+ tag = javascript_options[:hidden] || javascript_options[:embedded] ?
114
+ hidden_field_tag(name, value, options) :
115
+ text_field_tag(name, value, options)
116
+
117
+ calendar_date_select_output(tag, options, javascript_options)
118
+ end
119
+
120
+ # Similar to the difference between +text_field_tag+ and +text_field+, this method behaves like +text_field+
121
+ #
122
+ # 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
123
+ def calendar_date_select(object, method, options={})
124
+ obj = options[:object] || instance_variable_get("@#{object}")
125
+
126
+ if !options.include?(:time) && obj.class.respond_to?("columns_hash")
127
+ column_type = (obj.class.columns_hash[method.to_s].type rescue nil)
128
+ options[:time] = true if column_type == :datetime
129
+ end
130
+
131
+ use_time = options[:time]
132
+
133
+ if options[:time].to_s=="mixed"
134
+ use_time = false if Date===(obj.respond_to?(method) && obj.send(method))
135
+ end
136
+
137
+ options, javascript_options = calendar_date_select_process_options(options)
138
+
139
+ options[:value] ||=
140
+ if(obj.respond_to?(method) && obj.send(method).respond_to?(:strftime))
141
+ obj.send(method).strftime(CalendarDateSelect.date_format_string(use_time))
142
+ elsif obj.respond_to?("#{method}_before_type_cast")
143
+ obj.send("#{method}_before_type_cast")
144
+ elsif obj.respond_to?(method)
145
+ obj.send(method).to_s
146
+ else
147
+ nil
148
+ end
149
+
150
+ tag = ActionView::Helpers::InstanceTag.new_with_backwards_compatibility(object, method, self, options.delete(:object))
151
+ calendar_date_select_output(
152
+ tag.to_input_field_tag( (javascript_options[:hidden] || javascript_options[:embedded]) ? "hidden" : "text", options),
153
+ options,
154
+ javascript_options
155
+ )
156
+ end
157
+
158
+ private
159
+ # extracts any options passed into calendar date select, appropriating them to either the Javascript call or the html tag.
160
+ def calendar_date_select_process_options(options)
161
+ options, javascript_options = CalendarDateSelect.default_options.merge(options), {}
162
+ callbacks = [:before_show, :before_close, :after_show, :after_close, :after_navigate]
163
+ for key in [:time, :valid_date_check, :embedded, :buttons, :clear_button, :format, :year_range, :month_year, :popup, :hidden, :minute_interval, :on_modal] + callbacks
164
+ javascript_options[key] = options.delete(key) if options.has_key?(key)
165
+ end
166
+
167
+ # if passing in mixed, pad it with single quotes
168
+ javascript_options[:time] = "'mixed'" if javascript_options[:time].to_s=="mixed"
169
+ javascript_options[:month_year] = "'#{javascript_options[:month_year]}'" if javascript_options[:month_year]
170
+
171
+ # if we are forcing the popup, automatically set the readonly property on the input control.
172
+ if javascript_options[:popup].to_s == "force"
173
+ javascript_options[:popup] = "'force'"
174
+ options[:readonly] = true
175
+ end
176
+
177
+ if (vdc=javascript_options.delete(:valid_date_check))
178
+ if vdc.include?(";") || vdc.include?("function")
179
+ 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");
180
+ end
181
+
182
+ vdc = "return(#{vdc})" unless vdc.include?("return")
183
+ vdc = "function(date) { #{vdc} }" unless vdc.include?("function")
184
+ javascript_options[:valid_date_check] = vdc
185
+ end
186
+
187
+ javascript_options[:popup_by] ||= "this" if javascript_options[:hidden]
188
+
189
+ # surround any callbacks with a function, if not already done so
190
+ for key in callbacks
191
+ javascript_options[key] = "function(param) { #{javascript_options[key]} }" unless javascript_options[key].include?("function") if javascript_options[key]
192
+ end
193
+
194
+ javascript_options[:year_range] = format_year_range(javascript_options[:year_range] || 10)
195
+ [options, javascript_options]
196
+ end
197
+
198
+ def calendar_date_select_output(input, options = {}, javascript_options = {})
199
+ out = input
200
+ if javascript_options[:embedded]
201
+ uniq_id = "cds_placeholder_#{(rand*100000).to_i}"
202
+ # we need to be able to locate the target input element, so lets stick an invisible span tag here we can easily locate
203
+ out << content_tag(:span, nil, :style => "display: none; position: absolute;", :id => uniq_id)
204
+ out << javascript_tag("new CalendarDateSelect( $('#{uniq_id}').previous(), #{options_for_javascript(javascript_options)} ); ")
205
+ else
206
+ out << " "
207
+ out << image_tag(options[:image],
208
+ :onclick => "new CalendarDateSelect( $(this).previous(), #{options_for_javascript(javascript_options)} );",
209
+ :style => 'border:0px; cursor:pointer;',
210
+ :class=>'calendar_date_select_popup_icon')
211
+ end
212
+ out
213
+ end
214
+
215
+ def format_year_range(year) # nodoc
216
+ return year unless year.respond_to?(:first)
217
+ return "[#{year.first}, #{year.last}]" unless year.first.respond_to?(:strftime)
218
+ return "[#{year.first.year}, #{year.last.year}]"
219
+ end
220
+ end
221
+
222
+ # Helper method for form builders
223
+ module ActionView
224
+ module Helpers
225
+ class FormBuilder
226
+ def calendar_date_select(method, options = {})
227
+ @template.calendar_date_select(@object_name, method, options.merge(:object => @object))
228
+ end
229
+ end
230
+ end
231
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: harlan-calendar_date_select
3
3
  version: !ruby/object:Gem::Version
4
- version: "1.13"
4
+ version: 1.13.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tim Harper
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-11-23 00:00:00 -08:00
12
+ date: 2009-02-18 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -42,6 +42,7 @@ files:
42
42
  - js_test/unit/cds_helper_methods.html
43
43
  - js_test/unittest.js
44
44
  - lib/calendar_date_select/calendar_date_select.rb
45
+ - lib/calendar_date_select/form_helpers.rb
45
46
  - lib/calendar_date_select/includes_helper.rb
46
47
  - lib/calendar_date_select.rb
47
48
  - Manifest.txt
@@ -101,5 +102,5 @@ rubygems_version: 1.2.0
101
102
  signing_key:
102
103
  specification_version: 2
103
104
  summary: A prototype-based date-select widget
104
- test_files: []
105
-
105
+ test_files:
106
+ - spec/spec_helper.rb