|
| 1 | +(function (window, angular) { |
| 2 | + 'use strict'; |
| 3 | + |
| 4 | + angular.module('angular-server-form', []) |
| 5 | + .provider('serverForm', [function () { |
| 6 | + |
| 7 | + var provider = this; |
| 8 | + |
| 9 | + provider.errorsKey = 'errors'; |
| 10 | + provider.logging = true; |
| 11 | + provider.$get = ['$http', '$q', '$log', function ($http, $q, $log) { |
| 12 | + |
| 13 | + // Private |
| 14 | + // Set error message on form value |
| 15 | + function setError (form, message) { |
| 16 | + // Only NgModelController has $render and $setViewValue |
| 17 | + // Don't call $setValidity on FormController |
| 18 | + if (form.$render || form.$setViewValue) { |
| 19 | + form.$setValidity('server', false); |
| 20 | + } |
| 21 | + form.$setPristine(); |
| 22 | + form.$server = message; |
| 23 | + } |
| 24 | + |
| 25 | + var self = this; |
| 26 | + |
| 27 | + // Serialize form to data object |
| 28 | + self.serialize = function serialize (form) { |
| 29 | + |
| 30 | + // recurse over the form |
| 31 | + // if the property is a value it gets assigned |
| 32 | + // otherwise dive inside |
| 33 | + function formCrawler (form) { |
| 34 | + var response = {}; |
| 35 | + for (var prop in form) { |
| 36 | + if (form.hasOwnProperty(prop) && prop[0] !== '$') { |
| 37 | + if (form[prop].hasOwnProperty('$modelValue')) { |
| 38 | + response[prop] = form[prop].$modelValue; |
| 39 | + } else { |
| 40 | + response[prop] = formCrawler(form[prop]); |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + return response; |
| 45 | + } |
| 46 | + |
| 47 | + var response = {}; |
| 48 | + |
| 49 | + // set root if form name is set |
| 50 | + if (form.$name) { |
| 51 | + response[form.$name] = formCrawler(form); |
| 52 | + } else { |
| 53 | + response = formCrawler(form); |
| 54 | + } |
| 55 | + return response; |
| 56 | + }; |
| 57 | + |
| 58 | + // Apply all messages from errors object on form |
| 59 | + // The error value must be an array of strings or a string |
| 60 | + self.applyErrors = function applyErrors (form, errors) { |
| 61 | + if (angular.isString(errors)) { |
| 62 | + // If it's a string then set the error message |
| 63 | + setError(form, errors); |
| 64 | + } else if (angular.isArray(errors)) { |
| 65 | + // If it's an array then join them and set the error message |
| 66 | + setError(form, errors.join(', ')); |
| 67 | + } else { |
| 68 | + // Otherwise we must crawl the errors |
| 69 | + for (var prop in errors) { |
| 70 | + if (errors.hasOwnProperty(prop)) { |
| 71 | + if (form.hasOwnProperty(prop)) { |
| 72 | + // If the form has a property with the same error name |
| 73 | + self.applyErrors(form[prop], errors[prop]); |
| 74 | + } else if (form.$name === prop) { |
| 75 | + // errors object is for the whole form, dive in |
| 76 | + self.applyErrors(form, errors[prop]); |
| 77 | + } else { |
| 78 | + if (provider.logging) { |
| 79 | + $log.warn('No place to render property', prop, 'in form', form); |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + }; |
| 86 | + |
| 87 | + // Crawl form and reset errors |
| 88 | + self.clearErrors = function clearErrors (form) { |
| 89 | + // Only NgModelController has $render and $setViewValue |
| 90 | + // Don't call $setValidity on FormController |
| 91 | + if (form.$render || form.$setViewValue) { |
| 92 | + form.$setValidity('server', true); |
| 93 | + } |
| 94 | + form.$setPristine(); |
| 95 | + form.$server = ''; |
| 96 | + |
| 97 | + for (var prop in form) { |
| 98 | + if (form.hasOwnProperty(prop) && prop[0] !== '$') { |
| 99 | + clearErrors(form[prop]); |
| 100 | + } |
| 101 | + } |
| 102 | + }; |
| 103 | + |
| 104 | + self.submit = function submit (form, config) { |
| 105 | + self.clearErrors(form); // resets previous server errors |
| 106 | + form.$submitting = true; |
| 107 | + form.$saved = false; |
| 108 | + |
| 109 | + return $http(config) |
| 110 | + .success(function () { |
| 111 | + form.$saved = true; |
| 112 | + }) |
| 113 | + .error(function(res, status) { |
| 114 | + form.$saved = false; |
| 115 | + if (status === 422) { |
| 116 | + if (provider.errorsKey) { |
| 117 | + self.applyErrors(form, res[provider.errorsKey]); |
| 118 | + } else { |
| 119 | + self.applyErrors(form, res); |
| 120 | + } |
| 121 | + } |
| 122 | + }) |
| 123 | + .finally(function () { |
| 124 | + form.$submitting = false; |
| 125 | + }); |
| 126 | + }; |
| 127 | + |
| 128 | + return self; |
| 129 | + }]; |
| 130 | + }]) |
| 131 | + .directive('serverForm', ['serverForm', function (serverForm) { |
| 132 | + |
| 133 | + return { |
| 134 | + restrict: 'A', |
| 135 | + scope: { |
| 136 | + url: '@', |
| 137 | + method: '@', |
| 138 | + onSuccess: '=', |
| 139 | + onError: '=' |
| 140 | + }, |
| 141 | + require: 'form', |
| 142 | + link: function postLink(scope, iElement, iAttrs, form) { |
| 143 | + |
| 144 | + function submitForm (ev) { |
| 145 | + if (!form.$submitting) { |
| 146 | + var config = { |
| 147 | + url: scope.url, |
| 148 | + method: scope.method ? scope.method : 'POST', |
| 149 | + data: serverForm.serialize(form) |
| 150 | + }; |
| 151 | + |
| 152 | + serverForm.submit(form, config) |
| 153 | + .then(scope.onSuccess, scope.onError); |
| 154 | + } |
| 155 | + |
| 156 | + ev.preventDefault(); |
| 157 | + scope.$apply(); |
| 158 | + } |
| 159 | + |
| 160 | + var submitting = false; |
| 161 | + |
| 162 | + iElement.on('submit', submitForm); |
| 163 | + scope.$on('destroy', function () { |
| 164 | + iElement.off('submit', submitForm); |
| 165 | + }); |
| 166 | + |
| 167 | + } |
| 168 | + }; |
| 169 | + }]); |
| 170 | + |
| 171 | +})(window, window.angular); |
0 commit comments