blob: cbe8445caed7633dd79f1d49c2cf98139c8ad0a5 [file] [log] [blame]
Ittay Stern6f900cc2018-08-29 17:01:32 +03001import {Component, OnInit, ViewChild} from '@angular/core';
2import {DrawingBoardTreeComponent} from "./drawing-board-tree/drawing-board-tree.component";
3import {AvailableModelsTreeComponent} from "./available-models-tree/available-models-tree.component";
Sonsino, Ofir (os0695)ff76b5e2018-07-10 15:57:37 +03004import {ITreeNode} from "angular-tree-component/dist/defs/api";
5import {TreeComponent} from 'angular-tree-component';
Ittay Stern6f900cc2018-08-29 17:01:32 +03006import {ActivatedRoute} from "@angular/router";
7import * as _ from 'lodash';
8import {DrawingBoardModes} from "./drawing-board.modes";
9import {NgRedux} from "@angular-redux/store";
10import {AppState} from "../../shared/store/reducers";
11import {updateDrawingBoardStatus} from "../../shared/storeUtil/utils/global/global.actions";
12import {FeatureFlagsService, Features} from "../../shared/services/featureFlag/feature-flags.service";
13import {ComponentInfoService} from "./component-info/component-info.service";
14import {ComponentInfoModel, ComponentInfoType} from "./component-info/component-info-model";
Sonsino, Ofir (os0695)ff76b5e2018-07-10 15:57:37 +030015
16@Component({
17 selector: 'service-planning',
18 templateUrl: './service-planning.component.html',
19 styleUrls: ['./service-planning.component.scss']
20})
21
Ittay Stern6f900cc2018-08-29 17:01:32 +030022export class ServicePlanningComponent implements OnInit {
Sonsino, Ofir (os0695)ff76b5e2018-07-10 15:57:37 +030023
Ittay Stern6f900cc2018-08-29 17:01:32 +030024 constructor(private route: ActivatedRoute,
25 private store: NgRedux<AppState>) {
26 }
27
28 pageMode: DrawingBoardModes = DrawingBoardModes.CREATE;
Sonsino, Ofir (os0695)ff76b5e2018-07-10 15:57:37 +030029 @ViewChild(DrawingBoardTreeComponent) drawingModelTree;
30 @ViewChild(AvailableModelsTreeComponent) availableModelTree;
31
32 isShowTree(): boolean {
33 return true;
34 }
35
36 public highlightNodeBySelectingInstance(modelId: number): void {
Ittay Stern6f900cc2018-08-29 17:01:32 +030037 // modelId might be undefined, e.g., if selected instance has no source in model
38 let matchInstance = modelId ? this.availableModelTree.tree.treeModel.getNodeBy((node: ITreeNode) => (node.data.modelUniqueId) === modelId) : undefined;
39 if (matchInstance) {
40 matchInstance.setActiveAndVisible().expand();
41 } else {
42 this.clearSelectionInTree(this.availableModelTree.tree);
43 }
Sonsino, Ofir (os0695)ff76b5e2018-07-10 15:57:37 +030044 }
45
Ittay Stern6f900cc2018-08-29 17:01:32 +030046 public highlightInstancesBySelectingNode(uniqueId: string): void {
47 if (this.isShowTree()) {
Sonsino, Ofir (os0695)ff76b5e2018-07-10 15:57:37 +030048 let _this = this;
Ittay Stern6f900cc2018-08-29 17:01:32 +030049 let matchInstances = _this.searchTree(uniqueId);
Sonsino, Ofir (os0695)ff76b5e2018-07-10 15:57:37 +030050 if (!matchInstances.length)
51 _this.clearSelectionInTree(_this.drawingModelTree.tree);
52 matchInstances.forEach(function (instance, index) {
Ittay Stern6f900cc2018-08-29 17:01:32 +030053 let multi: boolean = !!index;
Sonsino, Ofir (os0695)ff76b5e2018-07-10 15:57:37 +030054 _this.drawingModelTree.tree.treeModel.getNodeById(instance.id)
55 .setActiveAndVisible(multi).expand();
56 });
57
58 }
59 }
60
61 clearSelectionInTree(tree: TreeComponent): void {
62 let activateNode = tree.treeModel.getActiveNode();
63 activateNode ? activateNode.toggleActivated().blur() : null;
64 }
65
Ittay Stern6f900cc2018-08-29 17:01:32 +030066 searchTree(uniqueId: string) {
Sonsino, Ofir (os0695)ff76b5e2018-07-10 15:57:37 +030067 let _this = this;
68 let results = [];
69 let nodes = _this.drawingModelTree.nodes;
70 nodes.forEach(function (node) {
Ittay Stern6f900cc2018-08-29 17:01:32 +030071 _this.searchTreeNode(node, uniqueId, results);
Sonsino, Ofir (os0695)ff76b5e2018-07-10 15:57:37 +030072 });
73 return results;
74 }
75
Ittay Stern6f900cc2018-08-29 17:01:32 +030076 searchTreeNode(node, uniqueId: string, results): void {
77 if ((node.modelUniqueId) === uniqueId) {
Sonsino, Ofir (os0695)ff76b5e2018-07-10 15:57:37 +030078 results.push(node);
79 }
Ittay Stern6f900cc2018-08-29 17:01:32 +030080
81 if (node.children != null) {
82 for (let i = 0; i < node.children.length; i++) {
83 this.searchTreeNode(node.children[i], uniqueId, results);
Sonsino, Ofir (os0695)ff76b5e2018-07-10 15:57:37 +030084 }
85 }
86 }
87
Ittay Stern6f900cc2018-08-29 17:01:32 +030088 ngOnInit(): void {
89 this.pageMode = (!_.isNil(this.route.routeConfig.path) && this.route.routeConfig.path !== "") ? this.route.routeConfig.path as DrawingBoardModes : DrawingBoardModes.CREATE;
90 this.store.dispatch(updateDrawingBoardStatus(this.pageMode));
91 }
Sonsino, Ofir (os0695)ff76b5e2018-07-10 15:57:37 +030092
Ittay Stern6f900cc2018-08-29 17:01:32 +030093 isShowComponentInfo():boolean {
94 return FeatureFlagsService.getFlagState(Features.FLAG_1906_COMPONENT_INFO, this.store)
95 }
Sonsino, Ofir (os0695)ff76b5e2018-07-10 15:57:37 +030096
Ittay Stern6f900cc2018-08-29 17:01:32 +030097 clickOutside(): void{
98 this.clearSelectionInTree(this.drawingModelTree.tree);
99 this.clearSelectionInTree(this.availableModelTree.tree);
100 ComponentInfoService.triggerComponentInfoChange.next(new ComponentInfoModel(ComponentInfoType.SERVICE, [], []))
Sonsino, Ofir (os0695)ff76b5e2018-07-10 15:57:37 +0300101 }
102}
103
Ittay Stern6f900cc2018-08-29 17:01:32 +0300104export class ServicePlanningEmptyComponent extends ServicePlanningComponent {
105 isShowTree(): boolean {
106 return false;
107 }
108}
Sonsino, Ofir (os0695)ff76b5e2018-07-10 15:57:37 +0300109