Debian Codegraph @

addons/web/static/src/views/calendar/calendar_common/calendar_common_popover.js

raw
1 import { _t } from "@web/core/l10n/translation";
2 import { Dialog } from "@web/core/dialog/dialog";
3 import { evaluateBooleanExpr } from "@web/core/py_js/py";
4 import { is24HourFormat } from "@web/core/l10n/time";
5 import { registry } from "@web/core/registry";
6 import { Field } from "@web/views/fields/field";
7 import { Record } from "@web/model/record";
8 import { getFormattedDateSpan } from "@web/views/calendar/utils";
9
10 import { Component, useExternalListener } from "@odoo/owl";
11
12 export class CalendarCommonPopover extends Component {
13 static template = "web.CalendarCommonPopover";
14 static subTemplates = {
15 popover: "web.CalendarCommonPopover.popover",
16 body: "web.CalendarCommonPopover.body",
17 footer: "web.CalendarCommonPopover.footer",
18 };
19 static components = {
20 Dialog,
21 Field,
22 Record,
23 };
24 static props = {
25 close: Function,
26 record: Object,
27 model: Object,
28 createRecord: Function,
29 deleteRecord: Function,
30 editRecord: Function,
31 };
32
33 setup() {
34 this.time = null;
35 this.timeDuration = null;
36 this.date = null;
37 this.dateDuration = null;
38
39 useExternalListener(window, "pointerdown", (e) => e.preventDefault(), { capture: true });
40
41 this.computeDateTimeAndDuration();
42 }
43
44 get activeFields() {
45 return this.props.model.activeFields;
46 }
47 get isEventEditable() {
48 return this.props.model.canEdit;
49 }
50 get isEventDeletable() {
51 return this.props.model.canDelete;
52 }
53 get isEventViewable() {
54 return true;
55 }
56 get hasFooter() {
57 return this.isEventEditable || this.isEventDeletable || this.isEventViewable;
58 }
59
60 isInvisible(fieldNode, record) {
61 return evaluateBooleanExpr(fieldNode.invisible, record.evalContextWithVirtualIds);
62 }
63
64 getFormattedValue(fieldName, record) {
65 const fieldInfo = this.props.model.popoverFieldNodes[fieldName];
66 const field = this.props.model.fields[fieldName];
67 let format;
68 const formattersRegistry = registry.category("formatters");
69 if (fieldInfo.widget && formattersRegistry.contains(fieldInfo.widget)) {
70 format = formattersRegistry.get(fieldInfo.widget);
71 } else {
72 format = formattersRegistry.get(field.type);
73 }
74 return format(record.data[fieldName]);
75 }
76
77 computeDateTimeAndDuration() {
78 const record = this.props.record;
79 const { start, end } = record;
80 const isSameDay = start.hasSame(end, "day");
81
82 if (!record.isTimeHidden && !record.isAllDay && isSameDay) {
83 const timeFormat = is24HourFormat() ? "HH:mm" : "hh:mm a";
84 this.time = `${start.toFormat(timeFormat)} - ${end.toFormat(timeFormat)}`;
85
86 const duration = end.diff(start, ["hours", "minutes"]);
87 const formatParts = [];
88 if (duration.hours > 0) {
89 const hourString = duration.hours === 1 ? _t("hour") : _t("hours");
90 formatParts.push(`h '${hourString}'`);
91 }
92 if (duration.minutes > 0) {
93 const minuteStr = duration.minutes === 1 ? _t("minute") : _t("minutes");
94 formatParts.push(`m '${minuteStr}'`);
95 }
96 this.timeDuration = duration.toFormat(formatParts.join(", "));
97 }
98
99 if (!this.props.model.isDateHidden) {
100 this.date = getFormattedDateSpan(start, end);
101
102 if (record.isAllDay) {
103 if (isSameDay) {
104 this.dateDuration = _t("All day");
105 } else {
106 const duration = end.plus({ day: 1 }).diff(start, "days");
107 this.dateDuration = duration.toFormat(`d '${_t("days")}'`);
108 }
109 }
110 }
111 }
112
113 onEditEvent() {
114 this.props.editRecord(this.props.record);
115 this.props.close();
116 }
117 onDeleteEvent() {
118 this.props.deleteRecord(this.props.record);
119 this.props.close();
120 }
121 }