Skip to content

Commit

Permalink
Replace position enum (#874)
Browse files Browse the repository at this point in the history
* Replace Position enum with type

* Use Position type in typescript definition

* Remove Position none
  • Loading branch information
mbraak authored Dec 26, 2024
1 parent 417c221 commit ca57b13
Show file tree
Hide file tree
Showing 15 changed files with 97 additions and 190 deletions.
39 changes: 21 additions & 18 deletions src/dragAndDropHandler/generateHitAreas.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { Node } from "../node";
import { Position } from "../position";
import { Node, Position } from "../node";
import { getOffsetTop } from "../util";
import iterateVisibleNodes from "./iterateVisibleNodes";
import { HitArea } from "./types";

interface HitPosition {
node: Node;
position: Position;
position: null | Position;
top: number;
}

Expand All @@ -17,7 +16,11 @@ export const generateHitPositions = (
const hitPositions: HitPosition[] = [];
let lastTop = 0;

const addHitPosition = (node: Node, position: number, top: number) => {
const addHitPosition = (
node: Node,
position: null | Position,
top: number,
) => {
hitPositions.push({
node,
position,
Expand All @@ -29,9 +32,9 @@ export const generateHitPositions = (
const handleAfterOpenFolder = (node: Node, nextNode: Node | null) => {
if (node === currentNode || nextNode === currentNode) {
// Cannot move before or after current item
addHitPosition(node, Position.None, lastTop);
addHitPosition(node, null, lastTop);
} else {
addHitPosition(node, Position.After, lastTop);
addHitPosition(node, "after", lastTop);
}
};

Expand All @@ -44,20 +47,20 @@ export const generateHitPositions = (

if (node === currentNode) {
// Cannot move after current item
addHitPosition(node, Position.None, top);
addHitPosition(node, null, top);
} else {
addHitPosition(node, Position.Inside, top);
addHitPosition(node, "inside", top);

// Cannot move before current item
if (nextNode !== currentNode) {
addHitPosition(node, Position.After, top);
addHitPosition(node, "after", top);
}
}
};

const handleFirstNode = (node: Node) => {
if (node !== currentNode && node.element) {
addHitPosition(node, Position.Before, getOffsetTop(node.element));
addHitPosition(node, "before", getOffsetTop(node.element));
}
};

Expand All @@ -70,16 +73,16 @@ export const generateHitPositions = (

if (node === currentNode) {
// Cannot move inside current item
addHitPosition(node, Position.None, top);
addHitPosition(node, null, top);
} else {
addHitPosition(node, Position.Inside, top);
addHitPosition(node, "inside", top);
}

if (nextNode === currentNode || node === currentNode) {
// Cannot move before or after current item
addHitPosition(node, Position.None, top);
addHitPosition(node, null, top);
} else {
addHitPosition(node, Position.After, top);
addHitPosition(node, "after", top);
}
};

Expand All @@ -90,11 +93,11 @@ export const generateHitPositions = (
// Dnd over the current element is not possible: add a position with type None for the top and the bottom.
const top = getOffsetTop(element);
const height = element.clientHeight;
addHitPosition(node, Position.None, top);
addHitPosition(node, null, top);

if (height > 5) {
// Subtract 5 pixels to allow more space for the next element.
addHitPosition(node, Position.None, top + height - 5);
addHitPosition(node, null, top + height - 5);
}

// Stop iterating
Expand All @@ -103,7 +106,7 @@ export const generateHitPositions = (

// Cannot move before current item
if (node.children[0] !== currentNode) {
addHitPosition(node, Position.Inside, getOffsetTop(element));
addHitPosition(node, "inside", getOffsetTop(element));
}

// Continue iterating
Expand Down Expand Up @@ -136,7 +139,7 @@ export const generateHitAreasForGroup = (
for (let i = 0; i < positionCount; i++) {
const position = positionsInGroup[i] as HitPosition;

if (position.position !== Position.None) {
if (position.position) {
hitAreas.push({
bottom: areaTop + areaHeight,
node: position.node,
Expand Down
18 changes: 5 additions & 13 deletions src/dragAndDropHandler/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
import { PositionInfo } from "../mouseUtils";
import { Node } from "../node";
import NodeElement from "../nodeElement";
import { getPositionName, Position } from "../position";
import { getElementPosition } from "../util";
import binarySearch from "./binarySearch";
import DragElement from "./dragElement";
Expand Down Expand Up @@ -254,9 +253,7 @@ export class DragAndDropHandler {
return true;
}

const positionName = getPositionName(area.position);

return this.onCanMoveTo(currentItem.node, area.node, positionName);
return this.onCanMoveTo(currentItem.node, area.node, area.position);
}

private clear(): void {
Expand Down Expand Up @@ -322,16 +319,15 @@ export class DragAndDropHandler {
private moveItem(positionInfo: PositionInfo): void {
if (
this.currentItem &&
this.hoveredArea &&
this.hoveredArea.position !== Position.None &&
this.hoveredArea?.position &&
this.canMoveToArea(this.hoveredArea, this.currentItem)
) {
const movedNode = this.currentItem.node;
const targetNode = this.hoveredArea.node;
const position = this.hoveredArea.position;
const previousParent = movedNode.parent;

if (position === Position.Inside) {
if (position === "inside") {
this.hoveredArea.node.is_open = true;
}

Expand All @@ -351,7 +347,7 @@ export class DragAndDropHandler {
do_move: doMove,
moved_node: movedNode,
original_event: positionInfo.originalEvent,
position: getPositionName(position),
position,
previous_parent: previousParent,
target_node: targetNode,
},
Expand All @@ -376,11 +372,7 @@ export class DragAndDropHandler {
private mustOpenFolderTimer(area: HitArea): boolean {
const node = area.node;

return (
node.isFolder() &&
!node.is_open &&
area.position === Position.Inside
);
return node.isFolder() && !node.is_open && area.position === "inside";
}

private removeDropHint(): void {
Expand Down
3 changes: 1 addition & 2 deletions src/dragAndDropHandler/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Node } from "../node";
import { Position } from "../position";
import { Node, Position } from "../node";

export interface DropHint {
remove: () => void;
Expand Down
12 changes: 5 additions & 7 deletions src/node.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { isNodeRecordWithChildren } from "./nodeUtils";
import { Position } from "./position";

export type Position = "after" | "before" | "inside";

type IterateCallback = (node: Node, level: number) => boolean;

Expand Down Expand Up @@ -514,7 +515,7 @@ export class Node implements INode {
movedNode.parent.doRemoveChild(movedNode);

switch (position) {
case Position.After: {
case "after": {
if (targetNode.parent) {
targetNode.parent.addChildAtPosition(
movedNode,
Expand All @@ -525,7 +526,7 @@ export class Node implements INode {
return false;
}

case Position.Before: {
case "before": {
if (targetNode.parent) {
targetNode.parent.addChildAtPosition(
movedNode,
Expand All @@ -536,14 +537,11 @@ export class Node implements INode {
return false;
}

case Position.Inside: {
case "inside": {
// move inside as first child
targetNode.addChildAtPosition(movedNode, 0);
return true;
}

default:
return false;
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/nodeElement/folderElement.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { OnFinishOpenNode, TriggerEvent } from "../jqtreeMethodTypes";
import { Position } from "../position";
import { Position } from "../node";
import NodeElement, { NodeElementParams } from "./index";

interface FolderElementParams extends NodeElementParams {
Expand Down Expand Up @@ -117,7 +117,7 @@ class FolderElement extends NodeElement {
}

protected mustShowBorderDropHint(position: Position): boolean {
return !this.node.is_open && position === Position.Inside;
return !this.node.is_open && position === "inside";
}

private getButton(): HTMLLinkElement {
Expand Down
9 changes: 4 additions & 5 deletions src/nodeElement/ghostDropHint.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { DropHint } from "../dragAndDropHandler/types";
import { Node } from "../node";
import { Position } from "../position";
import { Node, Position } from "../node";

class GhostDropHint implements DropHint {
private element: HTMLElement;
Expand All @@ -13,15 +12,15 @@ class GhostDropHint implements DropHint {
this.ghost = this.createGhostElement();

switch (position) {
case Position.After:
case "after":
this.moveAfter();
break;

case Position.Before:
case "before":
this.moveBefore();
break;

case Position.Inside: {
case "inside": {
if (node.isFolder() && node.is_open) {
this.moveInsideOpenFolder();
} else {
Expand Down
7 changes: 3 additions & 4 deletions src/nodeElement/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { DropHint } from "../dragAndDropHandler/types";
import { GetScrollLeft } from "../jqtreeMethodTypes";
import { Node } from "../node";
import { Position } from "../position";
import { Node, Position } from "../node";
import BorderDropHint from "./borderDropHint";
import GhostDropHint from "./ghostDropHint";

Expand Down Expand Up @@ -32,7 +31,7 @@ class NodeElement {
this.init(node);
}

public addDropHint(position: number): DropHint {
public addDropHint(position: Position): DropHint {
if (this.mustShowBorderDropHint(position)) {
return new BorderDropHint(this.element, this.getScrollLeft());
} else {
Expand Down Expand Up @@ -89,7 +88,7 @@ class NodeElement {
}

protected mustShowBorderDropHint(position: Position): boolean {
return position === Position.Inside;
return position === "inside";
}
}

Expand Down
28 changes: 0 additions & 28 deletions src/position.ts

This file was deleted.

Loading

0 comments on commit ca57b13

Please sign in to comment.