aboutsummaryrefslogtreecommitdiffstats
path: root/catalog-ui/src/app/view-models/workspace/tabs/icons/icons-view-model.ts
blob: 03dad2cc06ed7b8cbdfcea44ab444f61678f09d6 (plain)
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
/**
 * Created by obarda on 4/4/2016.
 */
'use strict';
import {ComponentFactory} from "app/utils";
import {AvailableIconsService} from "app/services";
import {IWorkspaceViewModelScope} from "app/view-models/workspace/workspace-view-model";
import {IMainCategory, ISubCategory} from "app/models";

export interface IIconsScope extends IWorkspaceViewModelScope {
    icons:Array<string>;
    iconSprite:string;
    setComponentIcon(iconSrc:string):void;
}

export class IconsViewModel {

    static '$inject' = [
        '$scope',
        'Sdc.Services.AvailableIconsService',
        'ComponentFactory',
        '$state'
    ];

    constructor(private $scope:IIconsScope,
                private availableIconsService:AvailableIconsService,
                private ComponentFactory:ComponentFactory,
                private $state:ng.ui.IStateService) {


        this.initScope();
        this.initIcons();
        this.$scope.updateSelectedMenuItem();
        this.$scope.iconSprite = this.$scope.component.iconSprite;

        if (this.$scope.component.isResource()) {
            this.initVendor();
        }
    }

    private initialIcon:string = this.$scope.component.icon;
    private initIcons = ():void => {

        // For subcategories that where created by admin, there is no icons
        this.$scope.icons = new Array<string>();
        if (this.$scope.component.categories && this.$scope.component.categories.length > 0) {

            _.forEach(this.$scope.component.categories, (category:IMainCategory):void => {
                if (category.icons) {
                    this.$scope.icons = this.$scope.icons.concat(category.icons);
                }
                if (category.subcategories) {
                    _.forEach(category.subcategories, (subcategory:ISubCategory):void => {
                        if (subcategory.icons) {
                            this.$scope.icons = this.$scope.icons.concat(subcategory.icons);
                        }
                    });
                }
            });
        }

        if (this.$scope.component.isResource()) {
            let resourceType:string = this.$scope.component.getComponentSubType();
            if (resourceType === 'VL') {
                this.$scope.icons = ['vl'];
            }
            if (resourceType === 'CP') {
                this.$scope.icons = ['cp'];
            }
        }

        if (this.$scope.icons.length === 0) {
            this.$scope.icons = this.availableIconsService.getIcons(this.$scope.component.componentType);
        }
        //we always add the defual icon to the list
        this.$scope.icons.push('defaulticon');
    };

    private initVendor = ():void => {
        let vendors:Array<string> = this.availableIconsService.getIcons(this.$scope.component.componentType).slice(5, 19);
        let vendorName = this.$scope.component.vendorName.toLowerCase();
        if ('at&t' === vendorName) {
            vendorName = 'att';
        }
        if ('nokia' === vendorName) {
            vendorName = 'nokiasiemens';
        }
        let vendor:string = _.find(vendors, (vendor:string)=> {
            return vendor.replace(/[_]/g, '').toLowerCase() === vendorName;
        });

        if (vendor && this.$scope.icons.indexOf(vendor) === -1) {
            this.$scope.icons.push(vendor);
        }
    };

    private initScope():void {
        this.$scope.icons = [];
        this.$scope.setValidState(true);
        //if(this.$scope.component.icon === DEFAULT_ICON){
        //    //this.$scope.setValidState(false);
        //}

        this.$scope.setComponentIcon = (iconSrc:string):void => {
            this.$state.current.data.unsavedChanges = !this.$scope.isViewMode() && (iconSrc != this.initialIcon);
            this.$scope.component.icon = iconSrc;
            // this.$scope.setValidState(true);
        };

    }
}