{"size": 2190, "ext": "js", "max_stars_repo_path": "custom/gnap-angular/js/develop/gnap/validation-message.directive.js", "max_stars_repo_name": "infrabel/themes-gnap", "max_stars_repo_stars_event_min_datetime": "2021-03-08T09:38:31.000Z", "max_stars_repo_stars_event_max_datetime": "2021-03-08T09:38:31.000Z", "max_issues_repo_path": "custom/gnap-angular/js/develop/gnap/validation-message.directive.js", "max_issues_repo_name": "infrabel/themes-gnap", "max_issues_repo_issues_event_min_datetime": "2015-01-06T15:50:41.000Z", "max_issues_repo_issues_event_max_datetime": "2016-01-29T11:54:28.000Z", "max_forks_repo_path": "custom/gnap-angular/js/develop/gnap/validation-message.directive.js", "max_forks_repo_name": "infrabel/themes-gnap", "max_forks_repo_forks_event_min_datetime": "2015-01-07T14:22:48.000Z", "max_forks_repo_forks_event_max_datetime": "2016-05-02T09:05:24.000Z"}
{"max_stars_count": 1, "max_issues_count": 20, "max_forks_count": 10, "avg_line_length": 35.3225806452, "max_line_length": 174, "alphanum_fraction": 0.4748858447}
'use strict';

/**
 * @desc A directive containing a message for a specified validation rule. Implementation based on ngMessages: https://docs.angularjs.org/api/ngMessages/directive/ngMessages.
 * @file validation-message.directive.js
 * @example <div gnap-validation-message when="required">Please enter a name.</div>
 */
(function () {
    angular
        .module('gnap')
        .directive('gnapValidationMessage', gnapValidationMessage);

    gnapValidationMessage.$inject = ['$animate'];

    function gnapValidationMessage($animate) {
        return {
            require: '^gnapValidationMessages',
            transclude: 'element',
            terminal: true,
            restrict: 'A',
            link: link
        };

        function link($scope, $element, $attrs, gnapValidationMessages, $transclude) {
            var index, element;

            var commentNode = $element[0];
            var parentNode = commentNode.parentNode;
            for (var i = 0, j = 0; i < parentNode.childNodes.length; i++) {
                var node = parentNode.childNodes[i];
                if (node.nodeType === 8 /* COMMENT_NODE */ && node.nodeValue.indexOf('gnapValidationMessage') >= 0) {
                    if (node === commentNode) {
                        index = j;
                        break;
                    }
                    j++;
                }
            }

            gnapValidationMessages.registerMessage(index, {
                type: $attrs.gnapValidationMessage || $attrs.when,
                attach: function () {
                    if (!element) {
                        $transclude($scope, function (clone) {
                            $animate.enter(clone, null, $element);
                            element = clone;
                            element.addClass('label');
                            element.addClass('label-danger');
                        });
                    }
                },
                detach: function () {
                    if (element) {
                        $animate.leave(element);
                        element = null;
                    }
                }
            });
        }
    }
})();
