You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@superset.apache.org by GitBox <gi...@apache.org> on 2022/11/07 18:16:34 UTC

[GitHub] [superset] eric-briscoe commented on a diff in pull request #21520: feat: create table component based on ant design Table

eric-briscoe commented on code in PR #21520:
URL: https://github.com/apache/superset/pull/21520#discussion_r1015752837


##########
superset-frontend/src/components/Table/utils/InteractiveTableUtils.ts:
##########
@@ -0,0 +1,237 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import { SUPERSET_TABLE_COLUMN } from 'src/components/Table';
+import { withinRange } from './utils';
+
+export default class InteractiveTableUtils {
+  tableRef: HTMLTableElement | null;
+
+  columnRef: HTMLElement | null;
+
+  setDerivedColumns: Function;
+
+  isDragging: boolean;
+
+  resizable: boolean;
+
+  reorderable: boolean;
+
+  derivedColumns: Array<any>;
+
+  RESIZE_INDICATOR_THRESHOLD: number;
+
+  constructor(
+    tableRef: HTMLTableElement,
+    derivedColumns: Array<any>,
+    setDerivedColumns: Function,
+  ) {
+    this.setDerivedColumns = setDerivedColumns;
+    this.tableRef = tableRef;
+    this.isDragging = false;
+    this.RESIZE_INDICATOR_THRESHOLD = 8;
+    this.resizable = false;
+    this.reorderable = false;
+    this.derivedColumns = [...derivedColumns];
+    document.addEventListener('mouseup', this.handleMouseup);
+  }
+
+  clearListeners = () => {
+    document.removeEventListener('mouseup', this.handleMouseup);
+    this.initializeResizeableColumns(false, this.tableRef);
+    this.initializeDragDropColumns(false, this.tableRef);
+  };
+
+  setTableRef = (table: HTMLTableElement) => {
+    this.tableRef = table;
+  };
+
+  getColumnIndex = (): number => {
+    let index = -1;
+    const parent: HTMLElement | null | undefined = this.columnRef
+      ?.parentNode as HTMLElement;
+    if (parent) {
+      index = Array.prototype.indexOf.call(parent.children, this.columnRef);
+    }
+    return index;
+  };
+
+  handleColumnDragStart = (ev: DragEvent): void => {
+    const target: HTMLElement = ev?.currentTarget as HTMLElement;
+    if (target) {
+      this.columnRef = target;
+    }
+    this.isDragging = true;
+    const index = this.getColumnIndex();
+    const columnData = this.derivedColumns[index];
+    const dragData = { index, columnData };
+    ev?.dataTransfer?.setData(SUPERSET_TABLE_COLUMN, JSON.stringify(dragData));
+  };
+
+  handleDragDrop = (ev: DragEvent): void => {
+    const data = ev.dataTransfer?.getData?.(SUPERSET_TABLE_COLUMN);
+    if (data) {
+      ev.preventDefault();
+      const parent: Element = (ev.currentTarget as HTMLElement)
+        ?.parentNode as HTMLElement;
+      const dropIndex = Array.prototype.indexOf.call(
+        parent.children,
+        ev.currentTarget,
+      );
+      const dragIndex = this.getColumnIndex();
+      const columnsCopy = [...this.derivedColumns];
+      const removedItem = columnsCopy.slice(dragIndex, dragIndex + 1);
+      columnsCopy.splice(dragIndex, 1);
+      columnsCopy.splice(dropIndex, 0, removedItem[0]);
+      this.derivedColumns = [...columnsCopy];
+      this.setDerivedColumns(columnsCopy);
+    }
+  };
+
+  allowDrop = (ev: DragEvent): void => {
+    ev.preventDefault();
+  };
+
+  handleMouseDown = (event: MouseEvent) => {
+    const target: HTMLElement = event?.currentTarget as HTMLElement;
+    if (target) {
+      this.columnRef = target;
+      if (
+        event &&
+        withinRange(
+          event.offsetX,
+          target.offsetWidth,
+          this.RESIZE_INDICATOR_THRESHOLD,
+        )
+      ) {
+        // @ts-ignore

Review Comment:
   @arash TypeScript does not like the image import, bit this is valid for webpack to import the resource



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org