| 1 |
import { ViewApi, DatePointApi } from '@fullcalendar/core' |
| 2 |
import { |
| 3 |
Hit, |
| 4 |
interactionSettingsStore, |
| 5 |
PointerDragEvent, |
| 6 |
parseEventDef, createEventInstance, EventTuple, |
| 7 |
createEmptyEventStore, eventTupleToStore, |
| 8 |
config, |
| 9 |
DateSpan, |
| 10 |
EventInteractionState, |
| 11 |
DragMetaInput, DragMeta, parseDragMeta, |
| 12 |
elementMatches, |
| 13 |
enableCursor, disableCursor, |
| 14 |
isInteractionValid, |
| 15 |
ElementDragging, |
| 16 |
CalendarContext, |
| 17 |
getDefaultEventEnd, |
| 18 |
refineEventDef, |
| 19 |
EventImpl, |
| 20 |
} from '@fullcalendar/core/internal' |
| 21 |
import { HitDragging } from '../interactions/HitDragging.js' |
| 22 |
import { buildDatePointApiWithContext } from '../utils.js' |
| 23 |
|
| 24 |
export type DragMetaGenerator = DragMetaInput | ((el: HTMLElement) => DragMetaInput) |
| 25 |
|
| 26 |
export interface ExternalDropApi extends DatePointApi { |
| 27 |
draggedEl: HTMLElement |
| 28 |
jsEvent: UIEvent |
| 29 |
view: ViewApi |
| 30 |
} |
| 31 |
|
| 32 |
/* |
| 33 |
Given an already instantiated draggable object for one-or-more elements, |
| 34 |
Interprets any dragging as an attempt to drag an events that lives outside |
| 35 |
of a calendar onto a calendar. |
| 36 |
*/ |
| 37 |
export class ExternalElementDragging { |
| 38 |
hitDragging: HitDragging |
| 39 |
receivingContext: CalendarContext | null = null |
| 40 |
droppableEvent: EventTuple | null = null // will exist for all drags, even if create:false |
| 41 |
suppliedDragMeta: DragMetaGenerator | null = null |
| 42 |
dragMeta: DragMeta | null = null |
| 43 |
|
| 44 |
constructor(dragging: ElementDragging, suppliedDragMeta?: DragMetaGenerator) { |
| 45 |
let hitDragging = this.hitDragging = new HitDragging(dragging, interactionSettingsStore) |
| 46 |
hitDragging.requireInitial = false // will start outside of a component |
| 47 |
hitDragging.emitter.on('dragstart', this.handleDragStart) |
| 48 |
hitDragging.emitter.on('hitupdate', this.handleHitUpdate) |
| 49 |
hitDragging.emitter.on('dragend', this.handleDragEnd) |
| 50 |
|
| 51 |
this.suppliedDragMeta = suppliedDragMeta |
| 52 |
} |
| 53 |
|
| 54 |
handleDragStart = (ev: PointerDragEvent) => { |
| 55 |
this.dragMeta = this.buildDragMeta(ev.subjectEl as HTMLElement) |
| 56 |
} |
| 57 |
|
| 58 |
buildDragMeta(subjectEl: HTMLElement) { |
| 59 |
if (typeof this.suppliedDragMeta === 'object') { |
| 60 |
return parseDragMeta(this.suppliedDragMeta) |
| 61 |
} |
| 62 |
if (typeof this.suppliedDragMeta === 'function') { |
| 63 |
return parseDragMeta(this.suppliedDragMeta(subjectEl)) |
| 64 |
} |
| 65 |
return getDragMetaFromEl(subjectEl) |
| 66 |
} |
| 67 |
|
| 68 |
handleHitUpdate = (hit: Hit | null, isFinal: boolean, ev: PointerDragEvent) => { |
| 69 |
let { dragging } = this.hitDragging |
| 70 |
let receivingContext: CalendarContext | null = null |
| 71 |
let droppableEvent: EventTuple | null = null |
| 72 |
let isInvalid = false |
| 73 |
let interaction: EventInteractionState = { |
| 74 |
affectedEvents: createEmptyEventStore(), |
| 75 |
mutatedEvents: createEmptyEventStore(), |
| 76 |
isEvent: this.dragMeta!.create, |
| 77 |
} |
| 78 |
|
| 79 |
if (hit) { |
| 80 |
receivingContext = hit.context |
| 81 |
|
| 82 |
if (this.canDropElOnCalendar(ev.subjectEl as HTMLElement, receivingContext)) { |
| 83 |
droppableEvent = computeEventForDateSpan( |
| 84 |
hit.dateSpan, |
| 85 |
this.dragMeta!, |
| 86 |
receivingContext, |
| 87 |
) |
| 88 |
|
| 89 |
interaction.mutatedEvents = eventTupleToStore(droppableEvent) |
| 90 |
isInvalid = !isInteractionValid(interaction, hit.dateProfile, receivingContext) |
| 91 |
|
| 92 |
if (isInvalid) { |
| 93 |
interaction.mutatedEvents = createEmptyEventStore() |
| 94 |
droppableEvent = null |
| 95 |
} |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
this.displayDrag(receivingContext, interaction) |
| 100 |
|
| 101 |
// show mirror if no already-rendered mirror element OR if we are shutting down the mirror (?) |
| 102 |
// TODO: wish we could somehow wait for dispatch to guarantee render |
| 103 |
dragging.setMirrorIsVisible( |
| 104 |
isFinal || !droppableEvent || !document.querySelector('.fc-event-mirror'), // TODO: turn className into constant |
| 105 |
// TODO: somehow query FullCalendars WITHIN shadow-roots for existing event-mirror els |
| 106 |
) |
| 107 |
|
| 108 |
if (!isInvalid) { |
| 109 |
enableCursor() |
| 110 |
} else { |
| 111 |
disableCursor() |
| 112 |
} |
| 113 |
|
| 114 |
if (!isFinal) { |
| 115 |
dragging.setMirrorNeedsRevert(!droppableEvent) |
| 116 |
|
| 117 |
this.receivingContext = receivingContext |
| 118 |
this.droppableEvent = droppableEvent |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
handleDragEnd = (pev: PointerDragEvent) => { |
| 123 |
let { receivingContext, droppableEvent } = this |
| 124 |
|
| 125 |
this.clearDrag() |
| 126 |
|
| 127 |
if (receivingContext && droppableEvent) { |
| 128 |
let finalHit = this.hitDragging.finalHit! |
| 129 |
let finalView = finalHit.context.viewApi |
| 130 |
let dragMeta = this.dragMeta! |
| 131 |
|
| 132 |
receivingContext.emitter.trigger('drop', { |
| 133 |
...buildDatePointApiWithContext(finalHit.dateSpan, receivingContext), |
| 134 |
draggedEl: pev.subjectEl as HTMLElement, |
| 135 |
jsEvent: pev.origEvent as MouseEvent, // Is this always a mouse event? See #4655 |
| 136 |
view: finalView, |
| 137 |
}) |
| 138 |
|
| 139 |
if (dragMeta.create) { |
| 140 |
let addingEvents = eventTupleToStore(droppableEvent) |
| 141 |
|
| 142 |
receivingContext.dispatch({ |
| 143 |
type: 'MERGE_EVENTS', |
| 144 |
eventStore: addingEvents, |
| 145 |
}) |
| 146 |
|
| 147 |
if (pev.isTouch) { |
| 148 |
receivingContext.dispatch({ |
| 149 |
type: 'SELECT_EVENT', |
| 150 |
eventInstanceId: droppableEvent.instance.instanceId, |
| 151 |
}) |
| 152 |
} |
| 153 |
|
| 154 |
// signal that an external event landed |
| 155 |
receivingContext.emitter.trigger('eventReceive', { |
| 156 |
event: new EventImpl( |
| 157 |
receivingContext, |
| 158 |
droppableEvent.def, |
| 159 |
droppableEvent.instance, |
| 160 |
), |
| 161 |
relatedEvents: [], |
| 162 |
revert() { |
| 163 |
receivingContext.dispatch({ |
| 164 |
type: 'REMOVE_EVENTS', |
| 165 |
eventStore: addingEvents, |
| 166 |
}) |
| 167 |
}, |
| 168 |
draggedEl: pev.subjectEl as HTMLElement, |
| 169 |
view: finalView, |
| 170 |
}) |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
this.receivingContext = null |
| 175 |
this.droppableEvent = null |
| 176 |
} |
| 177 |
|
| 178 |
displayDrag(nextContext: CalendarContext | null, state: EventInteractionState) { |
| 179 |
let prevContext = this.receivingContext |
| 180 |
|
| 181 |
if (prevContext && prevContext !== nextContext) { |
| 182 |
prevContext.dispatch({ type: 'UNSET_EVENT_DRAG' }) |
| 183 |
} |
| 184 |
|
| 185 |
if (nextContext) { |
| 186 |
nextContext.dispatch({ type: 'SET_EVENT_DRAG', state }) |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
clearDrag() { |
| 191 |
if (this.receivingContext) { |
| 192 |
this.receivingContext.dispatch({ type: 'UNSET_EVENT_DRAG' }) |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
canDropElOnCalendar(el: HTMLElement, receivingContext: CalendarContext): boolean { |
| 197 |
let dropAccept = receivingContext.options.dropAccept |
| 198 |
|
| 199 |
if (typeof dropAccept === 'function') { |
| 200 |
return dropAccept.call(receivingContext.calendarApi, el) |
| 201 |
} |
| 202 |
|
| 203 |
if (typeof dropAccept === 'string' && dropAccept) { |
| 204 |
return Boolean(elementMatches(el, dropAccept)) |
| 205 |
} |
| 206 |
|
| 207 |
return true |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
// Utils for computing event store from the DragMeta |
| 212 |
// ---------------------------------------------------------------------------------------------------- |
| 213 |
|
| 214 |
function computeEventForDateSpan(dateSpan: DateSpan, dragMeta: DragMeta, context: CalendarContext): EventTuple { |
| 215 |
let defProps = { ...dragMeta.leftoverProps } |
| 216 |
|
| 217 |
for (let transform of context.pluginHooks.externalDefTransforms) { |
| 218 |
Object.assign(defProps, transform(dateSpan, dragMeta)) |
| 219 |
} |
| 220 |
|
| 221 |
let { refined, extra } = refineEventDef(defProps, context) |
| 222 |
let def = parseEventDef( |
| 223 |
refined, |
| 224 |
extra, |
| 225 |
dragMeta.sourceId, |
| 226 |
dateSpan.allDay, |
| 227 |
context.options.forceEventDuration || Boolean(dragMeta.duration), // hasEnd |
| 228 |
context, |
| 229 |
) |
| 230 |
|
| 231 |
let start = dateSpan.range.start |
| 232 |
|
| 233 |
// only rely on time info if drop zone is all-day, |
| 234 |
// otherwise, we already know the time |
| 235 |
if (dateSpan.allDay && dragMeta.startTime) { |
| 236 |
start = context.dateEnv.add(start, dragMeta.startTime) |
| 237 |
} |
| 238 |
|
| 239 |
let end = dragMeta.duration ? |
| 240 |
context.dateEnv.add(start, dragMeta.duration) : |
| 241 |
getDefaultEventEnd(dateSpan.allDay, start, context) |
| 242 |
|
| 243 |
let instance = createEventInstance(def.defId, { start, end }) |
| 244 |
|
| 245 |
return { def, instance } |
| 246 |
} |
| 247 |
|
| 248 |
// Utils for extracting data from element |
| 249 |
// ---------------------------------------------------------------------------------------------------- |
| 250 |
|
| 251 |
function getDragMetaFromEl(el: HTMLElement): DragMeta { |
| 252 |
let str = getEmbeddedElData(el, 'event') |
| 253 |
let obj = str ? |
| 254 |
JSON.parse(str) : |
| 255 |
{ create: false } // if no embedded data, assume no event creation |
| 256 |
|
| 257 |
return parseDragMeta(obj) |
| 258 |
} |
| 259 |
|
| 260 |
config.dataAttrPrefix = '' |
| 261 |
|
| 262 |
function getEmbeddedElData(el: HTMLElement, name: string): string { |
| 263 |
let prefix = config.dataAttrPrefix |
| 264 |
let prefixedName = (prefix ? prefix + '-' : '') + name |
| 265 |
|
| 266 |
return el.getAttribute('data-' + prefixedName) || '' |
| 267 |
} |