Skip to content

Commit ea7f7d7

Browse files
committed
Support for "actions" button group
Which can only hold buttons, no fields. Buttons still has not way to actually call an action though...
1 parent a2633ef commit ea7f7d7

File tree

4 files changed

+102
-0
lines changed

4 files changed

+102
-0
lines changed

problem.txt

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
2+
3+
4+
Olika vägar frammåt:
5+
====================
6+
7+
8+
9+
Problemställningen:
10+
-------------------
11+
json-form fixar en hel del validering, dels genom att läggapå attribut som
12+
min och max på input och dels genom att validera via en schema validtor.
13+
14+
Jag tänkte undvika validatorn och köra "the angular way" genom att ha all validering
15+
i formuläret via HTML5 valideringar och directives som ng-pattern, ng-maxlength osv
16+
antagligen ett par egna.
17+
18+
Hittils har algoritmen sett ut såhär:
19+
#1 schema => defaultForm,
20+
#2 defaultForm+form => finalForm
21+
#3 loopa finalForm och skapa html, dvs all info för validering i formuläret.
22+
23+
Kruxet är att formulär definitionen i json form *inte* innehåller tillräckligt med valideringar
24+
(den innehåller några) utan jag måste lägga till form attribut för dem. (json form tar dem från
25+
schemat)
26+
27+
tex så om schema innehåller "maxLength" attribut så sätts "maxlength" på input fältet, men om form
28+
definitionen innehåller "maxLength" (eller "maxlength" för den delen) så händer inget.
29+
30+
I min algoritm ovan där form definitionen måste innehålla all info för generering av html och
31+
de attribut som styr upp validering så måste jag helt enkelt hitta på eget.
32+
33+
Frågan är hur ska detta egna se ut?
34+
35+
36+
#1 Egna form attribut
37+
När det inte finns ngt form attribut i json forms defintion så skapar jag ett eget, t.ex. så skulle
38+
maxLength bara kopieras till form objektet.
39+
+ minsta motståndets lag
40+
- kluddig lösning, motverkar lite att vara json form kompatibel
41+
- kan allt stödjas?
42+
43+
44+
#2 Eget schema directive
45+
Istället skapa ett eget directive som antingen validerar själv eller lägger till de attribut som
46+
behövs för att validera. Schemat fås genom require och ng-model värdet pekar ut vart i schemat.
47+
Kan möjligtvis implementeras med tv4.
48+
+ snygg lösning
49+
- krångligt directive, måste ta hänsyn till om tex maximum, required osv redan satts via form
50+
definitionen.
51+
52+
53+
54+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<div class="btn-group">
2+
<input ng-repeat-start="item in form.items"
3+
type="submit"
4+
class="btn btn-primary"
5+
value="item.title"
6+
ng-if="item.type === 'submit'">
7+
<button ng-repeat-end class="btn btn-default" ng-if="item.type !== 'submit'">{{item.title}}</button>
8+
</div>

src/directives/decorators/bootstrap/bootstrap-decorator.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ function($parse, $compile, $http, $templateCache){
1616
if (form.type === 'section') {
1717
return 'directives/decorators/bootstrap/section.html';
1818
}
19+
if (form.type === 'actions') {
20+
return 'directives/decorators/bootstrap/actions.html';
21+
}
1922
if (form.type === 'select') {
2023
return 'directives/decorators/bootstrap/select.html';
2124
}

test/schema-form-test.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,10 +537,47 @@ describe('Schema form',function(){
537537

538538
tmpl.children().length.should.be.equal(1);
539539
tmpl.children().eq(0).is('div').should.be.true;
540+
tmpl.children().eq(0).hasClass('btn-group').should.be.false;
540541
tmpl.children().eq(0).children().length.should.be.eq(2);
541542
});
542543
});
543544

545+
it('should handle "action" groups, same as "section" but with a bootstrap class "btn-group"',function(){
546+
547+
inject(function($compile,$rootScope){
548+
var scope = $rootScope.$new();
549+
scope.person = {};
550+
551+
scope.schema = exampleSchema;
552+
553+
scope.form = [{
554+
type: "actions",
555+
items: [
556+
{
557+
type: 'submit',
558+
title: 'yes'
559+
},
560+
{
561+
type: 'button',
562+
title: 'no'
563+
}
564+
]
565+
}];
566+
567+
var tmpl = angular.element('<form sf-schema="schema" sf-form="form" sf-model="person"></form>');
568+
569+
$compile(tmpl)(scope);
570+
$rootScope.$apply();
571+
572+
tmpl.children().length.should.be.equal(1);
573+
tmpl.children().eq(0).is('div').should.be.true;
574+
tmpl.children().eq(0).hasClass('btn-group').should.be.true;
575+
tmpl.children().eq(0).children().length.should.be.eq(2);
576+
tmpl.children().eq(0).children().eq(0).is('input').should.be.true;
577+
tmpl.children().eq(0).children().eq(1).is('button').should.be.true;
578+
});
579+
});
580+
544581

545582
});
546583

0 commit comments

Comments
 (0)