| 1 |
import { getLocalYearAndWeek } from "@web/core/l10n/dates"; |
| 2 |
import { localization } from "@web/core/l10n/localization"; |
| 3 |
import { convertRecordToEvent, getColor } from "@web/views/calendar/utils"; |
| 4 |
import { useCalendarPopover } from "@web/views/calendar/hooks/calendar_popover_hook"; |
| 5 |
import { useFullCalendar } from "@web/views/calendar/hooks/full_calendar_hook"; |
| 6 |
import { makeWeekColumn } from "@web/views/calendar/calendar_common/calendar_common_week_column"; |
| 7 |
import { CalendarYearPopover } from "@web/views/calendar/calendar_year/calendar_year_popover"; |
| 8 |
|
| 9 |
import { Component, useEffect, useRef } from "@odoo/owl"; |
| 10 |
|
| 11 |
export class CalendarYearRenderer extends Component { |
| 12 |
static components = { |
| 13 |
Popover: CalendarYearPopover, |
| 14 |
}; |
| 15 |
static template = "web.CalendarYearRenderer"; |
| 16 |
static props = { |
| 17 |
model: Object, |
| 18 |
createRecord: Function, |
| 19 |
editRecord: Function, |
| 20 |
deleteRecord: Function, |
| 21 |
isWeekendVisible: { type: Boolean, optional: true }, |
| 22 |
}; |
| 23 |
|
| 24 |
setup() { |
| 25 |
this.months = luxon.Info.months(); |
| 26 |
this.fcs = {}; |
| 27 |
for (const month of this.months) { |
| 28 |
this.fcs[month] = useFullCalendar( |
| 29 |
`fullCalendar-${month}`, |
| 30 |
this.getOptionsForMonth(month) |
| 31 |
); |
| 32 |
} |
| 33 |
this.popover = useCalendarPopover(this.constructor.components.Popover); |
| 34 |
this.rootRef = useRef("root"); |
| 35 |
|
| 36 |
useEffect(() => { |
| 37 |
this.updateSize(); |
| 38 |
}); |
| 39 |
} |
| 40 |
|
| 41 |
get options() { |
| 42 |
return { |
| 43 |
dayHeaderFormat: "EEEEE", |
| 44 |
dateClick: this.onDateClick, |
| 45 |
dayCellClassNames: this.getDayCellClassNames, |
| 46 |
initialDate: this.props.model.date.toISO(), |
| 47 |
initialView: "dayGridMonth", |
| 48 |
direction: localization.direction, |
| 49 |
droppable: true, |
| 50 |
editable: this.props.model.canEdit, |
| 51 |
dayMaxEventRows: this.props.model.eventLimit, |
| 52 |
eventClassNames: this.eventClassNames, |
| 53 |
eventDidMount: this.onEventDidMount, |
| 54 |
eventResizableFromStart: true, |
| 55 |
events: (_, successCb) => successCb(this.mapRecordsToEvents()), |
| 56 |
firstDay: this.props.model.firstDayOfWeek, |
| 57 |
headerToolbar: { start: false, center: "title", end: false }, |
| 58 |
height: "auto", |
| 59 |
locale: luxon.Settings.defaultLocale, |
| 60 |
longPressDelay: 500, |
| 61 |
navLinks: false, |
| 62 |
nowIndicator: true, |
| 63 |
select: this.onSelect, |
| 64 |
selectMinDistance: 5, // needed to not trigger select when click |
| 65 |
selectMirror: true, |
| 66 |
selectable: this.props.model.canCreate, |
| 67 |
showNonCurrentDates: false, |
| 68 |
timeZone: luxon.Settings.defaultZone.name, |
| 69 |
titleFormat: { month: "long", year: "numeric" }, |
| 70 |
unselectAuto: false, |
| 71 |
weekNumberCalculation: (date) => getLocalYearAndWeek(date).week, |
| 72 |
weekNumbers: false, |
| 73 |
weekNumberFormat: { week: "numeric" }, |
| 74 |
windowResize: this.onWindowResize, |
| 75 |
eventContent: this.onEventContent, |
| 76 |
viewDidMount: this.viewDidMount, |
| 77 |
weekends: this.props.isWeekendVisible, |
| 78 |
fixedWeekCount: false, |
| 79 |
}; |
| 80 |
} |
| 81 |
|
| 82 |
get customOptions() { |
| 83 |
return { |
| 84 |
weekNumbersWithinDays: true, |
| 85 |
}; |
| 86 |
} |
| 87 |
|
| 88 |
viewDidMount({ el, view }) { |
| 89 |
const showWeek = view.calendar.currentData.options.weekNumbers; |
| 90 |
const weekText = view.calendar.currentData.options.weekText; |
| 91 |
const weekColumn = !this.customOptions.weekNumbersWithinDays; |
| 92 |
if (showWeek && weekColumn) { |
| 93 |
makeWeekColumn({ el, weekText }); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
mapRecordsToEvents() { |
| 98 |
return Object.values(this.props.model.records).map((r) => this.convertRecordToEvent(r)); |
| 99 |
} |
| 100 |
convertRecordToEvent(record) { |
| 101 |
return { |
| 102 |
...convertRecordToEvent(record, true), |
| 103 |
display: "background", |
| 104 |
}; |
| 105 |
} |
| 106 |
getDateWithMonth(month) { |
| 107 |
return this.props.model.date.set({ month: this.months.indexOf(month) + 1 }).toISO(); |
| 108 |
} |
| 109 |
getOptionsForMonth(month) { |
| 110 |
return { |
| 111 |
...this.options, |
| 112 |
initialDate: this.getDateWithMonth(month), |
| 113 |
}; |
| 114 |
} |
| 115 |
getPopoverProps(date, records) { |
| 116 |
return { |
| 117 |
date, |
| 118 |
records, |
| 119 |
model: this.props.model, |
| 120 |
createRecord: this.props.createRecord, |
| 121 |
deleteRecord: this.props.deleteRecord, |
| 122 |
editRecord: this.props.editRecord, |
| 123 |
}; |
| 124 |
} |
| 125 |
openPopover(target, date, records) { |
| 126 |
this.popover.open(target, this.getPopoverProps(date, records), "o_cw_popover"); |
| 127 |
} |
| 128 |
unselect() { |
| 129 |
for (const fc of Object.values(this.fcs)) { |
| 130 |
fc.api.unselect(); |
| 131 |
} |
| 132 |
} |
| 133 |
updateSize() { |
| 134 |
const height = window.innerHeight - this.rootRef.el.getBoundingClientRect().top; |
| 135 |
this.rootRef.el.style.height = `${height}px`; |
| 136 |
} |
| 137 |
|
| 138 |
onDateClick(info) { |
| 139 |
if (this.env.isSmall) { |
| 140 |
this.props.model.load({ |
| 141 |
date: luxon.DateTime.fromISO(info.dateStr), |
| 142 |
scale: "day", |
| 143 |
}); |
| 144 |
return; |
| 145 |
} |
| 146 |
|
| 147 |
// With date value we don't want to change the time, we need the exact date |
| 148 |
const date = luxon.DateTime.fromISO(info.dateStr); |
| 149 |
const records = Object.values(this.props.model.records).filter((r) => |
| 150 |
luxon.Interval.fromDateTimes(r.start.startOf("day"), r.end.endOf("day")).contains(date) |
| 151 |
); |
| 152 |
|
| 153 |
this.popover.close(); |
| 154 |
if (records.length) { |
| 155 |
const target = info.dayEl; |
| 156 |
this.openPopover(target, date, records); |
| 157 |
} else if (this.props.model.canCreate) { |
| 158 |
this.props.createRecord({ |
| 159 |
// With date value we don't want to change the time, we need the exact date |
| 160 |
start: luxon.DateTime.fromISO(info.dateStr), |
| 161 |
isAllDay: true, |
| 162 |
}); |
| 163 |
} |
| 164 |
} |
| 165 |
getDayCellClassNames(info) { |
| 166 |
const date = luxon.DateTime.fromJSDate(info.date).toISODate(); |
| 167 |
if (this.props.model.unusualDays.includes(date)) { |
| 168 |
return ["o_calendar_disabled"]; |
| 169 |
} |
| 170 |
return []; |
| 171 |
} |
| 172 |
eventClassNames({ event }) { |
| 173 |
const classesToAdd = []; |
| 174 |
classesToAdd.push("o_event"); |
| 175 |
const record = this.props.model.records[event.id]; |
| 176 |
if (record) { |
| 177 |
const color = getColor(record.colorIndex); |
| 178 |
if (typeof color === "number") { |
| 179 |
classesToAdd.push(`o_calendar_color_${color}`); |
| 180 |
} else if (typeof color !== "string") { |
| 181 |
classesToAdd.push("o_calendar_color_0"); |
| 182 |
} |
| 183 |
|
| 184 |
if (record.isHatched) { |
| 185 |
classesToAdd.push("o_event_hatched"); |
| 186 |
} |
| 187 |
if (record.isStriked) { |
| 188 |
classesToAdd.push("o_event_striked"); |
| 189 |
} |
| 190 |
} |
| 191 |
return classesToAdd; |
| 192 |
} |
| 193 |
onEventDidMount(info) { |
| 194 |
const { el, event } = info; |
| 195 |
el.dataset.eventId = event.id; |
| 196 |
const record = this.props.model.records[event.id]; |
| 197 |
if (record) { |
| 198 |
const color = getColor(record.colorIndex); |
| 199 |
if (typeof color === "string") { |
| 200 |
el.style.backgroundColor = color; |
| 201 |
} |
| 202 |
} |
| 203 |
} |
| 204 |
async onSelect(info) { |
| 205 |
this.popover.close(); |
| 206 |
await this.props.createRecord({ |
| 207 |
// With date value we don't want to change the time, we need the exact date |
| 208 |
start: luxon.DateTime.fromISO(info.startStr), |
| 209 |
end: luxon.DateTime.fromISO(info.endStr).minus({ days: 1 }), |
| 210 |
isAllDay: true, |
| 211 |
}); |
| 212 |
this.unselect(); |
| 213 |
} |
| 214 |
onWindowResize() { |
| 215 |
this.updateSize(); |
| 216 |
} |
| 217 |
|
| 218 |
onEventContent(info) { |
| 219 |
// Remove the title on the background event like in FCv4 |
| 220 |
if (info.event.display?.includes("background")) { |
| 221 |
return null; |
| 222 |
} |
| 223 |
} |
| 224 |
} |