Code coverage report for src/directives/message.js

Statements: 96.55% (28 / 29)      Branches: 78.26% (18 / 23)      Functions: 100% (8 / 8)      Lines: 96.43% (27 / 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       52 17   52         140 140 140 141 17 17 17               140 410 264   146 146 12       8           146       146   146 2               144           140   140 253 253              
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) {
            if (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];
 
          if (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);
        }
      });
 
    }
  };
}]);