Code coverage report for src/directives/message.js

Statements: 86.21% (25 / 29)      Branches: 65.22% (15 / 23)      Functions: 87.5% (7 / 8)      Lines: 89.29% (25 / 28)      Ignored: none     

All files » src/directives/ » message.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 751       1 1   1         8 8 8 8 1 1 1               8 24 16   8 8 2                   8       8   8                 8           8   8 15 15              
angular.module('schemaForm').directive('sfMessage',
['$injector', 'sfErrorMessage', function($injector, sfErrorMessage) {
 
  //Inject sanitizer if it exists
  var $sanitize = $injector.has('$sanitize') ?
                  $injector.get('$sanitize') : function(html) { return html; };
 
  return {
    scope: false,
    restrict: 'EA',
    link: function(scope, element, attrs) {
 
      var message = '';
      Eif (attrs.sfMessage) {
        scope.$watch(attrs.sfMessage, function(msg) {
          if (msg) {
            message = $sanitize(msg);
            Eif (scope.ngModel) {
              update(scope.ngModel.$valid);
            } else {
              update();
            }
          }
        });
      }
 
      var update = function(valid) {
        if (valid && !scope.hasError()) {
          element.html(message);
        } else {
          var errors = [];
          angular.forEach(((scope.ngModel && scope.ngModel.$error) || {}), function(status, code) {
            Iif (status) {
              // if true then there is an error
              // Angular 1.3 removes properties, so we will always just have errors.
              // Angular 1.2 sets them to false.
              errors.push(code);
            }
          });
 
          // In Angular 1.3 we use one $validator to stop the model value from getting updated.
          // this means that we always end up with a 'schemaForm' error.
          errors = errors.filter(function(e) { return e !== 'schemaForm'; });
 
          // We only show one error.
          // TODO: Make that optional
          var error = errors[0];
 
          Iif (error) {
            element.html(sfErrorMessage.interpolate(
              error,
              scope.ngModel.$modelValue,
              scope.ngModel.$viewValue,
              scope.form,
              scope.options && scope.options.validationMessage
            ));
          } else {
            element.html(message);
          }
        }
      };
 
      // Update once.
      update();
 
      scope.$watchCollection('ngModel.$error', function() {
        Eif (scope.ngModel) {
          update(scope.ngModel.$valid);
        }
      });
 
    }
  };
}]);