| 1 |
import { useComponent, useEffect, useExternalListener, useRef } from "@odoo/owl"; |
| 2 |
import { makeDraggableHook } from "@web/core/utils/draggable_hook_builder_owl"; |
| 3 |
import { shallowEqual } from "@web/core/utils/objects"; |
| 4 |
import { closest } from "@web/core/utils/ui"; |
| 5 |
import { useCallbackRecorder } from "@web/search/action_hook"; |
| 6 |
|
| 7 |
const CELL_SELECTOR = `.fc-day:not(.fc-col-header-cell)`; |
| 8 |
const ROW_SELECTOR = `tr[role="row"]`; |
| 9 |
const EVENT_CONTAINER_SELECTOR = ".fc-daygrid-event-harness"; |
| 10 |
const IGNORE_SELECTOR = [".fc-event", ".fc-more-cell", ".fc-more-popover"].join(","); |
| 11 |
|
| 12 |
function getClosestCell(ctx) { |
| 13 |
const { pointer, ref } = ctx; |
| 14 |
return closest(ref.el.querySelectorAll(CELL_SELECTOR), pointer); |
| 15 |
} |
| 16 |
|
| 17 |
function getElementIndex(element) { |
| 18 |
return [].indexOf.call(element?.parentNode.children || [], element); |
| 19 |
} |
| 20 |
|
| 21 |
function getCoordinates(cell) { |
| 22 |
const colIndex = getElementIndex(cell); |
| 23 |
const rowIndex = getElementIndex(cell.closest(ROW_SELECTOR)); |
| 24 |
return { colIndex, rowIndex }; |
| 25 |
} |
| 26 |
|
| 27 |
function getBlockBounds({ initCoord, coord }) { |
| 28 |
const [startColIndex, endColIndex] = [initCoord.colIndex, coord.colIndex].sort((a, b) => a - b); |
| 29 |
const [startRowIndex, endRowIndex] = [initCoord.rowIndex, coord.rowIndex].sort((a, b) => a - b); |
| 30 |
return { startColIndex, endColIndex, startRowIndex, endRowIndex }; |
| 31 |
} |
| 32 |
|
| 33 |
function getSelectedCellsInBlock(ctx) { |
| 34 |
const { cellIsSelectable, current, ref } = ctx; |
| 35 |
const { startColIndex, endColIndex, startRowIndex, endRowIndex } = getBlockBounds(current); |
| 36 |
const selectedCells = []; |
| 37 |
for (const cell of ref.el.querySelectorAll(`tbody tr[role="row"] .fc-day`)) { |
| 38 |
const { colIndex, rowIndex } = getCoordinates(cell); |
| 39 |
if ( |
| 40 |
startColIndex <= colIndex && |
| 41 |
colIndex <= endColIndex && |
| 42 |
startRowIndex <= rowIndex && |
| 43 |
rowIndex <= endRowIndex && |
| 44 |
cellIsSelectable(cell) |
| 45 |
) { |
| 46 |
selectedCells.push(cell); |
| 47 |
} |
| 48 |
} |
| 49 |
return { selectedCells }; |
| 50 |
} |
| 51 |
|
| 52 |
function getSelectedCellsBetween2Cells(ctx, prevCell, cellClicked) { |
| 53 |
const { cellIsSelectable, ref } = ctx; |
| 54 |
const cells = [...ref.el.querySelectorAll(`tbody tr[role="row"] .fc-day`)]; |
| 55 |
const index1 = cells.indexOf(prevCell); |
| 56 |
if (index1 === -1) { |
| 57 |
return new Set([cellClicked]); |
| 58 |
} |
| 59 |
const index2 = cells.indexOf(cellClicked); |
| 60 |
const [startIndex, endIndex] = [index1, index2].sort((a, b) => a - b); |
| 61 |
return new Set(cells.slice(startIndex, endIndex + 1).filter((cell) => cellIsSelectable(cell))); |
| 62 |
} |
| 63 |
|
| 64 |
// @ts-ignore |
| 65 |
const useBlockSelection = makeDraggableHook({ |
| 66 |
name: "useBlockSelection", |
| 67 |
acceptedParams: { |
| 68 |
cellIsSelectable: [Function], |
| 69 |
}, |
| 70 |
onComputeParams({ ctx, params }) { |
| 71 |
ctx.followCursor = false; |
| 72 |
ctx.cellIsSelectable = params.cellIsSelectable; |
| 73 |
}, |
| 74 |
onWillStartDrag({ addClass, ctx }) { |
| 75 |
const { current, ref } = ctx; |
| 76 |
addClass(ref.el, "pe-auto"); |
| 77 |
const cell = getClosestCell(ctx); |
| 78 |
addClass(cell, "pe-auto"); |
| 79 |
const coord = getCoordinates(cell); |
| 80 |
current.initCoord = coord; |
| 81 |
current.coord = coord; |
| 82 |
return getSelectedCellsInBlock(ctx); |
| 83 |
}, |
| 84 |
onDragStart({ ctx }) { |
| 85 |
return getSelectedCellsInBlock(ctx); |
| 86 |
}, |
| 87 |
onDrag({ ctx }) { |
| 88 |
const { current } = ctx; |
| 89 |
const cell = getClosestCell(ctx); |
| 90 |
const coord = getCoordinates(cell); |
| 91 |
if (shallowEqual(current.coord, coord)) { |
| 92 |
return; |
| 93 |
} |
| 94 |
current.coord = coord; |
| 95 |
return getSelectedCellsInBlock(ctx); |
| 96 |
}, |
| 97 |
onDrop({ ctx }) { |
| 98 |
return getSelectedCellsInBlock(ctx); |
| 99 |
}, |
| 100 |
}); |
| 101 |
|
| 102 |
export function useSquareSelection(params = {}) { |
| 103 |
const cellIsSelectable = params.cellIsSelectable || (() => true); |
| 104 |
const component = useComponent(); |
| 105 |
const ref = useRef("fullCalendar"); |
| 106 |
const highlightClass = "o-highlight"; |
| 107 |
|
| 108 |
const removeHighlight = () => { |
| 109 |
ref.el.querySelectorAll(`.${highlightClass}`).forEach((node) => { |
| 110 |
node.classList.remove(highlightClass); |
| 111 |
}); |
| 112 |
}; |
| 113 |
|
| 114 |
let allSelectedCells = new Set(); |
| 115 |
const getAllCells = (cells, action) => { |
| 116 |
cells = new Set(cells); |
| 117 |
switch (action) { |
| 118 |
case "add": |
| 119 |
return allSelectedCells.union(cells); |
| 120 |
case "toggle": |
| 121 |
return allSelectedCells.symmetricDifference(cells); |
| 122 |
case "replace": |
| 123 |
return cells; |
| 124 |
} |
| 125 |
}; |
| 126 |
|
| 127 |
const highlight = ({ selectedCells }) => { |
| 128 |
removeHighlight(); |
| 129 |
selectedCells.forEach((node) => { |
| 130 |
node.classList.add(highlightClass); |
| 131 |
}); |
| 132 |
}; |
| 133 |
|
| 134 |
useCallbackRecorder(component.props.callbackRecorder, () => { |
| 135 |
allSelectedCells = new Set(); |
| 136 |
prevSelectedCell = null; |
| 137 |
removeHighlight(); |
| 138 |
}); |
| 139 |
|
| 140 |
let action = null; |
| 141 |
let prevSelectedCell = null; |
| 142 |
const update = ({ selectedCells }) => { |
| 143 |
const allSelectedCells = getAllCells(selectedCells, action); |
| 144 |
highlight({ selectedCells: allSelectedCells }); |
| 145 |
}; |
| 146 |
|
| 147 |
const selectState = useBlockSelection({ |
| 148 |
enable: () => component.props.model.hasMultiCreate, |
| 149 |
ignore: EVENT_CONTAINER_SELECTOR, |
| 150 |
elements: CELL_SELECTOR, |
| 151 |
ref, |
| 152 |
edgeScrolling: { speed: 40, threshold: 150 }, |
| 153 |
cellIsSelectable, |
| 154 |
onDragStart: ({ selectedCells }) => { |
| 155 |
prevSelectedCell = null; |
| 156 |
action = ctrlPressed ? "add" : "replace"; |
| 157 |
update({ selectedCells }); |
| 158 |
}, |
| 159 |
onDrag: update, |
| 160 |
onDrop: ({ selectedCells }) => { |
| 161 |
allSelectedCells = getAllCells(selectedCells, action); |
| 162 |
action = null; |
| 163 |
highlight({ selectedCells: allSelectedCells }); |
| 164 |
component.props.onSquareSelection([...allSelectedCells]); |
| 165 |
}, |
| 166 |
}); |
| 167 |
|
| 168 |
const onClick = (ev) => { |
| 169 |
if (selectState.dragging) { |
| 170 |
return; |
| 171 |
} |
| 172 |
const ignoreElement = ev.target.closest(IGNORE_SELECTOR); |
| 173 |
if (ignoreElement) { |
| 174 |
return; |
| 175 |
} |
| 176 |
const eventContainer = ev.target.closest(EVENT_CONTAINER_SELECTOR); |
| 177 |
if (eventContainer) { |
| 178 |
return; |
| 179 |
} |
| 180 |
const cell = ev.target.closest(CELL_SELECTOR); |
| 181 |
if (!cell) { |
| 182 |
return; |
| 183 |
} |
| 184 |
const coord = getCoordinates(cell); |
| 185 |
const current = { initCoord: coord, coord }; |
| 186 |
const pseudoCtx = { current, ref, cellIsSelectable }; |
| 187 |
const { selectedCells } = getSelectedCellsInBlock(pseudoCtx); |
| 188 |
const selectedCell = selectedCells[0]; |
| 189 |
if (prevSelectedCell && shiftPressed) { |
| 190 |
allSelectedCells = getSelectedCellsBetween2Cells( |
| 191 |
pseudoCtx, |
| 192 |
prevSelectedCell, |
| 193 |
selectedCell |
| 194 |
); |
| 195 |
} else { |
| 196 |
const action = ctrlPressed ? "toggle" : "replace"; |
| 197 |
allSelectedCells = getAllCells(selectedCells, action); |
| 198 |
} |
| 199 |
if (!prevSelectedCell || !shiftPressed) { |
| 200 |
prevSelectedCell = selectedCell; |
| 201 |
} |
| 202 |
highlight({ selectedCells: allSelectedCells }); |
| 203 |
component.props.onSquareSelection([...allSelectedCells]); |
| 204 |
}; |
| 205 |
|
| 206 |
useEffect( |
| 207 |
(el, hasMultiCreate) => { |
| 208 |
if (!hasMultiCreate) { |
| 209 |
return; |
| 210 |
} |
| 211 |
el && el.addEventListener("click", onClick); |
| 212 |
return () => { |
| 213 |
el && el.removeEventListener("click", onClick); |
| 214 |
}; |
| 215 |
}, |
| 216 |
() => [ref.el, component.props.model.hasMultiCreate] |
| 217 |
); |
| 218 |
|
| 219 |
let ctrlPressed = false; |
| 220 |
let shiftPressed = false; |
| 221 |
function onWindowKeyDown(ev) { |
| 222 |
if (ev.key === "Control") { |
| 223 |
ctrlPressed = true; |
| 224 |
} else if (ev.key === "Shift") { |
| 225 |
shiftPressed = true; |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
function onWindowKeyUp(ev) { |
| 230 |
if (ev.key === "Control") { |
| 231 |
ctrlPressed = false; |
| 232 |
} else if (ev.key === "Shift") { |
| 233 |
shiftPressed = false; |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
useExternalListener(window, "keydown", onWindowKeyDown); |
| 238 |
useExternalListener(window, "keyup", onWindowKeyUp); |
| 239 |
} |