Advertisement
Guest User

Untitled

a guest
Jan 17th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.97 KB | None | 0 0
  1. class DateTimePicker < React::Component::Base
  2. param :record # typically an active_record model, but can be any object really
  3. param :field # attr (read and write) of the object to be updated.
  4.  
  5. after_mount do
  6. initialize_timepicker
  7. initialize_datepicker
  8. end
  9.  
  10. def initialize_timepicker
  11. Element["##{input_id}_time"].timepicker({ timeFormat: 'H:i' }.to_n)
  12. Element["##{input_id}_time"].on(:change) do |e|
  13. timepicker_val = e.target.value
  14. time = Time.parse("2000-01-01 #{timepicker_val}").strftime('%H:%M:%S')
  15. update_time(time)
  16. end
  17. end
  18.  
  19. def initialize_datepicker
  20. Element["##{input_id}_date"].datepicker({
  21. dateFormat: 'mm-dd-yy',
  22. onSelect: ->(date, _i) { update_date(date) }
  23. }.to_n)
  24. end
  25.  
  26. def input_id
  27. "#{params.record.class.name.downcase}_#{params.field}"
  28. end
  29.  
  30. def input_name
  31. "#{params.record.class.name.downcase}[#{params.field}]"
  32. end
  33.  
  34. def current_datetime_or_now
  35. return Time.now if params.record.send(params.field).nil?
  36. Time.parse(params.record.send(params.field))
  37. end
  38.  
  39. def date_value
  40. return '' if params.record.send(params.field).nil?
  41. Time.parse(params.record.send(params.field)).strftime('%m-%d-%Y')
  42. end
  43.  
  44. def time_value
  45. return '' if params.record.send(params.field).nil?
  46. Time.parse(params.record.send(params.field)).strftime('%H:%M')
  47. end
  48.  
  49. def update_date(date)
  50. previous_date = current_datetime_or_now
  51. mon, d, y = date.split('-')
  52. h, m, s = previous_date.strftime('%H:%M:%S').split(':')
  53. params.record.send("#{params.field}=", Time.new(y, mon, d, h, m, s))
  54. return unless Element["##{input_id}_time"].value.blank?
  55. Element["##{input_id}_time"].value = params.record.send(params.field).strftime('%H:%M')
  56. end
  57.  
  58. def update_time(time)
  59. previous_time = current_datetime_or_now
  60. h, m, s = time.split(':')
  61. y, mon, d = previous_time.strftime('%Y-%m-%d').split('-')
  62. params.record.send("#{params.field}=", Time.new(y, mon, d, h, m, s))
  63. return unless Element["##{input_id}_date"].value.blank?
  64. Element["##{input_id}_date"].value = params.record.send(params.field).strftime('%d-%m-%Y')
  65. end
  66.  
  67. def date_override
  68. return unless Element["##{input_id}_date"].value.blank?
  69. params.record.send("#{params.field}=", nil)
  70. Element["##{input_id}_time"].value = nil
  71. end
  72.  
  73. render(:div, class: 'row') do
  74. INPUT(id: input_id, name: input_name, type: :hidden, value: params.record.send(params.field))
  75. DIV(class: 'col-sm-6') do
  76. INPUT(id: "#{input_id}_date", name: "#{input_name}[date]", class: 'form-control datepicker',
  77. defaultValue: date_value, placeholder: 'Date')
  78. .on(:blur) { date_override }
  79. end
  80. DIV(class: 'col-sm-6') do
  81. INPUT(id: "#{input_id}_time", name: "#{input_name}[time]", class: 'form-control timepicker',
  82. defaultValue: time_value, placeholder: 'Time')
  83. end
  84. end
  85. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement