Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added ability to provide function to find to find by other than id #208

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ dockMove(source: TabData | PanelData, target: string | TabData | PanelData | Box
find PanelData or TabData by id

```typescript
find(id: string): PanelData | TabData;
find(id: string | ((item: PanelData | TabData | BoxData) => boolean), filter?: Filter): PanelData | TabData | BoxData | undefined;
```

### updateTab [🗎](https://ticlo.github.io/rc-dock/classes/docklayout.docklayout-1.html#updatetab)
Expand Down
2 changes: 1 addition & 1 deletion es/Algorithm.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export declare enum Filter {
AnyTabPanel = 123,
All = 127
}
export declare function find(layout: LayoutData, id: string, filter?: Filter): PanelData | TabData | BoxData | undefined;
export declare function find(layout: LayoutData, id: string | ((item: PanelData | TabData | BoxData) => boolean), filter?: Filter): PanelData | TabData | BoxData | undefined;
export declare function addNextToTab(layout: LayoutData, source: TabData | PanelData, target: TabData, direction: DropDirection): LayoutData;
export declare function addTabToPanel(layout: LayoutData, source: TabData | PanelData, panel: PanelData, idx?: number): LayoutData;
export declare function converToPanel(source: TabData | PanelData): PanelData;
Expand Down
9 changes: 6 additions & 3 deletions es/Algorithm.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,16 @@ export function nextZIndex(current) {
}
return ++_zCount;
}
function compareFindId(item, id) {
return item && (typeof id === 'function' ? id(item) : item.id === id);
}
function findInPanel(panel, id, filter) {
if (panel.id === id && (filter & Filter.Panel)) {
if (compareFindId(panel, id) && (filter & Filter.Panel)) {
return panel;
}
if (filter & Filter.Tab) {
for (let tab of panel.tabs) {
if (tab.id === id) {
if (compareFindId(tab, id)) {
return tab;
}
}
Expand All @@ -75,7 +78,7 @@ function findInPanel(panel, id, filter) {
}
function findInBox(box, id, filter) {
let result;
if ((filter | Filter.Box) && (box === null || box === void 0 ? void 0 : box.id) === id) {
if ((filter | Filter.Box) && compareFindId(box, id)) {
return box;
}
if (!(box === null || box === void 0 ? void 0 : box.children)) {
Expand Down
2 changes: 1 addition & 1 deletion es/DockData.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ export interface DockContext {
/**
* Find PanelData or TabData by id
*/
find(id: string, filter?: Filter): PanelData | TabData | BoxData | undefined;
find(id: string | ((item: PanelData | TabData | BoxData) => boolean), filter?: Filter): PanelData | TabData | BoxData | undefined;
/**
* Update a tab with new TabData
* @param id tab id to update
Expand Down
2 changes: 1 addition & 1 deletion es/DockLayout.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export declare class DockLayout extends DockPortalManager implements DockContext
*/
dockMove(source: TabData | PanelData, target: string | TabData | PanelData | BoxData | null, direction: DropDirection, floatPosition?: FloatPosition): void;
/** @inheritDoc */
find(id: string, filter?: Algorithm.Filter): PanelData | TabData | BoxData | undefined;
find(id: string | ((item: PanelData | TabData | BoxData) => boolean), filter?: Algorithm.Filter): PanelData | TabData | BoxData | undefined;
/** @ignore */
getLayoutSize(): LayoutSize;
/** @inheritDoc */
Expand Down
2 changes: 1 addition & 1 deletion lib/Algorithm.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export declare enum Filter {
AnyTabPanel = 123,
All = 127
}
export declare function find(layout: LayoutData, id: string, filter?: Filter): PanelData | TabData | BoxData | undefined;
export declare function find(layout: LayoutData, id: string | ((item: PanelData | TabData | BoxData) => boolean), filter?: Filter): PanelData | TabData | BoxData | undefined;
export declare function addNextToTab(layout: LayoutData, source: TabData | PanelData, target: TabData, direction: DropDirection): LayoutData;
export declare function addTabToPanel(layout: LayoutData, source: TabData | PanelData, panel: PanelData, idx?: number): LayoutData;
export declare function converToPanel(source: TabData | PanelData): PanelData;
Expand Down
9 changes: 6 additions & 3 deletions lib/Algorithm.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,16 @@ function nextZIndex(current) {
return ++_zCount;
}
exports.nextZIndex = nextZIndex;
function compareFindId(item, id) {
return item && (typeof id === 'function' ? id(item) : item.id === id);
}
function findInPanel(panel, id, filter) {
if (panel.id === id && (filter & Filter.Panel)) {
if (compareFindId(panel, id) && (filter & Filter.Panel)) {
return panel;
}
if (filter & Filter.Tab) {
for (let tab of panel.tabs) {
if (tab.id === id) {
if (compareFindId(tab, id)) {
return tab;
}
}
Expand All @@ -81,7 +84,7 @@ function findInPanel(panel, id, filter) {
}
function findInBox(box, id, filter) {
let result;
if ((filter | Filter.Box) && (box === null || box === void 0 ? void 0 : box.id) === id) {
if ((filter | Filter.Box) && compareFindId(box, id)) {
return box;
}
if (!(box === null || box === void 0 ? void 0 : box.children)) {
Expand Down
2 changes: 1 addition & 1 deletion lib/DockData.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ export interface DockContext {
/**
* Find PanelData or TabData by id
*/
find(id: string, filter?: Filter): PanelData | TabData | BoxData | undefined;
find(id: string | ((item: PanelData | TabData | BoxData) => boolean), filter?: Filter): PanelData | TabData | BoxData | undefined;
/**
* Update a tab with new TabData
* @param id tab id to update
Expand Down
2 changes: 1 addition & 1 deletion lib/DockLayout.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export declare class DockLayout extends DockPortalManager implements DockContext
*/
dockMove(source: TabData | PanelData, target: string | TabData | PanelData | BoxData | null, direction: DropDirection, floatPosition?: FloatPosition): void;
/** @inheritDoc */
find(id: string, filter?: Algorithm.Filter): PanelData | TabData | BoxData | undefined;
find(id: string | ((item: PanelData | TabData | BoxData) => boolean), filter?: Algorithm.Filter): PanelData | TabData | BoxData | undefined;
/** @ignore */
getLayoutSize(): LayoutSize;
/** @inheritDoc */
Expand Down
16 changes: 10 additions & 6 deletions src/Algorithm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,24 +79,28 @@ export function nextZIndex(current?: number): number {
return ++_zCount;
}

function compareFindId(item: PanelData | TabData | BoxData, id: string | ((item: PanelData | TabData | BoxData) => boolean)): boolean {
return item && (typeof id === 'function' ? id(item) : item.id === id);
}


function findInPanel(panel: PanelData, id: string, filter: Filter): PanelData | TabData | undefined {
if (panel.id === id && (filter & Filter.Panel)) {
function findInPanel(panel: PanelData, id: string | ((item: PanelData | TabData | BoxData) => boolean), filter: Filter): PanelData | TabData | undefined {
if (compareFindId(panel, id) && (filter & Filter.Panel)) {
return panel;
}
if (filter & Filter.Tab) {
for (let tab of panel.tabs) {
if (tab.id === id) {
if (compareFindId(tab, id)) {
return tab;
}
}
}
return undefined;
}

function findInBox(box: BoxData | undefined, id: string, filter: Filter): PanelData | TabData | BoxData | undefined {
function findInBox(box: BoxData | undefined, id: string | ((item: PanelData | TabData | BoxData) => boolean), filter: Filter): PanelData | TabData | BoxData | undefined {
let result: PanelData | TabData | BoxData | undefined;
if ((filter | Filter.Box) && box?.id === id) {
if ((filter | Filter.Box) && compareFindId(box, id)) {
return box;
}
if (!box?.children) {
Expand Down Expand Up @@ -133,7 +137,7 @@ export enum Filter {
}


export function find(layout: LayoutData, id: string, filter: Filter = Filter.AnyTabPanel): PanelData | TabData | BoxData | undefined {
export function find(layout: LayoutData, id: string | ((item: PanelData | TabData | BoxData) => boolean), filter: Filter = Filter.AnyTabPanel): PanelData | TabData | BoxData | undefined {
let result: PanelData | TabData | BoxData | undefined;

if (filter & Filter.Docked) {
Expand Down
2 changes: 1 addition & 1 deletion src/DockData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ export interface DockContext {
/**
* Find PanelData or TabData by id
*/
find(id: string, filter?: Filter): PanelData | TabData | BoxData | undefined;
find(id: string | ((item: PanelData | TabData | BoxData) => boolean), filter?: Filter): PanelData | TabData | BoxData | undefined;

/**
* Update a tab with new TabData
Expand Down
2 changes: 1 addition & 1 deletion src/DockLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ export class DockLayout extends DockPortalManager implements DockContext {
}

/** @inheritDoc */
find(id: string, filter?: Algorithm.Filter): PanelData | TabData | BoxData | undefined {
find(id: string | ((item: PanelData | TabData | BoxData) => boolean), filter?: Algorithm.Filter): PanelData | TabData | BoxData | undefined {
return Algorithm.find(this.getLayout(), id, filter);
}

Expand Down