Skip to content

Commit

Permalink
Merge pull request #1776 from DhivinX/feat/optional-sorting-functions
Browse files Browse the repository at this point in the history
Optional sorting functions
  • Loading branch information
xeolabs authored Jan 14, 2025
2 parents adac8b2 + 69a0de6 commit 8f1d4ee
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/plugins/TreeViewPlugin/TreeViewPlugin.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Plugin} from "../../viewer/Plugin.js";
import {RenderService} from "./RenderService.js";
import { Plugin } from "../../viewer/Plugin.js";
import { RenderService } from "./RenderService.js";

const treeViews = [];

Expand Down Expand Up @@ -363,6 +363,8 @@ export class TreeViewPlugin extends Plugin {
* @param {RenderService} [cfg.renderService] Optional {@link RenderService} to use. Defaults to the {@link TreeViewPlugin}'s default {@link RenderService}.
* @param {Boolean} [cfg.showIndeterminate=false] When true, will show indeterminate state for checkboxes when some but not all child nodes are checked
* @param {Boolean} [cfg.showProjectNode=false] When true, will show top level project node when hierarchy is set to "storeys"
* @param {Function} [cfg.elevationSortFunction] Optional function to replace the default elevation sort function. The function should take two nodes and return -1, 0 or 1.
* @param {Function} [cfg.defaultSortFunction] Optional function to replace the default sort function. The function should take two nodes and return -1, 0 or 1.
*/
constructor(viewer, cfg = {}) {

Expand Down Expand Up @@ -416,6 +418,8 @@ export class TreeViewPlugin extends Plugin {
this._renderService = cfg.renderService || new RenderService();
this._showIndeterminate = cfg.showIndeterminate ?? false;
this._showProjectNode = cfg.showProjectNode ?? false;
this._elevationSortFunction = cfg.elevationSortFunction ?? undefined;
this._defaultSortFunction = cfg.defaultSortFunction ?? undefined;

if (!this._renderService) {
throw new Error('TreeViewPlugin: no render service set');
Expand Down Expand Up @@ -1229,9 +1233,11 @@ export class TreeViewPlugin extends Plugin {
}
const firstChild = children[0];
if ((this._hierarchy === "storeys" || this._hierarchy === "containment") && firstChild.type === "IfcBuildingStorey") {
children.sort(this._getSpatialSortFunc());
if (this._elevationSortFunction) children.sort(this._elevationSortFunction);
else children.sort(this._getSpatialSortFunc());
} else {
children.sort(this._alphaSortFunc);
if (this._defaultSortFunction) children.sort(this._defaultSortFunction)
else children.sort(this._alphaSortFunc);
}
for (let i = 0, len = children.length; i < len; i++) {
const node = children[i];
Expand Down
4 changes: 4 additions & 0 deletions types/plugins/TreeViewPlugin/TreeViewPlugin.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ export declare type TreeViewPluginConfiguration = {
renderService?: ITreeViewRenderService;
/** When true, will show indeterminate state for checkboxes when some but not all child nodes are checked */
showIndeterminate?: boolean;
/** Optional function to replace the default elevation sort function. The function should take two nodes and return -1, 0 or 1. */
elevationSortFunction?: (node1: TreeViewNode, node2: TreeViewNode) => number;
/** Optional function to replace the default sort function. The function should take two nodes and return -1, 0 or 1. */
defaultSortFunction?: (node1: TreeViewNode, node2: TreeViewNode) => number;
};

/**
Expand Down

0 comments on commit 8f1d4ee

Please sign in to comment.