| 1 |
import { loadBundle } from "@web/core/assets"; |
| 2 |
|
| 3 |
import { onMounted, onPatched, onWillStart, onWillUnmount, useComponent, useRef } from "@odoo/owl"; |
| 4 |
|
| 5 |
export function useFullCalendar(refName, params) { |
| 6 |
const component = useComponent(); |
| 7 |
const ref = useRef(refName); |
| 8 |
let instance = null; |
| 9 |
|
| 10 |
function boundParams() { |
| 11 |
const newParams = {}; |
| 12 |
for (const key in params) { |
| 13 |
const value = params[key]; |
| 14 |
newParams[key] = typeof value === "function" ? value.bind(component) : value; |
| 15 |
} |
| 16 |
return newParams; |
| 17 |
} |
| 18 |
|
| 19 |
onWillStart(async () => await loadBundle("web.fullcalendar_lib")); |
| 20 |
|
| 21 |
onMounted(() => { |
| 22 |
try { |
| 23 |
instance = new FullCalendar.Calendar(ref.el, boundParams()); |
| 24 |
instance.render(); |
| 25 |
} catch (e) { |
| 26 |
throw new Error(`Cannot instantiate FullCalendar\n${e.message}`); |
| 27 |
} |
| 28 |
}); |
| 29 |
|
| 30 |
onPatched(() => { |
| 31 |
instance.refetchEvents(); |
| 32 |
instance.setOption("weekends", component.props.isWeekendVisible); |
| 33 |
if (params.weekNumbers && component.props.model.scale === "year") { |
| 34 |
instance.destroy(); |
| 35 |
instance.render(); |
| 36 |
} |
| 37 |
}); |
| 38 |
onWillUnmount(() => { |
| 39 |
instance.destroy(); |
| 40 |
}); |
| 41 |
|
| 42 |
return { |
| 43 |
get api() { |
| 44 |
return instance; |
| 45 |
}, |
| 46 |
get el() { |
| 47 |
return ref.el; |
| 48 |
}, |
| 49 |
}; |
| 50 |
} |