EventDrag
Feature
Allows user to drag and drop events within the Scheduler Pro, to change startDate or resource assignment. This class extends the Scheduler's EventDrag feature for use in Scheduler Pro.
const schedulerPro = new SchedulerPro({ appendTo : targetElement, // makes scheduler as high as it needs to be to fit rows autoHeight : true, startDate : new Date(2018, 4, 6), endDate : new Date(2018, 4, 13), columns : [ { field : 'name', text : 'Name', width : 100 } ], resources : [ { id : 1, name : 'Bernard' }, { id : 2, name : 'Bianca' } ], events : [ { id : 1, resourceId : 1, name : 'Drag me', startDate : '2018-05-06', endDate : '2018-05-07' }, { id : 2, resourceId : 2, name : 'Or me', startDate : '2018-05-07', endDate : '2018-05-07' }, { id : 3, resourceId : 2, name : 'But not me', startDate : '2018-05-08', endDate : '2018-05-10', draggable : false, eventColor : 'red' } ] }); This feature is enabled by default.
Customizing the drag drop tooltip
To show custom HTML in the tooltip, please see the tooltipTemplate config. Example:
features: {
eventDrag : {
// A minimal start date tooltip
tooltipTemplate : ({ eventRecord, startDate }) => {
return DateHelper.format(startDate, 'HH:mm');
}
}
}
Constraining the drag drop area
You can constrain how the dragged event is allowed to move by using the following configs
- constrainDragToResource Resource fixed, only allowed to change start date
- constrainDragToTimeSlot Start date is fixed, only move between resources
- getDateConstraints A method on the Scheduler instance which lets you define the date range for the dragged event programmatically
// Enable dragging + constrain drag to current resource
const scheduler = new SchedulerPro({
features : {
eventDrag : {
constrainDragToResource : true
}
}
});
Drag drop events from outside
Dragging unplanned events from an external grid is a very popular use case. There are several demos showing you how to do this. Please see the Drag from grid demo and study the Drag from grid guide to learn more.
Drag drop events to outside target
You can also drag events outside the schedule area by setting constrainDragToTimeline to false. You should also either:
- provide a validatorFn to programmatically define if a drop location is valid or not
- configure a externalDropTargetSelector CSS selector to define where drops are allowed
See this demo to see this in action.
Validating drag drop
It is easy to programmatically decide what is a valid drag drop operation. Use the validatorFn and return either true / false (optionally a message to show to the user).
features : {
eventDrag : {
validatorFn({ eventRecords, newResource }) {
const task = eventRecords[0],
valid = newResource.role === task.resource.role;
return {
valid : newResource.role === task.resource.role,
message : valid ? '' : 'Resource role does not match required role for this task'
};
}
}
}
See this demo to see validation in action.
If you instead want to do a single validation upon drop, you can listen to beforeEventDropFinalize and set the valid flag on the context object provided.
const scheduler = new SchedulerPro({
listeners : {
beforeEventDropFinalize({ context }) {
const { eventRecords } = context;
// Don't allow dropping events in the past
context.valid = Date.now() <= eventRecords[0].startDate;
}
}
});
Preventing drag of certain events
To prevent certain events from being dragged, you have two options. You can set draggable to false in your data, or you can listen for the beforeEventDrag event and return false to block the drag.
new SchedulerPro({
listeners : {
beforeEventDrag({ eventRecord }) {
// Don't allow dragging events that have already started
return Date.now() <= eventRecord.startDate;
}
}
})
Useful configs and functions
| Member | Description |
|---|---|
| beforeEventDrag | Fires before drag starts, return false to prevent |
| eventDragStart | Fires when event drag begins |
| eventDrag | Fires continuously during drag |
| eventDrop | Fires when event is dropped at new position |
| beforeEventDropFinalize | Async finalizer before drop is applied |
| tooltipTemplate | Template for the drag tooltip |
Please see also the event model whose dates are updated on drag
Configs
Configs are options you supply in a configuration object when creating an instance of this class-
Set to
falseto allow dragging tasks outside the client Scheduler. Useful when you want to drag tasks between multiple Scheduler instances -
An object used to configure the internal DragHelper class
-
When enabled, the event being dragged always "snaps" to the exact start date that it will have after drop.
-
Specifies whether to show tooltip while dragging event
Has a corresponding runtime showTooltip property.
-
The
eventDragandtaskDragevents are normally only triggered when the drag operation will lead to a change in date or assignment. By setting this config tofalse, that logic is bypassed to trigger events for each native mouse move event.Has a corresponding runtime throttleDragEvent property.
-
Set this to
trueto always copy the event on drag drop operation.Has a corresponding runtime alwaysCopy property.
-
Set to true to only allow dragging events within the same resource.
Has a corresponding runtime constrainDragToResource property.
-
Set to true to only allow dragging events to different resources, and disallow rescheduling by dragging.
Has a corresponding runtime constrainDragToTimeSlot property.
-
A modifier key (CTRL, SHIFT, ALT, META) that when pressed will copy an event instead of moving it. Set to empty string to disable copying. Defaults to "CTRL" which is the Ctrl-key for Windows, and Meta-key for MacOS.
Has a corresponding runtime copyKey property.
-
Event can be copied two ways: either by adding new assignment to an existing event ('assignment'), or by copying the event itself ('event'). 'auto' mode will pick 'event' for a single-assignment mode (when event has
resourceIdfield) and 'assignment' mode otherwise.Has a corresponding runtime copyMode property.
-
A CSS selector specifying elements outside the scheduler element which are valid drop targets.
-
Set to
trueto only allow dragging in one direction (based on initial movement)Has a corresponding runtime singleDirection property.
-
The
thisreference for the validatorFn -
Internal listeners, that cannot be removed by the user.
-
The widget which this plugin is to attach to.
Has a corresponding runtime client property.
-
Set to
falseto disable localization of this object.
Properties
Properties are getters/setters or publicly accessible variables on this class-
Identifies an object as an instance of EventDrag class, or subclass thereof.
-
Identifies an object as an instance of Events class, or subclass thereof.
-
Identifies an object as an instance of Localizable class, or subclass thereof.
-
A class property getter for the default values of internal properties for this class.
-
An empty array that can be used as a default value.
-
An empty object that can be used as a default value.
-
Identifies an object as an instance of DragBase class, or subclass thereof.
-
Identifies an object as an instance of EventDrag class, or subclass thereof.
-
Identifies an object as an instance of InstancePlugin class, or subclass thereof.
-
Returns true if a drag operation is active
-
Specifies whether to show tooltip while dragging event
Has a corresponding showTooltip config.
-
The
eventDragandtaskDragevents are normally only triggered when the drag operation will lead to a change in date or assignment. By setting this config tofalse, that logic is bypassed to trigger events for each native mouse move event.Has a corresponding throttleDragEvent config.
-
Set this to
trueto always copy the event on drag drop operation.Has a corresponding alwaysCopy config.
-
Set to true to only allow dragging events within the same resource.
Has a corresponding constrainDragToResource config.
-
Set to true to only allow dragging events to different resources, and disallow rescheduling by dragging.
Has a corresponding constrainDragToTimeSlot config.
-
A modifier key (CTRL, SHIFT, ALT, META) that when pressed will copy an event instead of moving it. Set to empty string to disable copying. Defaults to "CTRL" which is the Ctrl-key for Windows, and Meta-key for MacOS.
Has a corresponding copyKey config.
-
Event can be copied two ways: either by adding new assignment to an existing event ('assignment'), or by copying the event itself ('event'). 'auto' mode will pick 'event' for a single-assignment mode (when event has
resourceIdfield) and 'assignment' mode otherwise.Has a corresponding copyMode config.
-
Mode of the current drag drop operation.
-
Set to
trueto only allow dragging in one direction (based on initial movement)Has a corresponding singleDirection config.
-
Returns a copy of the full configuration which was used to configure this object.
-
This property is set to
truebefore theconstructorreturns. -
This property is set to
trueon entry to the destroy method. It remains on the objects after returning fromdestroy(). If isDestroyed istrue, this property will also betrue, so there is no need to test for both (for example,comp.isDestroying || comp.isDestroyed). -
The Widget which was passed into the constructor, which is the Widget we are providing extra services for.
Has a corresponding client config.
-
Get the global LocaleHelper
-
Get the global LocaleManager
Functions
Functions are methods available for calling on the class-
This optional class method is called when a class is mixed in using the mixin() method.
-
Registers this class type with its Factory
-
Gets html to display in tooltip while dragging event. Uses clockTemplate to display start & end dates.
-
Triggered while dragging an event. Updates drag data, validation etc.
-
Triggered when dragging of an event starts. Initializes drag data associated with the event being dragged.
-
Triggered when dropping an event. Finalizes the operation.
-
Triggered internally on invalid drop.
-
Called when scheduler is rendered. Sets up drag and drop and hover tooltip.
-
Updates drag data's dates and validity (calls #validatorFn if specified)
-
Update assignments being dragged
-
Internal function used to hook destroy() calls when using thisObj
-
Internal function used restore hooked destroy() calls when using thisObj
-
Auto detaches listeners registered from start, if set as detachable
-
Internal function used to run a callback function after an event is triggered
-
Removes all listeners registered to this object by the application.
-
This will merge a feature's (subclass of InstancePlugin) keyMap with it's client's keyMap.