2020-05-25 21:30:55 -05:00
|
|
|
import * as ko from "knockout";
|
|
|
|
import * as ViewModels from "../../Contracts/ViewModels";
|
|
|
|
import * as Constants from "../../Common/Constants";
|
|
|
|
import * as DataModels from "../../Contracts/DataModels";
|
|
|
|
import { Action, ActionModifiers } from "../../Shared/Telemetry/TelemetryConstants";
|
|
|
|
import UserDefinedFunctionTab from "../Tabs/UserDefinedFunctionTab";
|
|
|
|
import TelemetryProcessor from "../../Shared/Telemetry/TelemetryProcessor";
|
2020-07-20 12:59:40 -05:00
|
|
|
import Explorer from "../Explorer";
|
2020-07-27 12:58:27 -05:00
|
|
|
import { deleteUserDefinedFunction } from "../../Common/DocumentClientUtilityBase";
|
2020-05-25 21:30:55 -05:00
|
|
|
|
2020-07-21 13:50:51 -05:00
|
|
|
export default class UserDefinedFunction {
|
2020-05-25 21:30:55 -05:00
|
|
|
public nodeKind: string;
|
2020-07-20 12:59:40 -05:00
|
|
|
public container: Explorer;
|
2020-05-25 21:30:55 -05:00
|
|
|
public collection: ViewModels.Collection;
|
|
|
|
public self: string;
|
|
|
|
public rid: string;
|
|
|
|
public id: ko.Observable<string>;
|
|
|
|
public body: ko.Observable<string>;
|
|
|
|
|
2020-07-20 12:59:40 -05:00
|
|
|
constructor(container: Explorer, collection: ViewModels.Collection, data: DataModels.UserDefinedFunction) {
|
2020-05-25 21:30:55 -05:00
|
|
|
this.nodeKind = "UserDefinedFunction";
|
|
|
|
this.container = container;
|
|
|
|
|
|
|
|
this.collection = collection;
|
|
|
|
this.self = data._self;
|
|
|
|
this.rid = data._rid;
|
|
|
|
this.id = ko.observable(data.id);
|
|
|
|
this.body = ko.observable(data.body);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static create(source: ViewModels.Collection, event: MouseEvent) {
|
2020-07-09 13:53:37 -07:00
|
|
|
const id = source.container.tabsManager.getTabs(ViewModels.CollectionTabKind.UserDefinedFunctions).length + 1;
|
2020-05-25 21:30:55 -05:00
|
|
|
const userDefinedFunction = <DataModels.UserDefinedFunction>{
|
|
|
|
id: "",
|
|
|
|
body: "function userDefinedFunction(){}"
|
|
|
|
};
|
2020-07-09 13:53:37 -07:00
|
|
|
|
|
|
|
const userDefinedFunctionTab: UserDefinedFunctionTab = new UserDefinedFunctionTab({
|
2020-05-25 21:30:55 -05:00
|
|
|
resource: userDefinedFunction,
|
|
|
|
isNew: true,
|
|
|
|
tabKind: ViewModels.CollectionTabKind.UserDefinedFunctions,
|
|
|
|
title: `New UDF ${id}`,
|
|
|
|
tabPath: "",
|
|
|
|
collection: source,
|
|
|
|
node: source,
|
|
|
|
hashLocation: `${Constants.HashRoutePrefixes.collectionsWithIds(source.databaseId, source.id())}/udf`,
|
|
|
|
selfLink: "",
|
|
|
|
isActive: ko.observable(false),
|
2020-07-09 13:53:37 -07:00
|
|
|
onUpdateTabsButtons: source.container.onUpdateTabsButtons
|
2020-05-25 21:30:55 -05:00
|
|
|
});
|
|
|
|
|
2020-07-09 13:53:37 -07:00
|
|
|
source.container.tabsManager.activateNewTab(userDefinedFunctionTab);
|
2020-05-25 21:30:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public open = () => {
|
|
|
|
this.select();
|
|
|
|
|
2020-07-09 13:53:37 -07:00
|
|
|
const userDefinedFunctionTabs: UserDefinedFunctionTab[] = this.container.tabsManager.getTabs(
|
|
|
|
ViewModels.CollectionTabKind.UserDefinedFunctions,
|
|
|
|
(tab: ViewModels.Tab) => tab.collection && tab.collection.rid === this.rid
|
|
|
|
) as UserDefinedFunctionTab[];
|
|
|
|
let userDefinedFunctionTab: UserDefinedFunctionTab = userDefinedFunctionTabs && userDefinedFunctionTabs[0];
|
|
|
|
|
|
|
|
if (userDefinedFunctionTab) {
|
|
|
|
this.container.tabsManager.activateTab(userDefinedFunctionTab);
|
|
|
|
} else {
|
2020-05-25 21:30:55 -05:00
|
|
|
const userDefinedFunctionData = <DataModels.UserDefinedFunction>{
|
|
|
|
_rid: this.rid,
|
|
|
|
_self: this.self,
|
|
|
|
id: this.id(),
|
|
|
|
body: this.body()
|
|
|
|
};
|
|
|
|
|
|
|
|
userDefinedFunctionTab = new UserDefinedFunctionTab({
|
|
|
|
resource: userDefinedFunctionData,
|
|
|
|
isNew: false,
|
|
|
|
tabKind: ViewModels.CollectionTabKind.UserDefinedFunctions,
|
|
|
|
title: userDefinedFunctionData.id,
|
|
|
|
tabPath: "",
|
|
|
|
collection: this.collection,
|
|
|
|
node: this,
|
|
|
|
hashLocation: `${Constants.HashRoutePrefixes.collectionsWithIds(
|
|
|
|
this.collection.databaseId,
|
|
|
|
this.collection.id()
|
|
|
|
)}/udfs/${this.id()}`,
|
|
|
|
selfLink: "",
|
|
|
|
isActive: ko.observable(false),
|
2020-07-09 13:53:37 -07:00
|
|
|
onUpdateTabsButtons: this.container.onUpdateTabsButtons
|
2020-05-25 21:30:55 -05:00
|
|
|
});
|
|
|
|
|
2020-07-09 13:53:37 -07:00
|
|
|
this.container.tabsManager.activateNewTab(userDefinedFunctionTab);
|
|
|
|
}
|
2020-05-25 21:30:55 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
public select() {
|
|
|
|
this.container.selectedNode(this);
|
|
|
|
TelemetryProcessor.trace(Action.SelectItem, ActionModifiers.Mark, {
|
|
|
|
description: "UDF item node",
|
|
|
|
databaseAccountName: this.container.databaseAccount().name,
|
|
|
|
defaultExperience: this.container.defaultExperience(),
|
|
|
|
dataExplorerArea: Constants.Areas.ResourceTree
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-06-15 12:16:52 +02:00
|
|
|
public delete() {
|
2020-05-25 21:30:55 -05:00
|
|
|
if (!window.confirm("Are you sure you want to delete the user defined function?")) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const userDefinedFunctionData = <DataModels.UserDefinedFunction>{
|
|
|
|
_rid: this.rid,
|
|
|
|
_self: this.self,
|
|
|
|
id: this.id(),
|
|
|
|
body: this.body()
|
|
|
|
};
|
2020-07-27 12:58:27 -05:00
|
|
|
deleteUserDefinedFunction(this.collection, userDefinedFunctionData).then(
|
2020-05-25 21:30:55 -05:00
|
|
|
() => {
|
2020-07-09 13:53:37 -07:00
|
|
|
this.container.tabsManager.removeTabByComparator(
|
|
|
|
(tab: ViewModels.Tab) => tab.node && tab.node.rid === this.rid
|
|
|
|
);
|
2020-05-25 21:30:55 -05:00
|
|
|
this.collection.children.remove(this);
|
|
|
|
},
|
|
|
|
reason => {}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|