Guest User

Untitled

a guest
May 26th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.02 KB | None | 0 0
  1. # Calendar Date Select by Tim Harper
  2.  
  3. class CalendarDateSelect
  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. :finnish => {
  15. :date => "%d.%m.%Y",
  16. :time => " %H:%M",
  17. :javascript_include => "format_finnish"
  18. },
  19. :american => {
  20. :date => "%m/%d/%Y",
  21. :time => " %I:%M %p",
  22. :javascript_include => "format_american"
  23. },
  24. :euro_24hr => {
  25. :date => "%d %B %Y",
  26. :time => " %H:%M",
  27. :javascript_include => "format_euro_24hr"
  28. },
  29. :italian => {
  30. :date => "%d/%m/%Y",
  31. :time => " %H:%M",
  32. :javascript_include => "format_italian"
  33. }
  34. }
  35.  
  36. cattr_accessor :image
  37. @@image = "calendar_date_select/calendar.gif"
  38.  
  39. cattr_reader :format
  40. @@format = FORMATS[:natural]
  41.  
  42. class << self
  43. def format=(format)
  44. raise "CalendarDateSelect: Unrecognized format specification: #{format}" unless FORMATS.has_key?(format)
  45. @@format = FORMATS[format]
  46. end
  47.  
  48. def javascript_format_include
  49. @@format[:javascript_include] && "calendar_date_select/#{@@format[:javascript_include]}"
  50. end
  51.  
  52. def date_format_string(time=false)
  53. @@format[:date] + ( time ? @@format[:time] : "" )
  54. end
  55.  
  56. def format_date(date)
  57. if Date===date
  58. date.strftime(date_format_string(false))
  59. else
  60. date.strftime(date_format_string(true))
  61. end
  62. end
  63.  
  64. def has_time?(value)
  65. /[0-9]:[0-9]{2}/.match(value.to_s)
  66. end
  67. end
  68.  
  69. module FormHelper
  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
  83.  
  84. # extracts any options passed into calendar date select, appropriating them to either the Javascript call or the html tag.
  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
  122.  
  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
  156.  
  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
  176.  
  177. private
  178. def format_year_range(year) # nodoc
  179. return year unless year.respond_to?(:first)
  180. return "[#{year.first}, #{year.last}]" unless year.first.respond_to?(:strftime)
  181. return "[#{year.first.year}, #{year.last.year}]"
  182. end
  183. end
  184. end
  185.  
  186.  
  187. module ActionView
  188. module Helpers
  189. class FormBuilder
  190. def calendar_date_select(method, options = {})
  191. @template.calendar_date_select(@object_name, method, options.merge(:object => @object))
  192. end
  193. end
  194. end
  195. end
Add Comment
Please, Sign In to add comment