|
1 | | -angular.module('schemaForm').directive('schemaValidate', ['sfValidator', 'sfSelect', function(sfValidator, sfSelect) { |
2 | | - return { |
3 | | - restrict: 'A', |
4 | | - scope: false, |
5 | | - // We want the link function to be *after* the input directives link function so we get access |
6 | | - // the parsed value, ex. a number instead of a string |
7 | | - priority: 500, |
8 | | - require: 'ngModel', |
9 | | - link: function(scope, element, attrs, ngModel) { |
10 | | - |
11 | | - |
12 | | - // We need the ngModelController on several places, |
13 | | - // most notably for errors. |
14 | | - // So we emit it up to the decorator directive so it can put it on scope. |
15 | | - scope.$emit('schemaFormPropagateNgModelController', ngModel); |
16 | | - |
17 | | - var error = null; |
18 | | - |
19 | | - var getForm = function() { |
20 | | - if (!form) { |
21 | | - form = scope.$eval(attrs.schemaValidate); |
22 | | - } |
23 | | - return form; |
24 | | - }; |
25 | | - var form = getForm(); |
26 | | - if (form.copyValueTo) { |
27 | | - ngModel.$viewChangeListeners.push(function() { |
28 | | - var paths = form.copyValueTo; |
29 | | - angular.forEach(paths, function(path) { |
30 | | - sfSelect(path, scope.model, ngModel.$modelValue); |
| 1 | +angular.module('schemaForm').directive('schemaValidate', ['sfValidator', 'sfSelect', 'sfUnselect', '$parse', 'sfRetainModel', |
| 2 | + function(sfValidator, sfSelect, sfUnselect, $parse, sfRetainModel) { |
| 3 | + |
| 4 | + return { |
| 5 | + restrict: 'A', |
| 6 | + scope: false, |
| 7 | + // We want the link function to be *after* the input directives link function so we get access |
| 8 | + // the parsed value, ex. a number instead of a string |
| 9 | + priority: 500, |
| 10 | + require: 'ngModel', |
| 11 | + link: function(scope, element, attrs, ngModel) { |
| 12 | + |
| 13 | + // We need the ngModelController on several places, |
| 14 | + // most notably for errors. |
| 15 | + // So we emit it up to the decorator directive so it can put it on scope. |
| 16 | + scope.$emit('schemaFormPropagateNgModelController', ngModel); |
| 17 | + |
| 18 | + var error = null; |
| 19 | + |
| 20 | + var getForm = function() { |
| 21 | + if (!form) { |
| 22 | + form = scope.$eval(attrs.schemaValidate); |
| 23 | + } |
| 24 | + return form; |
| 25 | + }; |
| 26 | + var form = getForm(); |
| 27 | + if (form.copyValueTo) { |
| 28 | + ngModel.$viewChangeListeners.push(function() { |
| 29 | + var paths = form.copyValueTo; |
| 30 | + angular.forEach(paths, function(path) { |
| 31 | + sfSelect(path, scope.model, ngModel.$modelValue); |
| 32 | + }); |
31 | 33 | }); |
32 | | - }); |
33 | | - } |
| 34 | + } |
34 | 35 |
|
35 | | - // Validate against the schema. |
| 36 | + // Validate against the schema. |
36 | 37 |
|
37 | | - var validate = function(viewValue) { |
38 | | - form = getForm(); |
39 | | - //Still might be undefined |
40 | | - if (!form) { |
41 | | - return viewValue; |
42 | | - } |
| 38 | + var validate = function(viewValue) { |
| 39 | + form = getForm(); |
| 40 | + //Still might be undefined |
| 41 | + if (!form) { |
| 42 | + return viewValue; |
| 43 | + } |
43 | 44 |
|
44 | | - // Omit TV4 validation |
45 | | - if (scope.options && scope.options.tv4Validation === false) { |
46 | | - return viewValue; |
47 | | - } |
| 45 | + // Omit TV4 validation |
| 46 | + if (scope.options && scope.options.tv4Validation === false) { |
| 47 | + return viewValue; |
| 48 | + } |
48 | 49 |
|
49 | | - var result = sfValidator.validate(form, viewValue); |
50 | | - // Since we might have different tv4 errors we must clear all |
51 | | - // errors that start with tv4- |
52 | | - Object.keys(ngModel.$error) |
| 50 | + var result = sfValidator.validate(form, viewValue); |
| 51 | + // Since we might have different tv4 errors we must clear all |
| 52 | + // errors that start with tv4- |
| 53 | + Object.keys(ngModel.$error) |
53 | 54 | .filter(function(k) { return k.indexOf('tv4-') === 0; }) |
54 | 55 | .forEach(function(k) { ngModel.$setValidity(k, true); }); |
55 | 56 |
|
56 | | - if (!result.valid) { |
57 | | - // it is invalid, return undefined (no model update) |
58 | | - ngModel.$setValidity('tv4-' + result.error.code, false); |
59 | | - error = result.error; |
60 | | - return undefined; |
| 57 | + if (!result.valid) { |
| 58 | + // it is invalid, return undefined (no model update) |
| 59 | + ngModel.$setValidity('tv4-' + result.error.code, false); |
| 60 | + error = result.error; |
| 61 | + return undefined; |
| 62 | + } |
| 63 | + return viewValue; |
| 64 | + }; |
| 65 | + |
| 66 | + // Custom validators, parsers, formatters etc |
| 67 | + if (typeof form.ngModel === 'function') { |
| 68 | + form.ngModel(ngModel); |
61 | 69 | } |
62 | | - return viewValue; |
63 | | - }; |
64 | 70 |
|
65 | | - // Custom validators, parsers, formatters etc |
66 | | - if (typeof form.ngModel === 'function') { |
67 | | - form.ngModel(ngModel); |
68 | | - } |
| 71 | + ['$parsers', '$viewChangeListeners', '$formatters'].forEach(function(attr) { |
| 72 | + if (form[attr] && ngModel[attr]) { |
| 73 | + form[attr].forEach(function(fn) { |
| 74 | + ngModel[attr].push(fn); |
| 75 | + }); |
| 76 | + } |
| 77 | + }); |
69 | 78 |
|
70 | | - ['$parsers', '$viewChangeListeners', '$formatters'].forEach(function(attr) { |
71 | | - if (form[attr] && ngModel[attr]) { |
72 | | - form[attr].forEach(function(fn) { |
73 | | - ngModel[attr].push(fn); |
74 | | - }); |
75 | | - } |
76 | | - }); |
| 79 | + ['$validators', '$asyncValidators'].forEach(function(attr) { |
| 80 | + // Check if our version of angular has i, i.e. 1.3+ |
| 81 | + if (form[attr] && ngModel[attr]) { |
| 82 | + angular.forEach(form[attr], function(fn, name) { |
| 83 | + ngModel[attr][name] = fn; |
| 84 | + }); |
| 85 | + } |
| 86 | + }); |
77 | 87 |
|
78 | | - ['$validators', '$asyncValidators'].forEach(function(attr) { |
79 | | - // Check if our version of angular has i, i.e. 1.3+ |
80 | | - if (form[attr] && ngModel[attr]) { |
81 | | - angular.forEach(form[attr], function(fn, name) { |
82 | | - ngModel[attr][name] = fn; |
83 | | - }); |
84 | | - } |
85 | | - }); |
86 | | - |
87 | | - // Get in last of the parses so the parsed value has the correct type. |
88 | | - // We don't use $validators since we like to set different errors depeding tv4 error codes |
89 | | - ngModel.$parsers.push(validate); |
90 | | - |
91 | | - // Listen to an event so we can validate the input on request |
92 | | - scope.$on('schemaFormValidate', function() { |
93 | | - if (ngModel.$setDirty) { |
94 | | - // Angular 1.3+ |
95 | | - ngModel.$setDirty(); |
96 | | - validate(ngModel.$modelValue); |
97 | | - } else { |
98 | | - // Angular 1.2 |
99 | | - ngModel.$setViewValue(ngModel.$viewValue); |
| 88 | + // Get in last of the parses so the parsed value has the correct type. |
| 89 | + // We don't use $validators since we like to set different errors depeding tv4 error codes |
| 90 | + ngModel.$parsers.push(validate); |
| 91 | + |
| 92 | + // Listen to an event so we can validate the input on request |
| 93 | + scope.$on('schemaFormValidate', function() { |
| 94 | + if (ngModel.$setDirty) { |
| 95 | + // Angular 1.3+ |
| 96 | + ngModel.$setDirty(); |
| 97 | + validate(ngModel.$modelValue); |
| 98 | + } else { |
| 99 | + // Angular 1.2 |
| 100 | + ngModel.$setViewValue(ngModel.$viewValue); |
| 101 | + } |
| 102 | + |
| 103 | + }); |
| 104 | + |
| 105 | + |
| 106 | + var DEFAULT_DESTROY_STRATEGY = getGlobalOptionsDestroyStrategy(); |
| 107 | + |
| 108 | + function getGlobalOptionsDestroyStrategy() { |
| 109 | + var defaultStrategy = undefined; |
| 110 | + if (scope.options && scope.options.hasOwnProperty('destroyStrategy')) { |
| 111 | + var globalOptionsDestroyStrategy = scope.options.destroyStrategy; |
| 112 | + var isValidFormDefaultDestroyStrategy = (globalOptionsDestroyStrategy === undefined || |
| 113 | + globalOptionsDestroyStrategy === '' || |
| 114 | + globalOptionsDestroyStrategy === null || |
| 115 | + globalOptionsDestroyStrategy === 'retain'); |
| 116 | + if (isValidFormDefaultDestroyStrategy) { |
| 117 | + defaultStrategy = globalOptionsDestroyStrategy; |
| 118 | + } |
| 119 | + else { |
| 120 | + console.warn('Unrecognized globalOptions destroyStrategy: %s \'%s\'. Used undefined instead.', |
| 121 | + typeof globalOptionsDestroyStrategy, globalOptionsDestroyStrategy); |
| 122 | + } |
| 123 | + } |
| 124 | + return defaultStrategy; |
100 | 125 | } |
101 | 126 |
|
102 | | - }); |
| 127 | + // Clean up the model when the corresponding form field is $destroy-ed. |
| 128 | + // Default behavior can be supplied as a globalOption, and behavior can be overridden in the form definition. |
| 129 | + scope.$on('$destroy', function() { |
| 130 | + |
| 131 | + var form = getForm(); |
| 132 | + var conditionResult = $parse(form.condition); |
| 133 | + var formModelNotRetained = !sfRetainModel.getFlag(); |
| 134 | + |
| 135 | + // If condition is defined and not satisfied and the sfSchema model should not be retained. |
| 136 | + if (form.hasOwnProperty('condition') && !conditionResult(scope) && formModelNotRetained) { |
| 137 | + |
| 138 | + // Either set in form definition, or as part of globalOptions. |
| 139 | + var destroyStrategy = |
| 140 | + !form.hasOwnProperty('destroyStrategy') ? DEFAULT_DESTROY_STRATEGY : form.destroyStrategy; |
| 141 | + var schemaType = getSchemaType(); |
| 142 | + |
| 143 | + if (destroyStrategy && destroyStrategy !== 'retain') { |
| 144 | + // Don't recognize the strategy, so give a warning. |
| 145 | + console.warn('%s has defined unrecognized destroyStrategy: \'%s\'. Used default instead.', |
| 146 | + attrs.name, destroyStrategy); |
| 147 | + destroyStrategy = DEFAULT_DESTROY_STRATEGY; |
| 148 | + } |
| 149 | + else if (schemaType !== 'string' && destroyStrategy === '') { |
| 150 | + // Only 'string' type fields can have an empty string value as a valid option. |
| 151 | + console.warn('%s attempted to use empty string destroyStrategy on non-string form type. ' + |
| 152 | + 'Used default instead.', attrs.name); |
| 153 | + destroyStrategy = DEFAULT_DESTROY_STRATEGY; |
| 154 | + } |
| 155 | + |
| 156 | + if (destroyStrategy === 'retain') { |
| 157 | + return; // Valid option to avoid destroying data in the model. |
| 158 | + } |
| 159 | + |
| 160 | + destroyUsingStrategy(destroyStrategy); |
| 161 | + |
| 162 | + function destroyUsingStrategy(strategy) { |
| 163 | + var strategyIsDefined = (strategy === null || strategy === '' || strategy === undefined); |
| 164 | + if (!strategyIsDefined) { |
| 165 | + strategy = DEFAULT_DESTROY_STRATEGY; |
| 166 | + } |
| 167 | + sfUnselect(scope.form.key, scope.model, strategy); |
| 168 | + } |
| 169 | + |
| 170 | + function getSchemaType() { |
| 171 | + var sType; |
| 172 | + if (form.schema) { |
| 173 | + sType = form.schema.type; |
| 174 | + } |
| 175 | + else { |
| 176 | + sType = null; |
| 177 | + } |
| 178 | + return sType; |
| 179 | + } |
| 180 | + } |
| 181 | + }); |
| 182 | + |
103 | 183 |
|
104 | | - scope.schemaError = function() { |
105 | | - return error; |
106 | | - }; |
| 184 | + scope.schemaError = function() { |
| 185 | + return error; |
| 186 | + }; |
107 | 187 |
|
108 | | - } |
109 | | - }; |
110 | | -}]); |
| 188 | + } |
| 189 | + }; |
| 190 | + }]); |
0 commit comments