blob: b6751b2108159a7cb45f27a172a60f3c0888fc5e [file] [log] [blame]
Matteo Scandolobba1d782016-07-12 15:56:23 -07001(function () {
2 'use strict';
3
4 angular.module('xos.dashboardManager')
5 .directive('dashboardForm', function(){
6 return {
7 restrict: 'E',
8 scope: {},
9 bindToController: true,
10 controllerAs: 'vm',
11 templateUrl: 'templates/dashboard-form.tpl.html',
12 controller: function($stateParams, $log, Dashboards){
13
14 this.dashboard = {
15 enabled: true
16 };
17
18 if($stateParams.id){
19 Dashboards.get({id: $stateParams.id}).$promise
20 .then(dash => {
21 this.dashboard = dash;
22 })
23 .catch(e => {
24 console.log(e);
25 })
26 }
27
28 this.formConfig = {
29 exclude: [
30 'backend_register',
31 'controllers',
32 'deployments',
33 'enacted',
34 'humanReadableName',
35 'lazy_blocked',
36 'no_policy',
37 'no_sync',
38 'policed',
39 'write_protect'
40 ],
41 actions: [
42 {
43 label: 'Save',
44 icon: 'ok',
45 cb: (item) => {
46 this.createOrUpdateDashboard(item);
47 },
48 class: 'success'
49 },
50 {
51 label: 'Esport to TOSCA',
52 icon: 'export',
53 cb: (item) => {
54 this.toTosca(item);
55 },
56 class: 'primary'
57 }
58 ],
59 formName: 'dashboardForm',
60 feedback: {
61 show: false,
62 message: 'Form submitted successfully !!!',
63 type: 'success'
64 },
65 fields: {
66 name: {
67 type: 'string',
68 validators: {
69 required: true
70 }
71 },
72 url: {
73 type: 'string',
74 validators: {
75 required: true
76 }
77 },
78 enabled: {
79 type: 'boolean'
80 }
81 }
82 };
83
84 this.createOrUpdateDashboard = dashboard => {
85 let p;
86 if(dashboard.id){
87 delete dashboard.controllers;
88 delete dashboard.deployments;
89 p = dashboard.$save();
90 }
91 else{
92 p = Dashboards.save(dashboard).$promise;
93 }
94
95 p.then(res => {
96 this.formConfig.feedback.show = true;
97 })
98 .catch(e => {
99 $log.info(e);
100 this.formConfig.feedback.show = true;
101 this.formConfig.feedback.message = e;
102 this.formConfig.feedback.type = 'error';
103 })
104 };
105
106 this.toTosca = dashboard => {
107 const yaml = {}
108 yaml[dashboard.name] = {
109 type: 'tosca.nodes.DashboardView',
110 properties: {
111 url: dashboard.url
112 }
113 };
114 this.tosca = jsyaml.dump(yaml).replace(/'/g, '');
115
116 const yamlRequirements = {
117 requirements: []
118 };
119 const dashboardRequirements = {};
120 dashboardRequirements[`${dashboard.name.toLowerCase()}_dashboard`] = {
121 node: dashboard.name,
122 relationship: 'tosca.relationships.UsesDashboard'
123 }
124 yamlRequirements.requirements.push(dashboardRequirements);
125 this.toscaRequirements = jsyaml.dump(yamlRequirements).replace(/'/g, '');
126 };
127 }
128 }
129 });
130})();