'use strict'; /** * @ngdoc object * @name angular.module.ng.$compileProvider.directive.form.FormController * * @property {boolean} pristine True if user has not interacted with the form yet. * @property {boolean} dirty True if user has already interacted with the form. * @property {boolean} valid True if all of the containg widgets are valid. * @property {boolean} invalid True if at least one containing widget is invalid. * * @property {Object} error Is an object hash, containing references to all invalid widgets, where * * - keys are error ids (such as `REQUIRED`, `URL` or `EMAIL`), * - values are arrays of widgets that are invalid with given error. * * @description * `FormController` keeps track of all its widgets as well as state of them form, such as being valid/invalid or dirty/pristine. * * Each {@link angular.module.ng.$compileProvider.directive.form form} directive creates an instance * of `FormController`. * */ 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); } } /** * @ngdoc function * @name angular.module.ng.$compileProvider.directive.form.FormController#registerWidget * @methodOf angular.module.ng.$compileProvider.directive.form.FormController * @function * * @param {Object} widget Widget to register (controller of a widget) * @param {string=} alias Name alias of the widget. * (If specified, widget will be accesible as a form property) * * @description * */ FormController.prototype.registerWidget = function(widget, alias) { if (alias && !this.hasOwnProperty(alias)) { widget.widgetId = alias; this[alias] = widget; } }; /** * @ngdoc widget * @name angular.module.ng.$compileProvider.directive.form * * @scope * @description * Directive that instantiates * {@link angular.module.ng.$compileProvider.directive.form.FormController FormController}. * * If `name` attribute is specified, the controller is published to the scope as well. * * # 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 `