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

Change-Id: I937adf2ced95312a66086c8e20a2386b02a40934
diff --git a/views/ngXosViews/dashboardManager/src/css/main.css b/views/ngXosViews/dashboardManager/src/css/main.css
new file mode 100644
index 0000000..01c272a
--- /dev/null
+++ b/views/ngXosViews/dashboardManager/src/css/main.css
@@ -0,0 +1,23 @@
+#xosDashboardManager {
+  /* DRAG AND DROP STYLING */ }
+  #xosDashboardManager .col-xs-10 + .col-xs-2.text-right {
+    padding-top: 20px; }
+  #xosDashboardManager ul[dnd-list] {
+    min-height: 42px;
+    border: 1px dashed #337ab7;
+    padding: 20px;
+    margin-bottom: 0; }
+    #xosDashboardManager ul[dnd-list] li {
+      list-style: none; }
+    #xosDashboardManager ul[dnd-list] li:not(:last-child) {
+      margin-bottom: 20px; }
+    #xosDashboardManager ul[dnd-list] li:hover {
+      cursor: pointer; }
+    #xosDashboardManager ul[dnd-list] li.dndDraggingSource {
+      display: none; }
+  #xosDashboardManager ul[dnd-list] > li, #xosDashboardManager .dashboard-container {
+    display: block;
+    border: 1px solid #337ab7;
+    padding: 20px; }
+    #xosDashboardManager ul[dnd-list] > li a, #xosDashboardManager .dashboard-container a {
+      float: right; }
diff --git a/views/ngXosViews/dashboardManager/src/index.html b/views/ngXosViews/dashboardManager/src/index.html
new file mode 100644
index 0000000..371caa2
--- /dev/null
+++ b/views/ngXosViews/dashboardManager/src/index.html
@@ -0,0 +1,39 @@
+<!-- browserSync -->
+<!-- bower:css -->
+<link rel="stylesheet" href="vendor/bootstrap-css/css/bootstrap.min.css" />
+<link rel="stylesheet" href="vendor/angular-chart.js/dist/angular-chart.css" />
+<!-- endbower -->
+<!-- endcss -->
+<!-- inject:css -->
+<link rel="stylesheet" href="/css/main.css">
+<link rel="stylesheet" href="/../../../xos/core/static/xosNgLib.css">
+<!-- endinject -->
+
+<div ng-app="xos.dashboardManager" id="xosDashboardManager" class="container-fluid">
+  <div ui-view></div>
+</div>
+
+<!-- bower:js -->
+<script src="vendor/jquery/dist/jquery.js"></script>
+<script src="vendor/angular/angular.js"></script>
+<script src="vendor/angular-drag-and-drop-lists/angular-drag-and-drop-lists.js"></script>
+<script src="vendor/js-yaml/dist/js-yaml.js"></script>
+<script src="vendor/angular-mocks/angular-mocks.js"></script>
+<script src="vendor/angular-ui-router/release/angular-ui-router.js"></script>
+<script src="vendor/angular-cookies/angular-cookies.js"></script>
+<script src="vendor/angular-animate/angular-animate.js"></script>
+<script src="vendor/angular-resource/angular-resource.js"></script>
+<script src="vendor/lodash/lodash.js"></script>
+<script src="vendor/bootstrap-css/js/bootstrap.min.js"></script>
+<script src="vendor/Chart.js/Chart.js"></script>
+<script src="vendor/angular-chart.js/dist/angular-chart.js"></script>
+<script src="vendor/d3/d3.js"></script>
+<script src="vendor/angular-recursion/angular-recursion.js"></script>
+<!-- endbower -->
+<!-- endjs -->
+<!-- inject:js -->
+<script src="/../../../xos/core/xoslib/static/js/vendor/ngXosHelpers.js"></script>
+<script src="/.tmp/main.js"></script>
+<script src="/.tmp/user-dashboards.directive.js"></script>
+<script src="/.tmp/dashboard-form.directive.js"></script>
+<!-- endinject -->
\ No newline at end of file
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
diff --git a/views/ngXosViews/dashboardManager/src/sass/main.scss b/views/ngXosViews/dashboardManager/src/sass/main.scss
new file mode 100644
index 0000000..a8aff76
--- /dev/null
+++ b/views/ngXosViews/dashboardManager/src/sass/main.scss
@@ -0,0 +1,42 @@
+@import '../../../../style/sass/lib/_variables.scss';
+@import '../../../../style/sass/bootstrap/bootstrap/_variables.scss';
+
+#xosDashboardManager {
+
+  .col-xs-10 + .col-xs-2.text-right {
+    padding-top: $line-height-computed;
+  }
+
+  /* DRAG AND DROP STYLING */
+  ul[dnd-list] {
+    min-height: 42px;
+    border: 1px dashed $brand-primary;
+    padding: $line-height-computed;;
+    margin-bottom: 0;
+
+    li {
+      list-style: none;
+    }
+
+    li:not(:last-child){
+      margin-bottom: $line-height-computed;;
+    }
+
+    li:hover{
+      cursor: pointer;
+    }
+
+    li.dndDraggingSource {
+      display: none;
+    }
+  }
+
+  ul[dnd-list] > li, .dashboard-container {
+    display: block;
+    border: 1px solid $brand-primary;
+    padding: $line-height-computed;
+    a {
+      float: right;
+    }
+  }
+}
\ No newline at end of file
diff --git a/views/ngXosViews/dashboardManager/src/templates/dashboard-form.tpl.html b/views/ngXosViews/dashboardManager/src/templates/dashboard-form.tpl.html
new file mode 100644
index 0000000..6a584e4
--- /dev/null
+++ b/views/ngXosViews/dashboardManager/src/templates/dashboard-form.tpl.html
@@ -0,0 +1,39 @@
+<div class="row">
+  <div class="col-xs-10">
+    <h1>Manage Dashboard</h1>
+  </div>
+  <div class="col-xs-2 text-right">
+    <a ui-sref="manage-user-dashboards" class="btn btn-success">Back</a>
+  </div>
+</div>
+<div class="row">
+  <div class="col-xs-12">
+    <xos-form ng-model="vm.dashboard" config="vm.formConfig"></xos-form>
+  </div>
+</div>
+<div class="row" ng-show="vm.tosca">
+  <div class="col-sm-6">
+    <div class="row">
+      <div class="col-xs-12">
+        <xos-alert show="true" config="{type: 'info'}">
+          Include this lines in a TOSCA recipe to load the {{vm.dashboard.name}} dashboard in the system.
+        </xos-alert>
+      </div>
+      <div class="col-xs-12">
+        <pre>{{vm.tosca}}</pre>
+      </div>
+    </div>
+  </div>
+  <div class="col-sm-6">
+    <div class="row">
+      <div class="col-xs-12">
+        <xos-alert show="true" config="{type: 'info'}">
+          Add this lines as a requirement for the user in a TOSCA recipe to assign the {{vm.dashboard.name}} to the user.
+        </xos-alert>
+      </div>
+      <div class="col-xs-12">
+        <pre>{{vm.toscaRequirements}}</pre>
+      </div>
+    </div>
+  </div>
+</div>
\ No newline at end of file
diff --git a/views/ngXosViews/dashboardManager/src/templates/user-dashboards.tpl.html b/views/ngXosViews/dashboardManager/src/templates/user-dashboards.tpl.html
new file mode 100644
index 0000000..d43f19d
--- /dev/null
+++ b/views/ngXosViews/dashboardManager/src/templates/user-dashboards.tpl.html
@@ -0,0 +1,66 @@
+<div class="row">
+  <div class="col-xs-10">
+    <h1>Manage Your Dashboards</h1>
+  </div>
+  <div class="col-xs-2 text-right">
+    <a ui-sref="add-dashboards" class="btn btn-success">Add</a>
+  </div>
+</div>
+
+<div class="row">
+  <div class="col-xs-6">
+    <div class="panel panel-primary">
+      <div class="panel-heading">
+        <h3 class="panel-title">
+          Available Dashboards
+        </h3>
+      </div>
+      <div class="panel-body">
+        <ul dnd-list="vm.list.disabled"
+          dnd-drop="vm.addedToList(event, index, item, external, 'disabled')">
+          <li ng-repeat="item in vm.list.disabled"
+            dnd-moved="vm.removeFromList('disabled', item.id)"
+            dnd-draggable="item">
+            {{item.name}}
+            <a ui-sref="edit-dashboards({id: item.id,})"><i class="glyphicon glyphicon-pencil"></i></a>
+          </li>
+        </ul>
+      </div>
+    </div>
+  </div>
+  <div class="col-xs-6">
+    <div class="panel panel-primary">
+      <div class="panel-heading">
+        <h3 class="panel-title">
+          Enabled Dashboards
+        </h3>
+      </div>
+      <div class="panel-body">
+        <ul dnd-list="vm.list.enabled"
+          dnd-drop="vm.addedToList(event, index, item, external, 'enabled')">
+          <li ng-repeat="item in vm.list.enabled | orderBy : 'order'"
+            dnd-moved="vm.removeFromList('enabled', item.id)"
+            dnd-draggable="item">
+            {{item.name}}
+            <a ui-sref="edit-dashboards({id: item.id,})"><i class="glyphicon glyphicon-pencil"></i></a>
+          </li>
+        </ul>
+      </div>
+    </div>
+  </div>
+</div>
+<div class="row" ng-show="vm.disabled.length > 0">
+  <div class="col-xs-12">
+    <h1>Disabled Dashboard</h1>
+  </div>
+  <div class="col-xs-12">
+    <div class="row">
+      <div class="col-sm-2" ng-repeat="item in vm.disabled | orderBy : 'id'">
+        <span class="dashboard-container">
+          {{item.name}}
+          <a ui-sref="edit-dashboards({id: item.id,})"><i class="glyphicon glyphicon-pencil"></i></a>
+        </span>
+      </div>
+    </div>
+  </div>
+</div>