| 1 |
/* ************************************************************************ */ |
| 2 |
/* */ |
| 3 |
/* This file is part of Frama-C. */ |
| 4 |
/* */ |
| 5 |
/* Copyright (C) 2007-2022 */ |
| 6 |
/* CEA (Commissariat à l'énergie atomique et aux énergies */ |
| 7 |
/* alternatives) */ |
| 8 |
/* */ |
| 9 |
/* you can redistribute it and/or modify it under the terms of the GNU */ |
| 10 |
/* Lesser General Public License as published by the Free Software */ |
| 11 |
/* Foundation, version 2.1. */ |
| 12 |
/* */ |
| 13 |
/* It is distributed in the hope that it will be useful, */ |
| 14 |
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */ |
| 15 |
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ |
| 16 |
/* GNU Lesser General Public License for more details. */ |
| 17 |
/* */ |
| 18 |
/* See the GNU Lesser General Public License version 2.1 */ |
| 19 |
/* for more details (enclosed in the file licenses/LGPLv2.1). */ |
| 20 |
/* */ |
| 21 |
/* ************************************************************************ */ |
| 22 |
|
| 23 |
/** |
| 24 |
@packageDocumentation |
| 25 |
@module dome/dnd |
| 26 |
@description |
| 27 |
|
| 28 |
## Drag & Drop |
| 29 |
|
| 30 |
A Drag & Drop sequence is monitored by a D&D controller. |
| 31 |
Drag Sources initiates the a D&D sequence, and the D&D controller will then |
| 32 |
notify the Drop Target when they are dragged over. |
| 33 |
|
| 34 |
More precisely, the D&D controller is notified with all events of any D&D sequence. |
| 35 |
Drag sources are notified will all events of the D&D sequences they have initiated. |
| 36 |
Drop targets are only notified when they are actually participating into some D&D sequence. |
| 37 |
For instance, when the dragged item directly moves from one target into another one, both |
| 38 |
targets will be notified, but not the others. |
| 39 |
|
| 40 |
|
| 41 |
## Dragging Events |
| 42 |
|
| 43 |
Many dragging events are triggered during a D&D sequence. |
| 44 |
All event listeners will be notified with a single argument, |
| 45 |
which is a `Dragging` object with the following properties: |
| 46 |
- `move`: the kind of move (see below); |
| 47 |
- `meta`: a boolean indicating whether Ctrl, Alt or Meta key is pressed during the move; |
| 48 |
- `held`: a boolean indicating whether the mouse is holding over position; |
| 49 |
- `sourceClientRect`: the position of the drag source element; |
| 50 |
- `targetClientRect`: the position of the drop target element; |
| 51 |
- `deltaX`, `deltaY`: the current offset position of the dragged element, |
| 52 |
relative to its _initial_ position; |
| 53 |
- `clientX`, `clientY`: the current client position of the _cursor_, as obtained from the dragging event; |
| 54 |
- `position`: the current position from React-Draggable API (see DragSource documentation). |
| 55 |
|
| 56 |
All coordinates are provided in the window coordinate system, |
| 57 |
as returned by the `getBoundingClientRect` DOM method. |
| 58 |
|
| 59 |
|
| 60 |
## Dragging State |
| 61 |
|
| 62 |
During the entire sequence, the D&D controller is responsible for managing a shared |
| 63 |
state among source and targets. |
| 64 |
The drag source and each drop targets may update the shared state when receiving |
| 65 |
events, and will be re-rendered in turn when necessary. |
| 66 |
|
| 67 |
This design eases the separation of concern: each participant in the D&D |
| 68 |
sequence is only responsible with its own rendering, while synchronizing between the |
| 69 |
participants is ensured through the global state. |
| 70 |
|
| 71 |
|
| 72 |
## Kind of Moves |
| 73 |
|
| 74 |
During a D&D sequence, different kind of events can be triggered. |
| 75 |
They can be distinguished with the `move` property of the dragging events, |
| 76 |
as follows: |
| 77 |
- `'START'`: D&D sequence initiated; |
| 78 |
- `'STOP'`: D&D sequence finished without drop target; |
| 79 |
- `'DROP'`: D&D sequence finished over some drop target; |
| 80 |
- `'DRAG'`: the source is dragged over some drop target; |
| 81 |
- `'HOLD'`: the source is holding the position for a while; |
| 82 |
- `'MOVE'`: the source is moving again after holding for a while ; |
| 83 |
- `'ENTER'`: the source is entering a target from _no_ other target; |
| 84 |
- `'LEAVE'`: the source is leaving a target into _no_ other target; |
| 85 |
- `'SWITCH'`: the source is leaving a target into another target. |
| 86 |
|
| 87 |
Notice that in kind of a `'SWITCH'` event, the leaved target will receive a `'LEAVE'` event |
| 88 |
and the entered target will receive an `'ENTER'` event. |
| 89 |
|
| 90 |
|
| 91 |
## Event Callbacks |
| 92 |
|
| 93 |
D&D controllers, drag sources and drop targets can listen to |
| 94 |
dragging events _via_ various callbacks. |
| 95 |
|
| 96 |
Each callback has signature `(Dragging) => void` and is sensible to |
| 97 |
specific kinds of events, as described below: |
| 98 |
- `onDnd()`: all kind of events; |
| 99 |
- `onStart()`: `START` events; |
| 100 |
- `onEnter()`: `ENTER` and `SWITCH` events; |
| 101 |
- `onLeave()`: `LEAVE` and `SWITCH` events; |
| 102 |
- `onDrag()`: `DRAG` and `MOVE` events; |
| 103 |
- `onHold()`: `HOLD` events; |
| 104 |
- `onMove()`: `MOVE` events; |
| 105 |
- `onDrop()`: `DROP` events; |
| 106 |
- `onStop()`: `STOP` events; |
| 107 |
- `onUpdate()`: only for D&D controllers, see below. |
| 108 |
|
| 109 |
In case of a `SWITCH` event, the `onLeave` listener is called first, |
| 110 |
then the `onEnter` listener. This is only relevant for the D&D controller |
| 111 |
and the drag source, since drop targets will receive either `ENTER` or `LEAVE` events. |
| 112 |
|
| 113 |
The same `Dragging` object is shared across all the event listeners, but |
| 114 |
not among different drag source and drop targets. For each source or target |
| 115 |
participating into the event, the `onDnd` listener is called first, |
| 116 |
then the more specific ones, when defined. Hence, you can modify the `Dragging` |
| 117 |
object during the process of `onDnd` and safely propagate those modifications accross |
| 118 |
all listeners. |
| 119 |
|
| 120 |
Inside a D&D sequence, the leaved targets are notified first, |
| 121 |
then the source and the target, and finally the D&D controller. |
| 122 |
This allow you to maintain the D&D state in sync with the different participants. |
| 123 |
|
| 124 |
Additionally, the D&D controller might be equiped with |
| 125 |
an `onUpdate()` handler, which is responsible for listening only to state update events. |
| 126 |
This event handler will receive the current D&D state in argument, with the cumulated |
| 127 |
updates from all participants merged in. |
| 128 |
|
| 129 |
*/ |
| 130 |
|
| 131 |
import _ from 'lodash' ; |
| 132 |
import React from 'react' ; |
| 133 |
import Draggable, { DraggableCore } from 'react-draggable'; |
| 134 |
|
| 135 |
const HOLD_TIME = 100 ; /* time in ms */ |
| 136 |
const HOLD_FIRE = 6 ; /* number of HOLD_TIME before « hold » */ |
| 137 |
const HOLD_MOVE = 3 ; /* maximum moved pixels for « holding » */ |
| 138 |
|
| 139 |
// -------------------------------------------------------------------------- |
| 140 |
// --- Events Dispatcher |
| 141 |
// -------------------------------------------------------------------------- |
| 142 |
|
| 143 |
const notify = ( cb, evt ) => cb && cb(evt); |
| 144 |
|
| 145 |
/** |
| 146 |
@method |
| 147 |
@summary Dispatch a dragging event over listeners. |
| 148 |
@param {Dragging} dragging - the Dragging event to dispatch over |
| 149 |
@param {object} [handlers] - an object with (optional) listeners |
| 150 |
@description |
| 151 |
|
| 152 |
Dispatch a `Dragging` event among listeners defined in the `handlers` object, |
| 153 |
when defined. For instance, `disptachEvents( evt , { onStop: myCallback } )` |
| 154 |
will trigger `myCallback(evt)` if and only if `evt` is a `STOP` event. |
| 155 |
|
| 156 |
See event listeners documentation above for more details on listeners. |
| 157 |
|
| 158 |
You normally don't have to call this function, unless when you need to add |
| 159 |
D&D features into your own component. Here is a typical pattern: |
| 160 |
|
| 161 |
const MyComponent = (props) => ( |
| 162 |
<DragSource |
| 163 |
dnd={props.dnd} |
| 164 |
onDnd={(evt) => DnD.dispatchEvent(Object.assign(evt,…),props)} > |
| 165 |
… |
| 166 |
</DragSource> |
| 167 |
); |
| 168 |
|
| 169 |
*/ |
| 170 |
export const dispatchEvent = ( dragging , handlers ) => { |
| 171 |
if (!handlers) return; |
| 172 |
const dragged = Object.assign( {} , dragging ); |
| 173 |
notify( handlers.onDnd , dragged ); |
| 174 |
switch( dragged.move ) { |
| 175 |
case 'START': |
| 176 |
notify( handlers.onStart, dragged ); |
| 177 |
return; |
| 178 |
case 'STOP': |
| 179 |
notify( handlers.onStop, dragged ); |
| 180 |
return; |
| 181 |
case 'ENTER': |
| 182 |
notify( handlers.onEnter, dragged ); |
| 183 |
return; |
| 184 |
case 'SWITCH': |
| 185 |
notify( handlers.onLeave, dragged ); |
| 186 |
notify( handlers.onEnter, dragged ); |
| 187 |
return; |
| 188 |
case 'LEAVE': |
| 189 |
notify( handlers.onLeave, dragged ); |
| 190 |
return; |
| 191 |
case 'DRAG': |
| 192 |
notify( handlers.onDrag, dragged ); |
| 193 |
return; |
| 194 |
case 'MOVE': |
| 195 |
notify( handlers.onMove, dragged ); |
| 196 |
notify( handlers.onDrag, dragged ); |
| 197 |
return; |
| 198 |
case 'HOLD': |
| 199 |
notify( handlers.onHold, dragged ); |
| 200 |
return; |
| 201 |
case 'DROP': |
| 202 |
notify( handlers.onDrop, dragged ); |
| 203 |
return; |
| 204 |
default: |
| 205 |
return; |
| 206 |
} |
| 207 |
}; |
| 208 |
|
| 209 |
// -------------------------------------------------------------------------- |
| 210 |
// --- Dnd Controllers |
| 211 |
// -------------------------------------------------------------------------- |
| 212 |
|
| 213 |
/** |
| 214 |
@class |
| 215 |
@summary D&D Controller. |
| 216 |
@param {object} [handlers] - D&D events callbacks |
| 217 |
@property {object} state - Current D&D state |
| 218 |
@property {Dragging} dragging - Last D&D event |
| 219 |
@description |
| 220 |
D&D controlers are responsible for managing various drop targets |
| 221 |
and dispatching the drag source events among them. |
| 222 |
|
| 223 |
The handler object shall define listeners to the D&D dragging events. |
| 224 |
Each callback is sensible to specific kind of events, as specified |
| 225 |
in the [[dome/dnd]] documentation. |
| 226 |
|
| 227 |
You need a D&D controller whenever an element can be dragged |
| 228 |
between different containers. D&D controllers also offers an |
| 229 |
abstraction over dragging events, dispatching among several |
| 230 |
drop targets and the management of « holding » the mouse over |
| 231 |
one drop target. |
| 232 |
|
| 233 |
Normally, you simply have to create D&D controllers in your global |
| 234 |
(or local) state and pass them around into the hierarchy of your |
| 235 |
components. For lower-level control, see the documentation of |
| 236 |
the exposed methods. |
| 237 |
|
| 238 |
Having several D&D controllers allows you to assign specific |
| 239 |
sources to specific targets. You shall have one D&D controller |
| 240 |
for each « kind » of data that you can drag over. |
| 241 |
|
| 242 |
The `state` and `dragging` properties are shared across all partipants |
| 243 |
of a D&D sequence, which are also re-rendered when the state is updated. |
| 244 |
Outside of a D&D sequence, the `state` and `dragging` properties are undefined. |
| 245 |
|
| 246 |
*/ |
| 247 |
|
| 248 |
export class DnD { |
| 249 |
|
| 250 |
constructor(handlers) { |
| 251 |
this.kid = 0 ; |
| 252 |
this.targets = {} ; |
| 253 |
this.tickHold = this.tickHold.bind(this); |
| 254 |
this.triggerUpdate = this.triggerUpdate.bind(this); |
| 255 |
this.triggerDnd = this.triggerDnd.bind(this); |
| 256 |
this.handlers = handlers || {} ; |
| 257 |
} |
| 258 |
|
| 259 |
/** @summary Update the D&D event handlers. |
| 260 |
@param {object} [handlers] - D&D events callbacks |
| 261 |
@description |
| 262 |
Replace the event handlers by those specified in the `handlers` object. |
| 263 |
Not mentionned callbacks are left unchanged. |
| 264 |
If the `null` object is given, all current handlers are removed. |
| 265 |
*/ |
| 266 |
setHandlers( handlers ) { |
| 267 |
this.handlers = |
| 268 |
handlers === null ? {} : Object.assign( this.handlers, handlers ); |
| 269 |
} |
| 270 |
|
| 271 |
//--- Registering |
| 272 |
|
| 273 |
/** @summary Registers a target into the D&D controller. |
| 274 |
@param {React.ref} ref - a referrence on the drop target DOM element |
| 275 |
@param {function} callback - generic D&D events callback |
| 276 |
@return {ident} the target identifier |
| 277 |
@description |
| 278 |
You normally don't call this method by yoursef, it is automatically |
| 279 |
called when `<DropTarget/>` are mounted in the DOM. |
| 280 |
*/ |
| 281 |
register( ref, callback ) |
| 282 |
{ |
| 283 |
if (!ref) return undefined; |
| 284 |
const id = 'DOME$' + (++this.kid) ; |
| 285 |
this.targets[id] = { id, ref , callback } ; |
| 286 |
return id; |
| 287 |
} |
| 288 |
|
| 289 |
//--- Updating |
| 290 |
|
| 291 |
/** @summary Update the event handlers associated to a drop target. |
| 292 |
@param {ident} id - the drop target identifier |
| 293 |
@param {React.ref} ref - a referrence on the drop target DOM element |
| 294 |
@param {function} callback - generic D&D events callback |
| 295 |
@description |
| 296 |
You normally don't call this method by yoursef, it is automatically |
| 297 |
called when `<DropTarget/>` are updated. If the target data is falsy, the target identifier |
| 298 |
is used instead. |
| 299 |
*/ |
| 300 |
update( id, ref, callback ) { |
| 301 |
if (this.targets[id]) { |
| 302 |
this.targets[id] = { id, ref, callback }; |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
//--- Un-Registering |
| 307 |
|
| 308 |
/** @summary Un-register a target from the D&D controller. |
| 309 |
@param {ident} id - the drop target identifier to remove |
| 310 |
@description |
| 311 |
You normally don't call this method by yoursef, it is automatically |
| 312 |
called when `<DropTarget/>` are unmounted from the DOM. |
| 313 |
*/ |
| 314 |
remove( id ) { |
| 315 |
delete this.targets[id] ; |
| 316 |
} |
| 317 |
|
| 318 |
//--- Generic Mouse Event |
| 319 |
|
| 320 |
handleMouseEvent( evt, x, y ) { |
| 321 |
const d = this.dragging ; |
| 322 |
d.meta = evt.ctrKey || evt.altKey || evt.metaKey ; |
| 323 |
d.deltaX = x - this.rootX ; |
| 324 |
d.deltaY = y - this.rootY ; |
| 325 |
d.position = {x,y} ; |
| 326 |
const ex = d.clientX = evt.clientX ; |
| 327 |
const ey = d.clientY = evt.clientY ; |
| 328 |
d.targetClientRect = undefined ; |
| 329 |
this.leaved = this.target ; |
| 330 |
this.leavedHandler = this.targetHandler ; |
| 331 |
this.target = undefined ; |
| 332 |
this.targetHandler = undefined ; |
| 333 |
const elts = document.elementsFromPoint(ex,ey); |
| 334 |
elts.find((elt) => { |
| 335 |
const target = _.find( this.targets, (tgt) => tgt.ref.current === elt ); |
| 336 |
if (target) { |
| 337 |
const { id, callback } = target ; |
| 338 |
this.target = id ; |
| 339 |
this.targetHandler = target.callback ; |
| 340 |
if (id === this.leaved) this.leavedHandler = undefined ; |
| 341 |
d.targetClientRect = elt.getBoundingClientRect(); |
| 342 |
return true; |
| 343 |
} |
| 344 |
return false; |
| 345 |
}); |
| 346 |
} |
| 347 |
|
| 348 |
//--- Starting D&D sequence |
| 349 |
|
| 350 |
/** @summary Initiates a new drag. |
| 351 |
@param {Event} evt - the mouse event originating the drag |
| 352 |
@param {number} x - initial horizontal coordinate |
| 353 |
@param {number} y - initial vertical coordinate |
| 354 |
@param {Element} node - the source DOM element |
| 355 |
@param {function} callback - generic D&D events callback |
| 356 |
@description |
| 357 |
You normally don't call this method by sourself, it is automatically |
| 358 |
called by `DragSource` objects. |
| 359 |
|
| 360 |
Initial `(x,y)` coordinates can be in any coordinate system, they |
| 361 |
are only used to compute relative offsets of further D&D events. |
| 362 |
They can be typically taken from React-Draggable events data. |
| 363 |
|
| 364 |
The generic callback shall handle two kind of events: |
| 365 |
- `callback()` when the global D&D state has been updated; |
| 366 |
- `callback(Dragging)` when some D&D event is triggered. |
| 367 |
|
| 368 |
Generally, you shall `forceUpdate()` your component in the first case, |
| 369 |
and `dispatchEvent()` the event to D&D listeners in the second case. |
| 370 |
|
| 371 |
*/ |
| 372 |
handleStart( evt, x, y, node, callback ) { |
| 373 |
if (this.dragging) { |
| 374 |
this.triggerDnd('STOP'); |
| 375 |
if (this.timer) clearInterval(this.timer); |
| 376 |
} |
| 377 |
this.state = {}; |
| 378 |
this.dragging = { |
| 379 |
sourceClientRect: node.getBoundingClientRect() |
| 380 |
}; |
| 381 |
this.sourceHandler = callback ; |
| 382 |
this.targetHandler = undefined ; |
| 383 |
this.leavedHandler = undefined ; |
| 384 |
this.rootX = x; |
| 385 |
this.rootY = y; |
| 386 |
this.lastX = x; |
| 387 |
this.lastY = y; |
| 388 |
this.ticks = 0; |
| 389 |
this.handleMouseEvent( evt, x, y ); |
| 390 |
this.triggerDnd( 'START' ); |
| 391 |
this.timer = setInterval( this.tickHold , HOLD_TIME ); |
| 392 |
} |
| 393 |
|
| 394 |
//--- Dragging D&D sequence |
| 395 |
|
| 396 |
/** @summary Dispatch a drag-event amoung registered drop targets. |
| 397 |
@param {Event} evt - the dragging mouse event |
| 398 |
@param {number} x - the horizontal coordinate |
| 399 |
@param {number} y - the vertical coordinate |
| 400 |
@description |
| 401 |
You normally don't call this method by sourself, it is automatically |
| 402 |
called by `DragSource` objects. |
| 403 |
|
| 404 |
The `(x,y)` coordinates shall be in the same coordinate system than |
| 405 |
when starting the D&D sequence. It is only used to compute relative |
| 406 |
the offsets of the associated D&D event. |
| 407 |
*/ |
| 408 |
handleDrag( evt, x, y ) { |
| 409 |
let kind = 'DRAG' ; |
| 410 |
let delta = Math.abs( x - this.lastX ) + Math.abs( y - this.lastY ); |
| 411 |
this.lastX = x ; |
| 412 |
this.lastY = y ; |
| 413 |
if (delta > HOLD_MOVE) { |
| 414 |
this.ticks = 0 ; |
| 415 |
const d = this.dragging ; |
| 416 |
if (d.held) { |
| 417 |
d.held = false ; |
| 418 |
kind = 'MOVE' ; |
| 419 |
} |
| 420 |
} |
| 421 |
this.handleMouseEvent( evt, x, y ); |
| 422 |
const target = this.target ; |
| 423 |
const leaved = this.leaved ; |
| 424 |
if (target !== leaved) { |
| 425 |
if (target && !leaved) kind = 'ENTER' ; else |
| 426 |
if (!target && leaved) kind = 'LEAVE' ; else |
| 427 |
if (target && leaved) kind = 'SWITCH' ; |
| 428 |
} |
| 429 |
this.triggerDnd(kind); |
| 430 |
} |
| 431 |
|
| 432 |
//--- Stopping D&D sequence |
| 433 |
|
| 434 |
/** @summary Dispatch a drop-event over registered drop targets. |
| 435 |
@param {Event} evt - the dropping mouse event |
| 436 |
@param {number} x - the horizontal coordinate |
| 437 |
@param {number} y - the vertical coordinate |
| 438 |
@description |
| 439 |
You normally don't call this method by sourself, it is automatically |
| 440 |
called by `DragSource` objects. |
| 441 |
|
| 442 |
The `(x,y)` coordinates shall be in the same coordinate system than |
| 443 |
when starting the D&D sequence. It is only used to compute relative |
| 444 |
the offsets of the associated D&D event. |
| 445 |
*/ |
| 446 |
handleStop( evt, x, y ) { |
| 447 |
clearInterval( this.timer ); |
| 448 |
this.handleMouseEvent( evt, x, y ); |
| 449 |
this.triggerDnd(this.target ? 'DROP' : 'STOP'); |
| 450 |
this.dragging = undefined ; |
| 451 |
this.state = undefined ; |
| 452 |
this.timer = undefined; |
| 453 |
this.ticks = undefined; |
| 454 |
this.triggerUpdate(); |
| 455 |
} |
| 456 |
|
| 457 |
//--- Hold Detection |
| 458 |
|
| 459 |
tickHold() { |
| 460 |
if (this.dragging && (this.ticks++) > HOLD_FIRE) |
| 461 |
{ |
| 462 |
this.dragging.held = true ; |
| 463 |
this.triggerDnd('HOLD'); |
| 464 |
this.ticks = 0 ; |
| 465 |
} |
| 466 |
} |
| 467 |
|
| 468 |
//--- Update State |
| 469 |
|
| 470 |
/** |
| 471 |
@summary Update the controller state. |
| 472 |
@parameter {object} update - the data to update the state with |
| 473 |
@description |
| 474 |
Update the state with the given updates, like React component states. |
| 475 |
This will force the drag source target and drop target components to re-render. |
| 476 |
*/ |
| 477 |
setState(update) { |
| 478 |
const state = this.state ; |
| 479 |
if (state && update) { |
| 480 |
this.state = Object.assign( state, update ); |
| 481 |
if (!this.dirty) { |
| 482 |
this.dirty = true ; |
| 483 |
setImmediate( this.triggerUpdate ); |
| 484 |
} |
| 485 |
} |
| 486 |
} |
| 487 |
|
| 488 |
triggerUpdate() { |
| 489 |
this.dirty = false ; |
| 490 |
notify( this.leavedHandler ); // always different from target |
| 491 |
notify( this.sourceHandler ); |
| 492 |
notify( this.targetHandler ); |
| 493 |
notify( this.handlers.onUpdate , this.state ); |
| 494 |
} |
| 495 |
|
| 496 |
//--- Trigger Dragging Events |
| 497 |
|
| 498 |
triggerDnd(move) { |
| 499 |
const d = this.dragging ; |
| 500 |
d.move = move ; |
| 501 |
switch( move ) { |
| 502 |
case 'ENTER': |
| 503 |
notify( this.sourceHandler, d ); |
| 504 |
notify( this.targetHandler, d ); |
| 505 |
break; |
| 506 |
case 'LEAVE': |
| 507 |
notify( this.leavedHandler, d ); |
| 508 |
notify( this.sourceHandler, d ); |
| 509 |
break; |
| 510 |
case 'SWITCH': |
| 511 |
d.move = 'LEAVE' ; notify( this.leavedHandler, d ); |
| 512 |
d.move = 'SWITCH' ; notify( this.sourceHandler, d ); |
| 513 |
d.move = 'ENTER' ; notify( this.targetHandler, d ); |
| 514 |
d.move = 'SWITCH' ; |
| 515 |
break; |
| 516 |
default: |
| 517 |
notify( this.leavedHandler, d ); // allways different from target |
| 518 |
notify( this.sourceHandler, d ); |
| 519 |
notify( this.targetHandler, d ); |
| 520 |
break; |
| 521 |
} |
| 522 |
dispatchEvent( d, this.handlers ); |
| 523 |
} |
| 524 |
|
| 525 |
} |
| 526 |
|
| 527 |
// -------------------------------------------------------------------------- |
| 528 |
// --- Drag Overlay |
| 529 |
// -------------------------------------------------------------------------- |
| 530 |
|
| 531 |
/** |
| 532 |
@summary Display a Drag Source with an Overlay. |
| 533 |
@property {Dragging} [dragging] - current dragging event |
| 534 |
@property {string} [className] - source content class |
| 535 |
@property {object} [style] - source content style |
| 536 |
@property {object} [draggedStyle] - additional style for the source placeholder |
| 537 |
@property {object} [draggingStyle] - additional style for the source being dragged |
| 538 |
@property {number} [offsetX] - horizontal offset during drag (default 4) |
| 539 |
@property {number} [offsetY] - vertical offset during drag (default 4) |
| 540 |
@property {number} [zIndex] - fixed positioning during drag (default 1) |
| 541 |
@property {any} [...divProps] - other properties to inject in the root element |
| 542 |
@property {React.children|function} [children] - drag source content |
| 543 |
@description |
| 544 |
When dragged, a source drag is generally blit by its embedding container. |
| 545 |
Using z-index CSS property might be a solution, but this is generally not |
| 546 |
enough to escape from a positionned parent `<div>` element. |
| 547 |
|
| 548 |
The alternative is to separate the internal content of |
| 549 |
a drag source from its outer `<div>`, and to use fixed positionning |
| 550 |
during a D&D sequence. |
| 551 |
|
| 552 |
The `<Overlay>` component implements this behaviour. The root element is |
| 553 |
normally laid out with the `className`, `style` and `divProps` properties and |
| 554 |
its internal content is wrapped inside an internal `<div>` container. |
| 555 |
|
| 556 |
During dragging, this normal layout is slightly altered, without causing |
| 557 |
extra React mounting or unmounting. The outer `<div>` is styled with its |
| 558 |
original class plus the `'dome-dragged'` class which provides by default a |
| 559 |
light-blue background and invisible border style. The inner class is styled |
| 560 |
with its original class plus the `dome-dragging` class, canceled margins and |
| 561 |
fixed positionning with respect to the dragging event. You may use those |
| 562 |
additinal classes to add conditional styling when dragging. |
| 563 |
|
| 564 |
Additionnaly, when dragged around, the source content is offsets |
| 565 |
with `offsetX` and `offsetY`, which defaults to 4 pixels each. |
| 566 |
|
| 567 |
Finally, you may supply a custom rendering function for rendering the drag |
| 568 |
source content, which is fed with the current dragging event. |
| 569 |
*/ |
| 570 |
|
| 571 |
export const Overlay = ({ |
| 572 |
dragging, className='', style, styleDragged, styleDragging, |
| 573 |
offsetX=4, offsetY=4, insetX=0, insetY=0, |
| 574 |
zIndex=1, |
| 575 |
children, |
| 576 |
...divProps |
| 577 |
}) => { |
| 578 |
let outerClass,outerStyle,innerStyle,innerClass ; |
| 579 |
if (dragging) { |
| 580 |
const { left, top, width, height } = dragging.sourceClientRect ; |
| 581 |
const position = { |
| 582 |
position: 'fixed', |
| 583 |
left: left + offsetX + dragging.deltaX, |
| 584 |
top: top + offsetY + dragging.deltaY, |
| 585 |
width, height, zIndex, margin: 0 |
| 586 |
}; |
| 587 |
const holder = { |
| 588 |
width, height |
| 589 |
}; |
| 590 |
outerClass = 'dome-dragged ' + className ; |
| 591 |
innerClass = 'dome-dragging ' + className ; |
| 592 |
outerStyle = Object.assign( {}, style, styleDragged, holder ); |
| 593 |
innerStyle = Object.assign( {}, style, styleDragging, position ); |
| 594 |
} else { |
| 595 |
outerClass = className ; |
| 596 |
outerStyle = style ; |
| 597 |
innerClass = undefined ; |
| 598 |
innerStyle = undefined ; |
| 599 |
} |
| 600 |
return ( |
| 601 |
<div className={outerClass} style={outerStyle} {...divProps}> |
| 602 |
<div className={innerClass} style={innerStyle}> |
| 603 |
{typeof(children)==='function' ? children(dragging) : children} |
| 604 |
</div> |
| 605 |
</div> |
| 606 |
); |
| 607 |
}; |
| 608 |
|
| 609 |
// -------------------------------------------------------------------------- |
| 610 |
// --- Drag Sources |
| 611 |
// -------------------------------------------------------------------------- |
| 612 |
|
| 613 |
/** |
| 614 |
@class |
| 615 |
@summary Drag Source Component. |
| 616 |
@property {DnD} [dnd] - the associated D&D controller |
| 617 |
@property {string} [handle] - DOM selector initiating the drag (eg. `'.handle'`) |
| 618 |
@property {boolean} [disabled] - cancel D&D events if non falsy value |
| 619 |
@property {object} [draggable] - additional properties for the internal react-draggable wrapper |
| 620 |
@property {function} [onDnd] - callback to D&D generic events |
| 621 |
@property {function} [onXxx] - callback to other D&D specific events |
| 622 |
@property {string} [className] - the class of the internal 'div' component (unless custom rendering) |
| 623 |
@property {object} [style] - the style of the internal 'div' component (unless custom rendering) |
| 624 |
@property {object} [position] - relative position of the internal 'div' component (unless custom rendering) |
| 625 |
@property {boolean|object} [overlay] - use an `Overlay` to display the content |
| 626 |
@property {React.children|function} children - element content or custom rendering function |
| 627 |
@description |
| 628 |
|
| 629 |
Creates a drag source element and manage its dragging state. The component |
| 630 |
is rendered by an internal `<div>` element with the given class and style with specific |
| 631 |
handlers attached in. You can use the `'position'` property to modify its relative position |
| 632 |
_after_ a D&D sequence, the position being `{x:0,y:0}`. This position is set relative to the normal |
| 633 |
positioning of the internal `<div>` element, as specified by the provided class and style properties. |
| 634 |
Notice that during dragging, your component is attached an additional `'dome-dragging'` class name that |
| 635 |
you can use to alter its rendering. |
| 636 |
|
| 637 |
Alternatively, you may provide a custom rendering function as the unique children of the Drag Source element. |
| 638 |
The function will be given the current D&D dragging events, or `undefined` when outside a D&D sequence. |
| 639 |
The custom rendering _shall_ return a single root element supporting property injection in order for the Drag |
| 640 |
Source element to attach its event handlers into it. You are then fully responsible for rendering and positioning |
| 641 |
the element both _during_ and _outside_ of a D&D sequence. No internal `<div>` element is inserted for you, |
| 642 |
and the `className`, `style` and `position` properties are ignored. |
| 643 |
|
| 644 |
Finally, you may also use an [[Overlay]] component to render |
| 645 |
your content, by setting `overlay=true` or passing `overlay` the desired properties. |
| 646 |
|
| 647 |
When dragged, the [[DnD]] controller is informed and dispatches |
| 648 |
the dragging events to the registered drop targets. The `handle` selector can be used |
| 649 |
to identify which parts of the content may initiate the drag. By default, any drag event |
| 650 |
may initiate the drag. |
| 651 |
|
| 652 |
The `onDnd` event listener receives all D&D events associated to this drop target. |
| 653 |
See event callbacks documentation for more details on other specific event listeners. |
| 654 |
|
| 655 |
The `draggable` object, when provided, is used to inject additional properties |
| 656 |
into the internal `Draggable` or `DraggableCore` wrapper used to manage D&D events. |
| 657 |
By default, a full featured `Draggable` component is used. |
| 658 |
When a custom rendering function is specified, a `DraggableCore` is used instead. |
| 659 |
See [react-draggable](https://github.com/mzabriskie/react-draggable) |
| 660 |
documentation for more details. |
| 661 |
|
| 662 |
*/ |
| 663 |
|
| 664 |
export class DragSource extends React.Component |
| 665 |
{ |
| 666 |
constructor(props) |
| 667 |
{ |
| 668 |
super(props); |
| 669 |
this.onStart = this.onStart.bind(this); |
| 670 |
this.onDrag = this.onDrag.bind(this); |
| 671 |
this.onStop = this.onStop.bind(this); |
| 672 |
this.handleDnd = this.handleDnd.bind(this); |
| 673 |
this.state = { }; |
| 674 |
} |
| 675 |
|
| 676 |
componentDidMount() { |
| 677 |
this.mounted = true; |
| 678 |
} |
| 679 |
|
| 680 |
componentWillUnmount() { |
| 681 |
this.mounted = false; |
| 682 |
} |
| 683 |
|
| 684 |
onStart(evt,drag) { |
| 685 |
const { dnd, disabled } = this.props; |
| 686 |
if (dnd) dnd.handleStart(evt,drag.x,drag.y,drag.node,this.handleDnd); |
| 687 |
} |
| 688 |
|
| 689 |
onDrag(evt,drag) { |
| 690 |
const { dnd, disabled } = this.props ; |
| 691 |
if (dnd) dnd.handleDrag(evt,drag.x,drag.y); |
| 692 |
} |
| 693 |
|
| 694 |
onStop(evt,drag) { |
| 695 |
const { dnd, disabled } = this.props ; |
| 696 |
if (dnd) dnd.handleStop(evt,drag.x,drag.y); |
| 697 |
} |
| 698 |
|
| 699 |
handleDnd(dragging) { |
| 700 |
if (!this.mounted) return; |
| 701 |
if (!dragging) |
| 702 |
this.forceUpdate(); |
| 703 |
else { |
| 704 |
dispatchEvent( dragging, this.props ); |
| 705 |
switch(dragging.move) { |
| 706 |
case 'STOP': |
| 707 |
case 'DROP': |
| 708 |
this.setState({dragging:undefined}); |
| 709 |
break; |
| 710 |
default: |
| 711 |
this.setState({dragging}); |
| 712 |
break; |
| 713 |
} |
| 714 |
} |
| 715 |
} |
| 716 |
|
| 717 |
render() { |
| 718 |
const { dnd, disabled, className, style, |
| 719 |
handle, draggable, overlay, |
| 720 |
children } = this.props ; |
| 721 |
if (overlay || typeof(children)==='function') { |
| 722 |
const dragging = this.state.dragging ; |
| 723 |
return ( |
| 724 |
<DraggableCore |
| 725 |
handle={handle} |
| 726 |
disabled={!dnd || disabled} |
| 727 |
onStart={this.onStart} |
| 728 |
onDrag={this.onDrag} |
| 729 |
onStop={this.onStop} |
| 730 |
{...draggable} |
| 731 |
> |
| 732 |
{ overlay ? |
| 733 |
( |
| 734 |
<Overlay |
| 735 |
dragging={dragging} |
| 736 |
className={className} style={style} |
| 737 |
{...overlay} > |
| 738 |
{children} |
| 739 |
</Overlay> |
| 740 |
) |
| 741 |
: children(dragging) |
| 742 |
} |
| 743 |
</DraggableCore> |
| 744 |
); |
| 745 |
} else { |
| 746 |
const { className, style, position={x:0,y:0} } = this.props ; |
| 747 |
return ( |
| 748 |
<Draggable |
| 749 |
handle={handle} |
| 750 |
disabled={!dnd || disabled} |
| 751 |
position={position} |
| 752 |
defaultClassName='' |
| 753 |
defaultClassNameDragged='' |
| 754 |
defaultClassNameDragging='dome-dragging' |
| 755 |
onStart={this.onStart} |
| 756 |
onDrag={this.onDrag} |
| 757 |
onStop={this.onStop} |
| 758 |
{...draggable} |
| 759 |
> |
| 760 |
<div className={className} style={style}>{children}</div> |
| 761 |
</Draggable> |
| 762 |
); |
| 763 |
} |
| 764 |
} |
| 765 |
|
| 766 |
} |
| 767 |
|
| 768 |
// -------------------------------------------------------------------------- |
| 769 |
// --- Drop Target |
| 770 |
// -------------------------------------------------------------------------- |
| 771 |
|
| 772 |
/** |
| 773 |
@class |
| 774 |
@summary Drop Target Container. |
| 775 |
@property {DnD} [dnd] - the associated D&D Controller |
| 776 |
@property {function} [onDnd] - callback to D&D generic events |
| 777 |
@property {function} [onXxx] - callback to other D&D specific events |
| 778 |
@property {string} [className] - class name(s) of the internal `<div>` element (unless custom rendering) |
| 779 |
@property {object} [style] - style property for the internal `<div>` element (unless custom rendering) |
| 780 |
@property {React.ref} [targetRef] - the React ref to use for identifying the target DOM element |
| 781 |
@property {React.children|function} children - element content or custom rendering function |
| 782 |
@description |
| 783 |
|
| 784 |
Crates a drop target container and register its callbacks into the specified |
| 785 |
[[DnD]] controller. In case the DnD controller is undefined, |
| 786 |
the drag events are just disabled. |
| 787 |
|
| 788 |
The drop target container must inform the D&D controller of its DOM element that is responsible |
| 789 |
for reacting to dragging events. By default, the drop target element renders its children elements |
| 790 |
inside an internal `<div>` element with the specified class ans style properties. Notice that, during |
| 791 |
a D&D sequence, the `dome-dragging` class name is added to this `<div>` element and you can use this selector |
| 792 |
for your styling. |
| 793 |
|
| 794 |
Alternatively, you may provide a custom rendering function as the unique children of the Drag Source element. |
| 795 |
Your rendering function will receive the current D&D dragging of the D&D sequence, if any. |
| 796 |
The [targetRef] property shall be used to identify the target DOM element, otherwize, the drop target |
| 797 |
will not respond to dragging events. |
| 798 |
In case of custom rendering, the `className` and `style` properties are ignored. |
| 799 |
|
| 800 |
During a D&D sequence, the `onDnd` event listener receives all D&D events associated |
| 801 |
to this drop target. See event callbacks documentation for more details on |
| 802 |
other specific event listeners. |
| 803 |
|
| 804 |
*/ |
| 805 |
export class DropTarget extends React.Component |
| 806 |
{ |
| 807 |
|
| 808 |
constructor(props) { |
| 809 |
super(props); |
| 810 |
this.targetRef = this.props.targetRef || React.createRef() ; |
| 811 |
this.handleDnd = this.handleDnd.bind(this); |
| 812 |
this.state = {}; |
| 813 |
} |
| 814 |
|
| 815 |
componentDidMount() { |
| 816 |
const { dnd } = this.props ; |
| 817 |
this.id = dnd && dnd.register( this.targetRef, this.handleDnd ); |
| 818 |
this.mounted = true ; |
| 819 |
} |
| 820 |
|
| 821 |
componentDidUpdate() { |
| 822 |
const { dnd } = this.props ; |
| 823 |
dnd && dnd.update(this.id, this.targetRef, this.handleDnd ); |
| 824 |
} |
| 825 |
|
| 826 |
componentWillUnmount() { |
| 827 |
const { dnd } = this.props ; |
| 828 |
dnd && dnd.remove( this.id ); |
| 829 |
this.mounted = false ; |
| 830 |
} |
| 831 |
|
| 832 |
handleDnd(dragging) { |
| 833 |
if (!this.mounted) return; |
| 834 |
if (!dragging) |
| 835 |
this.forceUpdate(); |
| 836 |
else { |
| 837 |
dispatchEvent( dragging, this.props ); |
| 838 |
switch(dragging.move) { |
| 839 |
case 'STOP': |
| 840 |
case 'DROP': |
| 841 |
case 'LEAVE': |
| 842 |
this.setState({dragging:undefined}); |
| 843 |
break; |
| 844 |
default: |
| 845 |
this.setState({dragging}); |
| 846 |
break; |
| 847 |
} |
| 848 |
} |
| 849 |
} |
| 850 |
|
| 851 |
render() { |
| 852 |
const targetRef = this.targetRef ; |
| 853 |
const { children } = this.props ; |
| 854 |
const { dragging } = this.state ; |
| 855 |
if (typeof(children)==='function') |
| 856 |
return children(dragging); |
| 857 |
else { |
| 858 |
let { className='', style } = this.props ; |
| 859 |
return ( |
| 860 |
<div ref={targetRef} |
| 861 |
className={ dragging ? className + ' dome-dragging' : className } |
| 862 |
style={style} > |
| 863 |
{children} |
| 864 |
</div> |
| 865 |
); |
| 866 |
} |
| 867 |
} |
| 868 |
|
| 869 |
} |
| 870 |
|
| 871 |
// -------------------------------------------------------------------------- |