| 1 |
import { Dialog } from "@web/core/dialog/dialog"; |
| 2 |
import { formatDate } from "@web/core/l10n/dates"; |
| 3 |
import { getColor } from "../utils"; |
| 4 |
import { getFormattedDateSpan } from "@web/views/calendar/utils"; |
| 5 |
|
| 6 |
import { Component } from "@odoo/owl"; |
| 7 |
|
| 8 |
export class CalendarYearPopover extends Component { |
| 9 |
static components = { Dialog }; |
| 10 |
static template = "web.CalendarYearPopover"; |
| 11 |
static subTemplates = { |
| 12 |
popover: "web.CalendarYearPopover.popover", |
| 13 |
body: "web.CalendarYearPopover.body", |
| 14 |
footer: "web.CalendarYearPopover.footer", |
| 15 |
record: "web.CalendarYearPopover.record", |
| 16 |
}; |
| 17 |
static props = { |
| 18 |
close: Function, |
| 19 |
date: true, |
| 20 |
model: Object, |
| 21 |
records: Array, |
| 22 |
createRecord: Function, |
| 23 |
deleteRecord: Function, |
| 24 |
editRecord: Function, |
| 25 |
}; |
| 26 |
|
| 27 |
get recordGroups() { |
| 28 |
return this.computeRecordGroups(); |
| 29 |
} |
| 30 |
|
| 31 |
get dialogTitle() { |
| 32 |
return formatDate(this.props.date, { format: "DDD" }); |
| 33 |
} |
| 34 |
|
| 35 |
computeRecordGroups() { |
| 36 |
const recordGroups = this.groupRecords(); |
| 37 |
return this.getSortedRecordGroups(recordGroups); |
| 38 |
} |
| 39 |
groupRecords() { |
| 40 |
const recordGroups = {}; |
| 41 |
for (const record of this.props.records) { |
| 42 |
const start = record.start; |
| 43 |
const end = record.end; |
| 44 |
|
| 45 |
const duration = end.diff(start, "days").days; |
| 46 |
const modifiedRecord = Object.create(record); |
| 47 |
modifiedRecord.startHour = |
| 48 |
!record.isAllDay && duration < 1 ? start.toFormat("HH:mm") : ""; |
| 49 |
|
| 50 |
const formattedDate = getFormattedDateSpan(start, end); |
| 51 |
if (!(formattedDate in recordGroups)) { |
| 52 |
recordGroups[formattedDate] = { |
| 53 |
title: formattedDate, |
| 54 |
start, |
| 55 |
end, |
| 56 |
records: [], |
| 57 |
}; |
| 58 |
} |
| 59 |
recordGroups[formattedDate].records.push(modifiedRecord); |
| 60 |
} |
| 61 |
return Object.values(recordGroups); |
| 62 |
} |
| 63 |
getRecordClass(record) { |
| 64 |
const { colorIndex } = record; |
| 65 |
const color = getColor(colorIndex); |
| 66 |
if (color && typeof color === "number") { |
| 67 |
return `o_calendar_color_${color}`; |
| 68 |
} |
| 69 |
return ""; |
| 70 |
} |
| 71 |
getRecordStyle(record) { |
| 72 |
const { colorIndex } = record; |
| 73 |
const color = getColor(colorIndex); |
| 74 |
if (color && typeof color === "string") { |
| 75 |
return `background-color: ${color};`; |
| 76 |
} |
| 77 |
return ""; |
| 78 |
} |
| 79 |
getSortedRecordGroups(recordGroups) { |
| 80 |
return recordGroups.sort((a, b) => { |
| 81 |
if (a.start.hasSame(a.end, "days")) { |
| 82 |
return Number.MIN_SAFE_INTEGER; |
| 83 |
} else if (b.start.hasSame(b.end, "days")) { |
| 84 |
return Number.MAX_SAFE_INTEGER; |
| 85 |
} else if (a.start.toMillis() - b.start.toMillis() === 0) { |
| 86 |
return a.end.toMillis() - b.end.toMillis(); |
| 87 |
} |
| 88 |
return a.start.toMillis() - b.start.toMillis(); |
| 89 |
}); |
| 90 |
} |
| 91 |
|
| 92 |
onCreateButtonClick() { |
| 93 |
this.props.createRecord({ |
| 94 |
start: this.props.date, |
| 95 |
isAllDay: true, |
| 96 |
}); |
| 97 |
this.props.close(); |
| 98 |
} |
| 99 |
onRecordClick(record) { |
| 100 |
this.props.editRecord(record); |
| 101 |
this.props.close(); |
| 102 |
} |
| 103 |
} |