| Module | CalendarDateSelect::FormHelper |
| In: |
vendor/plugins/super_inplace_controls/lib/calendar_date_select.rb
|
# File vendor/plugins/super_inplace_controls/lib/calendar_date_select.rb, line 123
123: def calendar_date_select(object, method, options={})
124: obj = options.include?(:object) ? options[:object] : instance_eval("@#{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.send(method)
135: end
136:
137: calendar_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(object, method, self, nil, options.delete(:object))
151: calendar_date_select_output(
152: tag.to_input_field_tag( (calendar_options[:hidden] || calendar_options[:embedded]) ? "hidden" : "text", options),
153: calendar_options
154: )
155: end
# File vendor/plugins/super_inplace_controls/lib/calendar_date_select.rb, line 157
157: def calendar_date_select_output(input, calendar_options = {})
158: out = input
159: if calendar_options[:embedded]
160: uniq_id = "cds_placeholder_#{(rand*100000).to_i}"
161: # we need to be able to locate the target input element, so lets stick an invisible span tag here we can easily locate
162: out << content_tag(:span, nil, :style => "display: none; position: absolute;", :id => uniq_id)
163:
164: out << javascript_tag("new CalendarDateSelect( $('#{uniq_id}').previous(), #{options_for_javascript(calendar_options)} ); ")
165: else
166: out << " "
167:
168: out << image_tag(CalendarDateSelect.image,
169: :onclick => "new CalendarDateSelect( $(this).previous(), #{options_for_javascript(calendar_options)} );",
170: :style => 'border:0px; cursor:pointer;',
171: :class => "calendar_date_select_image" )
172: end
173:
174: out
175: end
extracts any options passed into calendar date select, appropriating them to either the Javascript call or the html tag.
# File vendor/plugins/super_inplace_controls/lib/calendar_date_select.rb, line 85
85: def calendar_date_select_process_options(options)
86: calendar_options = {}
87: callbacks = [:before_show, :before_close, :after_show, :after_close, :after_navigate]
88: for key in [:time, :valid_date_check, :embedded, :buttons, :format, :year_range, :month_year, :popup, :hidden] + callbacks
89: calendar_options[key] = options.delete(key) if options.has_key?(key)
90: end
91:
92: # if passing in mixed, pad it with single quotes
93: calendar_options[:time] = "'mixed'" if calendar_options[:time].to_s=="mixed"
94: calendar_options[:month_year] = "'#{calendar_options[:month_year]}'" if calendar_options[:month_year]
95:
96: # if we are forcing the popup, automatically set the readonly property on the input control.
97: if calendar_options[:popup].to_s == "force"
98: calendar_options[:popup] = "'force'"
99: options[:readonly] = true
100: end
101:
102: if (vdc = calendar_options.delete(:valid_date_check))
103: if vdc.include?(";") || vdc.include?("function")
104: throw ":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");
105: end
106:
107: vdc = "return(#{vdc})" unless vdc.include?("return")
108: vdc = "function(date) { #{vdc} }" unless vdc.include?("function")
109: calendar_options[:valid_date_check] = vdc
110: end
111:
112: calendar_options[:popup_by] ||= "this" if calendar_options[:hidden]
113:
114: # surround any callbacks with a function, if not already done so
115: for key in callbacks
116: calendar_options[key] = "function(param) { #{calendar_options[key]} }" unless calendar_options[key].include?("function") if calendar_options[key]
117: end
118:
119: calendar_options[:year_range] = format_year_range(calendar_options[:year_range] || 10)
120: calendar_options
121: end
# File vendor/plugins/super_inplace_controls/lib/calendar_date_select.rb, line 70
70: def calendar_date_select_tag( name, value = nil, options = {})
71: calendar_options = calendar_date_select_process_options(options)
72: value = (value.strftime(calendar_options[:format]) rescue value) if (value.respond_to?("strftime"))
73:
74: calendar_options.delete(:format)
75:
76: options[:id] ||= name
77: tag = calendar_options[:hidden] || calendar_options[:embedded] ?
78: hidden_field_tag(name, value, options) :
79: text_field_tag(name, value, options)
80:
81: calendar_date_select_output(tag, calendar_options)
82: end