|
| 1 | +import { NodeService } from "../../../../../wise5/services/nodeService"; |
| 2 | +import { TeacherProjectService } from "../../../../../wise5/services/teacherProjectService"; |
| 3 | + |
| 4 | +export class EditAdvancedComponentAngularJSController { |
| 5 | + |
| 6 | + authoringComponentContent: any; |
| 7 | + componentId: string; |
| 8 | + nodeId: string; |
| 9 | + allowedConnectedComponentTypes: string[] = []; |
| 10 | + idToOrder: any; |
| 11 | + |
| 12 | + static $inject = ['NodeService', 'ProjectService']; |
| 13 | + |
| 14 | + constructor(protected NodeService: NodeService, protected ProjectService: TeacherProjectService) { |
| 15 | + } |
| 16 | + |
| 17 | + $onInit(): void { |
| 18 | + this.authoringComponentContent = this.ProjectService.getComponentByNodeIdAndComponentId( |
| 19 | + this.nodeId, this.componentId); |
| 20 | + this.idToOrder = this.ProjectService.idToOrder; |
| 21 | + } |
| 22 | + |
| 23 | + componentChanged(): void { |
| 24 | + this.ProjectService.nodeChanged(); |
| 25 | + } |
| 26 | + |
| 27 | + addConnectedComponent(): void { |
| 28 | + this.addConnectedComponentAndSetComponentIdIfPossible(); |
| 29 | + this.componentChanged(); |
| 30 | + } |
| 31 | + |
| 32 | + addConnectedComponentAndSetComponentIdIfPossible(): void { |
| 33 | + const connectedComponent = this.createConnectedComponent(); |
| 34 | + if (this.authoringComponentContent.connectedComponents == null) { |
| 35 | + this.authoringComponentContent.connectedComponents = []; |
| 36 | + } |
| 37 | + this.authoringComponentContent.connectedComponents.push(connectedComponent); |
| 38 | + this.automaticallySetConnectedComponentComponentIdIfPossible(connectedComponent); |
| 39 | + } |
| 40 | + |
| 41 | + automaticallySetConnectedComponentComponentIdIfPossible(connectedComponent: any): void { |
| 42 | + let numberOfAllowedComponents = 0; |
| 43 | + let allowedComponent = null; |
| 44 | + for (const component of this.ProjectService.getComponentsByNodeId(connectedComponent.nodeId)) { |
| 45 | + if (this.isConnectedComponentTypeAllowed(component.type) && |
| 46 | + component.id != this.componentId) { |
| 47 | + numberOfAllowedComponents += 1; |
| 48 | + allowedComponent = component; |
| 49 | + } |
| 50 | + } |
| 51 | + if (numberOfAllowedComponents === 1) { |
| 52 | + connectedComponent.componentId = allowedComponent.id; |
| 53 | + connectedComponent.type = 'importWork'; |
| 54 | + } |
| 55 | + this.automaticallySetConnectedComponentTypeIfPossible(connectedComponent); |
| 56 | + } |
| 57 | + |
| 58 | + connectedComponentTypeChanged(connectedComponent: any): void { |
| 59 | + this.componentChanged(); |
| 60 | + } |
| 61 | + |
| 62 | + connectedComponentNodeIdChanged(connectedComponent: any): void { |
| 63 | + connectedComponent.componentId = null; |
| 64 | + connectedComponent.type = null; |
| 65 | + this.automaticallySetConnectedComponentComponentIdIfPossible(connectedComponent); |
| 66 | + this.componentChanged(); |
| 67 | + } |
| 68 | + |
| 69 | + connectedComponentComponentIdChanged(connectedComponent: any): void { |
| 70 | + this.automaticallySetConnectedComponentTypeIfPossible(connectedComponent); |
| 71 | + this.componentChanged(); |
| 72 | + } |
| 73 | + |
| 74 | + isConnectedComponentTypeAllowed(componentType: string): boolean { |
| 75 | + return this.allowedConnectedComponentTypes.includes(componentType); |
| 76 | + } |
| 77 | + |
| 78 | + automaticallySetConnectedComponentTypeIfPossible(connectedComponent: any): void { |
| 79 | + if (connectedComponent.componentId != null) { |
| 80 | + connectedComponent.type = 'importWork'; |
| 81 | + } |
| 82 | + this.automaticallySetConnectedComponentFieldsIfPossible(connectedComponent); |
| 83 | + } |
| 84 | + |
| 85 | + automaticallySetConnectedComponentFieldsIfPossible(connectedComponent: any): void { |
| 86 | + } |
| 87 | + |
| 88 | + createConnectedComponent(): any { |
| 89 | + return { |
| 90 | + nodeId: this.nodeId, |
| 91 | + componentId: null, |
| 92 | + type: null |
| 93 | + }; |
| 94 | + } |
| 95 | + |
| 96 | + deleteConnectedComponent(index: number): void { |
| 97 | + if (confirm($localize`Are you sure you want to delete this connected component?`)) { |
| 98 | + if (this.authoringComponentContent.connectedComponents != null) { |
| 99 | + this.authoringComponentContent.connectedComponents.splice(index, 1); |
| 100 | + } |
| 101 | + this.componentChanged(); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + getNodePositionAndTitleByNodeId(nodeId: string): string { |
| 106 | + return this.ProjectService.getNodePositionAndTitleByNodeId(nodeId); |
| 107 | + } |
| 108 | + |
| 109 | + isApplicationNode(nodeId: string): boolean { |
| 110 | + return this.ProjectService.isApplicationNode(nodeId); |
| 111 | + } |
| 112 | + |
| 113 | + getComponentsByNodeId(nodeId: string): any[] { |
| 114 | + return this.ProjectService.getComponentsByNodeId(nodeId); |
| 115 | + } |
| 116 | + |
| 117 | + setShowSubmitButtonValue(show: boolean): void { |
| 118 | + if (show == null || show == false) { |
| 119 | + this.authoringComponentContent.showSaveButton = false; |
| 120 | + this.authoringComponentContent.showSubmitButton = false; |
| 121 | + } else { |
| 122 | + this.authoringComponentContent.showSaveButton = true; |
| 123 | + this.authoringComponentContent.showSubmitButton = true; |
| 124 | + } |
| 125 | + this.NodeService.broadcastComponentShowSubmitButtonValueChanged({ |
| 126 | + nodeId: this.nodeId, |
| 127 | + componentId: this.componentId, |
| 128 | + showSubmitButton: show |
| 129 | + }); |
| 130 | + } |
| 131 | + |
| 132 | + getConnectedComponentType( |
| 133 | + {nodeId, componentId}: { nodeId: string, componentId: string }) { |
| 134 | + const component = this.ProjectService.getComponentByNodeIdAndComponentId(nodeId, componentId); |
| 135 | + if (component != null) { |
| 136 | + return component.type; |
| 137 | + } |
| 138 | + return null; |
| 139 | + } |
| 140 | + |
| 141 | +} |
0 commit comments