Skip to content

Commit fd68f55

Browse files
committed
Add back examples
1 parent 66da104 commit fd68f55

22 files changed

+1671
-130
lines changed

examples/add-on/calculate.js

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/**
2+
* I calculate a field value based on a provided mathematical string
3+
*
4+
* @homepage https://github.com/Anthropic/angular-schema-form-calculate
5+
*
6+
* NOTE: This version is not in sync with the source repo as it has
7+
* been modified to work with the new version of the framework in development
8+
*
9+
* @example
10+
* {
11+
* "type":"calculate",
12+
* "key":"area", // the key to COPY the RESULT to (doesn't need 'model' prefix)
13+
* "watch":["length","width"], // The KEYS to WATCH for changes
14+
* "calculate":"model.length * model.width"
15+
* }
16+
*/
17+
angular
18+
.module('schemaForm')
19+
.run(function($templateCache) {
20+
// A template to use
21+
$templateCache.put('calculated-fields.html','<calculate sf-field-model model="model" form="form" />');
22+
})
23+
.directive('calculate', ['$compile', '$http', 'sfBuilder', 'sfSelect', '$interpolate', 'schemaFormDecorators',
24+
function($compile, $http, sfBuilder, sfSelect, $interpolate, schemaFormDecoratorsProvider) {
25+
return {
26+
restrict: 'E',
27+
scope: true,
28+
link: {
29+
post: function(scope, element, attrs, ctrl) {
30+
var watchKeys = scope.form.watch,
31+
key, keyFixed, exec,
32+
i;
33+
34+
scope.form.format = scope.form.format || 'number';
35+
36+
for (i=0; i < watchKeys.length; i++) {
37+
key = watchKeys[i].split('[]');
38+
keyFixed = key.reduce(function(pv, cv, ci, ta){
39+
return '' + pv + ((cv[0]=='.')? '['+scope.$i[ci-1]+']'+cv: cv);
40+
});
41+
42+
scope.$watch(keyFixed,
43+
function (val, old) {
44+
var newValue = $interpolate('{{' + scope.form.calculate + '}}', false, null, true)({
45+
model: scope.model,
46+
$i: scope.$i,
47+
$index: scope.$index,
48+
path: scope.path
49+
});
50+
51+
if(scope.form.lookup) {
52+
scope.model.calculated = encodeURIComponent(newValue);
53+
var lookup = $interpolate(scope.form.lookup, false, null, true)(scope.model);
54+
$http.get(lookup, { responseType: 'json' })
55+
.success(function(response, status) {
56+
if(response.data) update(response.data);
57+
})
58+
.error(function(data, status) {
59+
scope.form.options = [];
60+
scope.form.selectedOption = '';
61+
});
62+
}
63+
else {
64+
update(newValue);
65+
};
66+
67+
function update(value) {
68+
if(scope.form.format == 'number') value = Number(value);
69+
sfSelect(scope.form.key, scope.model, value);
70+
};
71+
});
72+
};
73+
}
74+
}
75+
};
76+
}
77+
])
78+
.config([ 'schemaFormDecoratorsProvider', 'sfBuilderProvider',
79+
function(schemaFormDecoratorsProvider, sfBuilderProvider) {
80+
var sfField = sfBuilderProvider.builders.sfField;
81+
var ngModel = sfBuilderProvider.builders.ngModel;
82+
var ngModelOptions = sfBuilderProvider.builders.ngModelOptions;
83+
var defaults = [ sfField, ngModel ];
84+
85+
schemaFormDecoratorsProvider.defineAddOn(
86+
'bootstrapDecorator',
87+
'calculate',
88+
'calculated-fields.html',
89+
defaults
90+
);
91+
}
92+
]);

examples/builder.html

Lines changed: 0 additions & 55 deletions
This file was deleted.

examples/builder2.html

Lines changed: 0 additions & 75 deletions
This file was deleted.

examples/data/array-radiobuttons.json

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
{
2+
"schema": {
3+
"type":"object",
4+
"properties":{
5+
"field1":{
6+
"type":"array",
7+
"items":{"type":"object",
8+
"properties":{
9+
"field":{"type":"string",
10+
"title":"Radio 1 Example",
11+
"enum":["valA","valB","valC"]
12+
}
13+
}
14+
}
15+
},
16+
"field2":{"type":"array",
17+
"items":{"type":"object",
18+
"properties":{
19+
"field":{"type":"string",
20+
"title":"Radio 2 Example",
21+
"enum":["val1","val2","val3"]
22+
}
23+
}
24+
}
25+
}
26+
}
27+
},
28+
"form": [
29+
{
30+
"key":"field1",
31+
"add":null,
32+
"notitle":true,
33+
"items":[
34+
{
35+
"key":"field1[].field",
36+
"type": "radiobuttons",
37+
"titleMap": [
38+
{ "value": "valA", "name": "Value A" },
39+
{ "value": "valB", "name": "Value B" },
40+
{ "value": "valC", "name": "Value C" }
41+
]
42+
}
43+
]
44+
},
45+
{
46+
"key":"field2",
47+
"add":"new",
48+
"notitle":true,
49+
"items":[
50+
{
51+
"key":"field2[].field",
52+
"type": "radios",
53+
"titleMap": [
54+
{ "value": "val1", "name": "Value 1" },
55+
{ "value": "val2", "name": "Value 2" },
56+
{ "value": "val3", "name": "Value 3" }
57+
]
58+
}
59+
]
60+
}
61+
]
62+
}

examples/data/array.json

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
{
2+
"schema": {
3+
"type": "object",
4+
"title": "Comment",
5+
"required": ["comments"],
6+
"properties": {
7+
"comments": {
8+
"type": "array",
9+
"maxItems": 2,
10+
"items": {
11+
"type": "object",
12+
"properties": {
13+
"name": {
14+
"title": "Name",
15+
"type": "string"
16+
},
17+
"email": {
18+
"title": "Email",
19+
"type": "string",
20+
"pattern": "^\\S+@\\S+$",
21+
"description": "Email will be used for evil."
22+
},
23+
"spam": {
24+
"title": "Spam",
25+
"type": "boolean",
26+
"default": true
27+
},
28+
"comment": {
29+
"title": "Comment",
30+
"type": "string",
31+
"maxLength": 20,
32+
"validationMessage": "Don't be greedy!"
33+
}
34+
},
35+
"required": ["name","comment"]
36+
}
37+
}
38+
}
39+
},
40+
"form": [
41+
{
42+
"type": "help",
43+
"helpvalue": "<h4>Array Example</h4><p>Try adding a couple of forms, reorder by drag'n'drop.</p>"
44+
},
45+
{
46+
"key": "comments",
47+
"add": "New",
48+
"style": {
49+
"add": "btn-success"
50+
},
51+
"items": [
52+
"comments[].name",
53+
"comments[].email",
54+
{
55+
"key": "comments[].spam",
56+
"type": "checkbox",
57+
"title": "Yes I want spam.",
58+
"condition": "model.comments[arrayIndex].email"
59+
},
60+
{
61+
"key": "comments[].comment",
62+
"type": "textarea"
63+
}
64+
]
65+
},
66+
{
67+
"type": "submit",
68+
"style": "btn-info",
69+
"title": "OK"
70+
}
71+
]
72+
}

0 commit comments

Comments
 (0)