This repository has been archived by the owner on Nov 22, 2019. It is now read-only.
/
view-canvas.component.ts
407 lines (372 loc) · 17.1 KB
/
view-canvas.component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/**
* @license
* Copyright 2017 JBoss Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Component, OnDestroy, OnInit, AfterViewInit, ViewEncapsulation } from "@angular/core";
import { AddSourcesCommand } from "@dataservices/virtualization/view-editor/command/add-sources-command";
import { RemoveSourcesCommand } from "@dataservices/virtualization/view-editor/command/remove-sources-command";
import { LoggerService } from "@core/logger.service";
import { ViewEditorEvent } from "@dataservices/virtualization/view-editor/event/view-editor-event";
import { ViewEditorEventType } from "@dataservices/virtualization/view-editor/event/view-editor-event-type.enum";
import { ViewEditorService } from "@dataservices/virtualization/view-editor/view-editor.service";
import { ViewEditorPart } from "@dataservices/virtualization/view-editor/view-editor-part.enum";
import { ViewEditorI18n } from "@dataservices/virtualization/view-editor/view-editor-i18n";
import { ViewEditorProgressChangeId } from "@dataservices/virtualization/view-editor/event/view-editor-save-progress-change-id.enum";
import { PathUtils } from "@dataservices/shared/path-utils";
import { NotificationType } from "patternfly-ng";
import { Subscription } from "rxjs/Subscription";
import { CanvasNode, CanvasLink } from '@dataservices/virtualization/view-editor/view-canvas/models';
import { CanvasConstants } from '@dataservices/virtualization/view-editor/view-canvas/canvas-constants';
import { CanvasService } from '@dataservices/virtualization/view-editor/view-canvas/canvas.service';
import { ViewCanvasEvent } from "@dataservices/virtualization/view-editor/view-canvas/event/view-canvas-event";
import { ViewCanvasEventType } from "@dataservices/virtualization/view-editor/view-canvas/event/view-canvas-event-type.enum";
import * as _ from "lodash";
import { AddCompositionCommand } from "@dataservices/virtualization/view-editor/command/add-composition-command";
import { RemoveCompositionCommand } from "@dataservices/virtualization/view-editor/command/remove-composition-command";
import { CommandFactory } from "@dataservices/virtualization/view-editor/command/command-factory";
import { ViewDefinition } from "@dataservices/shared/view-definition.model";
import { Composition } from "@dataservices/shared/composition.model";
@Component({
encapsulation: ViewEncapsulation.None,
selector: "app-view-canvas",
templateUrl: "./view-canvas.component.html",
styleUrls: ["./view-canvas.component.css"]
})
export class ViewCanvasComponent implements OnInit, OnDestroy {
// used by html
public readonly noSourcesAlert = ViewEditorI18n.noSourcesAlert;
public readonly viewSaveInProgressHeader: string = "Save In Progress: ";
public readonly viewSaveSuccessHeader: string = "Save Succeeded: ";
public readonly viewSaveFailedHeader: string = "Save Failed: ";
private readonly logger: LoggerService;
private readonly editorService: ViewEditorService;
private readonly canvasService: CanvasService;
private saveViewNotificationHeader: string;
private saveViewNotificationMessage: string;
private saveViewNotificationType = NotificationType.SUCCESS;
private saveViewNotificationVisible = false;
private editorSubscription: Subscription;
private canvasSubscription: Subscription;
private viewNameSaveInProgress: string;
constructor( editorService: ViewEditorService,
logger: LoggerService,
canvasService: CanvasService) {
this.editorService = editorService;
this.logger = logger;
this.canvasService = canvasService;
}
private viewStateChanged(source: ViewEditorPart, args: any[]): void {
if (args.length === 0)
return;
if (args[0] instanceof AddSourcesCommand) {
const cmd = args[0] as AddSourcesCommand;
this.createSourceNode(cmd);
} else if (args[0] instanceof RemoveSourcesCommand) {
const cmd = args[0] as RemoveSourcesCommand;
this.removeSourceNodes(cmd);
} else if (args[0] instanceof AddCompositionCommand) {
const cmd = args[0] as AddCompositionCommand;
this.createComposition(cmd);
} else if (args[0] instanceof RemoveCompositionCommand) {
const cmd = args[0] as RemoveCompositionCommand;
this.removeComposition(cmd);
}
}
/**
* @param {ViewEditorEvent} event the event being processed
*/
public handleEditorEvent( event: ViewEditorEvent ): void {
this.logger.debug( "ViewCanvasComponent received event: " + event.toString() );
// Initialize the canvas when set - using the ViewDefinition
if ( event.typeIsEditedViewSet()) {
const viewDefn = this.editorService.getEditorView();
this.initCanvas(viewDefn);
} else if ( event.typeIsEditorViewSaveProgressChanged() ) {
if ( event.args.length !== 0 ) {
// Detect changes in view editor save progress
if ( event.args[ 0 ] === ViewEditorProgressChangeId.IN_PROGRESS ) {
this.viewNameSaveInProgress = this.editorService.getEditorView().getName();
this.saveViewNotificationHeader = this.viewSaveInProgressHeader;
this.saveViewNotificationMessage = this.getViewNotificationMessage(ViewEditorProgressChangeId.IN_PROGRESS,
this.viewNameSaveInProgress);
this.saveViewNotificationType = NotificationType.INFO;
this.saveViewNotificationVisible = true;
} else if ( event.args[ 0 ] === ViewEditorProgressChangeId.COMPLETED_SUCCESS ) {
this.saveViewNotificationHeader = this.viewSaveSuccessHeader;
this.saveViewNotificationMessage = this.getViewNotificationMessage(ViewEditorProgressChangeId.COMPLETED_SUCCESS,
this.viewNameSaveInProgress);
this.saveViewNotificationType = NotificationType.SUCCESS;
// After 8 seconds, the notification is dismissed
setTimeout(() => this.saveViewNotificationVisible = false, 8000);
} else if ( event.args[ 0 ] === ViewEditorProgressChangeId.COMPLETED_FAILED ) {
this.saveViewNotificationHeader = this.viewSaveFailedHeader;
this.saveViewNotificationMessage = this.getViewNotificationMessage(ViewEditorProgressChangeId.COMPLETED_FAILED,
this.viewNameSaveInProgress);
this.saveViewNotificationType = NotificationType.DANGER;
// After 8 seconds, the notification is dismissed
setTimeout(() => this.saveViewNotificationVisible = false, 8000);
}
}
}
else if (event.typeIsViewStateChanged()) {
this.viewStateChanged(event.source, event.args);
}
else {
this.logger.debug( "ViewCanvasComponent not handling received editor event: " + event.toString());
}
}
/**
* Generates a view notification message based on the progress type and viewName
* @param {ViewEditorProgressChangeId} viewProgressId the progress type
* @param {string} viewName the view name
*/
private getViewNotificationMessage(viewProgressId: ViewEditorProgressChangeId, viewName: string): string {
let msg = "";
const viewStr = ( viewName && viewName !== null ) ? "'" + viewName + "'" : "";
if ( viewProgressId === ViewEditorProgressChangeId.IN_PROGRESS ) {
msg = "View save in progress for " + viewStr;
} else if ( viewProgressId === ViewEditorProgressChangeId.COMPLETED_SUCCESS ) {
msg = "View save SUCCESS for " + viewStr;
} else if ( viewProgressId === ViewEditorProgressChangeId.COMPLETED_FAILED ) {
msg = "View save FAILED for " + viewStr;
}
return msg;
}
private handleCanvasEvent(event: ViewCanvasEvent): void {
switch (event.type) {
case ViewCanvasEventType.CREATE_SOURCE:
const srcEvent = ViewEditorEvent.create(ViewEditorPart.CANVAS, ViewEditorEventType.CREATE_SOURCE, event.args);
this.editorService.editorEvent.emit(srcEvent);
break;
case ViewCanvasEventType.CREATE_COMPOSITION:
const compEvent = ViewEditorEvent.create(ViewEditorPart.CANVAS, ViewEditorEventType.CREATE_COMPOSITION, event.args);
this.editorService.editorEvent.emit(compEvent);
break;
case ViewCanvasEventType.DELETE_NODE:
const deleteEvt = ViewEditorEvent.create(ViewEditorPart.CANVAS, ViewEditorEventType.DELETE_NODE, event.args);
this.editorService.editorEvent.emit(deleteEvt);
break;
case ViewCanvasEventType.CANVAS_SELECTION_CHANGED:
const selectEvent = ViewEditorEvent.create(ViewEditorPart.CANVAS, ViewEditorEventType.CANVAS_SELECTION_CHANGED, event.args);
this.editorService.editorEvent.emit(selectEvent);
break;
default:
this.logger.debug("ViewCanvasComponent not handling received canvas event: " + event.toString());
}
}
/**
* Cleanup code when destroying the canvas and properties parts.
*/
public ngOnDestroy(): void {
this.editorSubscription.unsubscribe();
this.canvasSubscription.unsubscribe();
}
/**
* Initialization code run after construction.
*/
public ngOnInit(): void {
this.editorSubscription = this.editorService.editorEvent.subscribe( ( event ) => this.handleEditorEvent( event ) );
this.canvasSubscription = this.canvasService.canvasEvent.subscribe((event) => this.handleCanvasEvent(event));
}
/**
* @returns {boolean} `true` if view being edited is readonly
*/
public get readOnly(): boolean {
return this.editorService.isReadOnly();
}
/**
* @returns {boolean} true if save view notification is to be shown
*/
public get showSaveViewNotification(): boolean {
return this.saveViewNotificationVisible;
}
/**
* Initialize the canvas with the provided ViewDefinition
* @param {ViewDefinition} viewDefn the ViewDefinition
*/
private initCanvas( viewDefn: ViewDefinition ): void {
// Make sure canvas is cleared
this.canvasService.clear();
if (viewDefn && viewDefn !== null) {
// ------------------------
// Create the compositions
// --------------------------
const compositions = viewDefn.getCompositions();
if (compositions && compositions.length > 0) {
for (const composition of compositions) {
const cmd = CommandFactory.createAddCompositionCommand(composition) as AddCompositionCommand;
this.createComposition(cmd);
}
}
// ------------------------
// Create the source nodes
// ------------------------
const sourcePaths = viewDefn.getSourcePaths();
// ------------------------
// create a command for each source path to correspond to
// unique canvas nodes for each source
// ------------------------
for (const path of sourcePaths) {
const cmd = CommandFactory.createAddSourcesCommand(path) as AddSourcesCommand;
this.createSourceNode(cmd);
}
}
this.canvasService.update(true);
}
/**
* Create canvas nodes using the provided AddSourcesCommand
* @param {AddSourcesCommand} command the AddSourcesCommand
*/
private createSourceNode(command: AddSourcesCommand): void {
const sourcePaths: string[] = command.getSourcePaths();
for (let i = 0; i < sourcePaths.length; ++i) {
const srcPath = sourcePaths[i];
const existingNode = this.getCanvasNodeForSourcePath(srcPath);
if ( existingNode && existingNode !== null) {
return;
}
const update = (i === (sourcePaths.length - 1));
const id = command.getId( );
const label = "[" + PathUtils.getConnectionName(srcPath) +
"]: " + PathUtils.getSourceName(srcPath);
this.canvasService.createNode(id, command.getPayload(srcPath), CanvasConstants.SOURCE_TYPE, label, update);
this.createLink(null, srcPath);
}
}
/**
* Remove canvas nodes using the provided RemoveSourcesCommand
* @param {RemoveSourcesCommand} command the RemoveSourcesCommand
*/
private removeSourceNodes(command: RemoveSourcesCommand): void {
const srcPaths = command.getSourcePaths();
for (let i = 0; i < srcPaths.length; ++i) {
const srcPath = srcPaths[i];
const update = (i === (srcPaths.length - 1));
const srcNode = this.getCanvasNodeForSourcePath(srcPath);
this.canvasService.deleteNode(srcNode.id, update);
}
}
/**
* Create canvas nodes using the provided AddCompositionCommand
* @param {AddCompositionCommand} command the AddCompositionCommand
*/
private createComposition(command: AddCompositionCommand): void {
const composition = command.getComposition();
const compNodeId = this.canvasService.createNode(command.getId(), command.getPayload(), CanvasConstants.COMPOSITION_TYPE, composition.getName(), true);
// Create links to source nodes if not found
this.createLink(compNodeId, composition.getLeftSourcePath());
this.createLink(compNodeId, composition.getRightSourcePath());
}
/**
* Remove canvas nodes using the provided RemoveCompositionCommand
* @param {RemoveCompositionCommand} command the RemoveCompositionCommand
*/
private removeComposition(command: RemoveCompositionCommand): void {
const composition = command.getComposition();
// an undo add command may be executed here where the command ID will include
// a timestamp that does not match any canvas node
// Need to find the "name" property in the composition and match it with the
// name property of the payload
const compNode = this.getCompositionNodeForCompositionName(composition.getName());
this.canvasService.deleteNode(compNode.id, true);
}
/**
* Generate a links between the compositionNode and CanvasNode for the supplied path, if not found
* @param {string} compositionNodeId
* @param {string} sourcePath
*/
private createLink(compositionNodeId: string, sourcePath: string): void {
const sourceNode = this.getCanvasNodeForSourcePath(sourcePath);
if (sourceNode && sourceNode !== null) {
const sourceNodeId = sourceNode.id;
// See if link exists
let linkExists = false;
const canvasLinks: CanvasLink[] = this.canvasService.links();
for (const link of canvasLinks) {
const end1Id = link.source.id;
const end2Id = link.target.id;
if (end1Id === compositionNodeId && end2Id === sourceNodeId) {
linkExists = true;
break;
} else if (end1Id === sourceNodeId && end2Id === compositionNodeId) {
linkExists = true;
break;
}
}
if ( !linkExists ) {
if ( compositionNodeId === null ) {
const compNode = this.getCompositionNodeForSourcePath(sourcePath);
if ( compNode && compNode !== null ) {
compositionNodeId = compNode.id;
}
}
if ( compositionNodeId !== null ) {
this.canvasService.createLink(sourceNodeId, compositionNodeId, true);
}
}
}
}
private getCanvasNodeForSourcePath( sourcePath: string ): CanvasNode {
let nodeForPath: CanvasNode = null;
const canvasNodes: CanvasNode[] = this.canvasService.nodes();
for (const canvasNode of canvasNodes) {
const nodeId = canvasNode.id;
if (nodeId.startsWith(AddSourcesCommand.id)) {
const payload = canvasNode.decodePayload;
if (payload === sourcePath) {
nodeForPath = canvasNode;
break;
}
}
}
return nodeForPath;
}
/* if source path == null, then a source node has been added via undo sources command
* need to find the compositions on the canvas and check if it has an source path that matches
* then return that node
*/
private getCompositionNodeForSourcePath( sourcePath: string): CanvasNode {
const canvasNodes: CanvasNode[] = this.canvasService.nodes();
for (const canvasNode of canvasNodes) {
const nodeId = canvasNode.id;
if (nodeId.startsWith(AddCompositionCommand.id)) {
const payload = canvasNode.decodePayload;
const comp = Composition.create(JSON.parse(payload));
const leftPath = comp.getLeftSourcePath();
const rightPath = comp.getRightSourcePath();
if (leftPath === sourcePath) {
return canvasNode;
} else if ( rightPath === sourcePath) {
return canvasNode;
}
}
}
}
private getCompositionNodeForCompositionName( compName: string): CanvasNode {
const canvasNodes: CanvasNode[] = this.canvasService.nodes();
for (const canvasNode of canvasNodes) {
const nodeId = canvasNode.id;
if (nodeId.startsWith(AddCompositionCommand.id)) {
const payload = canvasNode.decodePayload;
const comp = Composition.create(JSON.parse(payload));
const compNodeName = comp.getName();
if (compNodeName === compName) {
return canvasNode;
}
}
}
return null;
}
}