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

Function formDirectiveFactory

lib/test/angular/1.4.3/angular.js:20304–20371  ·  view source on GitHub ↗
(isNgForm)

Source from the content-addressed store, hash-verified

20302 * related scope, under this name.
20303 */
20304var formDirectiveFactory = function(isNgForm) {
20305 return ['$timeout', function($timeout) {
20306 var formDirective = {
20307 name: 'form',
20308 restrict: isNgForm ? 'EAC' : 'E',
20309 controller: FormController,
20310 compile: function ngFormCompile(formElement, attr) {
20311 // Setup initial state of the control
20312 formElement.addClass(PRISTINE_CLASS).addClass(VALID_CLASS);
20313
20314 var nameAttr = attr.name ? 'name' : (isNgForm && attr.ngForm ? 'ngForm' : false);
20315
20316 return {
20317 pre: function ngFormPreLink(scope, formElement, attr, controller) {
20318 // if `action` attr is not present on the form, prevent the default action (submission)
20319 if (!('action' in attr)) {
20320 // we can't use jq events because if a form is destroyed during submission the default
20321 // action is not prevented. see #1238
20322 //
20323 // IE 9 is not affected because it doesn't fire a submit event and try to do a full
20324 // page reload if the form was destroyed by submission of the form via a click handler
20325 // on a button in the form. Looks like an IE9 specific bug.
20326 var handleFormSubmission = function(event) {
20327 scope.$apply(function() {
20328 controller.$commitViewValue();
20329 controller.$setSubmitted();
20330 });
20331
20332 event.preventDefault();
20333 };
20334
20335 addEventListenerFn(formElement[0], 'submit', handleFormSubmission);
20336
20337 // unregister the preventDefault listener so that we don't not leak memory but in a
20338 // way that will achieve the prevention of the default action.
20339 formElement.on('$destroy', function() {
20340 $timeout(function() {
20341 removeEventListenerFn(formElement[0], 'submit', handleFormSubmission);
20342 }, 0, false);
20343 });
20344 }
20345
20346 var parentFormCtrl = controller.$$parentForm;
20347
20348 if (nameAttr) {
20349 setter(scope, controller.$name, controller, controller.$name);
20350 attr.$observe(nameAttr, function(newValue) {
20351 if (controller.$name === newValue) return;
20352 setter(scope, controller.$name, undefined, controller.$name);
20353 parentFormCtrl.$$renameControl(controller, newValue);
20354 setter(scope, controller.$name, controller, controller.$name);
20355 });
20356 }
20357 formElement.on('$destroy', function() {
20358 parentFormCtrl.$removeControl(controller);
20359 if (nameAttr) {
20360 setter(scope, attr[nameAttr], undefined, controller.$name);
20361 }

Callers 1

angular.jsFile · 0.70

Calls 4

setterFunction · 0.85
addEventListenerFnFunction · 0.70
removeEventListenerFnFunction · 0.70
extendFunction · 0.70

Tested by

no test coverage detected