|
1 | | -angular.module('schemaForm').directive('schemaValidate', ['sfValidator', 'sfSelect', 'sfUnselect', '$parse', 'sfRetainModel', |
2 | | - function(sfValidator, sfSelect, sfUnselect, $parse, sfRetainModel) { |
| 1 | +angular.module('schemaForm').directive('schemaValidate', ['sfValidator', '$parse', |
| 2 | + function(sfValidator, $parse) { |
3 | 3 |
|
4 | 4 | return { |
5 | 5 | restrict: 'A', |
@@ -102,116 +102,6 @@ angular.module('schemaForm').directive('schemaValidate', ['sfValidator', 'sfSele |
102 | 102 |
|
103 | 103 | }); |
104 | 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; |
125 | | - }*/ |
126 | | - |
127 | | - |
128 | | - // Clean up the model when the corresponding form field is $destroy-ed. |
129 | | - // Default behavior can be supplied as a globalOption, and behavior can be overridden in the form definition. |
130 | | - scope.$on('$destroy', function() { |
131 | | - console.log('Schema validate destroy', scope.externalDestructionInProgress); |
132 | | - |
133 | | - // If the entire schema form is destroyed we don't touch the model |
134 | | - if (!scope.externalDestructionInProgress) { |
135 | | - var form = getForm(); |
136 | | - var destroyStrategy = form.destroyStrategy || |
137 | | - (scope.options && scope.options.destroyStrategy) || 'remove'; |
138 | | - console.log('going with destroy strategy ', destroyStrategy, scope.options) |
139 | | - // No key no model, and we might have strategy 'retain' |
140 | | - if (form.key && destroyStrategy !== 'retain') { |
141 | | - |
142 | | - // Get the object that has the property we wan't to clear. |
143 | | - var obj = scope.model; |
144 | | - if (form.key.length > 1) { |
145 | | - obj = sfSelect(form.key.slice(0, form.key.length - 1), obj); |
146 | | - } |
147 | | - |
148 | | - // We can get undefined here if the form hasn't been filled out entirely |
149 | | - if (obj === undefined) { |
150 | | - return; |
151 | | - } |
152 | | - |
153 | | - // Type can also be a list in JSON Schema |
154 | | - var type = (form.schema && form.schema.type) || ''; |
155 | | - |
156 | | - // Empty means '',{} and [] for appropriate types and undefined for the rest |
157 | | - console.log('destroy', destroyStrategy, form.key, type, obj); |
158 | | - if (destroyStrategy === 'empty' && type.indexOf('string') !== -1) { |
159 | | - obj[form.key.slice(-1)] = ''; |
160 | | - } else if (destroyStrategy === 'empty' && type.indexOf('object') !== -1) { |
161 | | - obj[form.key.slice(-1)] = {}; |
162 | | - } else if (destroyStrategy === 'empty' && type.indexOf('array') !== -1) { |
163 | | - obj[form.key.slice(-1)] = []; |
164 | | - } else if (destroyStrategy === 'null') { |
165 | | - obj[form.key.slice(-1)] = null; |
166 | | - } else { |
167 | | - delete obj[form.key.slice(-1)]; |
168 | | - } |
169 | | - } |
170 | | - } |
171 | | - |
172 | | - |
173 | | - /*var conditionResult = $parse(form.condition); |
174 | | - var formModelNotRetained = !sfRetainModel.getFlag(); |
175 | | -
|
176 | | - // If condition is defined and not satisfied and the sfSchema model should not be retained. |
177 | | - if (form.hasOwnProperty('condition') && !conditionResult(scope) && formModelNotRetained) { |
178 | | -
|
179 | | - // Either set in form definition, or as part of globalOptions. |
180 | | - var destroyStrategy = |
181 | | - !form.hasOwnProperty('destroyStrategy') ? DEFAULT_DESTROY_STRATEGY : form.destroyStrategy; |
182 | | - var schemaType = getSchemaType(); |
183 | | -
|
184 | | - if (destroyStrategy && destroyStrategy !== 'retain') { |
185 | | - // Don't recognize the strategy, so give a warning. |
186 | | - console.warn('%s has defined unrecognized destroyStrategy: \'%s\'. Used default instead.', |
187 | | - attrs.name, destroyStrategy); |
188 | | - destroyStrategy = DEFAULT_DESTROY_STRATEGY; |
189 | | - } |
190 | | - else if (schemaType !== 'string' && destroyStrategy === '') { |
191 | | - // Only 'string' type fields can have an empty string value as a valid option. |
192 | | - console.warn('%s attempted to use empty string destroyStrategy on non-string form type. ' + |
193 | | - 'Used default instead.', attrs.name); |
194 | | - destroyStrategy = DEFAULT_DESTROY_STRATEGY; |
195 | | - } |
196 | | -
|
197 | | - if (destroyStrategy === 'retain') { |
198 | | - return; // Valid option to avoid destroying data in the model. |
199 | | - } |
200 | | -
|
201 | | - destroyUsingStrategy(destroyStrategy); |
202 | | -
|
203 | | - function destroyUsingStrategy(strategy) { |
204 | | - var strategyIsDefined = (strategy === null || strategy === '' || strategy === undefined); |
205 | | - if (!strategyIsDefined) { |
206 | | - strategy = DEFAULT_DESTROY_STRATEGY; |
207 | | - } |
208 | | - sfUnselect(scope.form.key, scope.model, strategy); |
209 | | - } |
210 | | - } |
211 | | - */ |
212 | | - }); |
213 | | - |
214 | | - |
215 | 105 | scope.schemaError = function() { |
216 | 106 | return error; |
217 | 107 | }; |
|
0 commit comments