Class: Nemo::Components::MiniCalendar

Inherits:
Wee::Component
  • Object
show all
Defined in:
lib/nemo/components/calendar.rb

Overview

Browsable Calendar component

Use as a date picker

By default, clicking a day will answer with an associated Date object.

Use as a calendar viewer

No answer is given when in browse mode:

call( Nemo::Components::MiniCalendar.new(date).browse )

CSS Styles

Overload render_styles to add a custom <link> or <style> tag.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(date = Date.today) ⇒ MiniCalendar

Initialize the MiniCalendar



24
25
26
27
28
29
# File 'lib/nemo/components/calendar.rb', line 24

def initialize(date=Date.today)
  super()
  @month = Date.new(date.year, date.month, 1)
  @day = date
  @browse = false
end

Instance Attribute Details

#browse(value = true) ⇒ Object

Set to browse-only (no answer will be given)



17
18
19
# File 'lib/nemo/components/calendar.rb', line 17

def browse
  @browse
end

#dateObject

Holds the current chosen date



20
21
22
# File 'lib/nemo/components/calendar.rb', line 20

def date
  @date
end

Instance Method Details

#backObject

Return without changes



167
168
169
# File 'lib/nemo/components/calendar.rb', line 167

def back
  answer nil unless browse?
end

#backtrack_state(snapshot) ⇒ Object

Backtrack state



33
34
35
36
# File 'lib/nemo/components/calendar.rb', line 33

def backtrack_state(snapshot)
  super
  snapshot.add(self)
end

#browse?Boolean

True if in browser-only mode

Returns:

  • (Boolean)


47
48
49
# File 'lib/nemo/components/calendar.rb', line 47

def browse?
  @browse
end

#current_month?(date) ⇒ Boolean

True if the given date is the currently selected month

Returns:

  • (Boolean)


53
54
55
# File 'lib/nemo/components/calendar.rb', line 53

def current_month?(date)
  Date.new(date.year, date.month, 1) == @month
end

#go_nextObject

Select the next month



179
180
181
# File 'lib/nemo/components/calendar.rb', line 179

def go_next
  @month = next_month
end

#go_prevObject

Select the previous month



173
174
175
# File 'lib/nemo/components/calendar.rb', line 173

def go_prev
  @month = prev_month
end

#month_headingObject

String to be displayed as the month heading



77
78
79
# File 'lib/nemo/components/calendar.rb', line 77

def month_heading
  @month.strftime('%B %Y')
end

#next_monthObject

Date object representing the next month



71
72
73
# File 'lib/nemo/components/calendar.rb', line 71

def next_month
  @month >> 1
end

#prev_monthObject

Date object representing the previous month



65
66
67
# File 'lib/nemo/components/calendar.rb', line 65

def prev_month
  @month << 1
end

#renderObject

Render Calendar



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/nemo/components/calendar.rb', line 144

def render
  r.html do
    r.head { r.title('Calendar'); render_styles }
    r.body do
      r.text(sprintf('<!--Month: %s, Day: %s-->', @month, @day))
      r.table { r.table_row { r.table_header {
        r.table do
          render_header
          r.table_row { Date::ABBR_DAYNAMES.each { |day| r.table_header(day) } }
          @month.calendar.each do |week|
            r.table_row do
              week.each { |day| render_day(day) }
            end
          end
          render_footer
        end
      }}}
    end
  end
end

#render_day(date) ⇒ Object

Render a given day



89
90
91
92
93
94
95
# File 'lib/nemo/components/calendar.rb', line 89

def render_day(date)
  if current_month?(date)
    selected_day?(date) ? render_selected_day(date) : render_month_day(date)
  else
    render_other_day(date)
  end
end

Render Calendar footer



138
139
140
# File 'lib/nemo/components/calendar.rb', line 138

def render_footer
  r.table_row { r.table_header.colspan(7).with { r.encode_text(today_string) } }
end

#render_headerObject

Render Calender header



127
128
129
130
131
132
133
134
# File 'lib/nemo/components/calendar.rb', line 127

def render_header
  r.table_row do
    r.table_header.colspan(4).with { r.encode_text(month_heading) }
    r.table_header { r.anchor.callback { go_prev }.with(prev_month.strftime('%b')) }
    r.table_header { r.anchor.callback { go_next }.with(next_month.strftime('%b')) }
    r.table_header { browse? ? r.space : r.anchor.callback { back }.style('color: black').with('X') }
  end
end

#render_month_day(date) ⇒ Object

Render a day of the currently selected month



99
100
101
# File 'lib/nemo/components/calendar.rb', line 99

def render_month_day(date)
  r.table_data { r.anchor.callback { save(date) }.with(date.day) }
end

#render_other_day(date) ⇒ Object

Render days of the previous or next month



113
114
115
116
117
# File 'lib/nemo/components/calendar.rb', line 113

def render_other_day(date)
  r.table_data do
    r.anchor.style('color: silver').callback { save(date) }.with(date.day)
  end
end

#render_selected_day(date) ⇒ Object

Render the currently selected day



105
106
107
108
109
# File 'lib/nemo/components/calendar.rb', line 105

def render_selected_day(date)
  r.table_data.style('border: 1px solid black').with do
    r.anchor.style('font-weight: bold').callback { save(date) }.with(date.day)
  end
end

#render_stylesObject

Render CSS styles



121
122
123
# File 'lib/nemo/components/calendar.rb', line 121

def render_styles
  r.link.type('text/css').rel('stylesheet').href('/nemo/calendar.css')
end

#save(day) ⇒ Object

Save the given day



185
186
187
188
189
# File 'lib/nemo/components/calendar.rb', line 185

def save(day)
  @day = day
  @month = Date.new(day.year, day.month, 1)
  answer(day) unless browse?
end

#selected_day?(date) ⇒ Boolean

True if the given date is the currently selected day

Returns:

  • (Boolean)


59
60
61
# File 'lib/nemo/components/calendar.rb', line 59

def selected_day?(date)
  date == @day
end

#today_stringObject

String to be displayed indicating the current date



83
84
85
# File 'lib/nemo/components/calendar.rb', line 83

def today_string
  Date.today.strftime('Today is %A, %b %d %Y')
end