Commit d2281c4c authored by Leonardo Menezes's avatar Leonardo Menezes
Browse files

support edit index template

closes #18
parent d283b473
Loading
Loading
Loading
Loading
+22 −1
Original line number Diff line number Diff line
@@ -1306,6 +1306,7 @@ angular.module('cerebro').controller('TemplatesController', ['$scope',
    );

    $scope.editor = undefined;
    $scope.editMode = false;

    $scope.paginator = new Paginator(1, 10, [],
      new IndexTemplateFilter('', ''));
@@ -1314,6 +1315,17 @@ angular.module('cerebro').controller('TemplatesController', ['$scope',
      $scope.page = $scope.paginator.getPage();
    }, true);

    $scope.$watch('name', function(current, previous) {
      var isExistingTemplate = false;
      var templates = $scope.paginator.getCollection();
      templates.forEach(function(t) {
        if (t.name === current) {
          isExistingTemplate = true;
        }
      });
      $scope.editMode = isExistingTemplate;
    }, true);

    $scope.initEditor = function() {
      if (!$scope.editor) {
        $scope.editor = AceEditorService.init('template-body-editor');
@@ -1333,11 +1345,20 @@ angular.module('cerebro').controller('TemplatesController', ['$scope',
      );
    };

    $scope.edit = function(name, template) {
      $scope.name = name;
      $scope.editor.setValue(JSON.stringify(template, undefined, 2));
    };

    $scope.create = function(name) {
      try {
        var template = $scope.editor.getValue();
        var success = function(response) {
          if ($scope.editMode) {
            AlertService.info('Template successfully updated');
          } else {
            AlertService.info('Template successfully created');
          }
          $scope.loadTemplates();
        };
        var errorCallback = function(response) {
+8 −3
Original line number Diff line number Diff line
@@ -25,6 +25,7 @@
              <i class="fa fa-trash normal-action alert-danger pull-right" ng-click="delete(template.name)"
                 ng-hide="template.removed" data-toggle="modal" href="#confirm_dialog" target="_self"></i>
              <i class="fa fa-trash alert-danger pull-right disabled" ng-show="template.removed"></i>
              <i class="fa fa-pencil pull-right" ng-hide="template.removed" ng-click="edit(template.name, template.template)"></i>
            </td>
          </tr>
        </table>
@@ -32,7 +33,8 @@
    </div>
  </div>
  <div class="col-md-6">
    <h4>create new template</h4>
    <h4 ng-hide="editMode">create new template</h4>
    <h4 ng-show="editMode">update template {{name}}</h4>
    <div class="row">
      <div class="col-xs-12">
        <div class="form-group">
@@ -47,8 +49,11 @@
        </div>
      </div>
      <div class="col-xs-12 text-right">
        <button type="submit" class="btn btn-info" ng-click="create(name)">
          <i class="fa fa-file-o normal-action alert-info"> </i> create
        <button type="submit" class="btn btn-success" ng-click="create(name)" ng-hide="editMode">
          <i class="fa fa-file-o normal-action alert-success"> </i> create
        </button>
        <button type="submit" class="btn btn-warning" ng-click="create(name)" ng-show="editMode">
          <i class="fa fa-floppy-o normal-action alert-warning"> </i> update
        </button>
      </div>
    </div>
+22 −1
Original line number Diff line number Diff line
@@ -15,6 +15,7 @@ angular.module('cerebro').controller('TemplatesController', ['$scope',
    );

    $scope.editor = undefined;
    $scope.editMode = false;

    $scope.paginator = new Paginator(1, 10, [],
      new IndexTemplateFilter('', ''));
@@ -23,6 +24,17 @@ angular.module('cerebro').controller('TemplatesController', ['$scope',
      $scope.page = $scope.paginator.getPage();
    }, true);

    $scope.$watch('name', function(current, previous) {
      var isExistingTemplate = false;
      var templates = $scope.paginator.getCollection();
      templates.forEach(function(t) {
        if (t.name === current) {
          isExistingTemplate = true;
        }
      });
      $scope.editMode = isExistingTemplate;
    }, true);

    $scope.initEditor = function() {
      if (!$scope.editor) {
        $scope.editor = AceEditorService.init('template-body-editor');
@@ -42,11 +54,20 @@ angular.module('cerebro').controller('TemplatesController', ['$scope',
      );
    };

    $scope.edit = function(name, template) {
      $scope.name = name;
      $scope.editor.setValue(JSON.stringify(template, undefined, 2));
    };

    $scope.create = function(name) {
      try {
        var template = $scope.editor.getValue();
        var success = function(response) {
          if ($scope.editMode) {
            AlertService.info('Template successfully updated');
          } else {
            AlertService.info('Template successfully created');
          }
          $scope.loadTemplates();
        };
        var errorCallback = function(response) {
+43 −0
Original line number Diff line number Diff line
@@ -69,6 +69,22 @@ describe('TemplatesController', function() {
      expect(this.TemplatesDataService.create).toHaveBeenCalledWith('someTemplate', {key: 'value'}, jasmine.any(Function), jasmine.any(Function));
      expect(this.AlertService.info).toHaveBeenCalledWith('Template successfully created');
    });
    it('updates existing template', function() {
       this.scope.editMode = true;
       this.TemplatesDataService.create = function(name, template, success, error) {
         success('ok');
       };
       this.scope.editor = {
         getValue: function() {
         }
       };
       spyOn(this.TemplatesDataService, 'create').andCallThrough();
       spyOn(this.scope.editor, 'getValue').andReturn({key: 'value'});
       spyOn(this.AlertService, 'info').andReturn();
       this.scope.create('someTemplate');
       expect(this.TemplatesDataService.create).toHaveBeenCalledWith('someTemplate', {key: 'value'}, jasmine.any(Function), jasmine.any(Function));
       expect(this.AlertService.info).toHaveBeenCalledWith('Template successfully updated');
     });
    it('alerts about malformed template', function() {
      this.scope.editor = {
        getValue: function() {
@@ -125,4 +141,31 @@ describe('TemplatesController', function() {
    });
  });

  describe('editMode', function() {
    it('enables editMode when name changes to existing template', function() {
      var templates = [{name: 'tmp'}, {name: 'tmp2'}];
      spyOn(this.scope.paginator, 'getCollection').andReturn(templates);
      this.scope.name = 'tmp';
      this.scope.$digest();
      expect(this.scope.editMode).toEqual(true);
    });
    it('disables editMode when name changes to non existing template', function() {
      var templates = [{name: 'tmp'}, {name: 'tmp2'}];
      spyOn(this.scope.paginator, 'getCollection').andReturn(templates);
      this.scope.name = 'tm';
      this.scope.$digest();
      expect(this.scope.editMode).toEqual(false);
    });
  });

  describe('edit', function() {
    it('loads templates into form', function() {
      this.scope.editor = {setValue: function(){}};
      spyOn(this.scope.editor, 'setValue').andReturn();
      this.scope.edit('some name', {some: 'obj'});
      expect(this.scope.name).toEqual('some name');
      expect(this.scope.editor.setValue).toHaveBeenCalledWith('{\n  "some": "obj"\n}');
    });
  });

});