Class: HakumiComponents::Calendar::DisplayModel

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
app/components/hakumi_components/calendar/display_model.rb

Constant Summary collapse

DateRange =
T.type_alias { T.nilable([ T.nilable(Date), T.nilable(Date) ]) }
YearRangeValue =
T.type_alias { T.any(T::Range[Integer], T::Array[Integer]) }
YearRangeInput =
T.type_alias { T.nilable(YearRangeValue) }
DisabledDatePredicate =
T.type_alias { T.nilable(T.proc.params(date: Date).returns(T::Boolean)) }

Instance Method Summary collapse

Constructor Details

#initialize(current_date:, selected_value:, show_week:, year_range:, valid_range:, disabled_date:, locale_data:) ⇒ DisplayModel

Returns a new instance of DisplayModel.



25
26
27
28
29
30
31
32
33
34
# File 'app/components/hakumi_components/calendar/display_model.rb', line 25

def initialize(current_date:, selected_value:, show_week:, year_range:, valid_range:, disabled_date:, locale_data:)
  @current_date = T.let(current_date, Date)
  @selected_value = T.let(selected_value, T.nilable(Date))
  @show_week = T.let(show_week, T::Boolean)
  @year_range = T.let(year_range, YearRangeInput)
  @valid_range = T.let(valid_range, DateRange)
  @disabled_date = T.let(disabled_date, DisabledDatePredicate)
  @locale_data = T.let(locale_data, HakumiComponents::Calendar::LocalePack)
  @weeks_in_month = T.let(nil, T.nilable(T::Array[HakumiComponents::Calendar::WeekRow]))
end

Instance Method Details

#cell_classes(cell) ⇒ Object



140
141
142
143
144
145
146
147
148
# File 'app/components/hakumi_components/calendar/display_model.rb', line 140

def cell_classes(cell)
  classes = [ "hakumi-calendar-cell" ]
  classes << "hakumi-calendar-cell-in-view" if cell.in_month?
  classes << "hakumi-calendar-cell-today" if cell.today?
  classes << "hakumi-calendar-cell-selected" if cell.selected?
  classes << "hakumi-calendar-cell-disabled" if cell.disabled?
  classes << "hakumi-calendar-cell-weekend" if cell.weekend?
  classes.join(" ")
end

#date_disabled?(date) ⇒ Boolean

Returns:

  • (Boolean)


123
124
125
126
127
128
# File 'app/components/hakumi_components/calendar/display_model.rb', line 123

def date_disabled?(date)
  disabled_date = @disabled_date
  return false unless disabled_date

  disabled_date.call(date)
end

#date_in_valid_range?(date) ⇒ Boolean

Returns:

  • (Boolean)


131
132
133
134
135
136
137
# File 'app/components/hakumi_components/calendar/display_model.rb', line 131

def date_in_valid_range?(date)
  valid_range = @valid_range
  return true unless valid_range

  start_date, end_date = valid_range
  (start_date.nil? || date >= start_date) && (end_date.nil? || date <= end_date)
end

#decade_range_labelObject



109
110
111
112
# File 'app/components/hakumi_components/calendar/display_model.rb', line 109

def decade_range_label
  decade_start = (current_year / 10) * 10
  "#{decade_start}-#{decade_start + 9}"
end

#month_cell_classes(entry) ⇒ Object



151
152
153
# File 'app/components/hakumi_components/calendar/display_model.rb', line 151

def month_cell_classes(entry)
  panel_cell_classes(entry, "month")
end

#month_namesObject



42
43
44
# File 'app/components/hakumi_components/calendar/display_model.rb', line 42

def month_names
  @locale_data.months_full
end

#month_names_shortObject



47
48
49
# File 'app/components/hakumi_components/calendar/display_model.rb', line 47

def month_names_short
  @locale_data.months_short
end

#months_in_yearObject



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'app/components/hakumi_components/calendar/display_model.rb', line 57

def months_in_year
  today = Date.today
  selected = @selected_value

  (1..12).map do |month|
    HakumiComponents::Calendar::PanelEntry.new(
      value: month,
      label: month_names_short.fetch(month - 1),
      current: month == current_month && current_year == today.year,
      selected: !selected.nil? && month == selected.month && current_year == selected.year
    )
  end
end

#quarter_cell_classes(entry) ⇒ Object



156
157
158
# File 'app/components/hakumi_components/calendar/display_model.rb', line 156

def quarter_cell_classes(entry)
  panel_cell_classes(entry, "quarter")
end

#quarters_in_yearObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'app/components/hakumi_components/calendar/display_model.rb', line 72

def quarters_in_year
  today = Date.today
  selected = @selected_value
  current_quarter = ((current_month - 1) / 3) + 1
  selected_quarter = selected.nil? ? nil : ((selected.month - 1) / 3) + 1
  selected_year = selected&.year

  (1..4).map do |quarter|
    HakumiComponents::Calendar::PanelEntry.new(
      value: quarter,
      label: "Q#{quarter}",
      current: quarter == current_quarter && current_year == today.year,
      selected: !selected_quarter.nil? && quarter == selected_quarter && current_year == selected_year
    )
  end
end

#week_number(date) ⇒ Object



166
167
168
# File 'app/components/hakumi_components/calendar/display_model.rb', line 166

def week_number(date)
  date.cweek
end

#weekday_namesObject



37
38
39
# File 'app/components/hakumi_components/calendar/display_model.rb', line 37

def weekday_names
  @locale_data.days_short
end

#weeks_in_monthObject



52
53
54
# File 'app/components/hakumi_components/calendar/display_model.rb', line 52

def weeks_in_month
  @weeks_in_month ||= build_weeks_for_month(current_year, current_month)
end

#year_cell_classes(entry) ⇒ Object



161
162
163
# File 'app/components/hakumi_components/calendar/display_model.rb', line 161

def year_cell_classes(entry)
  panel_cell_classes(entry, "year")
end

#years_in_decadeObject



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'app/components/hakumi_components/calendar/display_model.rb', line 90

def years_in_decade
  decade_start = (current_year / 10) * 10
  start_year = decade_start - 1
  end_year = decade_start + 10
  today = Date.today
  selected = @selected_value

  (start_year..end_year).map do |year|
    HakumiComponents::Calendar::PanelEntry.new(
      value: year,
      label: year.to_s,
      current: year == today.year,
      selected: !selected.nil? && year == selected.year,
      enabled: year >= decade_start && year < decade_start + 10
    )
  end
end

#years_rangeObject



115
116
117
118
119
120
# File 'app/components/hakumi_components/calendar/display_model.rb', line 115

def years_range
  return @year_range.to_a if @year_range.is_a?(Range)
  return @year_range if @year_range.is_a?(Array)

  ((current_year - 10)..(current_year + 10)).to_a
end