@@ -4,8 +4,29 @@ import Rectangle from '../../util/Rectangle.mjs';
44import VDomUtil from '../../util/VDom.mjs' ;
55
66/**
7+ * @summary Manages the drag-and-drop reordering of items within a container, with support for window detachment.
8+ *
9+ * This class extends `Neo.draggable.container.DragZone` to provide sorting capabilities for `Neo.container.Base` instances.
10+ * It handles the complex logic of tracking item positions, swapping them during the drag operation, and updating
11+ * the container's layout upon drop.
12+ *
13+ * A key feature of this class is its support for **Window Detachment** (tearing tabs or items out of the main window).
14+ * When an item is dragged outside the browser window boundaries:
15+ * 1. The `startWindowDrag` method is triggered.
16+ * 2. The drag placeholder is hidden.
17+ * 3. The `calculateExpandedLayout` method dynamically computes a new layout for the remaining items, expanding them
18+ * to fill the empty space (animating `width`, `height`, `top`, and `left`).
19+ * 4. If the drag re-enters the window (`onDragBoundaryEntry`), the original layout snapshot is restored, and the
20+ * placeholder reappears, allowing for a seamless return to sorting mode.
21+ *
22+ * This class interacts closely with:
23+ * - `Neo.draggable.DragProxy`: For the visual representation of the dragged item.
24+ * - `Neo.main.addon.DragDrop`: For communicating drag state across the browser/OS environment.
25+ *
726 * @class Neo.draggable.container.SortZone
827 * @extends Neo.draggable.container.DragZone
28+ * @see Neo.draggable.container.DragZone
29+ * @see Neo.main.addon.DragDrop
930 */
1031class SortZone extends DragZone {
1132 static config = {
@@ -147,7 +168,16 @@ class SortZone extends DragZone {
147168 }
148169
149170 /**
150- * @param {Object } data
171+ * Handles the completion of the drag operation.
172+ *
173+ * This method is responsible for:
174+ * 1. **Finalizing the Drop:** If valid, it moves the DOM nodes to their final positions (via `Neo.applyDeltas`).
175+ * 2. **Cleanup:** Removes the drag placeholder and resets internal state flags (`isWindowDragging`, `currentIndex`, etc.).
176+ * 3. **Layout Restoration:** Resets the styles of all items (clearing the absolute positioning used during the drag)
177+ * so they return to the container's natural layout flow.
178+ * 4. **State Synchronization:** Calls `owner.moveTo()` to update the container's `items` array to reflect the new order.
179+ *
180+ * @param {Object } data - The drag end event data.
151181 */
152182 async onDragEnd ( data ) {
153183 let me = this ,
@@ -246,7 +276,19 @@ class SortZone extends DragZone {
246276 }
247277
248278 /**
249- * @param {Object } data
279+ * Handles the drag move event. This is the core logic loop for the drag operation.
280+ *
281+ * Responsibilities:
282+ * 1. **Window Drag Re-entry:** Checks if a window drag has re-entered the original container boundaries.
283+ * If so, it restores the original layout snapshot (`itemRects`) and shows the placeholder, effectively
284+ * "snapping" the dashboard back to its sortable state.
285+ * 2. **Window Drag Exit:** Detects if the drag proxy has left the container boundaries (if `enableProxyToPopup` is true)
286+ * and triggers the `dragBoundaryExit` event to potentially start a window drag.
287+ * 3. **Standard Sorting:** If not in window-drag mode, it calculates the drag delta and swaps items (`switchItems`)
288+ * if the threshold is crossed, updating the `currentIndex`.
289+ * 4. **Auto-Scrolling:** Manages auto-scrolling when dragging near the edges of the container.
290+ *
291+ * @param {Object } data - The drag move event data.
250292 */
251293 async onDragMove ( data ) {
252294 let me = this ;
@@ -380,7 +422,20 @@ class SortZone extends DragZone {
380422 }
381423
382424 /**
383- * @param {Object } data
425+ * Initializes the drag operation.
426+ *
427+ * Key actions:
428+ * 1. **Identify Drag Target:** Determines which item is being dragged (handling `dragHandleSelector` if present).
429+ * 2. **Snapshot Layout:** Captures the current DOM rectangles (`itemRects`) of all sortable items. This snapshot
430+ * is critical for:
431+ * - Calculating drag deltas for sorting.
432+ * - Restoring the layout after a window drag re-entry.
433+ * - Inferring gaps and offsets for `calculateExpandedLayout`.
434+ * 3. **Setup Proxy & Placeholder:** Configures the visual drag proxy and inserts the placeholder into the `sortableItems` list.
435+ * 4. **Apply Absolute Positioning:** Temporarily switches all items to `position: absolute` based on their captured
436+ * coordinates to enable smooth, GPU-accelerated movement during the drag.
437+ *
438+ * @param {Object } data - The drag start event data.
384439 */
385440 async onDragStart ( data ) {
386441 let me = this ,
@@ -514,8 +569,17 @@ class SortZone extends DragZone {
514569 }
515570
516571 /**
517- * Calculates the expanded layout for remaining items when one is dragged out.
518- * @returns {Object[] } Array of style objects for the items
572+ * Calculates a new layout for the remaining items when one item is dragged out of the container (e.g., into a new window).
573+ *
574+ * This method ensures the dashboard doesn't leave a "hole" where the dragged item was. Instead, it:
575+ * 1. **Infers Gaps & Offsets:** Analyzes the cached `itemRects` to mathematically derive the container's padding
576+ * and the gaps between items, ensuring the new layout respects the original design tokens.
577+ * 2. **Identifies Remaining Items:** Filters out the dragged component and its placeholder.
578+ * 3. **Distributes Space:** Calculates the available space (Total Size - Offsets - Gaps - Fixed Items) and distributes
579+ * it among flex items proportional to their flex values.
580+ * 4. **Generates Styles:** Returns a list of style objects (`top`, `left`, `width`, `height`) to be applied to the remaining items.
581+ *
582+ * @returns {Object[] } Array of objects containing the `item` reference and the calculated `style` object.
519583 */
520584 calculateExpandedLayout ( ) {
521585 let me = this ,
@@ -640,7 +704,19 @@ class SortZone extends DragZone {
640704 }
641705
642706 /**
643- * @param {Object } data
707+ * Handles the drag move event. This is the core logic loop for the drag operation.
708+ *
709+ * Responsibilities:
710+ * 1. **Window Drag Re-entry:** Checks if a window drag has re-entered the original container boundaries.
711+ * If so, it restores the original layout snapshot (`itemRects`) and shows the placeholder, effectively
712+ * "snapping" the dashboard back to its sortable state.
713+ * 2. **Window Drag Exit:** Detects if the drag proxy has left the container boundaries (if `enableProxyToPopup` is true)
714+ * and triggers the `dragBoundaryExit` event to potentially start a window drag.
715+ * 3. **Standard Sorting:** If not in window-drag mode, it calculates the drag delta and swaps items (`switchItems`)
716+ * if the threshold is crossed, updating the `currentIndex`.
717+ * 4. **Auto-Scrolling:** Manages auto-scrolling when dragging near the edges of the container.
718+ *
719+ * @param {Object } data - The drag move event data.
644720 */
645721 startWindowDrag ( data ) {
646722 let me = this ,
@@ -671,8 +747,18 @@ class SortZone extends DragZone {
671747 }
672748
673749 /**
674- * @param {Number } index1
675- * @param {Number } index2
750+ * Swaps two items in the sort list, updating their layout coordinates and the internal index map.
751+ *
752+ * This method handles the physical reordering of items during a drag operation. It performs the following:
753+ * 1. **Normalization:** Ensures indices are ordered correctly based on layout direction.
754+ * 2. **Geometry Calculation:** Swaps the dimensions (width/height) of the two items and recalculates
755+ * their positions (x/y), preserving the original gap between them. This ensures that items of different
756+ * sizes swap correctly without breaking the layout structure.
757+ * 3. **State Update:** Updates the `indexMap` to reflect the new logical order of items.
758+ * 4. **Visual Update:** Calls `updateItem` to apply the new coordinates to the DOM.
759+ *
760+ * @param {Number } index1 - The index of the first item to swap.
761+ * @param {Number } index2 - The index of the second item to swap.
676762 */
677763 switchItems ( index1 , index2 ) {
678764 let me = this ,
0 commit comments