2020-05-25 21:30:55 -05:00
|
|
|
import * as ko from "knockout";
|
|
|
|
import * as MostRecentActivity from "../MostRecentActivity/MostRecentActivity";
|
|
|
|
import * as React from "react";
|
|
|
|
import * as ViewModels from "../../Contracts/ViewModels";
|
|
|
|
import { NotebookContentItem } from "../Notebook/NotebookContentItem";
|
|
|
|
import { ReactAdapter } from "../../Bindings/ReactBindingHandler";
|
|
|
|
import { TreeComponent, TreeNode } from "../Controls/TreeComponent/TreeComponent";
|
|
|
|
import CollectionIcon from "../../../images/tree-collection.svg";
|
2020-07-20 12:59:40 -05:00
|
|
|
import Explorer from "../Explorer";
|
2020-08-06 14:03:46 -05:00
|
|
|
import { userContext } from "../../UserContext";
|
2020-05-25 21:30:55 -05:00
|
|
|
|
|
|
|
export class ResourceTreeAdapterForResourceToken implements ReactAdapter {
|
|
|
|
public parameters: ko.Observable<number>;
|
|
|
|
public myNotebooksContentRoot: NotebookContentItem;
|
|
|
|
|
2020-07-20 12:59:40 -05:00
|
|
|
public constructor(private container: Explorer) {
|
2020-05-25 21:30:55 -05:00
|
|
|
this.parameters = ko.observable(Date.now());
|
|
|
|
|
2020-07-27 16:05:25 -05:00
|
|
|
this.container.resourceTokenCollection.subscribe(() => this.triggerRender());
|
2020-05-25 21:30:55 -05:00
|
|
|
this.container.selectedNode.subscribe((newValue: any) => this.triggerRender());
|
2020-07-27 16:05:25 -05:00
|
|
|
this.container.tabsManager && this.container.tabsManager.activeTab.subscribe(() => this.triggerRender());
|
2020-05-25 21:30:55 -05:00
|
|
|
|
|
|
|
this.triggerRender();
|
|
|
|
}
|
|
|
|
|
|
|
|
public renderComponent(): JSX.Element {
|
|
|
|
const dataRootNode = this.buildCollectionNode();
|
|
|
|
return <TreeComponent className="dataResourceTree" rootNode={dataRootNode} />;
|
|
|
|
}
|
|
|
|
|
|
|
|
public buildCollectionNode(): TreeNode {
|
|
|
|
const collection: ViewModels.CollectionBase = this.container.resourceTokenCollection();
|
|
|
|
if (!collection) {
|
|
|
|
return {
|
|
|
|
label: undefined,
|
|
|
|
isExpanded: true,
|
|
|
|
children: []
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
const children: TreeNode[] = [];
|
|
|
|
children.push({
|
|
|
|
label: "Items",
|
|
|
|
onClick: () => {
|
|
|
|
collection.onDocumentDBDocumentsClick();
|
|
|
|
// push to most recent
|
2020-08-06 14:03:46 -05:00
|
|
|
this.container.mostRecentActivity.addItem(userContext.databaseAccount?.id, {
|
2020-05-25 21:30:55 -05:00
|
|
|
type: MostRecentActivity.Type.OpenCollection,
|
|
|
|
title: collection.id(),
|
|
|
|
description: "Data",
|
2020-10-13 13:29:39 -07:00
|
|
|
data: {
|
|
|
|
databaseId: collection.databaseId,
|
|
|
|
collectionId: collection.id()
|
|
|
|
}
|
2020-05-25 21:30:55 -05:00
|
|
|
});
|
|
|
|
},
|
2020-10-13 13:29:39 -07:00
|
|
|
isSelected: () =>
|
|
|
|
this.isDataNodeSelected(collection.databaseId, collection.id(), ViewModels.CollectionTabKind.Documents)
|
2020-05-25 21:30:55 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
const collectionNode: TreeNode = {
|
|
|
|
label: collection.id(),
|
|
|
|
iconSrc: CollectionIcon,
|
|
|
|
isExpanded: true,
|
|
|
|
children,
|
|
|
|
className: "collectionHeader",
|
|
|
|
onClick: () => {
|
|
|
|
// Rewritten version of expandCollapseCollection
|
|
|
|
this.container.selectedNode(collection);
|
|
|
|
this.container.onUpdateTabsButtons([]);
|
2020-10-13 13:29:39 -07:00
|
|
|
this.container.tabsManager.refreshActiveTab(
|
|
|
|
tab => tab.collection?.id() === collection.id() && tab.collection.databaseId === collection.databaseId
|
|
|
|
);
|
2020-05-25 21:30:55 -05:00
|
|
|
},
|
2020-10-13 13:29:39 -07:00
|
|
|
isSelected: () => this.isDataNodeSelected(collection.databaseId, collection.id())
|
2020-05-25 21:30:55 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
label: undefined,
|
|
|
|
isExpanded: true,
|
|
|
|
children: [collectionNode]
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-10-13 13:29:39 -07:00
|
|
|
public isDataNodeSelected(
|
|
|
|
databaseId: string,
|
|
|
|
collectionId?: string,
|
|
|
|
subnodeKind?: ViewModels.CollectionTabKind
|
|
|
|
): boolean {
|
2020-05-25 21:30:55 -05:00
|
|
|
if (!this.container.selectedNode || !this.container.selectedNode()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const selectedNode = this.container.selectedNode();
|
2020-10-13 13:29:39 -07:00
|
|
|
const isNodeSelected = collectionId
|
|
|
|
? (selectedNode as ViewModels.Collection).databaseId === databaseId && selectedNode.id() === collectionId
|
|
|
|
: selectedNode.id() === databaseId;
|
2020-05-25 21:30:55 -05:00
|
|
|
|
2020-10-13 13:29:39 -07:00
|
|
|
if (!isNodeSelected) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-05-25 21:30:55 -05:00
|
|
|
|
2020-10-13 13:29:39 -07:00
|
|
|
if (!subnodeKind) {
|
|
|
|
return true;
|
2020-05-25 21:30:55 -05:00
|
|
|
}
|
2020-10-13 13:29:39 -07:00
|
|
|
|
|
|
|
const activeTab = this.container.tabsManager.activeTab();
|
|
|
|
const selectedSubnodeKind = collectionId
|
|
|
|
? (selectedNode as ViewModels.Collection).selectedSubnodeKind()
|
|
|
|
: (selectedNode as ViewModels.Database).selectedSubnodeKind();
|
|
|
|
|
|
|
|
return activeTab && activeTab.tabKind === subnodeKind && selectedSubnodeKind === subnodeKind;
|
2020-05-25 21:30:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public triggerRender() {
|
|
|
|
window.requestAnimationFrame(() => this.parameters(Date.now()));
|
|
|
|
}
|
|
|
|
}
|