Dashboard Manager View
- added ng-drag-drop
- deleted old customize dashboard
- managing dashboards

Change-Id: I937adf2ced95312a66086c8e20a2386b02a40934
diff --git a/views/ngXosViews/dashboardManager/src/js/dashboard-form.directive.js b/views/ngXosViews/dashboardManager/src/js/dashboard-form.directive.js
new file mode 100644
index 0000000..b6751b2
--- /dev/null
+++ b/views/ngXosViews/dashboardManager/src/js/dashboard-form.directive.js
@@ -0,0 +1,130 @@
+(function () {
+  'use strict';
+
+  angular.module('xos.dashboardManager')
+  .directive('dashboardForm', function(){
+    return {
+      restrict: 'E',
+      scope: {},
+      bindToController: true,
+      controllerAs: 'vm',
+      templateUrl: 'templates/dashboard-form.tpl.html',
+      controller: function($stateParams, $log, Dashboards){
+
+        this.dashboard = {
+          enabled: true
+        };
+
+        if($stateParams.id){
+          Dashboards.get({id: $stateParams.id}).$promise
+          .then(dash => {
+            this.dashboard = dash;
+          })
+          .catch(e => {
+            console.log(e);
+          })
+        }
+
+        this.formConfig = {
+          exclude: [
+            'backend_register',
+            'controllers',
+            'deployments',
+            'enacted',
+            'humanReadableName',
+            'lazy_blocked',
+            'no_policy',
+            'no_sync',
+            'policed',
+            'write_protect'
+          ],
+          actions: [
+            {
+              label: 'Save',
+              icon: 'ok',
+              cb: (item) => {
+                this.createOrUpdateDashboard(item);
+              },
+              class: 'success'
+            },
+            {
+              label: 'Esport to TOSCA',
+              icon: 'export',
+              cb: (item) => {
+                this.toTosca(item);
+              },
+              class: 'primary'
+            }
+          ],
+          formName: 'dashboardForm',
+          feedback: {
+            show: false,
+            message: 'Form submitted successfully !!!',
+            type: 'success'
+          },
+          fields: {
+            name: {
+              type: 'string',
+              validators: {
+                required: true
+              }
+            },
+            url: {
+              type: 'string',
+              validators: {
+                required: true
+              }
+            },
+            enabled: {
+              type: 'boolean'
+            }
+          }
+        };
+
+        this.createOrUpdateDashboard = dashboard => {
+          let p;
+          if(dashboard.id){
+            delete dashboard.controllers;
+            delete dashboard.deployments;
+            p = dashboard.$save();
+          }
+          else{
+            p = Dashboards.save(dashboard).$promise;
+          }
+
+          p.then(res => {
+            this.formConfig.feedback.show = true;
+          })
+          .catch(e => {
+            $log.info(e);
+            this.formConfig.feedback.show = true;
+            this.formConfig.feedback.message = e;
+            this.formConfig.feedback.type = 'error';
+          })
+        };
+
+        this.toTosca = dashboard => {
+          const yaml = {}
+          yaml[dashboard.name] = {
+            type: 'tosca.nodes.DashboardView',
+            properties: {
+              url: dashboard.url
+            }
+          };
+          this.tosca = jsyaml.dump(yaml).replace(/'/g, '');
+
+          const yamlRequirements = {
+            requirements: []
+          };
+          const dashboardRequirements = {};
+          dashboardRequirements[`${dashboard.name.toLowerCase()}_dashboard`] = {
+            node: dashboard.name,
+            relationship: 'tosca.relationships.UsesDashboard'
+          }
+          yamlRequirements.requirements.push(dashboardRequirements);
+          this.toscaRequirements = jsyaml.dump(yamlRequirements).replace(/'/g, '');
+        };
+      }
+    }
+  });
+})();
\ No newline at end of file
diff --git a/views/ngXosViews/dashboardManager/src/js/main.js b/views/ngXosViews/dashboardManager/src/js/main.js
new file mode 100644
index 0000000..025e95d
--- /dev/null
+++ b/views/ngXosViews/dashboardManager/src/js/main.js
@@ -0,0 +1,55 @@
+'use strict';
+
+angular.module('xos.dashboardManager', [
+  'ngResource',
+  'ngCookies',
+  'ui.router',
+  'xos.helpers',
+  'dndLists'
+])
+.config(($stateProvider) => {
+  $stateProvider
+  .state('manage-user-dashboards', {
+    url: '/',
+    template: '<user-dashboards></user-dashboards>'
+  })
+  .state('add-dashboards', {
+    url: '/add',
+    template: '<dashboard-form></dashboard-form>'
+  })
+  .state('edit-dashboards', {
+    url: '/dashboards/:id',
+    template: '<dashboard-form></dashboard-form>'
+  });
+})
+.config(function($httpProvider){
+  $httpProvider.interceptors.push('NoHyperlinks');
+})
+.service('UserDashboards', function($http, $q){
+  this.query = () => {
+    const d = $q.defer();
+
+    $http.get('/api/utility/dashboards')
+    .then(res => {
+      d.resolve(res.data);
+    })
+    .catch(err => {
+      d.reject(err);
+    });
+
+    return {$promise: d.promise};
+  }
+
+  this.update = (dashboard) => {
+    const d = $q.defer();
+    $http.post('/api/utility/dashboards/', dashboard)
+    .then(res => {
+      d.resolve(res.data);
+    })
+    .catch(err => {
+      d.reject(err);
+    });
+
+    return {$promise: d.promise};
+  }
+});
\ No newline at end of file
diff --git a/views/ngXosViews/dashboardManager/src/js/user-dashboards.directive.js b/views/ngXosViews/dashboardManager/src/js/user-dashboards.directive.js
new file mode 100644
index 0000000..2912ad7
--- /dev/null
+++ b/views/ngXosViews/dashboardManager/src/js/user-dashboards.directive.js
@@ -0,0 +1,98 @@
+(function () {
+  angular.module('xos.dashboardManager')
+  .directive('userDashboards', function(){
+    return {
+      restrict: 'E',
+      scope: {},
+      bindToController: true,
+      controllerAs: 'vm',
+      templateUrl: 'templates/user-dashboards.tpl.html',
+      controller: function($q, _, UserDashboards, Dashboards){
+        
+        // retrieving user list
+        $q.all([
+          UserDashboards.query().$promise,
+          Dashboards.query({enabled: 'False'}).$promise,
+        ])
+        .then((res) => {
+          let [dashboards, disabled] = res;
+          this.disabled = disabled;
+          this.list = {
+            enabled: _.filter(dashboards, {shown: true}),
+            disabled: _.filter(dashboards, {shown: false})
+          };
+        })
+        .catch((e) => {
+          throw new Error(e);
+        });
+
+        this.removeFromList = (listName, itemId) => {
+          _.remove(this.list[listName], {id: itemId});
+        };
+
+        this.addToList = (listName, item) => {
+          this.list[listName].push(item)
+        };
+
+        this.isInList = (listName, item) => {
+          return _.find(this.list[listName], item);
+        };
+
+        this.reorderList = (list, newPos, oldPos, item) => {
+
+          let listToOrder = _.filter(list, i => {
+            // if is the item, skip it, it is already updated
+            if(i.id === item.id){
+              return false;
+            }
+            if(i.order < oldPos){
+              return true;
+            }
+            if(i.order >= newPos){
+              return true;
+            }
+            return false;
+          });
+
+          listToOrder = listToOrder.map(i => {
+            i.order++;
+            return i;
+          })
+        };
+
+        this.addedToList = (event, index, item, external, list) => {
+          let originalPosition;
+
+          // load the item in the list
+          let itemInList = this.isInList(list, item);
+          if(itemInList){
+            // if is in the list update it
+            originalPosition = item.order;
+            itemInList.order = index;
+            item.order = index;
+          }
+          else {
+            // create a new one
+            item.order = this.list[list].length;
+            item.shown = !item.shown;
+          }
+
+          const otherList = list === 'enabled' ? 'disabled' : 'enabled';
+
+          UserDashboards.update(item).$promise
+          .then((item) => {
+            if(!itemInList){
+              // if it is not in the list, update both lists
+              this.addToList(list, item);
+              this.removeFromList(otherList, item.id);
+            }
+            else {
+              // reorder
+              this.reorderList(this.list[list], index, originalPosition, item);
+            }
+          });
+        }
+      }
+    };
+  });
+})(); 
\ No newline at end of file