Debian Codegraph @

addons/hr_homeworking_calendar/static/src/calendar/common/calendar_common_renderer.js

raw
1 import { AttendeeCalendarCommonRenderer } from "@calendar/views/attendee_calendar/common/attendee_calendar_common_renderer";
2 import { AttendeeCalendarRenderer } from "@calendar/views/attendee_calendar/attendee_calendar_renderer";
3 import { user } from "@web/core/user";
4 import { patch } from "@web/core/utils/patch";
5 import { renderToString } from "@web/core/utils/render";
6 import { onPatched } from "@odoo/owl";
7
8 const { DateTime } = luxon;
9
10 patch(AttendeeCalendarCommonRenderer.prototype, {
11 setup() {
12 super.setup()
13
14 onPatched(() => {
15 // Force to rerender the FC.
16 // As it doesn't redraw the header when the event's data changes
17 this.fc.api.render();
18 });
19 },
20 get options(){
21 return {
22 ...super.options,
23 eventOrder: function(event1, event2){
24 if (event1.extendedProps.worklocation){
25 return -1;
26 } else {
27 if(event2.extendedProps.worklocation){
28 return 1;
29 } else {
30 return event1.start < event2.start ? -1 : 1;
31 }
32 }
33 },
34 dayCellDidMount: this.onDayCellDidMount,
35 dayHeaderDidMount: this.onDayHeaderDidMount,
36 dayHeaderWillUnmount: this.onDayHeaderWillUnmount,
37 };
38 },
39 handleWorkLocationClick(target, date) {
40 let worklocations = this.props.model.worklocations[date.toISODate()];
41 const worklocationSet = worklocations && Object.keys(worklocations).length > 0;
42 const actionElement = target.closest('.wl_action');
43 if (!actionElement) {
44 return;
45 }
46 const { location, id, create } = actionElement.dataset;
47 if (worklocationSet && !create) {
48 if (!worklocations.id) {
49 worklocations = worklocations[location] && worklocations[location].find(wl => wl.id == id);
50 }
51 if (worklocations) {
52 return this.openPopover(target, worklocations);
53 }
54 }
55 return this.props.openWorkLocationWizard(date);
56 },
57 onDayHeaderDidMount(info) {
58 if (this.props.model.scale === 'week' || this.props.model.scale === 'day') {
59 const date = DateTime.fromJSDate(info.date);
60 const handler = (event) => {
61 if (event.target.closest(".o_worklocation_btn")) {
62 this.handleWorkLocationClick(event.target, date);
63 }
64 };
65 this.customListeners = {
66 ...this.customListeners,
67 [date]: handler,
68 };
69 info.el.addEventListener("click", handler);
70 }
71 },
72 onDayHeaderWillUnmount(info) {
73 if (this.props.model.scale === 'week' || this.props.model.scale === 'day') {
74 const date = DateTime.fromJSDate(info.date);
75 const customListener = {...this.customListeners}[date];
76 if (customListener) {
77 info.el.removeEventListener("click", customListener);
78 delete this.customListeners[date];
79 }
80 }
81 },
82 onDayCellDidMount(info){
83 if (this.props.model.scale === 'month'){
84 const box = info.el.querySelector(`.fc-daygrid-day-top`);
85 if (!box)
86 return;
87 const content = renderToString(this.constructor.ButtonWorklocationTemplate, this.headerTemplateProps(info.date));
88 box.insertAdjacentHTML("beforeend", content);
89 }
90 },
91 onDateClick(info){
92 if (info.jsEvent && info.jsEvent.target.closest(".o_worklocation_btn")) {
93 const date = DateTime.fromJSDate(info.date);
94 this.handleWorkLocationClick(info.jsEvent.target, date);
95 } else {
96 super.onDateClick(...arguments)
97 }
98 },
99 headerTemplateProps(date) {
100 if (this.props.model.scale === "month") {
101 return super.headerTemplateProps(date);
102 }
103 const parsedDate = DateTime.fromJSDate(date).toISODate();
104 const multiCalendar = this.props.model.multiCalendar;
105 const showLine = ["week", "month"].includes(this.props.model.scale);
106 let worklocation = this.props.model.worklocations[parsedDate];
107 const workLocationSetForCurrentUser =
108 multiCalendar ?
109 Object.keys(worklocation).some(key => worklocation[key].some(wlItem => wlItem.userId === user.userId)
110 ) : worklocation?.userId === user.userId;
111
112 let displayedWorkLocation = worklocation ? (JSON.parse(JSON.stringify(worklocation))) : {};
113 // do not display the work locations of the current user if the user filter is not active
114 if (multiCalendar && !this.props.model.data.userFilterActive) {
115 for (let wl in worklocation){
116 displayedWorkLocation[wl] = worklocation[wl].filter(wlItem => wlItem.userId !== user.userId);
117 }
118 displayedWorkLocation = Object.fromEntries(Object.entries(displayedWorkLocation).filter(([_, wlItems]) => wlItems.length !== 0));
119 }
120
121 return {
122 ...super.headerTemplateProps(date),
123 worklocation : displayedWorkLocation,
124 workLocationSetForCurrentUser,
125 multiCalendar,
126 showLine,
127 iconMap: {
128 "office": "fa-building",
129 "home": "fa-home",
130 },
131 }
132 }
133 });
134
135
136 AttendeeCalendarRenderer.props = {
137 ...AttendeeCalendarRenderer.props,
138 openWorkLocationWizard: { type: Function, optional: true },
139 }
140 AttendeeCalendarCommonRenderer.props = {
141 ...AttendeeCalendarCommonRenderer.props,
142 openWorkLocationWizard: { type: Function, optional: true }
143 };
144
145 AttendeeCalendarCommonRenderer.WorklocationTemplate = "hr_homeworking_calendar.CalendarCommonRenderer.worklocation";
146 AttendeeCalendarCommonRenderer.ButtonWorklocationTemplate = "hr_homeworking_calendar.CalendarCommonRenderer.buttonWorklocation";
147 AttendeeCalendarCommonRenderer.headerTemplate = "hr_homeworking_calendar.CalendarCommonRendererHeader";