|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | +* Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | +* Licensed under the MIT License. See License.md in the project root for license information. |
| 4 | +*--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +import { KnownActiveRevisionsMode, type Container, type Revision } from "@azure/arm-appcontainers"; |
| 7 | +import { nonNullValue, nonNullValueAndProp, type TreeElementBase } from "@microsoft/vscode-azext-utils"; |
| 8 | +import { type AzureSubscription, type ViewPropertiesModel } from "@microsoft/vscode-azureresources-api"; |
| 9 | +import * as deepEqual from 'deep-eql'; |
| 10 | +import { TreeItemCollapsibleState, type TreeItem } from "vscode"; |
| 11 | +import { ext } from "../../extensionVariables"; |
| 12 | +import { localize } from "../../utils/localize"; |
| 13 | +import { getParentResource } from "../../utils/revisionDraftUtils"; |
| 14 | +import { treeUtils } from "../../utils/treeUtils"; |
| 15 | +import { type ContainerAppModel } from "../ContainerAppItem"; |
| 16 | +import { RevisionDraftDescendantBase } from "../revisionManagement/RevisionDraftDescendantBase"; |
| 17 | +import { RevisionDraftItem } from "../revisionManagement/RevisionDraftItem"; |
| 18 | +import { ContainerItem } from "./ContainerItem"; |
| 19 | +import { EnvironmentVariablesItem } from "./EnvironmentVariablesItem"; |
| 20 | +import { ImageItem } from "./ImageItem"; |
| 21 | + |
| 22 | +export class ContainersItem extends RevisionDraftDescendantBase { |
| 23 | + id: string; |
| 24 | + label: string; |
| 25 | + private containers: Container[] = []; |
| 26 | + |
| 27 | + constructor(public readonly subscription: AzureSubscription, |
| 28 | + public readonly containerApp: ContainerAppModel, |
| 29 | + public readonly revision: Revision,) { |
| 30 | + super(subscription, containerApp, revision); |
| 31 | + this.id = `${this.parentResource.id}/containers`; |
| 32 | + this.containers = nonNullValue(revision.template?.containers); |
| 33 | + this.label = this.containers.length === 1 ? localize('container', 'Container') : localize('containers', 'Containers'); |
| 34 | + } |
| 35 | + |
| 36 | + getChildren(): TreeElementBase[] { |
| 37 | + if (this.containers.length === 1) { |
| 38 | + return [new ImageItem(this.subscription, this.containerApp, this.revision, this.id, this.containers[0]), |
| 39 | + new EnvironmentVariablesItem(this.subscription, this.containerApp, this.revision, this.id, this.containers[0])]; |
| 40 | + } |
| 41 | + return nonNullValue(this.containers?.map(container => new ContainerItem(this.subscription, this.containerApp, this.revision, container))); |
| 42 | + } |
| 43 | + |
| 44 | + getTreeItem(): TreeItem { |
| 45 | + return { |
| 46 | + id: this.id, |
| 47 | + label: this.label, |
| 48 | + iconPath: treeUtils.getIconPath('containers'), |
| 49 | + collapsibleState: TreeItemCollapsibleState.Collapsed |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + private get parentResource(): ContainerAppModel | Revision { |
| 54 | + return getParentResource(this.containerApp, this.revision); |
| 55 | + } |
| 56 | + |
| 57 | + protected setProperties(): void { |
| 58 | + this.label = this.containers.length === 1 ? localize('container', 'Container') : localize('containers', 'Containers'); |
| 59 | + this.containers = nonNullValueAndProp(this.parentResource.template, 'containers'); |
| 60 | + } |
| 61 | + |
| 62 | + protected setDraftProperties(): void { |
| 63 | + this.label = this.containers.length === 1 ? localize('container*', 'Container*') : localize('containers*', 'Containers*'); |
| 64 | + this.containers = nonNullValueAndProp(ext.revisionDraftFileSystem.parseRevisionDraft(this), 'containers'); |
| 65 | + } |
| 66 | + |
| 67 | + viewProperties: ViewPropertiesModel = { |
| 68 | + label: 'Containers', |
| 69 | + getData: async () => { |
| 70 | + return this.containers.length === 1 ? this.containers[0] : JSON.stringify(this.containers) |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + hasUnsavedChanges(): boolean { |
| 75 | + // We only care about showing changes to descendants of the revision draft item when in multiple revisions mode |
| 76 | + if (this.containerApp.revisionsMode === KnownActiveRevisionsMode.Multiple && !RevisionDraftItem.hasDescendant(this)) { |
| 77 | + return false; |
| 78 | + } |
| 79 | + |
| 80 | + const draftTemplate = ext.revisionDraftFileSystem.parseRevisionDraft(this)?.containers; |
| 81 | + const currentTemplate = this.parentResource.template?.containers; |
| 82 | + |
| 83 | + if (!draftTemplate) { |
| 84 | + return false; |
| 85 | + } |
| 86 | + |
| 87 | + return !deepEqual(currentTemplate, draftTemplate); |
| 88 | + } |
| 89 | +} |
0 commit comments