Skip to content

Commit c6ff6e0

Browse files
committed
Merge branch 'feature/title-maps-title-maps' into development
2 parents 442fb38 + e5b30ed commit c6ff6e0

File tree

10 files changed

+262
-90
lines changed

10 files changed

+262
-90
lines changed

docs/index.md

Lines changed: 88 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -190,11 +190,11 @@ var schema = {
190190
{
191191
key: "surname",
192192
type: "select",
193-
titleMap: {
194-
"Andersson": "Andersson",
195-
"Johansson": "Johansson",
196-
"other": "Something else..."
197-
}
193+
titleMap: [
194+
{ value: "Andersson", name: "Andersson" },
195+
{ value: "Johansson", name: "Johansson" },
196+
{ value: "other", name: "Something else..."}
197+
]
198198
}
199199
]
200200
```
@@ -361,10 +361,28 @@ scope.
361361

362362
### select and checkboxes
363363

364-
*select* and *checkboxes* can take an object, ```titleMap```, where key is the value to be saved on the model
365-
and the value is the title of the option. In the case of *checkboxes* the values
366-
of the titleMap can be HTML.
364+
*select* and *checkboxes* can take an attribute, `titleMap`, wich defines a name
365+
and a value. The value is bound to the model while the name is used for display.
366+
In the case of *checkboxes* the names of the titleMap can be HTML.
367+
368+
A `titleMap` can be specified as either an object (same as in JSON Form), where
369+
the propery is the value and the value of that property is the name, or as
370+
a list of name-value objects. The latter is used internally and is the recomended
371+
format to use. Note that when defining a `titleMap` as an object the value is
372+
restricted to strings since property names of objects always is a string.
373+
374+
As a list:
375+
```javascript
376+
{
377+
type: "select",
378+
titleMap: [
379+
{ value: "yes", name: "Yes I do" },
380+
{ value: "no", name: "Hell no" }
381+
]
382+
}
383+
```
367384

385+
As an object:
368386
```javascript
369387
{
370388
type: "select",
@@ -423,9 +441,12 @@ We can change this with ```style``` attribute:
423441
```
424442

425443
### radios and radiobuttons
426-
Both type *radios* and *radiobuttons* work the same way, they take a titleMap
427-
and renders ordinary radio buttons or bootstrap 3 buttons inline. It's a
428-
cosmetic choice. The value in the titleMap can be HTML.
444+
Both type *radios* and *radiobuttons* work the same way.
445+
They take a `titleMap` and renders ordinary radio buttons or bootstrap 3 buttons
446+
inline. It's a cosmetic choice.
447+
448+
The `titleMap` is either a list or an object, see [select and checkboxes](#select-and-checkboxes)
449+
for details. The "name" part in the `titleMap` can be HTML.
429450

430451
Ex.
431452
```javascript
@@ -444,15 +465,45 @@ function FormCtrl($scope) {
444465
{
445466
key: "choice",
446467
type: "radiobuttons",
447-
titleMap: {
448-
one: "One",
449-
two: "More..."
468+
titleMap: [
469+
{ value: "one", name: "One" },
470+
{ value, "two", name: "More..." }
471+
]
472+
}
473+
];
474+
}
475+
```
476+
477+
The actual schema property it binds doesn't need to be a string with an enum.
478+
Here is an example creating a yes no radio buttons that binds to a boolean.
479+
480+
Ex.
481+
```javascript
482+
function FormCtrl($scope) {
483+
$scope.schema = {
484+
type: "object",
485+
properties: {
486+
confirm: {
487+
type: "boolean",
488+
default: false
450489
}
451490
}
491+
};
492+
493+
$scope.form = [
494+
{
495+
key: "choice",
496+
type: "radios",
497+
titleMap: [
498+
{ value: false, name: "No I don't understand these cryptic terms" },
499+
{ value: true, , name: "Yes this makes perfect sense to me" }
500+
]
501+
}
452502
];
453503
}
454504
```
455505

506+
456507
With *radiobuttons*, both selected and unselected buttons have btn-primary as default.
457508
We can change this with ```style``` attribute:
458509
```javascript
@@ -475,11 +526,10 @@ function FormCtrl($scope) {
475526
selected: "btn-success",
476527
unselected: "btn-default"
477528
},
478-
titleMap: {
479-
one: "One",
480-
two: "More..."
481-
}
482-
}
529+
titleMap: [
530+
{ value: "one", name: "One" },
531+
{ value, "two", name: "More..." }
532+
]
483533
];
484534
}
485535
```
@@ -515,9 +565,9 @@ function FormCtrl($scope) {
515565
```
516566
517567
### tabs
518-
The ```tabs``` form type lets you split your form into tabs. It is similar to
519-
```fieldset``` in that it just changes the presentation of the form. ```tabs```
520-
takes a option, also called ```tabs```, that is a list of tab objects. Each tab
568+
The `tabs` form type lets you split your form into tabs. It is similar to
569+
`fieldset` in that it just changes the presentation of the form. `tabs`
570+
takes a option, also called `tabs`, that is a list of tab objects. Each tab
521571
object consist of a *title* and a *items* list of form objects.
522572
523573
Ex.
@@ -570,17 +620,17 @@ function FormCtrl($scope) {
570620
```
571621
572622
### array
573-
The ```array``` form type is the default for the schema type ```array```.
574-
The schema for an array has the property ```"items"``` which in the JSON Schema
623+
The `array` form type is the default for the schema type `array`.
624+
The schema for an array has the property `"items"` which in the JSON Schema
575625
specification can be either another schema (i.e. and object), or a list of
576626
schemas. Only a schema is supported by Schema Form, and not the list of schemas.
577627
578-
The *form* definition has the option ```ìtems``` that should be a list
628+
The *form* definition has the option `items` that should be a list
579629
of form objects.
580630
581631
The rendered list of subforms each have a *"Remove"* button and at the bottom there
582632
is an *"Add"* button. The default *"Add"* button has class btn-default and text Add. Both
583-
could be changed using attribute ```add```, see example below.
633+
could be changed using attribute `add`, see example below.
584634
585635
If you like to have drag and drop reordering of arrays you also need
586636
[ui-sortable](https://github.com/angular-ui/ui-sortable) and its dependencies
@@ -589,7 +639,7 @@ what parts of jQueryUI that is needed. You can safely ignore these if you don't
589639
need the reordering.
590640
591641
In the form definition you can refer to properties of an array item by the empty
592-
bracket notation. In the ```key``` simply end the name of the array with ```[]```
642+
bracket notation. In the `key` simply end the name of the array with `[]`
593643
594644
Given the schema:
595645
```json
@@ -615,8 +665,8 @@ Given the schema:
615665
}
616666
}
617667
```
618-
Then ```subforms[].name``` refers to the property name of any subform item,
619-
```subforms[].emails[]``` refers to the subform of emails. See example below for
668+
Then `subforms[].name` refers to the property name of any subform item,
669+
`subforms[].emails[]` refers to the subform of emails. See example below for
620670
usage.
621671
622672
@@ -642,7 +692,7 @@ function FormCtrl($scope) {
642692
643693
644694
Example with sub form, note that you can get rid of the form field the object wrapping the
645-
subform fields gives you per default by using the ```items``` option in the
695+
subform fields gives you per default by using the `items` option in the
646696
form definition.
647697
648698
```javascript
@@ -689,24 +739,24 @@ function FormCtrl($scope) {
689739
690740
691741
### tabarray
692-
The ```tabarray``` form type behaves the same way and has the same options as
693-
```array``` but instead of rendering a list it renders a tab per item in list.
742+
The `tabarray` form type behaves the same way and has the same options as
743+
`array` but instead of rendering a list it renders a tab per item in list.
694744
695745
By default the tabs are on the left side (follows the default in JSON Form),
696-
but with the option ```tabType``` you can change that to eiter *"top"* or *"right"*
746+
but with the option `tabType` you can change that to eiter *"top"* or *"right"*
697747
as well.
698748
699749
Every tab page has a *"Remove"* button. The default *"Remove"* button has class btn-default
700-
and text Remove. Both could be changed using attribute ```remove```, see example below.
750+
and text Remove. Both could be changed using attribute `remove`, see example below.
701751
702-
In this case we have an *"Add"* link, not an *"Add"* button. Therefore, the attribute ```add```
752+
In this case we have an *"Add"* link, not an *"Add"* button. Therefore, the attribute `add`
703753
only changes the text of the link. See example below.
704754
705755
Bootstrap 3 doesn't have side tabs so to get proper styling you need to add the
706756
dependency [bootstrap-vertical-tabs](https://github.com/dbtek/bootstrap-vertical-tabs).
707757
It is not needed for tabs on top.
708758
709-
The ```title``` option is a bit special in ```tabarray```, it defines the title
759+
The `title` option is a bit special in `tabarray`, it defines the title
710760
of the tab and is considered a angular expression. The expression is evaluated
711761
with two extra variables in context: **value** and **$index**, where **value**
712762
is the value in the array (i.e. that tab) and **$index** the index.
@@ -766,9 +816,9 @@ function FormCtrl($scope) {
766816
Post process function
767817
---------------------
768818
769-
If you like to use ```["*"]``` as a form, or aren't in control of the form definitions
819+
If you like to use `["*"]` as a form, or aren't in control of the form definitions
770820
but really need to change or add something you can register a *post process*
771-
function with the ```schemaForm``` service provider. The post process function
821+
function with the `schemaForm` service provider. The post process function
772822
gets one argument, the final form merged with the defaults from the schema just
773823
before it's rendered, and should return a form.
774824

examples/bootstrap-example.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ <h3>Schema</h3>
8686
{ name: "Complex Key Support", data: 'data/complex-keys.json' },
8787
{ name: "Array", data: 'data/array.json' },
8888
{ name: "Tab Array", data: 'data/tabarray.json' },
89+
{ name: "TitleMap Examples", data: 'data/titlemaps.json' },
8990
{ name: "Kitchen Sink", data: 'data/sink.json' }
9091
];
9192

examples/data/sink.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,10 +175,10 @@
175175
{
176176
"key": "radio",
177177
"type": "radios",
178-
"titleMap": {
179-
"Transistor": "Transistor <br> Not the tube kind.",
180-
"Tube": "Tube <br> The tube kind."
181-
}
178+
"titleMap": [
179+
{ "value": "Transistor", "name": "Transistor <br> Not the tube kind." },
180+
{ "value": "Tube", "name": "Tube <br> The tube kind."}
181+
]
182182
},
183183
{
184184
"key": "radiobuttons",

examples/data/titlemaps.json

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
{
2+
"schema": {
3+
"type": "object",
4+
"properties": {
5+
"select": {
6+
"title": "Select without titleMap",
7+
"type": "string",
8+
"enum": ["a","b","c"]
9+
},
10+
"select2": {
11+
"title": "Select with titleMap (old style)",
12+
"type": "string",
13+
"enum": ["a","b","c"]
14+
},
15+
"noenum": { "type": "string", "title": "No enum, but forms says it's a select" },
16+
"array": {
17+
"title": "Array with enum defaults to 'checkboxes'",
18+
"type": "array",
19+
"items": {
20+
"type": "string",
21+
"enum": ["a","b","c"]
22+
}
23+
},
24+
"array2": {
25+
"title": "Array with titleMap",
26+
"type": "array",
27+
"items": {
28+
"type": "string",
29+
"enum": ["a","b","c"]
30+
}
31+
},
32+
"radios": {
33+
"title": "Basic radio button example",
34+
"type": "string",
35+
"enum": ["a","b","c"]
36+
},
37+
"radiobuttons": {
38+
"title": "Radio buttons used to switch a boolean",
39+
"type": "boolean",
40+
"default": false
41+
}
42+
}
43+
},
44+
"form": [
45+
"select",
46+
{
47+
"key": "select2",
48+
"type": "select",
49+
"titleMap": {
50+
"a": "A",
51+
"b": "B",
52+
"c": "C"
53+
}
54+
},
55+
{
56+
"key": "noenum",
57+
"type": "select",
58+
"titleMap": [
59+
{ "value":"a", "name": "A" },
60+
{ "value":"b", "name":"B" },
61+
{ "value":"c", "name":"C" }
62+
]
63+
},
64+
"array",
65+
{
66+
"key": "array2",
67+
"type": "checkboxes",
68+
"titleMap": [
69+
{ "value":"a", "name": "A" },
70+
{ "value":"b", "name":"B" },
71+
{ "value":"c", "name":"C" }
72+
]
73+
},
74+
{
75+
"key": "radios",
76+
"type": "radios",
77+
"titleMap": [
78+
{ "value":"c", "name": "C" },
79+
{ "value":"b", "name":"B" },
80+
{ "value":"a", "name":"A" }
81+
]
82+
},
83+
{
84+
"key":"radiobuttons",
85+
"type": "radiobuttons",
86+
"titleMap": [
87+
{"value": false, "name": "No way"},
88+
{"value": true, "name": "OK"}
89+
]
90+
}
91+
]
92+
}

src/directives/decorators/bootstrap/checkboxes.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
<div class="form-group" ng-class="{'has-error': hasError(), 'has-success': hasSuccess()}" ng-init="checkboxValues = listToCheckboxValues($$value$$)">
22
<label ng-show="showTitle()">{{form.title}}</label>
3-
<div class="checkbox" ng-repeat="value in form.schema.items.enum" >
3+
<div class="checkbox" ng-repeat="item in form.titleMap" >
44
<label>
55
<input type="checkbox"
66
sf-changed="form"
7-
ng-model="checkboxValues[value]"
7+
ng-model="checkboxValues[item.value]"
88
ng-change="$$value$$ = checkboxValuesToList(checkboxValues)" >
9-
<span ng-bind-html="form.titleMap[value] || value"></span>
9+
<span ng-bind-html="item.name"></span>
1010
</label>
1111

1212
</div>

src/directives/decorators/bootstrap/radio-buttons.html

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
<div class="form-group" ng-class="{'has-error': hasError(), 'has-success': hasSuccess()}">
22
<label ng-show="showTitle()">{{form.title}}</label>
33
<div class="btn-group">
4-
<label class="btn {{ (value === $$value$$) ? form.style.selected || 'btn-primary' : form.style.unselected || 'btn-primary'; }}"
5-
ng-class="{ active: value === $$value$$ }"
6-
ng-repeat="(value,name) in form.titleMap">
4+
<label class="btn {{ (item.value === $$value$$) ? form.style.selected || 'btn-primary' : form.style.unselected || 'btn-primary'; }}"
5+
ng-class="{ active: item.value === $$value$$ }"
6+
ng-repeat="item in form.titleMap">
77
<input type="radio"
88
sf-changed="form"
99
style="display: none;"
1010
ng-model="$$value$$"
11-
ng-value="value">
12-
<span ng-bind-html="name"></span>
11+
ng-value="item.value">
12+
<span ng-bind-html="item.name"></span>
1313
</label>
1414
</div>
1515
<div class="help-block" ng-show="form.description" ng-bind-html="form.description"></div>

0 commit comments

Comments
 (0)