changeset 135:bb3a7b0bf4c2

New multichart field clears on submit Submission may also be performed with Enter key. Reviewed-by: almac Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2017-July/024211.html
author Andrew Azores <aazores@redhat.com>
date Wed, 19 Jul 2017 15:48:35 -0400
parents 3e5a357f275f
children d1612c9b8411
files src/app/components/multichart/multichart.controller.js src/app/components/multichart/multichart.controller.spec.js src/app/components/multichart/multichart.html
diffstat 3 files changed, 21 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/src/app/components/multichart/multichart.controller.js	Wed Jul 19 15:26:29 2017 -0400
+++ b/src/app/components/multichart/multichart.controller.js	Wed Jul 19 15:48:35 2017 -0400
@@ -29,7 +29,8 @@
 import directives from 'shared/directives/directives.module.js';
 
 class MultiChartController {
-  constructor (multichartService) {
+  constructor ($scope, multichartService) {
+    this.scope = $scope;
     this.svc = multichartService;
     this.showErr = false;
   }
@@ -45,6 +46,10 @@
     }
     this.showErr = false;
     this.svc.addChart(chartName);
+    this.scope.newChartName = '';
+    let form = this.scope.newChartForm;
+    form.$setPristine();
+    form.$setUntouched();
   }
 
   isValid (chartName) {
--- a/src/app/components/multichart/multichart.controller.spec.js	Wed Jul 19 15:26:29 2017 -0400
+++ b/src/app/components/multichart/multichart.controller.spec.js	Wed Jul 19 15:48:35 2017 -0400
@@ -29,16 +29,22 @@
 
 describe('MultiChartController', () => {
 
-  let svc, ctrl;
+  let scope, svc, ctrl;
   beforeEach(() => {
     angular.mock.module(controllerModule);
-    angular.mock.inject($controller => {
+    angular.mock.inject(($rootScope, $controller) => {
       'ngInject';
+      scope = $rootScope.$new();
+      scope.newChartForm = {
+        $setPristine: sinon.spy(),
+        $setUntouched: sinon.spy()
+      };
       svc = {
         addChart: sinon.spy(),
         chartNames: ['foo', 'bar']
       };
       ctrl = $controller('MultichartController', {
+        $scope: scope,
         multichartService: svc
       });
     });
@@ -64,11 +70,14 @@
       ctrl.showErr.should.be.false();
     });
 
-    it('should call to service on success', () => {
+    it('should reset form and call to service on success', () => {
       svc.addChart.should.not.be.called();
       ctrl.createChart('foo');
       svc.addChart.should.be.calledOnce();
       svc.addChart.should.be.calledWith('foo');
+      scope.newChartName.should.equal('');
+      scope.newChartForm.$setUntouched.should.be.calledOnce();
+      scope.newChartForm.$setPristine.should.be.calledOnce();
     });
 
     it('should trim spaces from chart names', () => {
--- a/src/app/components/multichart/multichart.html	Wed Jul 19 15:26:29 2017 -0400
+++ b/src/app/components/multichart/multichart.html	Wed Jul 19 15:48:35 2017 -0400
@@ -9,11 +9,11 @@
 
   <div class="row">
     <div class="col-xs-12 col-md-4">
-      <div class="input-group">
+      <form name="newChartForm" class="input-group" ng-submit="ctrl.createChart(newChartName)">
         <span class="input-group-addon">Name</span>
         <input type="text" class="form-control" placeholder="Enter new chart name" ng-model="newChartName"/>
-        <button class="input-group-button" ng-click="ctrl.createChart(newChartName)">Create Chart</button>
-      </div>
+        <input type="submit" value="Create Chart"/>
+      </form>
     </div>
   </div>