Class: SimpleCalendar::Timeslot::TimeslotCalendar

Inherits:
Calendar
  • Object
show all
Defined in:
lib/simple_calendar/timeslot/timeslot_calendar.rb

Instance Method Summary collapse

Instance Method Details

#body_size_pxObject



39
40
41
# File 'lib/simple_calendar/timeslot/timeslot_calendar.rb', line 39

def body_size_px
  @options.fetch(:body_size_px, false)
end

#body_styleObject



63
64
65
66
67
68
69
70
71
# File 'lib/simple_calendar/timeslot/timeslot_calendar.rb', line 63

def body_style
  if layout == :vertical
    body_size_px ? "height:#{body_size_px}px" : ''
  elsif layout == :horizontal
    'height:100%'
  else
    ''
  end
end

#bucket_byObject



47
48
49
# File 'lib/simple_calendar/timeslot/timeslot_calendar.rb', line 47

def bucket_by
  @options.fetch(:bucket_by, false)
end

#bucket_title_sizeObject



31
32
33
34
35
36
37
# File 'lib/simple_calendar/timeslot/timeslot_calendar.rb', line 31

def bucket_title_size
  if display_bucket_title
    @options.fetch(:bucket_title_size, 20)
  else
    0
  end
end

#current_time_offsetObject



144
145
146
147
148
# File 'lib/simple_calendar/timeslot/timeslot_calendar.rb', line 144

def current_time_offset
  now = Time.zone.now
  offset = (now.hour * 60 + now.min) * px_per_minute
  offset
end

#date_format_stringObject



55
56
57
# File 'lib/simple_calendar/timeslot/timeslot_calendar.rb', line 55

def date_format_string
  @options.fetch(:date_format_string, false)
end

#date_heading_format_stringObject



59
60
61
# File 'lib/simple_calendar/timeslot/timeslot_calendar.rb', line 59

def date_heading_format_string
  @options.fetch(:date_heading_format_string, "%B %Y")
end

#day_height_pxObject



43
44
45
# File 'lib/simple_calendar/timeslot/timeslot_calendar.rb', line 43

def day_height_px
  @options.fetch(:day_height_px, 200)
end

#day_height_styleObject



73
74
75
# File 'lib/simple_calendar/timeslot/timeslot_calendar.rb', line 73

def day_height_style
  layout == :horizontal ? "height:#{day_height_px}px;" : ""
end

#day_sizeObject



77
78
79
# File 'lib/simple_calendar/timeslot/timeslot_calendar.rb', line 77

def day_size
  24 * 60 * px_per_minute
end

#display_bucket_titleObject



51
52
53
# File 'lib/simple_calendar/timeslot/timeslot_calendar.rb', line 51

def display_bucket_title
  @options.fetch(:display_bucket_title, false)
end

#display_current_time_indicatorObject



27
28
29
# File 'lib/simple_calendar/timeslot/timeslot_calendar.rb', line 27

def display_current_time_indicator
  @options.fetch(:display_current_time_indicator, false)
end

#display_gridObject



15
16
17
# File 'lib/simple_calendar/timeslot/timeslot_calendar.rb', line 15

def display_grid
  @options.fetch(:display_grid, true)
end

#event_height(event, day) ⇒ Object



81
82
83
84
85
86
87
88
89
90
# File 'lib/simple_calendar/timeslot/timeslot_calendar.rb', line 81

def event_height(event, day)
  minutes = if event.send(attribute).to_date != day
              (event.send(end_attribute) - event.send(end_attribute).midnight)/60
            elsif event.send(attribute).to_date < event.send(end_attribute).to_date
              (event.send(end_attribute).midnight - 60 - event.send(attribute))/60
            else
              (event.send(end_attribute) - event.send(attribute))/60
            end
  minutes * px_per_minute 
end

#event_top_distance(event, day) ⇒ Object



92
93
94
95
96
# File 'lib/simple_calendar/timeslot/timeslot_calendar.rb', line 92

def event_top_distance(event, day)
  return 0 if event.send(attribute).to_date != day
  #(event.send(attribute).hour - TimeslotCalendar::FIRST_HOUR_SLOT) * 60 * px_per_minute + event.send(attribute).min * px_per_minute
  event.send(attribute).hour * 60 * px_per_minute + event.send(attribute).min * px_per_minute
end

#grid_widthObject



19
20
21
22
23
24
25
# File 'lib/simple_calendar/timeslot/timeslot_calendar.rb', line 19

def grid_width
  if display_grid
    @options.fetch(:grid_width, "20px")
  else
    0
  end
end

#layoutObject



7
8
9
# File 'lib/simple_calendar/timeslot/timeslot_calendar.rb', line 7

def layout
  @options.fetch(:layout, :vertical)
end

#px_per_minuteObject



11
12
13
# File 'lib/simple_calendar/timeslot/timeslot_calendar.rb', line 11

def px_per_minute
  @options.fetch(:px_per_minute, 0.65)
end

#slot_events(events, day) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/simple_calendar/timeslot/timeslot_calendar.rb', line 107

def slot_events(events, day)
  r = {}
  events.each do |event|
    r[event] = [0, 0, event_height(event, day), event_top_distance(event, day)]
  end
  # Credit: https://stackoverflow.com/questions/11311410/visualization-of-calendar-events-algorithm-to-layout-events-with-maximum-width
  # Author: Markus Jarderot (https://stackoverflow.com/users/22364/markus-jarderot)
  columns = [[]]
  last_event_ending = nil
  events.each do |event|
    if !last_event_ending.nil? && event.send(attribute) > last_event_ending
      pack_events(r, columns)
      columns = [[]]
      last_event_ending = nil
    end
    placed = false
    columns.each do |col|
      unless events_collide(r, col.last, event)
        col << event
        placed = true
        break
      end
    end
    unless placed
      columns << [event]
    end
    event_end_time = event.send(end_attribute)
    if last_event_ending.nil? || event_end_time > last_event_ending
      last_event_ending = event_end_time
    end
  end
  if columns.size > 0
    pack_events(r, columns)
  end
  r
end

#split_into_buckets(events) ⇒ Object



98
99
100
101
102
103
104
105
# File 'lib/simple_calendar/timeslot/timeslot_calendar.rb', line 98

def split_into_buckets(events)
  if bucket_by
    return [[]] if events.size == 0
    events.group_by{|e| e.send bucket_by}.values
  else
    [events]
  end
end