Drag and Drop Without Writing JavaScript with WFC + Online Demo
Drag and Drop interactions are usually implemented with client-side JavaScript. Developers typically need to listen for pointer events, track the dragged element, prevent default browser behavior, update CSS classes, and finally move or remove the element. With WebForms Core (WFC) , the same interaction can be implemented by defining events and commands on the server. No custom JavaScript event…
Developing Drag and Drop interactions typically involves client-side JavaScript. Developers must handle pointer events, track the dragged element, prevent default browser actions, modify CSS classes, and manipulate the element's position. However, WebForms Core (WFC) simplifies this process dramatically. With WFC, the same Drag and Drop functionality can be achieved by defining events and commands on the server.
No custom JavaScript code is needed to implement this interaction. The following example demonstrates this concept using Node.js and Express. It is suggested to examine the accompanying drag-and-drop demo prior to reviewing the code. The demo is hosted at webforms-core.github.io.
The web page features two zones: a `dragZone` containing draggable elements and a `dropZone` serving as the destination area. When an item is dragged using a mouse, it can seamlessly transition between the two zones. For touch devices, where traditional HTML Drag and Drop may not be as effective, a tap can be employed to move an item between the zones.
The provided code snippet utilizes the Express framework in Node.js to set up the server. It imports the necessary modules from `express` and `WebForms`. An instance of `express` is created, and the static directory `public` is configured to serve static files. The application routes the root URL to display the web form.
To implement the Drag and Drop functionality, the `WebForms` object is configured with various event handlers. On the `dragZone`, event handlers are set for `dragStart`, `dragEnd`, `dragOver`, `dragLeave`, and `drop` events. These handlers are defined using the `setCommentEvent` method, mapping the respective event names to the corresponding functions. Additionally, event handlers for touch devices are also defined using `OnTouchStart` for the `dragZone`.
The `startIndex` method is invoked to initiate the Drag and Drop process. Various CSS classes are inserted or deleted based on the event type using methods like `insertClass` and `deleteClass`. For instance, when `dragOver` occurs, the `dropZone` receives the `highlight` class to visually indicate the drop target. Conversely, when `dragLeave` takes place, the `highlight` class is removed from the `dropZone`.
To complete the Drag and Drop operation, the `drop` event handler is triggered. It uses `Fetch.save` to remove the dragged element from the `dragZone`. Simultaneously, `Fetch.save` is employed on the `dropZone` to add the moved element. Finally, `startIndex` is called once more to finalize the Drag and Drop interaction on the `dropZone`, and the `addText` method is used to display the moved item.
Rewritten by urgent.news; may contain errors — check the original.
Source: dev.to