-
Notifications
You must be signed in to change notification settings - Fork 649
/
Copy pathsf-field.directive.js
362 lines (323 loc) · 13 KB
/
sf-field.directive.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
import angular from 'angular';
/**
* I am the directive for managing field properties
*
* @param {function} $parse
* @param {function} $compile
* @param {function} $interpolate
* @param {object} sfErrorMessage
* @param {object} sfPath
* @param {function} sfSelect
*
* @return {object} I am the object providing the directive API to Angular
*/
export default function($parse, $compile, $interpolate, sfErrorMessage, sfPath, sfSelect) {
const keyFormat = {
COMPLETE: '*',
PATH: 'string',
INDICES: 'number',
};
return {
restrict: 'AE',
replace: false,
transclude: false,
scope: true,
require: [ '^sfSchema', '?^form', '?^^sfKeyController' ],
link: {
pre: function(scope, element, attrs, ctrl) {
let sfSchema = ctrl[0];
// The ngModelController is used in some templates and
// is needed for error messages,
scope.$on('schemaFormPropagateNgModelController', function(event, ngModel) {
event.stopPropagation();
event.preventDefault();
scope.ngModel = ngModel;
});
// Fetch our form.
scope.initialForm = Object.assign({}, sfSchema.lookup['f' + attrs.sfField]);
scope.form = sfSchema.lookup['f' + attrs.sfField];
},
post: function(scope, element, attrs, ctrl) {
let sfSchema = ctrl[0];
let formCtrl = ctrl[1];
scope.getKey = function(requiredFormat) {
let format = requiredFormat || keyFormat.COMPLETE;
let key = (scope.parentKey) ? scope.parentKey.slice(0, scope.parentKey.length-1) : [];
// Only calculate completeKey if not already saved to form.key
if(scope.completeKey !== scope.form.key) {
if (typeof scope.$index === 'number') {
key = key.concat(scope.$index);
}
if(scope.form.key && scope.form.key.length) {
if(typeof key[key.length-1] === 'number' && scope.form.key.length >= 1) {
let trim = scope.form.key.length - key.length;
scope.completeKey =
trim > 0 ? key.concat(scope.form.key.slice(-trim)) : key;
}
else {
scope.completeKey = scope.form.key.slice();
}
}
}
// If there is no key then there's nothing to return
if(!Array.isArray(scope.completeKey)) {
return undefined;
}
// return the full key if not omiting any types via reduce
if (format === keyFormat.COMPLETE) {
return scope.completeKey;
}
else {
// else to clearly show that data must be ommited
return scope.completeKey.reduce((output, input, i) => {
if (-1 !== [ format ].indexOf((typeof input))) {
return output.concat(input);
}
return output;
}, []);
}
};
// Now that getKey is defined, run it! ...if there's a key.
if(scope.form.key) {
scope.form.key = scope.completeKey = scope.getKey();
}
// Keep error prone logic from the template
scope.showTitle = function() {
return scope.form && scope.form.notitle !== true && scope.form.title;
};
// Normalise names and ids
scope.fieldId = function(prependFormName, omitArrayIndexes) {
let omit = omitArrayIndexes || false;
let formName = (prependFormName && formCtrl && formCtrl.$name) ? formCtrl.$name : undefined;
let key = scope.completeKey;
if(Array.isArray(key)) {
return sfPath.name(key, '-', formName, omit);
}
else {
return '';
}
};
scope.listToCheckboxValues = function(list) {
let values = {};
angular.forEach(list, function(v) {
values[v] = true;
});
return values;
};
scope.checkboxValuesToList = function(values) {
let lst = [];
angular.forEach(values, function(v, k) {
if (v) {
lst.push(k);
}
});
return lst;
};
scope.buttonClick = function($event, form) {
if (angular.isFunction(form.onClick)) {
form.onClick($event, form);
}
else if (angular.isString(form.onClick)) {
if (sfSchema) {
// evaluating in scope outside of sfSchemas isolated scope
sfSchema.evalInParentScope(form.onClick, { '$event': $event, 'form': form });
}
else {
scope.$eval(form.onClick, { '$event': $event, 'form': form });
}
}
};
/**
* Evaluate an expression, i.e. scope.$eval
* but do it in sfSchemas parent scope sf-schema directive is used
*
* @param {string} expression
* @param {Object} locals (optional)
* @return {Any} the result of the expression
*/
scope.evalExpr = function(expression, locals) {
if (sfSchema) {
// evaluating in scope outside of sfSchemas isolated scope
return sfSchema.evalInParentScope(expression, locals);
}
return scope.$eval(expression, locals);
};
/**
* Evaluate an expression, i.e. scope.$eval
* in this decorators scope
*
* @param {string} expression
* @param {Object} locals (optional)
* @return {Any} the result of the expression
*/
scope.evalInScope = function(expression, locals) {
if (expression) {
return scope.$eval(expression, locals);
}
};
/**
* Interpolate the expression.
* Similar to `evalExpr()` and `evalInScope()`
* but will not fail if the expression is
* text that contains spaces.
*
* Use the Angular `{{ interpolation }}`
* braces to access properties on `locals`.
*
* @param {string} expression The string to interpolate.
* @param {Object} locals (optional) Properties that may be accessed in the
* `expression` string.
* @return {Any} The result of the expression or `undefined`.
*/
scope.interp = function(expression, locals) {
return (expression && $interpolate(expression)(locals));
};
// This works since we get the ngModel from the array or the schema-validate directive.
scope.hasSuccess = function() {
if (!scope.ngModel) {
return false;
}
if (scope.options && scope.options.pristine &&
scope.options.pristine.success === false) {
return scope.ngModel.$valid &&
!scope.ngModel.$pristine && !scope.ngModel.$isEmpty(scope.ngModel.$modelValue);
}
else {
return scope.ngModel.$valid &&
(!scope.ngModel.$pristine || !scope.ngModel.$isEmpty(scope.ngModel.$modelValue));
}
};
scope.hasError = function() {
if (!scope.ngModel) {
return false;
}
if (!scope.options || !scope.options.pristine || scope.options.pristine.errors !== false) {
// Show errors in pristine forms. The default.
// Note that "validateOnRender" option defaults to *not* validate initial form.
// so as a default there won't be any error anyway, but if the model is modified
// from the outside the error will show even if the field is pristine.
return scope.ngModel.$invalid;
}
else {
// Don't show errors in pristine forms.
return scope.ngModel.$invalid && !scope.ngModel.$pristine;
}
};
/**
* DEPRECATED: use sf-messages instead.
* Error message handler
* An error can either be a schema validation message or a angular js validtion
* error (i.e. required)
*/
scope.errorMessage = function(schemaError) {
return sfErrorMessage.interpolate(
(schemaError && schemaError.code + '') || 'default',
(scope.ngModel && scope.ngModel.$modelValue) || '',
(scope.ngModel && scope.ngModel.$viewValue) || '',
scope.form,
scope.options && scope.options.validationMessage
);
};
// append the field-id to the htmlClass
scope.form.htmlClass = scope.form.htmlClass || '';
scope.idClass = scope.fieldId(false) + ' ' + scope.fieldId(false, true);
let form = scope.form;
// Where there is a key there is probably a ngModel
if (form.key) {
// It looks better with dot notation.
scope.$on(
'schemaForm.error.' + form.key.join('.'),
function(event, error, validationMessage, validity, formName) {
// validationMessage and validity are mutually exclusive
formName = validity;
if (validationMessage === true || validationMessage === false) {
validity = validationMessage;
validationMessage = undefined;
};
// If we have specified a form name, and this model is not within
// that form, then leave things be.
if (formName != undefined && scope.ngModel.$$parentForm.$name !== formName) {
return;
};
if (scope.ngModel && error) {
if (scope.ngModel.$setDirty) {
scope.ngModel.$setDirty();
}
else {
// FIXME: Check that this actually works on 1.2
scope.ngModel.$dirty = true;
scope.ngModel.$pristine = false;
}
// Set the new validation message if one is supplied
// Does not work when validationMessage is just a string.
if (validationMessage) {
if (!form.validationMessage) {
form.validationMessage = {};
}
form.validationMessage[error] = validationMessage;
}
scope.ngModel.$setValidity(error, validity === true);
if (validity === true) {
// Re-trigger model validator, that model itself would be re-validated
scope.ngModel.$validate();
// Setting or removing a validity can change the field to believe its valid
// but its not. So lets trigger its validation as well.
scope.$broadcast('schemaFormValidate');
}
}
}
);
// Clean up the model when the corresponding form field is $destroy-ed.
// Default behavior can be supplied as a globalOption, and behavior can be overridden
// in the form definition.
scope.$on('$destroy', function() {
let key = scope.getKey();
// If the entire schema form is destroyed we don't touch the model
if (!scope.externalDestructionInProgress) {
const destroyStrategy = form.destroyStrategy ||
(scope.options && scope.options.destroyStrategy) || 'remove';
// No key no model, and we might have strategy 'retain'
if (key && destroyStrategy !== 'retain') {
// Type can also be a list in JSON Schema
const type = (form.schema && form.schema.type) || '';
// Empty means '',{} and [] for appropriate types and undefined for the rest
let value;
if (destroyStrategy === 'empty') {
value = type.indexOf('string') !== -1 ? '' :
type.indexOf('object') !== -1 ? {} :
type.indexOf('array') !== -1 ? [] : undefined;
}
else if (destroyStrategy === 'null') {
value = null;
}
if (value !== undefined) {
sfSelect(key, scope.model, value);
}
else {
// Get the object parent object
let obj = scope.model;
if (key.length > 1) {
obj = sfSelect(key.slice(0, key.length - 1), obj);
}
// parent can be undefined if the form hasn't been filled out
// entirely
if (obj === undefined) {
return;
}
// if parent is an array, then we have already been removed.
// set flag to all children (who are about to recieve a $destroy
// event as well) that we have already been destroyed
if (angular.isArray(obj)) {
scope.externalDestructionInProgress = true;
return;
}
delete obj[key[key.length-1]];
}
}
}
});
}
},
},
};
}