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