Matteo Scandolo | d2044a4 | 2017-08-07 16:08:28 -0700 | [diff] [blame^] | 1 | |
| 2 | /* |
| 3 | * Copyright 2017-present Open Networking Foundation |
| 4 | |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | |
Matteo Scandolo | bba1d78 | 2016-07-12 15:56:23 -0700 | [diff] [blame] | 19 | (function () { |
| 20 | 'use strict'; |
| 21 | |
| 22 | angular.module('xos.dashboardManager') |
| 23 | .directive('dashboardForm', function(){ |
| 24 | return { |
| 25 | restrict: 'E', |
| 26 | scope: {}, |
| 27 | bindToController: true, |
| 28 | controllerAs: 'vm', |
| 29 | templateUrl: 'templates/dashboard-form.tpl.html', |
| 30 | controller: function($stateParams, $log, Dashboards){ |
| 31 | |
| 32 | this.dashboard = { |
| 33 | enabled: true |
| 34 | }; |
| 35 | |
| 36 | if($stateParams.id){ |
| 37 | Dashboards.get({id: $stateParams.id}).$promise |
| 38 | .then(dash => { |
| 39 | this.dashboard = dash; |
| 40 | }) |
| 41 | .catch(e => { |
| 42 | console.log(e); |
| 43 | }) |
| 44 | } |
| 45 | |
| 46 | this.formConfig = { |
| 47 | exclude: [ |
| 48 | 'backend_register', |
| 49 | 'controllers', |
| 50 | 'deployments', |
| 51 | 'enacted', |
| 52 | 'humanReadableName', |
| 53 | 'lazy_blocked', |
| 54 | 'no_policy', |
| 55 | 'no_sync', |
| 56 | 'policed', |
Matteo Scandolo | 89634ce | 2016-08-03 17:27:32 -0700 | [diff] [blame] | 57 | 'write_protect', |
| 58 | 'icon', |
| 59 | 'icon_active' |
Matteo Scandolo | bba1d78 | 2016-07-12 15:56:23 -0700 | [diff] [blame] | 60 | ], |
| 61 | actions: [ |
| 62 | { |
| 63 | label: 'Save', |
| 64 | icon: 'ok', |
Arpit Agarwal | fecac78 | 2016-07-18 17:30:32 -0700 | [diff] [blame] | 65 | cb: (item,form) => { |
| 66 | |
| 67 | if (!form.$valid){ |
| 68 | return; |
| 69 | } |
| 70 | if(item.name && item.url && item.custom_icon){ |
| 71 | var indexOfXos = item.url.indexOf('xos'); |
| 72 | if (indexOfXos>=0){ |
| 73 | var dashboard_name = item.url.slice(indexOfXos+3,item.url.length).toLowerCase(); |
| 74 | item.icon =dashboard_name.concat('-icon.png'); |
| 75 | item.icon_active =dashboard_name.concat('-icon-active.png'); |
| 76 | } |
| 77 | else{ |
| 78 | item.icon ='default-icon.png'; |
| 79 | item.icon_active ='default-icon-active.png'; |
| 80 | } |
| 81 | } |
| 82 | else{ |
| 83 | item.icon ='default-icon.png'; |
| 84 | item.icon_active ='default-icon-active.png'; |
| 85 | } |
Matteo Scandolo | bba1d78 | 2016-07-12 15:56:23 -0700 | [diff] [blame] | 86 | this.createOrUpdateDashboard(item); |
| 87 | }, |
| 88 | class: 'success' |
| 89 | }, |
| 90 | { |
| 91 | label: 'Esport to TOSCA', |
| 92 | icon: 'export', |
| 93 | cb: (item) => { |
| 94 | this.toTosca(item); |
| 95 | }, |
| 96 | class: 'primary' |
| 97 | } |
| 98 | ], |
| 99 | formName: 'dashboardForm', |
| 100 | feedback: { |
| 101 | show: false, |
| 102 | message: 'Form submitted successfully !!!', |
| 103 | type: 'success' |
| 104 | }, |
| 105 | fields: { |
| 106 | name: { |
| 107 | type: 'string', |
| 108 | validators: { |
| 109 | required: true |
| 110 | } |
| 111 | }, |
| 112 | url: { |
| 113 | type: 'string', |
| 114 | validators: { |
| 115 | required: true |
| 116 | } |
| 117 | }, |
| 118 | enabled: { |
| 119 | type: 'boolean' |
Arpit Agarwal | fecac78 | 2016-07-18 17:30:32 -0700 | [diff] [blame] | 120 | }, |
| 121 | custom_icon: { |
| 122 | type: 'boolean' |
Matteo Scandolo | bba1d78 | 2016-07-12 15:56:23 -0700 | [diff] [blame] | 123 | } |
| 124 | } |
| 125 | }; |
| 126 | |
| 127 | this.createOrUpdateDashboard = dashboard => { |
| 128 | let p; |
| 129 | if(dashboard.id){ |
| 130 | delete dashboard.controllers; |
| 131 | delete dashboard.deployments; |
| 132 | p = dashboard.$save(); |
| 133 | } |
| 134 | else{ |
| 135 | p = Dashboards.save(dashboard).$promise; |
| 136 | } |
| 137 | |
| 138 | p.then(res => { |
| 139 | this.formConfig.feedback.show = true; |
| 140 | }) |
| 141 | .catch(e => { |
| 142 | $log.info(e); |
| 143 | this.formConfig.feedback.show = true; |
| 144 | this.formConfig.feedback.message = e; |
Arpit Agarwal | fecac78 | 2016-07-18 17:30:32 -0700 | [diff] [blame] | 145 | this.formConfig.feedback.type='danger'; |
Matteo Scandolo | bba1d78 | 2016-07-12 15:56:23 -0700 | [diff] [blame] | 146 | }) |
| 147 | }; |
| 148 | |
| 149 | this.toTosca = dashboard => { |
| 150 | const yaml = {} |
| 151 | yaml[dashboard.name] = { |
| 152 | type: 'tosca.nodes.DashboardView', |
| 153 | properties: { |
| 154 | url: dashboard.url |
| 155 | } |
| 156 | }; |
| 157 | this.tosca = jsyaml.dump(yaml).replace(/'/g, ''); |
| 158 | |
| 159 | const yamlRequirements = { |
| 160 | requirements: [] |
| 161 | }; |
| 162 | const dashboardRequirements = {}; |
| 163 | dashboardRequirements[`${dashboard.name.toLowerCase()}_dashboard`] = { |
| 164 | node: dashboard.name, |
| 165 | relationship: 'tosca.relationships.UsesDashboard' |
| 166 | } |
| 167 | yamlRequirements.requirements.push(dashboardRequirements); |
| 168 | this.toscaRequirements = jsyaml.dump(yamlRequirements).replace(/'/g, ''); |
| 169 | }; |
| 170 | } |
| 171 | } |
| 172 | }); |
| 173 | })(); |