| 1 |
import { browser } from "@web/core/browser/browser"; |
| 2 |
import { getLocalYearAndWeek } from "@web/core/l10n/dates"; |
| 3 |
import { localization } from "@web/core/l10n/localization"; |
| 4 |
import { is24HourFormat } from "@web/core/l10n/time"; |
| 5 |
import { useBus } from "@web/core/utils/hooks"; |
| 6 |
import { renderToFragment, renderToString } from "@web/core/utils/render"; |
| 7 |
import { useDebounced } from "@web/core/utils/timing"; |
| 8 |
import { makeWeekColumn } from "@web/views/calendar/calendar_common/calendar_common_week_column"; |
| 9 |
import { CalendarCommonPopover } from "@web/views/calendar/calendar_common/calendar_common_popover"; |
| 10 |
import { convertRecordToEvent, getColor } from "@web/views/calendar/utils"; |
| 11 |
import { useCalendarPopover } from "@web/views/calendar/hooks/calendar_popover_hook"; |
| 12 |
import { useFullCalendar } from "@web/views/calendar/hooks/full_calendar_hook"; |
| 13 |
import { useSquareSelection } from "@web/views/calendar/hooks/square_selection_hook"; |
| 14 |
|
| 15 |
import { Component, useEffect } from "@odoo/owl"; |
| 16 |
|
| 17 |
const SCALE_TO_FC_VIEW = { |
| 18 |
day: "timeGridDay", |
| 19 |
week: "timeGridWeek", |
| 20 |
month: "dayGridMonth", |
| 21 |
}; |
| 22 |
const SCALE_TO_HEADER_FORMAT = { |
| 23 |
day: "DDD", |
| 24 |
week: "EEE d", |
| 25 |
month: "EEEE", |
| 26 |
}; |
| 27 |
const SHORT_SCALE_TO_HEADER_FORMAT = { |
| 28 |
...SCALE_TO_HEADER_FORMAT, |
| 29 |
day: "D", |
| 30 |
month: "EEE", |
| 31 |
}; |
| 32 |
const HOUR_FORMATS = { |
| 33 |
12: { |
| 34 |
hour: "numeric", |
| 35 |
minute: "2-digit", |
| 36 |
omitZeroMinute: true, |
| 37 |
meridiem: "short", |
| 38 |
}, |
| 39 |
24: { |
| 40 |
hour: "numeric", |
| 41 |
minute: "2-digit", |
| 42 |
hour12: false, |
| 43 |
}, |
| 44 |
}; |
| 45 |
|
| 46 |
const { DateTime } = luxon; |
| 47 |
|
| 48 |
export class CalendarCommonRenderer extends Component { |
| 49 |
static components = { |
| 50 |
Popover: CalendarCommonPopover, |
| 51 |
}; |
| 52 |
static template = "web.CalendarCommonRenderer"; |
| 53 |
static eventTemplate = "web.CalendarCommonRenderer.event"; |
| 54 |
static headerTemplate = "web.CalendarCommonRendererHeader"; |
| 55 |
static props = { |
| 56 |
model: Object, |
| 57 |
isWeekendVisible: { type: Boolean, optional: true }, |
| 58 |
createRecord: Function, |
| 59 |
editRecord: Function, |
| 60 |
deleteRecord: Function, |
| 61 |
setDate: { type: Function, optional: true }, |
| 62 |
callbackRecorder: Object, |
| 63 |
onSquareSelection: Function, |
| 64 |
cleanSquareSelection: Function, |
| 65 |
}; |
| 66 |
|
| 67 |
setup() { |
| 68 |
this.fc = useFullCalendar("fullCalendar", this.options); |
| 69 |
this.clickTimeoutId = null; |
| 70 |
this.popover = useCalendarPopover(this.constructor.components.Popover); |
| 71 |
this.timeFormat = is24HourFormat() ? "HH:mm" : "hh:mm a"; |
| 72 |
useBus(this.props.model.bus, "SCROLL_TO_CURRENT_HOUR", () => |
| 73 |
this.fc.api.scrollToTime(`${luxon.DateTime.local().hour - 2}:00:00`) |
| 74 |
); |
| 75 |
|
| 76 |
const fullCalendarRenderDebounced = useDebounced(() => this.fc.api.updateSize(), 100, { |
| 77 |
immediate: true, |
| 78 |
trailing: true, |
| 79 |
}); |
| 80 |
const fullCalendarResizeObserver = new ResizeObserver(fullCalendarRenderDebounced); |
| 81 |
useEffect( |
| 82 |
(el) => { |
| 83 |
fullCalendarResizeObserver.observe(el); |
| 84 |
return () => fullCalendarResizeObserver.unobserve(el); |
| 85 |
}, |
| 86 |
() => [this.fc.el] |
| 87 |
); |
| 88 |
useSquareSelection({ |
| 89 |
cellIsSelectable: this.constructor.cellIsSelectable, |
| 90 |
}); |
| 91 |
} |
| 92 |
|
| 93 |
get options() { |
| 94 |
return { |
| 95 |
allDaySlot: true, |
| 96 |
allDayContent: "", |
| 97 |
dayHeaderFormat: this.env.isSmall |
| 98 |
? SHORT_SCALE_TO_HEADER_FORMAT[this.props.model.scale] |
| 99 |
: SCALE_TO_HEADER_FORMAT[this.props.model.scale], |
| 100 |
// we must handle clicks differently in multicreate mode: |
| 101 |
// fc is blocked by safePrevent in onPointerDown (draggable_hook_builder.js) |
| 102 |
dateClick: this.props.model.hasMultiCreate ? () => {} : this.onDateClick, |
| 103 |
dayCellClassNames: this.getDayCellClassNames, |
| 104 |
initialDate: this.props.model.date.toISO(), |
| 105 |
initialView: SCALE_TO_FC_VIEW[this.props.model.scale], |
| 106 |
direction: localization.direction, |
| 107 |
droppable: true, |
| 108 |
editable: this.props.model.canEdit, |
| 109 |
eventClick: this.onEventClick, |
| 110 |
eventDragStart: this.onEventDragStart, |
| 111 |
eventDrop: this.onEventDrop, |
| 112 |
dayMaxEventRows: this.props.model.eventLimit, |
| 113 |
moreLinkClick: this.onEventLimitClick, |
| 114 |
eventMouseEnter: this.onEventMouseEnter, |
| 115 |
eventMouseLeave: this.onEventMouseLeave, |
| 116 |
eventClassNames: this.eventClassNames, |
| 117 |
eventDidMount: this.onEventDidMount, |
| 118 |
eventContent: this.onEventContent, |
| 119 |
eventResizableFromStart: true, |
| 120 |
eventResize: this.onEventResize, |
| 121 |
eventResizeStart: this.onEventResizeStart, |
| 122 |
events: (_, successCb) => successCb(this.mapRecordsToEvents()), |
| 123 |
firstDay: this.props.model.firstDayOfWeek, |
| 124 |
headerToolbar: false, |
| 125 |
height: "100%", |
| 126 |
locale: luxon.Settings.defaultLocale, |
| 127 |
longPressDelay: 500, |
| 128 |
navLinks: false, |
| 129 |
nowIndicator: true, |
| 130 |
nowIndicatorContent: { |
| 131 |
html: ` |
| 132 |
<div class="o_calendar_time_indicator_now"></div> |
| 133 |
`, |
| 134 |
}, |
| 135 |
select: this.onSelect, |
| 136 |
selectAllow: this.isSelectionAllowed, |
| 137 |
selectMinDistance: 5, // needed to not trigger select when click |
| 138 |
selectMirror: true, |
| 139 |
selectable: !this.props.model.hasMultiCreate && this.props.model.canCreate, |
| 140 |
showNonCurrentDates: this.props.model.monthOverflow, |
| 141 |
slotLabelFormat: is24HourFormat() ? HOUR_FORMATS[24] : HOUR_FORMATS[12], |
| 142 |
snapDuration: { minutes: 15 }, |
| 143 |
timeZone: luxon.Settings.defaultZone.name, |
| 144 |
unselectAuto: false, |
| 145 |
weekNumberFormat: { |
| 146 |
week: this.props.model.scale === "month" || this.env.isSmall ? "numeric" : "long", |
| 147 |
}, |
| 148 |
weekends: this.props.isWeekendVisible, |
| 149 |
weekNumberCalculation: (date) => getLocalYearAndWeek(date).week, |
| 150 |
weekNumbers: true, |
| 151 |
dayHeaderContent: this.getHeaderHtml, |
| 152 |
eventDisplay: "block", // Restore old render in daygrid view for single-day timed events |
| 153 |
eventTimeFormat: is24HourFormat() ? HOUR_FORMATS[24] : HOUR_FORMATS[12], |
| 154 |
viewDidMount: this.viewDidMount, |
| 155 |
moreLinkDidMount: this.wrapMoreLink, |
| 156 |
fixedWeekCount: false, |
| 157 |
}; |
| 158 |
} |
| 159 |
|
| 160 |
get customOptions() { |
| 161 |
return { |
| 162 |
weekNumbersWithinDays: !this.env.isSmall, |
| 163 |
}; |
| 164 |
} |
| 165 |
|
| 166 |
viewDidMount({ el, view }) { |
| 167 |
const showWeek = view.calendar.currentData.options.weekNumbers; |
| 168 |
const weekText = view.calendar.currentData.options.weekText; |
| 169 |
const weekColumn = !this.customOptions.weekNumbersWithinDays; |
| 170 |
if (showWeek && weekColumn) { |
| 171 |
makeWeekColumn({ el, weekText }); |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
getStartTime(record) { |
| 176 |
return record.start.toFormat(this.timeFormat); |
| 177 |
} |
| 178 |
|
| 179 |
getEndTime(record) { |
| 180 |
return record.end.toFormat(this.timeFormat); |
| 181 |
} |
| 182 |
|
| 183 |
computeEventSelector(event) { |
| 184 |
return `[data-event-id="${event.id}"]`; |
| 185 |
} |
| 186 |
highlightEvent(event, className) { |
| 187 |
for (const el of this.fc.api.el.querySelectorAll(this.computeEventSelector(event))) { |
| 188 |
el.classList.add(className); |
| 189 |
} |
| 190 |
} |
| 191 |
unhighlightEvent(event, className) { |
| 192 |
for (const el of this.fc.api.el.querySelectorAll(this.computeEventSelector(event))) { |
| 193 |
el.classList.remove(className); |
| 194 |
} |
| 195 |
} |
| 196 |
mapRecordsToEvents() { |
| 197 |
return Object.values(this.props.model.records).map((r) => this.convertRecordToEvent(r)); |
| 198 |
} |
| 199 |
convertRecordToEvent(record) { |
| 200 |
return convertRecordToEvent(record); |
| 201 |
} |
| 202 |
getPopoverProps(record) { |
| 203 |
return { |
| 204 |
record, |
| 205 |
model: this.props.model, |
| 206 |
createRecord: this.props.createRecord, |
| 207 |
deleteRecord: this.props.deleteRecord, |
| 208 |
editRecord: this.props.editRecord, |
| 209 |
}; |
| 210 |
} |
| 211 |
openPopover(target, record) { |
| 212 |
const color = getColor(record.colorIndex); |
| 213 |
this.popover.open( |
| 214 |
target, |
| 215 |
this.getPopoverProps(record), |
| 216 |
`o_cw_popover card o_calendar_color_${typeof color === "number" ? color : 0}` |
| 217 |
); |
| 218 |
} |
| 219 |
|
| 220 |
onClick(info) { |
| 221 |
this.openPopover(info.el, this.props.model.records[info.event.id]); |
| 222 |
this.highlightEvent(info.event, "o_cw_custom_highlight"); |
| 223 |
} |
| 224 |
onDateClick(info) { |
| 225 |
if (info.jsEvent.defaultPrevented) { |
| 226 |
return; |
| 227 |
} |
| 228 |
this.props.createRecord(this.fcEventToRecord(info)); |
| 229 |
} |
| 230 |
getDayCellClassNames(info) { |
| 231 |
const date = luxon.DateTime.fromJSDate(info.date).toISODate(); |
| 232 |
if (this.props.model.unusualDays.includes(date)) { |
| 233 |
return ["o_calendar_disabled"]; |
| 234 |
} |
| 235 |
return []; |
| 236 |
} |
| 237 |
onDblClick(info) { |
| 238 |
this.props.editRecord(this.props.model.records[info.event.id]); |
| 239 |
} |
| 240 |
onEventClick(info) { |
| 241 |
if (this.clickTimeoutId) { |
| 242 |
this.onDblClick(info); |
| 243 |
browser.clearTimeout(this.clickTimeoutId); |
| 244 |
this.clickTimeoutId = null; |
| 245 |
} else { |
| 246 |
this.clickTimeoutId = browser.setTimeout(() => { |
| 247 |
this.onClick(info); |
| 248 |
this.clickTimeoutId = null; |
| 249 |
}, 250); |
| 250 |
} |
| 251 |
} |
| 252 |
onEventContent(arg) { |
| 253 |
const { event } = arg; |
| 254 |
if (event.start && event.end) { |
| 255 |
const dateFmt = (date) => |
| 256 |
luxon.DateTime.fromJSDate(date).toFormat(this.timeFormat); |
| 257 |
arg.timeText = `${dateFmt(event.start)} - ${dateFmt(event.end)}`; |
| 258 |
} |
| 259 |
const record = this.props.model.records[event.id]; |
| 260 |
if (record) { |
| 261 |
// This is needed in order to give the possibility to change the event template. |
| 262 |
const fragment = renderToFragment(this.constructor.eventTemplate, { |
| 263 |
...record, |
| 264 |
startTime: this.getStartTime(record), |
| 265 |
endTime: this.getEndTime(record), |
| 266 |
}); |
| 267 |
return { domNodes: fragment.children }; |
| 268 |
} |
| 269 |
return true; |
| 270 |
} |
| 271 |
eventClassNames({ el, event }) { |
| 272 |
const classesToAdd = []; |
| 273 |
classesToAdd.push("o_event"); |
| 274 |
const record = this.props.model.records[event.id]; |
| 275 |
|
| 276 |
if (record) { |
| 277 |
const color = getColor(record.colorIndex); |
| 278 |
if (typeof color === "number") { |
| 279 |
classesToAdd.push(`o_calendar_color_${color}`); |
| 280 |
} else if (typeof color !== "string") { |
| 281 |
classesToAdd.push("o_calendar_color_0"); |
| 282 |
} |
| 283 |
|
| 284 |
if (record.isHatched) { |
| 285 |
classesToAdd.push("o_event_hatched"); |
| 286 |
} |
| 287 |
if (record.isStriked) { |
| 288 |
classesToAdd.push("o_event_striked"); |
| 289 |
} |
| 290 |
if (record.duration <= 0.25) { |
| 291 |
classesToAdd.push("o_event_oneliner"); |
| 292 |
} |
| 293 |
if (DateTime.now() >= record.end) { |
| 294 |
classesToAdd.push("o_past_event"); |
| 295 |
} |
| 296 |
|
| 297 |
if (!record.isAllDay && !record.isTimeHidden && record.isMonth) { |
| 298 |
classesToAdd.push("o_event_dot"); |
| 299 |
} else if (record.isAllDay) { |
| 300 |
classesToAdd.push("o_event_allday"); |
| 301 |
} |
| 302 |
} |
| 303 |
return classesToAdd; |
| 304 |
} |
| 305 |
onEventDidMount({ el, event }) { |
| 306 |
el.dataset.eventId = event.id; |
| 307 |
const record = this.props.model.records[event.id]; |
| 308 |
|
| 309 |
if (record) { |
| 310 |
if (record.isMonth) { |
| 311 |
el.querySelector(".fc-event-main").classList.add( |
| 312 |
"d-flex", |
| 313 |
"gap-1", |
| 314 |
"text-truncate" |
| 315 |
); |
| 316 |
} |
| 317 |
const color = getColor(record.colorIndex); |
| 318 |
if (typeof color === "string") { |
| 319 |
el.style.backgroundColor = color; |
| 320 |
} |
| 321 |
|
| 322 |
if (!el.classList.contains("fc-bg")) { |
| 323 |
const bg = document.createElement("div"); |
| 324 |
bg.classList.add("fc-bg"); |
| 325 |
el.appendChild(bg); |
| 326 |
} |
| 327 |
} |
| 328 |
} |
| 329 |
async onSelect(info) { |
| 330 |
info.jsEvent.preventDefault(); |
| 331 |
this.popover.close(); |
| 332 |
await this.props.createRecord(this.fcEventToRecord(info)); |
| 333 |
this.fc.api.unselect(); |
| 334 |
} |
| 335 |
isSelectionAllowed(event) { |
| 336 |
return event.end.getDate() === event.start.getDate() || event.allDay; |
| 337 |
} |
| 338 |
onEventDrop(info) { |
| 339 |
this.fc.api.unselect(); |
| 340 |
this.props.model.updateRecord(this.fcEventToRecord(info.event), { moved: true }); |
| 341 |
} |
| 342 |
onEventResize(info) { |
| 343 |
this.fc.api.unselect(); |
| 344 |
this.props.model.updateRecord(this.fcEventToRecord(info.event)); |
| 345 |
} |
| 346 |
fcEventToRecord(event) { |
| 347 |
const { id, allDay, date, start, end } = event; |
| 348 |
const res = { |
| 349 |
start: luxon.DateTime.fromJSDate(date || start), |
| 350 |
isAllDay: allDay, |
| 351 |
}; |
| 352 |
if (end) { |
| 353 |
res.end = luxon.DateTime.fromJSDate(end); |
| 354 |
if (["week", "month"].includes(this.props.model.scale) && allDay) { |
| 355 |
res.end = res.end.minus({ days: 1 }); |
| 356 |
} |
| 357 |
} |
| 358 |
if (id) { |
| 359 |
const existingRecord = this.props.model.records[id]; |
| 360 |
if (this.props.model.scale === "month") { |
| 361 |
res.start = res.start?.set({ |
| 362 |
hour: existingRecord.start.hour, |
| 363 |
minute: existingRecord.start.minute, |
| 364 |
}); |
| 365 |
if (existingRecord.end) { |
| 366 |
res.end = res.end?.set({ |
| 367 |
hour: existingRecord.end.hour, |
| 368 |
minute: existingRecord.end.minute, |
| 369 |
}); |
| 370 |
} |
| 371 |
} |
| 372 |
res.id = existingRecord.id; |
| 373 |
} |
| 374 |
return res; |
| 375 |
} |
| 376 |
onEventMouseEnter(info) { |
| 377 |
this.highlightEvent(info.event, "o_cw_custom_highlight"); |
| 378 |
} |
| 379 |
onEventMouseLeave(info) { |
| 380 |
if (!info.event.id) { |
| 381 |
return; |
| 382 |
} |
| 383 |
this.unhighlightEvent(info.event, "o_cw_custom_highlight"); |
| 384 |
} |
| 385 |
onEventDragStart(info) { |
| 386 |
this.props.cleanSquareSelection(); |
| 387 |
info.el.classList.add(info.view.type); |
| 388 |
this.fc.api.unselect(); |
| 389 |
this.highlightEvent(info.event, "o_cw_custom_highlight"); |
| 390 |
} |
| 391 |
onEventResizeStart(info) { |
| 392 |
this.props.cleanSquareSelection(); |
| 393 |
this.fc.api.unselect(); |
| 394 |
this.highlightEvent(info.event, "o_cw_custom_highlight"); |
| 395 |
} |
| 396 |
onEventLimitClick() { |
| 397 |
this.fc.api.unselect(); |
| 398 |
return "popover"; |
| 399 |
} |
| 400 |
onWindowResize() { |
| 401 |
this.updateSize(); |
| 402 |
} |
| 403 |
|
| 404 |
getHeaderHtml({ date }) { |
| 405 |
return { |
| 406 |
html: renderToString(this.constructor.headerTemplate, this.headerTemplateProps(date)), |
| 407 |
}; |
| 408 |
} |
| 409 |
|
| 410 |
headerTemplateProps(date) { |
| 411 |
const scale = this.props.model.scale; |
| 412 |
// when rendering months, FullCalendar uses a date w/out tz |
| 413 |
// so use UTC instead of local tz when converting to DateTime |
| 414 |
const options = scale === "month" ? { zone: "UTC" } : {}; |
| 415 |
const { weekdayShort, weekdayLong, day } = DateTime.fromJSDate(date, options); |
| 416 |
return { |
| 417 |
weekdayShort, |
| 418 |
weekdayLong, |
| 419 |
day, |
| 420 |
scale, |
| 421 |
}; |
| 422 |
} |
| 423 |
|
| 424 |
wrapMoreLink({ el }) { |
| 425 |
const wrapper = document.createElement("div"); |
| 426 |
wrapper.classList.add("fc-more-cell"); |
| 427 |
el.parentNode.insertBefore(wrapper, el); |
| 428 |
wrapper.appendChild(el); |
| 429 |
} |
| 430 |
} |