Skip to content

Commit 4da8b0d

Browse files
author
Kamil Kisiela
committed
Merge branch 'release/v0.3.0'
2 parents 378a829 + 2cfc3e1 commit 4da8b0d

14 files changed

Lines changed: 351 additions & 50 deletions

CHANGELOG.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,19 @@
22
All notable changes to this project will be documented in this file.
33
This project adheres to [Semantic Versioning](http://semver.org/).
44

5+
## [0.3.0] - 2015-11-23
6+
### Added
7+
- Support autoformly.validation.messages in SimpleSchema
8+
- Add optional manual formly configuration for each field in autoFormly service
9+
- Implement more advanced filtering
10+
- `schema.autoformly.templateOptions.rows` to be displayed as textarea
11+
12+
### Deprecated
13+
- Filtering using schema keys as array
14+
15+
### Fixed
16+
- Prevent templatesObject.label overwriting
17+
518
## [0.2.1] - 2015-11-18
619
### Changed
720
- Refactor all files to use Strict Dependency Injection
@@ -20,6 +33,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
2033

2134
## 0.0.1 - 2015-11-14
2235

36+
[0.3.0]: https://github.com/wieldo/meteor-autoformly/compare/v0.2.1...v0.3.0
2337
[0.2.1]: https://github.com/wieldo/meteor-autoformly/compare/v0.2.0...v0.2.1
2438
[0.2.0]: https://github.com/wieldo/meteor-autoformly/compare/v0.1.0...v0.2.0
2539
[0.1.0]: https://github.com/wieldo/meteor-autoformly/compare/v0.0.1...v0.1.0

README.md

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,52 @@ meteor add wieldo:autoformly
1717

1818
## How to use it
1919

20+
21+
### Convert all schema fields
22+
2023
```javascript
2124
const fields = autoFormly.collection(BooksCollection);
2225
// or
2326
const fields = autoFormly.schema(BooksSchema);
2427
```
2528

29+
### Convert all schema fields excluding one
30+
```javascript
31+
const fields = autoFormly.schema(BooksSchema, {
32+
fields: {
33+
published: false
34+
}
35+
});
36+
```
37+
38+
### Convert selected schema fields
39+
```javascript
40+
const fields = autoFormly.schema(BooksSchema, {
41+
all: false,
42+
fields: {
43+
published: true,
44+
author: true,
45+
title: true
46+
}
47+
});
48+
```
49+
50+
### Extend formly configuration for selected field
51+
52+
```javascript
53+
const fields = autoFormly.schema(BooksSchema, {
54+
all: false,
55+
fields: {
56+
published: true,
57+
author: {
58+
templateOptions: {
59+
label: "Written by"
60+
}
61+
}
62+
}
63+
});
64+
```
65+
2666
## What is ready?
2767
Take a look at this docs:
2868

@@ -59,20 +99,21 @@ We're currently working on three other packages that are very useful in autoForm
5999
### Roadmap 1.0
60100

61101
- [x] Extend SimpleSchema to use `autoformly` property
62-
- [ ] Support for **_array of objects_**
102+
- [x] Add optional manual formly configuration for each field
103+
- [x] More advanced field filtering (show all / hide all / add excluding)
63104
- [x] `schema.key` as `formly.key`
64105
- [x] `schema.label` as `formly.templateOptions.label`
65106
- [x] `schema.optional` and required validator
66107
- [x] `schema.max` for **_String_** and **_Number_** types as **maxlength** and **maxnumber** validator
67108
- [x] `schema.min` for **_String_** and **_Number_** types as **minlength** and **minnumber** validator
68109
- [x] `schema.regEx` as **pattern** validator
69110
- [x] `schema.defaultValue` as `formly.defaultValue`
70-
- [ ] `schema.autoformly.rows` to be displayed textarea
111+
- [x] `schema.autoformly.templateOptions.rows` to be displayed as textarea
71112
- [x] **_Boolean_** type as checkbox
72113
- [x] `schema.autoformly.type` to be `formly.type`
73114
- [x] `schema.allowedValues` as select element (schema type is a String)
74-
- [ ] `schema.minCount` support
75-
- [ ] `schema.maxCount` support
115+
- [ ] `schema.minCount` and `schema.maxCount` support
116+
- [ ] Support for **_array of objects_**
76117
- [ ] Support for **_Object_** type fields
77118
- [ ] Support for server-side validation errors (like _unique_)
78119
- [ ] Interactive **demo**

docs/auto-formly-parsers.md

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,28 @@ autoFormlyParsers
55
AngularJS service.
66
```
77

8-
__File: [lib/client/auto-formly-parsers.js](../lib/client/auto-formly-parsers.js)__
8+
__File: [lib/client/auto-formly-parsers.js](../lib/client/auto-formly-parsers.js)__
9+
10+
-
11+
12+
### *autoFormlyParsers*.register(parser)
13+
14+
```
15+
Register new parsing function
16+
```
17+
18+
__Arguments__
19+
20+
* __collection__ *{Function}*
21+
22+
Parsing function with three arguments:
23+
24+
- *{string}* field's key
25+
- *{object}* copy of field's schema from SimpleSchema object
26+
- *{object}* object with formly configuration for field
27+
28+
29+
__Returns__ *{undefined}*
30+
31+
There is no need to return something. Use formly configuration object as reference.
32+

docs/auto-formly.md

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,37 +21,25 @@ __Arguments__
2121

2222
Mongo Collection
2323

24-
* __fields__ *{array|undefined}*
24+
* __options__ *{array|object|undefined}*
2525

26-
Optional filtering fields using collection's keys
26+
Optional filtering fields using array with collection's keys
27+
or options object.
2728

28-
__Returns__ *{Array}*
29-
30-
Array with fields to use in formly-form.
31-
32-
-
33-
34-
### *autoFormly*.schema(schema, fields)
35-
36-
```
37-
Parse SimpleSchema object to get array with fields
38-
```
29+
* __options.all__ *{boolean}*
3930

40-
__Arguments__
31+
Show all fields when true. Otherwise only these defined (see below)
4132

42-
* __collection__ *{SimpleSchema}*
33+
* __options.fields__ *{object}*
4334

44-
SimpleSchema instance
35+
Object with the field's key as property and formly field configuration (extends the auto-generated configuration).
36+
Instead of using formly configuration you can set field as visible or not using boolean value (true visible, false not used)
4537

46-
* __fields__ *{array|undefined}*
47-
48-
Optional filtering fields using collection's keys
4938

5039
__Returns__ *{Array}*
5140

5241
Array with fields to use in formly-form.
5342

54-
5543
-
5644

5745
### *autoFormly*.schema(schema, fields)
@@ -66,10 +54,11 @@ __Arguments__
6654

6755
SimpleSchema instance
6856

69-
* __fields__ *{array|undefined}*
57+
* __options__ *{array|object|undefined}*
7058

71-
Optional filtering fields using collection's keys
59+
See collection()
7260

7361
__Returns__ *{Array}*
7462

75-
Array with fields to use in formly-form.
63+
Array with fields to use in formly-form.
64+

lib/client/auto-formly-helpers.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,33 @@ autoFormlyHelpers = class autoFormlyHelpers {
22
createError(msg) {
33
return new Error(`[AutoFormly] ${msg}`);
44
}
5+
6+
createNestedObject(obj, path) {
7+
path = this.pathNestedObject(path);
8+
for (let i = 0; i < path.length; i++) {
9+
obj = obj[path[i]] = obj[path[i]] || {};
10+
}
11+
}
12+
13+
getNestedObject(obj, path) {
14+
path = this.pathNestedObject(path);
15+
let objCopy = angular.copy(obj);
16+
for (let i = 0; i < path.length; i++) {
17+
if (!objCopy.hasOwnProperty(path[i])) {
18+
objCopy = undefined;
19+
break;
20+
} else {
21+
objCopy = objCopy[path[i]];
22+
}
23+
}
24+
25+
return objCopy;
26+
}
27+
28+
pathNestedObject(path) {
29+
if ("string" === typeof path) {
30+
path = path.split('.');
31+
}
32+
return path;
33+
}
534
};

lib/client/auto-formly-parsers.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class autoFormlyParser extends autoFormlyHelpers {
6161
return;
6262
}
6363

64-
const formlyField = {};
64+
const formlyField = fieldSchema.autoformly ? fieldSchema.autoformly : {};
6565

6666
this.runParsers(fieldKey, fieldSchema, formlyField);
6767

lib/client/auto-formly.js

Lines changed: 55 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,15 @@ class autoFormly extends autoFormlyHelpers {
2626
* @public
2727
* @method autoFormly.collection
2828
* @param {Collection} collection
29-
* @param {array} fields - schema keys you want to use (optional)
29+
* @param {array|object} options - schema keys you want to use or formly fields configuration (optional)
3030
* @returns {array}
3131
*/
32-
collection(collection, fields) {
32+
collection(collection, options) {
3333
if (!collection.simpleSchema) {
3434
throw this.createError("Collection does not contain SimpleSchema");
3535
}
3636

37-
return this.schema(collection.simpleSchema(), fields);
37+
return this.schema(collection.simpleSchema(), options);
3838
}
3939

4040
/**
@@ -43,20 +43,63 @@ class autoFormly extends autoFormlyHelpers {
4343
* @public
4444
* @method autoFormly.schema
4545
* @param {SimpleSchema} schema
46-
* @param {array} fields - schema keys you want to use (optional)
46+
* @param {array|object} options - schema keys you want to use or formly fields configuration (optional)
4747
* @returns {array}
4848
*/
49-
schema(schema, fields) {
50-
if (!schema || !angular.isFunction(schema.schema)) {
49+
schema(schema, options) {
50+
let sortedSchema;
51+
const schemaCopy = angular.copy(schema);
52+
53+
/**
54+
* @deprecated avoid filtering with fields keys as array, use object configuration instead
55+
*/
56+
if (!schemaCopy || !angular.isFunction(schemaCopy.schema)) {
5157
throw this.createError("Schema has to be instance of SimpleSchema");
5258
}
5359

54-
let sortedSchema;
60+
// filter and return
61+
if (angular.isArray(options)) {
62+
return this.fields(this.filterSchema(schemaCopy, options));
63+
}
64+
65+
// no options
66+
if (angular.isUndefined(options)) {
67+
return this.fields(schemaCopy.schema());
68+
}
5569

56-
if (fields) {
57-
sortedSchema = this.filterSchema(schema, fields);
70+
//
71+
// handle options object
72+
//
73+
74+
// pass only object
75+
if (!angular.isObject(options)) {
76+
throw this.createError("Options have to be an array or object");
77+
}
78+
79+
// use filter?
80+
if (options.all === false) {
81+
// pass only with defined fields
82+
if (!angular.isObject(options.fields)) {
83+
throw this.createError("Missing or invalid property fields in options");
84+
}
85+
// skip fields marked as unused ("fieldKey": false)
86+
sortedSchema = this.filterSchema(schemaCopy, Object.keys(_.omit(options.fields, (val) => val === false)));
5887
} else {
59-
sortedSchema = schema.schema();
88+
sortedSchema = schemaCopy.schema();
89+
}
90+
91+
// check each field
92+
if (options.fields) {
93+
_.each(sortedSchema, (fSchema, fKey) => {
94+
// and skip field if marked as unused
95+
if (options.fields[fKey] === false) {
96+
delete sortedSchema[fKey];
97+
}
98+
// or extend autoformly in schema using defined field extension
99+
if (angular.isObject(options.fields[fKey])) {
100+
sortedSchema[fKey].autoformly = angular.merge({}, fSchema.autoformly, options.fields[fKey]);
101+
}
102+
});
60103
}
61104

62105
return this.fields(sortedSchema);
@@ -65,12 +108,12 @@ class autoFormly extends autoFormlyHelpers {
65108
/**
66109
*
67110
* @param {object} schema
68-
* @param {array} fields
111+
* @param {array} fields - schema keys you want to use (optional)
69112
* @returns {object}
70113
*/
71114
filterSchema(schema, fields) {
72115
if (!angular.isArray(fields)) {
73-
throw this.createError("Fields have to be an array");
116+
throw this.createError("Fields to filter have to be an array");
74117
}
75118

76119
const sorted = {};

lib/client/parsers/templateoptions-label.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ SetModule('autoFormly')
44
.run(['autoFormlyParser', (autoFormlyParser) => {
55

66
autoFormlyParser.register((fieldKey, fieldSchema, formlyField) => {
7-
if (!formlyField.templateOptions) {
8-
formlyField.templateOptions = {};
9-
}
7+
autoFormlyParser.createNestedObject(formlyField, 'templateOptions');
108

11-
formlyField.templateOptions.label = fieldSchema.label ? fieldSchema.label : fieldKey;
9+
// prevent overwriting
10+
if (!autoFormlyParser.getNestedObject(formlyField, 'templateOptions.label')) {
11+
formlyField.templateOptions.label = fieldSchema.label ? fieldSchema.label : fieldKey;
12+
}
1213
});
1314

1415
}]);

lib/client/parsers/type.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,17 @@ SetModule('autoFormly')
55

66
autoFormlyParser.register((fieldKey, fieldSchema, formlyField) => {
77

8-
if (fieldSchema.autoformly && fieldSchema.autoformly.type) {
8+
if (autoFormlyParser.getNestedObject(fieldSchema, 'autoformly.type')) {
99
formlyField.type = fieldSchema.autoformly.type;
1010
return;
1111
}
1212

1313
if (fieldSchema.type === String) {
14-
15-
if(angular.isDefined(fieldSchema.allowedValues)) {
14+
15+
if (angular.isDefined(fieldSchema.allowedValues)) {
1616
formlyField.type = 'select';
17+
} else if (autoFormlyParser.getNestedObject(fieldSchema, 'autoformly.templateOptions.rows')) {
18+
formlyField.type = 'textarea';
1719
} else {
1820
formlyField.type = 'input';
1921
}

0 commit comments

Comments
 (0)