MCPcopy
hub / github.com/angular-ui/ui-grid / formDirectiveFactory

Function formDirectiveFactory

lib/test/angular/1.8.0/angular.js:25054–25134  ·  view source on GitHub ↗
(isNgForm)

Source from the content-addressed store, hash-verified

25052 * related scope, under this name.
25053 */
25054var formDirectiveFactory = function(isNgForm) {
25055 return ['$timeout', '$parse', function($timeout, $parse) {
25056 var formDirective = {
25057 name: 'form',
25058 restrict: isNgForm ? 'EAC' : 'E',
25059 require: ['form', '^^?form'], //first is the form's own ctrl, second is an optional parent form
25060 controller: FormController,
25061 compile: function ngFormCompile(formElement, attr) {
25062 // Setup initial state of the control
25063 formElement.addClass(PRISTINE_CLASS).addClass(VALID_CLASS);
25064
25065 var nameAttr = attr.name ? 'name' : (isNgForm && attr.ngForm ? 'ngForm' : false);
25066
25067 return {
25068 pre: function ngFormPreLink(scope, formElement, attr, ctrls) {
25069 var controller = ctrls[0];
25070
25071 // if `action` attr is not present on the form, prevent the default action (submission)
25072 if (!('action' in attr)) {
25073 // we can't use jq events because if a form is destroyed during submission the default
25074 // action is not prevented. see #1238
25075 //
25076 // IE 9 is not affected because it doesn't fire a submit event and try to do a full
25077 // page reload if the form was destroyed by submission of the form via a click handler
25078 // on a button in the form. Looks like an IE9 specific bug.
25079 var handleFormSubmission = function(event) {
25080 scope.$apply(function() {
25081 controller.$commitViewValue();
25082 controller.$setSubmitted();
25083 });
25084
25085 event.preventDefault();
25086 };
25087
25088 formElement[0].addEventListener('submit', handleFormSubmission);
25089
25090 // unregister the preventDefault listener so that we don't not leak memory but in a
25091 // way that will achieve the prevention of the default action.
25092 formElement.on('$destroy', function() {
25093 $timeout(function() {
25094 formElement[0].removeEventListener('submit', handleFormSubmission);
25095 }, 0, false);
25096 });
25097 }
25098
25099 var parentFormCtrl = ctrls[1] || controller.$$parentForm;
25100 parentFormCtrl.$addControl(controller);
25101
25102 var setter = nameAttr ? getSetter(controller.$name) : noop;
25103
25104 if (nameAttr) {
25105 setter(scope, controller);
25106 attr.$observe(nameAttr, function(newValue) {
25107 if (controller.$name === newValue) return;
25108 setter(scope, undefined);
25109 controller.$$parentForm.$$renameControl(controller, newValue);
25110 setter = getSetter(controller.$name);
25111 setter(scope, controller);

Callers 1

angular.jsFile · 0.70

Calls 3

setterFunction · 0.85
getSetterFunction · 0.70
extendFunction · 0.70

Tested by

no test coverage detected