class Capybara::BootstrapDatepicker::Picker

The Picker class interacts with the datepicker

Public Class Methods

new() click to toggle source

Initializes the picker

# File lib/capybara-bootstrap-datepicker.rb, line 61
def initialize
  @element = find_picker
end

Public Instance Methods

find_day(value) click to toggle source

Get the day we want to click on @param value [Fixnum] the day of the desired date @return the DOM element to click on

# File lib/capybara-bootstrap-datepicker.rb, line 96
      def find_day(value)
        day_xpath = <<-eos
            .//*[contains(concat(' ', @class, ' '), ' day ')
            and not(contains(concat(' ', @class, ' '), ' old '))
            and not(contains(concat(' ', @class, ' '), ' new '))
            and normalize-space(text())='#{value}']
        eos
        days.find :xpath, day_xpath
      end
find_month(value) click to toggle source

Get the month we want to click on @param value [Fixnum] the month of the desired date @return the DOM element to click on

# File lib/capybara-bootstrap-datepicker.rb, line 89
def find_month(value)
  months.find ".month:nth-child(#{value})"
end
find_year(value) click to toggle source

Get the year we want to click on @param value [Fixnum] the year of the desired date @return the DOM element to click on

# File lib/capybara-bootstrap-datepicker.rb, line 82
def find_year(value)
  years.find '.year', text: value
end
goto_decade_panel() click to toggle source

Reveals the decade panel

# File lib/capybara-bootstrap-datepicker.rb, line 66
def goto_decade_panel
  current_month.click if days.visible?
  current_year.click if months.visible?
end
navigate_through_decades(value) click to toggle source

Navigates through the decade panels until the correct one @param value [Fixnum] the year of the desired date

Private Instance Methods

click_next_decade() click to toggle source

Click and display next decade

# File lib/capybara-bootstrap-datepicker.rb, line 182
def click_next_decade
  years.find('th.next').click
end
click_prev_decade() click to toggle source

Click and display previous decade

# File lib/capybara-bootstrap-datepicker.rb, line 177
def click_prev_decade
  years.find('th.prev').click
end
current_decade() click to toggle source

Get the current decade panel @param (see find_switch) @return (see find_switch)

# File lib/capybara-bootstrap-datepicker.rb, line 152
def current_decade
  find_switch :years
end
current_decade_minmax() click to toggle source

Get the current decade period @return [Array<Fixnum, Fixnum>] the min and max years of the decade

# File lib/capybara-bootstrap-datepicker.rb, line 172
def current_decade_minmax
  current_decade.text.split('-').map(&:to_i)
end
current_month() click to toggle source

Get the current month panel @param (see find_switch) @return (see find_switch)

# File lib/capybara-bootstrap-datepicker.rb, line 166
def current_month
  find_switch :days
end
current_year() click to toggle source

Get the current year panel @param (see find_switch) @return (see find_switch)

# File lib/capybara-bootstrap-datepicker.rb, line 159
def current_year
  find_switch :months
end
days() click to toggle source

Get the days panel @param (see find_period) @return (see find_period)

# File lib/capybara-bootstrap-datepicker.rb, line 145
def days
  find_period :days
end
find_period(period) click to toggle source

Get the datepicker panel @param period [:years, :months, :days] the panel’s period @return the DOM element of the panel

# File lib/capybara-bootstrap-datepicker.rb, line 117
def find_period(period)
  @element.find(".datepicker-#{period}", visible: false)
end
find_picker() click to toggle source

Get the datepicker @return the DOM element of the datepicker

# File lib/capybara-bootstrap-datepicker.rb, line 110
def find_picker
  Capybara.find(:xpath, '//body').find('.datepicker')
end
find_switch(period) click to toggle source

Get the up level panel @param (see find_period) @return the DOM element of the switch panel button

# File lib/capybara-bootstrap-datepicker.rb, line 124
def find_switch(period)
  send(period).find('th.datepicker-switch', visible: false)
end
gap(min, max) click to toggle source

Calculates the distance in decades between min and max @return [Fixnum] the distance in decades between min and max

# File lib/capybara-bootstrap-datepicker.rb, line 188
def gap(min, max)
  return 0 if min >= max
  (max - min) / 10
end
goto_next_decade(decade_end, value) click to toggle source

Go forward to the wanted decade @param decade_end [Fixnum] the last year of a decade @param value [Fixnum] the year of the desired date

# File lib/capybara-bootstrap-datepicker.rb, line 203
def goto_next_decade(decade_end, value)
  gap(decade_end, value).times { click_next_decade }
end
goto_prev_decade(value, decade_start) click to toggle source

Go backward to the wanted decade @param value [Fixnum] the year of the desired date @param decade_start [Fixnum] the first year of a decade

# File lib/capybara-bootstrap-datepicker.rb, line 196
def goto_prev_decade(value, decade_start)
  gap(value, decade_start).times { click_prev_decade }
end
months() click to toggle source

Get the months panel @param (see find_period) @return (see find_period)

# File lib/capybara-bootstrap-datepicker.rb, line 138
def months
  find_period :months
end
years() click to toggle source

Get the years panel @param (see find_period) @return (see find_period)

# File lib/capybara-bootstrap-datepicker.rb, line 131
def years
  find_period :years
end