Debian Codegraph @

addons/web/static/tests/legacy/views/calendar/helpers.js

raw symbols SCIP
1 /** @odoo-module alias=@web/../tests/views/calendar/helpers default=false */
2
3 import { uiService } from "@web/core/ui/ui_service";
4 import { createElement } from "@web/core/utils/xml";
5 import { registry } from "@web/core/registry";
6 import { Field } from "@web/views/fields/field";
7 import { clearRegistryWithCleanup, makeTestEnv } from "../../helpers/mock_env";
8 import { click, getFixture, mount, nextTick, triggerEvent } from "../../helpers/utils";
9 import { setupViewRegistries } from "@web/../tests/views/helpers";
10
11 export function makeEnv(services = {}) {
12 clearRegistryWithCleanup(registry.category("main_components"));
13 setupViewRegistries();
14 services = Object.assign(
15 {
16 ui: uiService,
17 },
18 services
19 );
20
21 for (const [key, service] of Object.entries(services)) {
22 registry.category("services").add(key, service, { force: true });
23 }
24
25 return makeTestEnv({
26 config: {
27 setDisplayName: () => {},
28 },
29 });
30 }
31
32 //------------------------------------------------------------------------------
33
34 export async function mountComponent(C, env, props) {
35 const target = getFixture();
36 return await mount(C, target, { env, props });
37 }
38
39 //------------------------------------------------------------------------------
40
41 export function makeFakeDate() {
42 return luxon.DateTime.local(2021, 7, 16, 8, 0, 0, 0);
43 }
44
45 export function makeFakeRecords() {
46 return {
47 1: {
48 id: 1,
49 title: "1 day, all day in July",
50 start: makeFakeDate(),
51 isAllDay: true,
52 end: makeFakeDate(),
53 },
54 2: {
55 id: 2,
56 title: "3 days, all day in July",
57 start: makeFakeDate().plus({ days: 2 }),
58 isAllDay: true,
59 end: makeFakeDate().plus({ days: 4 }),
60 },
61 3: {
62 id: 3,
63 title: "1 day, all day in June",
64 start: makeFakeDate().plus({ months: -1 }),
65 isAllDay: true,
66 end: makeFakeDate().plus({ months: -1 }),
67 },
68 4: {
69 id: 4,
70 title: "3 days, all day in June",
71 start: makeFakeDate().plus({ months: -1, days: 2 }),
72 isAllDay: true,
73 end: makeFakeDate().plus({ months: -1, days: 4 }),
74 },
75 5: {
76 id: 5,
77 title: "Over June and July",
78 start: makeFakeDate().startOf("month").plus({ days: -2 }),
79 isAllDay: true,
80 end: makeFakeDate().startOf("month").plus({ days: 2 }),
81 },
82 };
83 }
84
85 export const FAKE_FILTER_SECTIONS = [
86 {
87 label: "Attendees",
88 fieldName: "partner_ids",
89 avatar: {
90 model: "res.partner",
91 field: "avatar_128",
92 },
93 hasAvatar: true,
94 write: {
95 model: "filter_partner",
96 field: "partner_id",
97 },
98 canAddFilter: true,
99 filters: [
100 {
101 type: "user",
102 label: "Mitchell Admin",
103 active: true,
104 value: 3,
105 colorIndex: 3,
106 recordId: null,
107 canRemove: false,
108 hasAvatar: true,
109 },
110 {
111 type: "all",
112 label: "Everybody's calendar",
113 active: false,
114 value: "all",
115 colorIndex: null,
116 recordId: null,
117 canRemove: false,
118 hasAvatar: false,
119 },
120 {
121 type: "record",
122 label: "Brandon Freeman",
123 active: true,
124 value: 4,
125 colorIndex: 4,
126 recordId: 1,
127 canRemove: true,
128 hasAvatar: true,
129 },
130 {
131 type: "record",
132 label: "Marc Demo",
133 active: false,
134 value: 6,
135 colorIndex: 6,
136 recordId: 2,
137 canRemove: true,
138 hasAvatar: true,
139 },
140 ],
141 },
142 {
143 label: "Users",
144 fieldName: "user_id",
145 avatar: {
146 model: null,
147 field: null,
148 },
149 hasAvatar: false,
150 write: {
151 model: null,
152 field: null,
153 },
154 canAddFilter: false,
155 filters: [
156 {
157 type: "record",
158 label: "Brandon Freeman",
159 active: false,
160 value: 1,
161 colorIndex: false,
162 recordId: null,
163 canRemove: true,
164 hasAvatar: true,
165 },
166 {
167 type: "record",
168 label: "Marc Demo",
169 active: false,
170 value: 2,
171 colorIndex: false,
172 recordId: null,
173 canRemove: true,
174 hasAvatar: true,
175 },
176 ],
177 },
178 ];
179
180 export const FAKE_FIELDS = {
181 id: { string: "Id", type: "integer" },
182 user_id: { string: "User", type: "many2one", relation: "user", default: -1 },
183 partner_id: {
184 string: "Partner",
185 type: "many2one",
186 relation: "partner",
187 related: "user_id.partner_id",
188 default: 1,
189 },
190 name: { string: "Name", type: "char" },
191 start_date: { string: "Start Date", type: "date" },
192 stop_date: { string: "Stop Date", type: "date" },
193 start: { string: "Start Datetime", type: "datetime" },
194 stop: { string: "Stop Datetime", type: "datetime" },
195 delay: { string: "Delay", type: "float" },
196 allday: { string: "Is All Day", type: "boolean" },
197 partner_ids: {
198 string: "Attendees",
199 type: "one2many",
200 relation: "partner",
201 default: [[6, 0, [1]]],
202 },
203 type: { string: "Type", type: "integer" },
204 event_type_id: { string: "Event Type", type: "many2one", relation: "event_type" },
205 color: { string: "Color", type: "integer", related: "event_type_id.color" },
206 };
207
208 function makeFakeModelState() {
209 const fakeFieldNode = createElement("field", { name: "name" });
210 const fakeModels = { event: { fields: FAKE_FIELDS } };
211 return {
212 canCreate: true,
213 canDelete: true,
214 canEdit: true,
215 date: makeFakeDate(),
216 fieldMapping: {
217 date_start: "start_date",
218 date_stop: "stop_date",
219 date_delay: "delay",
220 all_day: "allday",
221 color: "color",
222 },
223 fieldNames: ["start_date", "stop_date", "color", "delay", "allday", "user_id"],
224 fields: FAKE_FIELDS,
225 filterSections: FAKE_FILTER_SECTIONS,
226 firstDayOfWeek: 0,
227 isDateHidden: false,
228 isTimeHidden: false,
229 hasAllDaySlot: true,
230 hasEditDialog: false,
231 quickCreate: false,
232 popoverFieldNodes: {
233 name: Field.parseFieldNode(fakeFieldNode, fakeModels, "event", "calendar"),
234 },
235 activeFields: {
236 name: {
237 context: "{}",
238 invisible: false,
239 readonly: false,
240 required: false,
241 onChange: false,
242 },
243 },
244 rangeEnd: makeFakeDate().endOf("month"),
245 rangeStart: makeFakeDate().startOf("month"),
246 records: makeFakeRecords(),
247 resModel: "event",
248 scale: "month",
249 scales: ["day", "week", "month", "year"],
250 unusualDays: [],
251 };
252 }
253
254 export function makeFakeModel(state = {}) {
255 return {
256 ...makeFakeModelState(),
257 load() {},
258 createFilter() {},
259 createRecord() {},
260 unlinkFilter() {},
261 unlinkRecord() {},
262 updateFilter() {},
263 updateRecord() {},
264 ...state,
265 };
266 }
267
268 // DOM Utils
269 //------------------------------------------------------------------------------
270
271 async function scrollTo(el, scrollParam) {
272 el.scrollIntoView(scrollParam);
273 await new Promise(window.requestAnimationFrame);
274 }
275
276 export function findPickedDate(target) {
277 return target.querySelector(".o_datetime_picker .o_selected");
278 }
279
280 export async function pickDate(target, date) {
281 const day = date.split("-")[2];
282 const iDay = parseInt(day, 10) - 1;
283 const el = target.querySelectorAll(`.o_datetime_picker .o_date_item_cell:not(.o_out_of_range)`)[
284 iDay
285 ];
286 el.scrollIntoView();
287 await click(el);
288 }
289
290 export function expandCalendarView(target) {
291 // Expends Calendar view and FC too
292 let tmpElement = target.querySelector(".fc");
293 do {
294 tmpElement = tmpElement.parentElement;
295 tmpElement.classList.add("h-100");
296 } while (!tmpElement.classList.contains("o_view_controller"));
297 }
298
299 export function findAllDaySlot(target, date) {
300 return target.querySelector(`.fc-daygrid-body .fc-day[data-date="${date}"]`);
301 }
302
303 export function findDateCell(target, date) {
304 return target.querySelector(`.fc-day[data-date="${date}"]`);
305 }
306
307 export function findEvent(target, eventId) {
308 return target.querySelector(`.o_event[data-event-id="${eventId}"]`);
309 }
310
311 export function findDateCol(target, date) {
312 return target.querySelector(`.fc-col-header-cell.fc-day[data-date="${date}"]`);
313 }
314
315 export function findTimeRow(target, time) {
316 return target.querySelector(`.fc-timegrid-slot[data-time="${time}"]`);
317 }
318
319 export async function triggerEventForCalendar(el, type, position = {}) {
320 const rect = el.getBoundingClientRect();
321 const x = position.x || rect.x + rect.width / 2;
322 const y = position.y || rect.y + rect.height / 2;
323 const attrs = {
324 which: 1,
325 clientX: x,
326 clientY: y,
327 };
328 await triggerEvent(el, null, type, attrs);
329 }
330
331 export async function clickAllDaySlot(target, date) {
332 const el = findAllDaySlot(target, date);
333 await scrollTo(el);
334 await triggerEventForCalendar(el, "mousedown");
335 await triggerEventForCalendar(el, "mouseup");
336 await nextTick();
337 }
338
339 export async function clickDate(target, date) {
340 const el = findDateCell(target, date);
341 await scrollTo(el);
342 await triggerEventForCalendar(el, "mousedown");
343 await triggerEventForCalendar(el, "mouseup");
344 await nextTick();
345 }
346
347 export async function clickEvent(target, eventId) {
348 const el = findEvent(target, eventId);
349 await scrollTo(el);
350 await click(el);
351 await nextTick();
352 }
353
354 export async function selectTimeRange(target, startDateTime, endDateTime) {
355 const [startDate, startTime] = startDateTime.split(" ");
356 const [endDate, endTime] = endDateTime.split(" ");
357
358 const startCol = findDateCol(target, startDate);
359 const endCol = findDateCol(target, endDate);
360 const startRow = findTimeRow(target, startTime);
361 const endRow = findTimeRow(target, endTime);
362
363 await scrollTo(startRow);
364 const startColRect = startCol.getBoundingClientRect();
365 const startRowRect = startRow.getBoundingClientRect();
366
367 await triggerEventForCalendar(startRow, "mousedown", {
368 x: startColRect.x + startColRect.width / 2,
369 y: startRowRect.y + 2,
370 });
371
372 await scrollTo(endRow, false);
373 const endColRect = endCol.getBoundingClientRect();
374 const endRowRect = endRow.getBoundingClientRect();
375
376 await triggerEventForCalendar(endRow, "mousemove", {
377 x: endColRect.x + endColRect.width / 2,
378 y: endRowRect.y - 2,
379 });
380 await triggerEventForCalendar(endRow, "mouseup", {
381 x: endColRect.x + endColRect.width / 2,
382 y: endRowRect.y - 2,
383 });
384 await nextTick();
385 }
386
387 export async function selectDateRange(target, startDate, endDate) {
388 const start = findDateCell(target, startDate);
389 const end = findDateCell(target, endDate);
390 await scrollTo(start);
391 await triggerEventForCalendar(start, "mousedown");
392 await scrollTo(end);
393 await triggerEventForCalendar(end, "mousemove");
394 await triggerEventForCalendar(end, "mouseup");
395 await nextTick();
396 }
397
398 export async function selectAllDayRange(target, startDate, endDate) {
399 const start = findAllDaySlot(target, startDate);
400 const end = findAllDaySlot(target, endDate);
401 await scrollTo(start);
402 await triggerEventForCalendar(start, "mousedown");
403 await scrollTo(end);
404 await triggerEventForCalendar(end, "mousemove");
405 await triggerEventForCalendar(end, "mouseup");
406 await nextTick();
407 }
408
409 export async function moveEventToDate(target, eventId, date, options = {}) {
410 const event = findEvent(target, eventId);
411 const cell = findDateCell(target, date);
412
413 await scrollTo(event);
414 await triggerEventForCalendar(event, "mousedown");
415
416 await scrollTo(cell);
417 await triggerEventForCalendar(cell, "mousemove");
418
419 if (!options.disableDrop) {
420 await triggerEventForCalendar(cell, "mouseup");
421 }
422 await nextTick();
423 }
424
425 export async function moveEventToTime(target, eventId, dateTime) {
426 const event = findEvent(target, eventId);
427 const [date, time] = dateTime.split(" ");
428
429 const col = findDateCol(target, date);
430 const row = findTimeRow(target, time);
431
432 // Find event position
433 await scrollTo(event);
434 const eventRect = event.getBoundingClientRect();
435 const eventPos = {
436 x: eventRect.x + eventRect.width / 2,
437 y: eventRect.y,
438 };
439
440 await triggerEventForCalendar(event, "mousedown", eventPos);
441
442 // Find target position
443 await scrollTo(row, false);
444 const colRect = col.getBoundingClientRect();
445 const rowRect = row.getBoundingClientRect();
446 const toPos = {
447 x: colRect.x + colRect.width / 2,
448 y: rowRect.y - 1,
449 };
450
451 await triggerEventForCalendar(row, "mousemove", toPos);
452 await triggerEventForCalendar(row, "mouseup", toPos);
453 await nextTick();
454 }
455
456 export async function moveEventToAllDaySlot(target, eventId, date) {
457 const event = findEvent(target, eventId);
458 const slot = findAllDaySlot(target, date);
459
460 // Find event position
461 await scrollTo(event);
462 const eventRect = event.getBoundingClientRect();
463 const eventPos = {
464 x: eventRect.x + eventRect.width / 2,
465 y: eventRect.y,
466 };
467 await triggerEventForCalendar(event, "mousedown", eventPos);
468
469 // Find target position
470 await scrollTo(slot);
471 const slotRect = slot.getBoundingClientRect();
472 const toPos = {
473 x: slotRect.x + slotRect.width / 2,
474 y: slotRect.y - 1,
475 };
476 await triggerEventForCalendar(slot, "mousemove", toPos);
477 await triggerEventForCalendar(slot, "mouseup", toPos);
478 await nextTick();
479 }
480
481 export async function resizeEventToTime(target, eventId, dateTime) {
482 const event = findEvent(target, eventId);
483 const [date, time] = dateTime.split(" ");
484
485 const col = findDateCol(target, date);
486 const row = findTimeRow(target, time);
487
488 // Find event position
489 await scrollTo(event);
490 await triggerEventForCalendar(event, "mouseover");
491
492 // Find event resizer
493 const resizer = event.querySelector(".fc-event-resizer-end");
494 resizer.style.display = "block";
495 resizer.style.width = "100%";
496 resizer.style.height = "1em";
497 resizer.style.bottom = "0";
498 const resizerRect = resizer.getBoundingClientRect();
499 const resizerPos = {
500 x: resizerRect.x + resizerRect.width / 2,
501 y: resizerRect.y + resizerRect.height / 2,
502 };
503 await triggerEventForCalendar(resizer, "mousedown", resizerPos);
504
505 // Find target position
506 await scrollTo(row, false);
507 const colRect = col.getBoundingClientRect();
508 const rowRect = row.getBoundingClientRect();
509 const toPos = {
510 x: colRect.x + colRect.width / 2,
511 y: rowRect.y - 1,
512 };
513
514 await triggerEventForCalendar(row, "mousemove", toPos);
515 await triggerEventForCalendar(row, "mouseup", toPos);
516 await nextTick();
517 }
518
519 export async function changeScale(target, scale) {
520 await click(target, `.o_view_scale_selector .scale_button_selection`);
521 await click(target, `.o-dropdown--menu .o_scale_button_${scale}`);
522 await nextTick();
523 }
524
525 export async function navigate(target, direction) {
526 await click(target, `.o_calendar_navigation_buttons .o_calendar_button_${direction}`);
527 }
528
529 export function findFilterPanelSection(target, sectionName) {
530 return target.querySelector(`.o_calendar_filter[data-name="${sectionName}"]`);
531 }
532
533 export function findFilterPanelFilter(target, sectionName, filterValue) {
534 return findFilterPanelSection(target, sectionName).querySelector(
535 `.o_calendar_filter_item[data-value="${filterValue}"]`
536 );
537 }
538
539 export function findFilterPanelSectionFilter(target, sectionName) {
540 return findFilterPanelSection(target, sectionName).querySelector(
541 `.o_calendar_filter_items_checkall`
542 );
543 }
544
545 export async function toggleFilter(target, sectionName, filterValue) {
546 const el = findFilterPanelFilter(target, sectionName, filterValue).querySelector(`input`);
547 await scrollTo(el);
548 await click(el);
549 }
550
551 export async function toggleSectionFilter(target, sectionName) {
552 const el = findFilterPanelSectionFilter(target, sectionName).querySelector(`input`);
553 await scrollTo(el);
554 await click(el);
555 }