'use strict'; FormController.$inject = ['$scope', 'name']; function FormController($scope, name) { var form = this, errors = form.error = {}; // publish the form into scope name(this); $scope.$on('$destroy', function(event, widget) { if (!widget) return; if (widget.widgetId) { delete form[widget.widgetId]; } forEach(errors, removeWidget, widget); }); $scope.$on('$valid', function(event, error, widget) { removeWidget(errors[error], error, widget); if (equals(errors, {})) { form.valid = true; form.invalid = false; } }); $scope.$on('$invalid', function(event, error, widget) { addWidget(error, widget); form.valid = false; form.invalid = true; }); $scope.$on('$viewTouch', function() { form.dirty = true; form.pristine = false; }); // init state form.dirty = false; form.pristine = true; form.valid = true; form.invalid = false; function removeWidget(queue, errorKey, widget) { if (queue) { widget = widget || this; // so that we can be used in forEach; for (var i = 0, length = queue.length; i < length; i++) { if (queue[i] === widget) { queue.splice(i, 1); if (!queue.length) { delete errors[errorKey]; } } } } } function addWidget(errorKey, widget) { var queue = errors[errorKey]; if (queue) { for (var i = 0, length = queue.length; i < length; i++) { if (queue[i] === widget) { return; } } } else { errors[errorKey] = queue = []; } queue.push(widget); } } FormController.prototype.registerWidget = function(widget, alias) { if (alias && !this.hasOwnProperty(alias)) { widget.widgetId = alias; this[alias] = widget; } }; /** * @ngdoc directive * @name angular.module.ng.$compileProvider.directive.form * * @scope * @description * Angular widget that creates a form scope using the * {@link angular.module.ng.$formFactory $formFactory} API. The resulting form scope instance is * attached to the DOM element using the jQuery `.data()` method under the `$form` key. * See {@link guide/dev_guide.forms forms} on detailed discussion of forms and widgets. * * * # Alias: `ng:form` * * In angular forms can be nested. This means that the outer form is valid when all of the child * forms are valid as well. However browsers do not allow nesting of `