2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
20 import {Component, IAppMenu, LeftPanelModel, NodesFactory, LeftPaletteComponent, CompositionCiNodeBase, ComponentInstance} from "app/models";
21 import {CompositionGraphGeneralUtils} from "../composition-graph/utils/composition-graph-general-utils";
22 import {EventListenerService} from "app/services";
23 import {ResourceType, GRAPH_EVENTS, EVENTS, ComponentInstanceFactory, ModalsHandler} from "app/utils";
24 import 'sdc-angular-dragdrop';
25 import {LeftPaletteLoaderService} from "../../../services/components/utils/composition-left-palette-service";
26 import {Resource} from "app/models/components/resource";
27 import {ComponentType} from "app/utils/constants";
29 interface IPaletteScope {
30 components:Array<LeftPaletteComponent>;
31 currentComponent:Component;
33 displaySortedCategories:any;
34 expandedSection:string;
37 event:JQueryEventObject,
38 components:LeftPaletteComponent,
42 sectionClick:(section:string)=>void;
43 searchComponents:(searchText:string)=>void;
44 onMouseOver:(displayComponent:LeftPaletteComponent)=>void;
45 onMouseOut:(displayComponent:LeftPaletteComponent)=>void;
46 dragStartCallback:(event:JQueryEventObject, ui, displayComponent:LeftPaletteComponent)=>void;
47 dragStopCallback:()=>void;
48 onDragCallback:(event:JQueryEventObject) => void;
50 setElementTemplate:(e:JQueryEventObject)=>void;
58 export class Palette implements ng.IDirective {
59 constructor(private $log:ng.ILogService,
60 private LeftPaletteLoaderService: LeftPaletteLoaderService,
62 private ComponentFactory,
63 private ComponentInstanceFactory:ComponentInstanceFactory,
64 private NodesFactory:NodesFactory,
65 private CompositionGraphGeneralUtils:CompositionGraphGeneralUtils,
66 private EventListenerService:EventListenerService,
67 private sdcMenu:IAppMenu,
68 private ModalsHandler:ModalsHandler) {
72 private fetchingComponentFromServer:boolean = false;
73 private nodeHtmlSubstitute:JQuery;
76 currentComponent: '=',
81 template = require('./palette.html');
83 link = (scope:IPaletteScope, el:JQuery) => {
84 this.nodeHtmlSubstitute = $('<div class="node-substitute"><span></span><img /></div>');
85 el.append(this.nodeHtmlSubstitute);
86 this.registerEventListenerForLeftPalette(scope);
87 // this.LeftPaletteLoaderService.loadLeftPanel(scope.currentComponent.componentType);
89 this.initComponents(scope);
90 this.initEvents(scope);
91 this.initDragEvents(scope);
92 this._initExpandedSection(scope, '');
93 el.on('$destroy', ()=> {
94 //remove listener of download event
95 this.unRegisterEventListenerForLeftPalette(scope);
99 private getUpdateLeftPaletteEventName = (component:Component):string => {
100 switch (component.componentType) {
101 case ComponentType.SERVICE:
102 return EVENTS.SERVICE_LEFT_PALETTE_UPDATE_EVENT;
103 case ComponentType.RESOURCE:
104 if((<Resource>component).resourceType == ResourceType.PNF){
105 return EVENTS.RESOURCE_PNF_LEFT_PALETTE_UPDATE_EVENT;
107 return EVENTS.RESOURCE_LEFT_PALETTE_UPDATE_EVENT;
110 console.log('ERROR: Component type '+ component.componentType + ' is not exists');
114 private registerEventListenerForLeftPalette = (scope:IPaletteScope):void => {
115 let updateEventName:string = this.getUpdateLeftPaletteEventName(scope.currentComponent);
116 this.EventListenerService.registerObserverCallback(updateEventName, () => {
117 this.updateLeftPanelDisplay(scope);
121 private unRegisterEventListenerForLeftPalette = (scope:IPaletteScope):void => {
122 let updateEventName:string = this.getUpdateLeftPaletteEventName(scope.currentComponent);
123 this.EventListenerService.unRegisterObserver(updateEventName);
126 private leftPanelResourceFilter(resourcesNotAbstract:Array<LeftPaletteComponent>, resourceFilterTypes:Array<string>):Array<LeftPaletteComponent> {
127 let filterResources = _.filter(resourcesNotAbstract, (component) => {
128 return resourceFilterTypes.indexOf(component.getComponentSubType()) > -1;
130 return filterResources;
133 private initLeftPanel(leftPanelComponents:Array<LeftPaletteComponent>, resourceFilterTypes:Array<string>):LeftPanelModel {
134 let leftPanelModel = new LeftPanelModel();
136 if (resourceFilterTypes && resourceFilterTypes.length) {
137 leftPanelComponents = this.leftPanelResourceFilter(leftPanelComponents, resourceFilterTypes);
139 leftPanelModel.numberOfElements = leftPanelComponents && leftPanelComponents.length || 0;
141 if (leftPanelComponents && leftPanelComponents.length) {
143 let categories:any = _.groupBy(leftPanelComponents, 'mainCategory');
144 for (let category in categories)
145 categories[category] = _.groupBy(categories[category], 'subCategory');
147 leftPanelModel.sortedCategories = categories;
149 return leftPanelModel;
153 private initEvents(scope:IPaletteScope) {
158 scope.sectionClick = (section:string) => {
159 if (section === scope.expandedSection) {
160 scope.expandedSection = '';
163 scope.expandedSection = section;
166 scope.onMouseOver = (displayComponent:LeftPaletteComponent) => {
167 if (scope.isOnDrag) {
170 scope.isOnDrag = true;
172 this.EventListenerService.notifyObservers(GRAPH_EVENTS.ON_PALETTE_COMPONENT_HOVER_IN, displayComponent);
173 this.$log.debug('palette::onMouseOver:: fired');
175 // if (this.CompositionGraphGeneralUtils.componentRequirementsAndCapabilitiesCaching.containsKey(displayComponent.uniqueId)) {
176 // this.$log.debug(`palette::onMouseOver:: component id ${displayComponent.uniqueId} found in cache`);
177 // let cacheComponent:Component = this.CompositionGraphGeneralUtils.componentRequirementsAndCapabilitiesCaching.getValue(displayComponent.uniqueId);
179 // //TODO: Danny: fire event to highlight matching nodes
180 // //showMatchingNodes(cacheComponent);
184 // this.$log.debug(`palette::onMouseOver:: component id ${displayComponent.uniqueId} not found in cache, initiating server get`);
185 // // This will bring the component from the server including requirements and capabilities
186 // // Check that we do not fetch many times, because only in the success we add the component to componentRequirementsAndCapabilitiesCaching
187 // if (this.fetchingComponentFromServer) {
191 // this.fetchingComponentFromServer = true;
192 // this.ComponentFactory.getComponentFromServer(displayComponent.componentSubType, displayComponent.uniqueId)
193 // .then((component:Component) => {
194 // this.$log.debug(`palette::onMouseOver:: component id ${displayComponent.uniqueId} fetch success`);
195 // // this.LeftPaletteLoaderService.updateSpecificComponentLeftPalette(component, scope.currentComponent.componentType);
196 // this.CompositionGraphGeneralUtils.componentRequirementsAndCapabilitiesCaching.setValue(component.uniqueId, component);
197 // this.fetchingComponentFromServer = false;
199 // //TODO: Danny: fire event to highlight matching nodes
200 // //showMatchingNodes(component);
203 // this.$log.debug('palette::onMouseOver:: component id fetch error');
204 // this.fetchingComponentFromServer = false;
210 scope.onMouseOut = () => {
211 scope.isOnDrag = false;
212 this.EventListenerService.notifyObservers(GRAPH_EVENTS.ON_PALETTE_COMPONENT_HOVER_OUT);
216 private initComponents(scope:IPaletteScope) {
217 scope.searchComponents = (searchText:any):void => {
218 scope.displaySortedCategories = this._searchComponents(searchText, scope.model.sortedCategories);
219 this._initExpandedSection(scope, searchText);
222 scope.isDragable = scope.currentComponent.isComplex();
223 this.updateLeftPanelDisplay(scope);
226 private updateLeftPanelDisplay(scope:IPaletteScope) {
227 let entityType:string = scope.currentComponent.componentType.toLowerCase();
228 let resourceFilterTypes:Array<string> = this.sdcConfig.resourceTypesFilter[entityType];
229 scope.components = this.LeftPaletteLoaderService.getLeftPanelComponentsForDisplay(scope.currentComponent);
230 scope.model = this.initLeftPanel(scope.components, resourceFilterTypes);
231 scope.displaySortedCategories = angular.copy(scope.model.sortedCategories);
234 private _initExpandedSection(scope:IPaletteScope, searchText:string):void {
235 if (searchText == '') {
236 let isContainingCategory:boolean = false;
237 let categoryToExpand:string;
238 if (scope.currentComponent && scope.currentComponent.categories && scope.currentComponent.categories[0]) {
239 categoryToExpand = this.sdcMenu.categoriesDictionary[scope.currentComponent.categories[0].name];
240 for (let category in scope.model.sortedCategories) {
241 if (categoryToExpand == category) {
242 isContainingCategory = true;
247 isContainingCategory ? scope.expandedSection = categoryToExpand : scope.expandedSection = 'Generic';
250 scope.expandedSection = Object.keys(scope.displaySortedCategories).sort()[0];
254 private initDragEvents(scope:IPaletteScope) {
255 scope.dragStartCallback = (event:IDragDropEvent, ui, displayComponent:LeftPaletteComponent):void => {
256 if (scope.isLoading || !scope.isDragable || scope.isViewOnly) {
260 let component = _.find(this.LeftPaletteLoaderService.getLeftPanelComponentsForDisplay(scope.currentComponent), (componentFullData:LeftPaletteComponent) => {
261 return displayComponent.uniqueId === componentFullData.uniqueId;
263 this.EventListenerService.notifyObservers(GRAPH_EVENTS.ON_PALETTE_COMPONENT_DRAG_START, scope.dragElement, component);
265 scope.isOnDrag = true;
267 // this.graphUtils.showMatchingNodes(component, myDiagram, scope.sdcConfig.imagesPath);
268 // document.addEventListener('mousemove', moveOnDocument);
269 event.dataTransfer.component = component;
272 scope.dragStopCallback = () => {
273 scope.isOnDrag = false;
276 scope.onDragCallback = (event:IDragDropEvent):void => {
277 this.EventListenerService.notifyObservers(GRAPH_EVENTS.ON_PALETTE_COMPONENT_DRAG_ACTION, event);
279 scope.setElementTemplate = (e) => {
280 let dragComponent:LeftPaletteComponent = _.find(this.LeftPaletteLoaderService.getLeftPanelComponentsForDisplay(scope.currentComponent),
281 (fullComponent:LeftPaletteComponent) => {
282 return (<any>angular.element(e.currentTarget).scope()).component.uniqueId === fullComponent.uniqueId;
284 let componentInstance:ComponentInstance = this.ComponentInstanceFactory.createComponentInstanceFromComponent(dragComponent);
285 let node:CompositionCiNodeBase = this.NodesFactory.createNode(componentInstance);
287 // myDiagram.dragFromPalette = node;
288 this.nodeHtmlSubstitute.find("img").attr('src', node.img);
289 scope.dragElement = this.nodeHtmlSubstitute.clone().show();
291 return scope.dragElement;
295 private _searchComponents = (searchText:string, categories:any):void => {
296 let displaySortedCategories = angular.copy(categories);
297 if (searchText != '') {
298 angular.forEach(categories, function (category:any, categoryKey) {
300 angular.forEach(category, function (subcategory:Array<LeftPaletteComponent>, subcategoryKey) {
301 let filteredResources = [];
302 angular.forEach(subcategory, function (component:LeftPaletteComponent) {
304 let resourceFilterTerm:string = component.searchFilterTerms;
305 if (resourceFilterTerm.indexOf(searchText.toLowerCase()) >= 0) {
306 filteredResources.push(component);
309 if (filteredResources.length > 0) {
310 displaySortedCategories[categoryKey][subcategoryKey] = filteredResources;
313 delete displaySortedCategories[categoryKey][subcategoryKey];
316 if (!(Object.keys(displaySortedCategories[categoryKey]).length > 0)) {
317 delete displaySortedCategories[categoryKey];
322 return displaySortedCategories;
325 public static factory = ($log,
326 LeftPaletteLoaderService,
329 ComponentInstanceFactory,
331 CompositionGraphGeneralUtils,
332 EventListenerService,
335 return new Palette($log,
336 LeftPaletteLoaderService,
339 ComponentInstanceFactory,
341 CompositionGraphGeneralUtils,
342 EventListenerService,
348 Palette.factory.$inject = [
350 'LeftPaletteLoaderService',
353 'ComponentInstanceFactory',
355 'CompositionGraphGeneralUtils',
356 'EventListenerService',