Files
cosmos-explorer/src/Explorer/Tree/DocumentId.ts
Laurent Nguyen 90c1439d34 Update prettier to latest. Remove tslint (#1641)
* Rev up prettier

* Reformat

* Remove deprecated tslint

* Remove call to tslint and update package-lock.json
2023-10-03 17:13:24 +02:00

84 lines
2.4 KiB
TypeScript

import * as ko from "knockout";
import * as DataModels from "../../Contracts/DataModels";
import { useDialog } from "../Controls/Dialog";
import DocumentsTab from "../Tabs/DocumentsTab";
export default class DocumentId {
public container: DocumentsTab;
public rid: string;
public self: string;
public ts: string;
public id: ko.Observable<string>;
public partitionKeyProperties: string[];
public partitionKey: DataModels.PartitionKey;
public partitionKeyValue: any[];
public stringPartitionKeyValues: string[];
public isDirty: ko.Observable<boolean>;
constructor(container: DocumentsTab, data: any, partitionKeyValue: any[]) {
this.container = container;
this.self = data._self;
this.rid = data._rid;
this.ts = data._ts;
this.partitionKeyValue = partitionKeyValue;
this.partitionKeyProperties = container?.partitionKeyProperties;
this.partitionKey = container && container.partitionKey;
this.stringPartitionKeyValues = this.getPartitionKeyValueAsString();
this.id = ko.observable(data.id);
this.isDirty = ko.observable(false);
}
public click() {
if (this.container.isEditorDirty()) {
useDialog
.getState()
.showOkCancelModalDialog(
"Unsaved changes",
"Your unsaved changes will be lost. Do you want to continue?",
"OK",
() => this.loadDocument(),
"Cancel",
undefined,
);
} else {
this.loadDocument();
}
}
public partitionKeyHeader(): Object {
if (!this.partitionKeyProperties || this.partitionKeyProperties.length === 0) {
return undefined;
}
if (!this.partitionKeyValue || this.partitionKeyValue.length === 0) {
return [{}];
}
return [this.partitionKeyValue];
}
public getPartitionKeyValueAsString(): string[] {
return this.partitionKeyValue?.map((partitionKeyValue) => {
const typeOfPartitionKeyValue: string = typeof partitionKeyValue;
if (
typeOfPartitionKeyValue === "undefined" ||
typeOfPartitionKeyValue === "null" ||
typeOfPartitionKeyValue === "object"
) {
return "";
}
if (typeOfPartitionKeyValue === "string") {
return partitionKeyValue;
}
return JSON.stringify(partitionKeyValue);
});
}
public async loadDocument(): Promise<void> {
await this.container.selectDocument(this);
}
}