-
Notifications
You must be signed in to change notification settings - Fork 418
/
Copy pathDnDSource.js
114 lines (102 loc) · 5.15 KB
/
DnDSource.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import { DragSource } from 'react-dnd'
import {ViewTypes, DATETIME_FORMAT} from './index'
import {DnDTypes} from './DnDTypes'
export default class DnDSource {
constructor(resolveDragObjFunc, DecoratedComponent, dndType = DnDTypes.EVENT) {
this.resolveDragObjFunc = resolveDragObjFunc;
this.DecoratedComponent = DecoratedComponent;
this.dndType = dndType;
this.dragSource = DragSource(this.dndType, this.getDragSpec(), this.getDragCollect)(this.DecoratedComponent);
}
getDragSpec = () => {
return {
beginDrag: (props, monitor, component) => {
return this.resolveDragObjFunc(props);
},
endDrag: (props, monitor, component) => {
if(!monitor.didDrop()) return;
const {moveEvent, newEvent, schedulerData } = props;
const {events, config, viewType, localeMoment} = schedulerData;
const item = monitor.getItem();
const type = monitor.getItemType();
const dropResult = monitor.getDropResult();
let slotId = dropResult.slotId, slotName = dropResult.slotName;
let newStart = dropResult.start, newEnd = dropResult.end;
let initialStart = dropResult.initialStart, initialEnd = dropResult.initialEnd;
let action = 'New';
let isEvent = type === DnDTypes.EVENT;
if(isEvent) {
const event = item;
if(config.relativeMove) {
newStart = localeMoment(event.start).add(localeMoment(newStart).diff(localeMoment(initialStart)), 'ms').format(DATETIME_FORMAT);
} else {
if(viewType !== ViewTypes.Day) {
let tmpMoment = localeMoment(newStart);
newStart = localeMoment(event.start).year(tmpMoment.year()).month(tmpMoment.month()).date(tmpMoment.date()).format(DATETIME_FORMAT);
}
}
newEnd = localeMoment(newStart).add(localeMoment(event.end).diff(localeMoment(event.start)), 'ms').format(DATETIME_FORMAT);
//if crossResourceMove disabled, slot returns old value
if(config.crossResourceMove === false) {
slotId = schedulerData._getEventSlotId(item);
slotName = undefined;
let slot = schedulerData.getSlotById(slotId);
if(!!slot)
slotName = slot.name;
}
action = 'Move';
}
let hasConflict = false;
if(config.checkConflict) {
let start = localeMoment(newStart),
end = localeMoment(newEnd);
events.forEach((e) =>{
if(schedulerData._getEventSlotId(e) === slotId && (!isEvent || e.id !== item.id)) {
let eStart = localeMoment(e.start),
eEnd = localeMoment(e.end);
if((start >= eStart && start < eEnd) || (end > eStart && end <= eEnd) || (eStart >= start && eStart < end) || (eEnd > start && eEnd <= end))
hasConflict = true;
}
});
}
if(hasConflict) {
const {conflictOccurred} = props;
if(conflictOccurred != undefined){
conflictOccurred(schedulerData, action, item, type, slotId, slotName, newStart, newEnd);
}
else {
console.log('Conflict occurred, set conflictOccurred func in Scheduler to handle it');
}
}
else {
if(isEvent) {
if (moveEvent !== undefined) {
moveEvent(schedulerData, item, slotId, slotName, newStart, newEnd);
}
}
else {
if(newEvent !== undefined)
newEvent(schedulerData, slotId, slotName, newStart, newEnd, type, item);
}
}
},
canDrag: (props) => {
const {schedulerData, resourceEvents} = props;
const item = this.resolveDragObjFunc(props);
if(schedulerData._isResizing()) return false;
const {config} = schedulerData;
return config.movable && (resourceEvents == undefined || !resourceEvents.groupOnly) && (item.movable == undefined || item.movable !== false);
}
}
}
getDragCollect = (connect, monitor) => {
return {
connectDragSource: connect.dragSource(),
isDragging: monitor.isDragging(),
connectDragPreview: connect.dragPreview()
};
}
getDragSource = () => {
return this.dragSource;
}
}