Matteo Scandolo | bba1d78 | 2016-07-12 15:56:23 -0700 | [diff] [blame] | 1 | (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', |
Arpit Agarwal | fecac78 | 2016-07-18 17:30:32 -0700 | [diff] [blame] | 45 | cb: (item,form) => { |
| 46 | |
| 47 | if (!form.$valid){ |
| 48 | return; |
| 49 | } |
| 50 | if(item.name && item.url && item.custom_icon){ |
| 51 | var indexOfXos = item.url.indexOf('xos'); |
| 52 | if (indexOfXos>=0){ |
| 53 | var dashboard_name = item.url.slice(indexOfXos+3,item.url.length).toLowerCase(); |
| 54 | item.icon =dashboard_name.concat('-icon.png'); |
| 55 | item.icon_active =dashboard_name.concat('-icon-active.png'); |
| 56 | } |
| 57 | else{ |
| 58 | item.icon ='default-icon.png'; |
| 59 | item.icon_active ='default-icon-active.png'; |
| 60 | } |
| 61 | } |
| 62 | else{ |
| 63 | item.icon ='default-icon.png'; |
| 64 | item.icon_active ='default-icon-active.png'; |
| 65 | } |
Matteo Scandolo | bba1d78 | 2016-07-12 15:56:23 -0700 | [diff] [blame] | 66 | this.createOrUpdateDashboard(item); |
| 67 | }, |
| 68 | class: 'success' |
| 69 | }, |
| 70 | { |
| 71 | label: 'Esport to TOSCA', |
| 72 | icon: 'export', |
| 73 | cb: (item) => { |
| 74 | this.toTosca(item); |
| 75 | }, |
| 76 | class: 'primary' |
| 77 | } |
| 78 | ], |
| 79 | formName: 'dashboardForm', |
| 80 | feedback: { |
| 81 | show: false, |
| 82 | message: 'Form submitted successfully !!!', |
| 83 | type: 'success' |
| 84 | }, |
| 85 | fields: { |
| 86 | name: { |
| 87 | type: 'string', |
| 88 | validators: { |
| 89 | required: true |
| 90 | } |
| 91 | }, |
| 92 | url: { |
| 93 | type: 'string', |
| 94 | validators: { |
| 95 | required: true |
| 96 | } |
| 97 | }, |
| 98 | enabled: { |
| 99 | type: 'boolean' |
Arpit Agarwal | fecac78 | 2016-07-18 17:30:32 -0700 | [diff] [blame] | 100 | }, |
| 101 | custom_icon: { |
| 102 | type: 'boolean' |
Matteo Scandolo | bba1d78 | 2016-07-12 15:56:23 -0700 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | }; |
| 106 | |
| 107 | this.createOrUpdateDashboard = dashboard => { |
| 108 | let p; |
| 109 | if(dashboard.id){ |
| 110 | delete dashboard.controllers; |
| 111 | delete dashboard.deployments; |
| 112 | p = dashboard.$save(); |
| 113 | } |
| 114 | else{ |
| 115 | p = Dashboards.save(dashboard).$promise; |
| 116 | } |
| 117 | |
| 118 | p.then(res => { |
| 119 | this.formConfig.feedback.show = true; |
| 120 | }) |
| 121 | .catch(e => { |
| 122 | $log.info(e); |
| 123 | this.formConfig.feedback.show = true; |
| 124 | this.formConfig.feedback.message = e; |
Arpit Agarwal | fecac78 | 2016-07-18 17:30:32 -0700 | [diff] [blame] | 125 | this.formConfig.feedback.type='danger'; |
Matteo Scandolo | bba1d78 | 2016-07-12 15:56:23 -0700 | [diff] [blame] | 126 | }) |
| 127 | }; |
| 128 | |
| 129 | this.toTosca = dashboard => { |
| 130 | const yaml = {} |
| 131 | yaml[dashboard.name] = { |
| 132 | type: 'tosca.nodes.DashboardView', |
| 133 | properties: { |
| 134 | url: dashboard.url |
| 135 | } |
| 136 | }; |
| 137 | this.tosca = jsyaml.dump(yaml).replace(/'/g, ''); |
| 138 | |
| 139 | const yamlRequirements = { |
| 140 | requirements: [] |
| 141 | }; |
| 142 | const dashboardRequirements = {}; |
| 143 | dashboardRequirements[`${dashboard.name.toLowerCase()}_dashboard`] = { |
| 144 | node: dashboard.name, |
| 145 | relationship: 'tosca.relationships.UsesDashboard' |
| 146 | } |
| 147 | yamlRequirements.requirements.push(dashboardRequirements); |
| 148 | this.toscaRequirements = jsyaml.dump(yamlRequirements).replace(/'/g, ''); |
| 149 | }; |
| 150 | } |
| 151 | } |
| 152 | }); |
| 153 | })(); |