1
+ // Render module for formly to display forms
2
+ angular . module ( 'formly.render' , [ ] ) ;
3
+ // Main Formly Module
4
+ angular . module ( 'formly' , [ 'formly.render' ] ) ;
5
+ 'use strict' ;
6
+ angular . module ( 'formly.render' ) . directive ( 'formlyField' , [
7
+ '$http' ,
8
+ '$compile' ,
9
+ function formlyField ( $http , $compile ) {
10
+ var getTemplateUrl = function ( type ) {
11
+ var templateUrl = '' ;
12
+ switch ( type ) {
13
+ case 'textarea' :
14
+ templateUrl = 'directives/formly-field-textarea.html' ;
15
+ break ;
16
+ case 'radio' :
17
+ templateUrl = 'directives/formly-field-radio.html' ;
18
+ break ;
19
+ case 'select' :
20
+ templateUrl = 'directives/formly-field-select.html' ;
21
+ break ;
22
+ case 'number' :
23
+ templateUrl = 'directives/formly-field-number.html' ;
24
+ break ;
25
+ case 'checkbox' :
26
+ templateUrl = 'directives/formly-field-checkbox.html' ;
27
+ break ;
28
+ case 'password' :
29
+ templateUrl = 'directives/formly-field-password.html' ;
30
+ break ;
31
+ case 'hidden' :
32
+ templateUrl = 'directives/formly-field-hidden.html' ;
33
+ break ;
34
+ case 'email' :
35
+ templateUrl = 'directives/formly-field-email.html' ;
36
+ break ;
37
+ case 'text' :
38
+ templateUrl = 'directives/formly-field-text.html' ;
39
+ break ;
40
+ default :
41
+ templateUrl = null ;
42
+ break ;
43
+ }
44
+ return templateUrl ;
45
+ } ;
46
+ return {
47
+ restrict : 'AE' ,
48
+ transclude : true ,
49
+ scope : {
50
+ optionsData : '&options' ,
51
+ formId : '@formId' ,
52
+ index : '@index' ,
53
+ value : '=formValue'
54
+ } ,
55
+ link : function fieldLink ( $scope , $element , $attr ) {
56
+ var templateUrl = getTemplateUrl ( $scope . options . type ) ;
57
+ if ( templateUrl ) {
58
+ $http . get ( templateUrl ) . success ( function ( data ) {
59
+ //template data returned
60
+ $element . html ( data ) ;
61
+ $compile ( $element . contents ( ) ) ( $scope ) ;
62
+ } ) ;
63
+ } else {
64
+ console . log ( 'Formly Error: template type \'' + $scope . options . type + '\' not supported.' ) ;
65
+ }
66
+ } ,
67
+ controller : [
68
+ '$scope' ,
69
+ function fieldController ( $scope ) {
70
+ $scope . options = $scope . optionsData ( ) ;
71
+ if ( $scope . options . default ) {
72
+ $scope . value = $scope . options . default ;
73
+ }
74
+ // set field id to link labels and fields
75
+ $scope . id = $scope . formId + $scope . options . type + $scope . index ;
76
+ }
77
+ ]
78
+ } ;
79
+ }
80
+ ] ) ;
81
+ 'use strict' ;
82
+ angular . module ( 'formly.render' ) . directive ( 'formlyForm' , function formlyForm ( ) {
83
+ return {
84
+ restrict : 'AE' ,
85
+ templateUrl : 'directives/formly-form.html' ,
86
+ replace : true ,
87
+ scope : {
88
+ formId : '@formId' ,
89
+ fields : '=fields' ,
90
+ options : '=options' ,
91
+ result : '=result'
92
+ } ,
93
+ controller : [
94
+ '$scope' ,
95
+ '$element' ,
96
+ function formController ( $scope , $element ) {
97
+ $scope . populateResult = function ( ) {
98
+ var formChildren = $element . children ( ) ;
99
+ var fieldScope ;
100
+ angular . forEach ( formChildren , function ( field , key ) {
101
+ // grab fields isolate scope
102
+ fieldScope = angular . element ( field ) . scope ( ) ;
103
+ $scope . result [ fieldScope . $index ] = fieldScope . value ;
104
+ } ) ;
105
+ } ;
106
+ }
107
+ ]
108
+ } ;
109
+ } ) ;
110
+ angular . module ( 'formly.render' ) . run ( [
111
+ '$templateCache' ,
112
+ function ( $templateCache ) {
113
+ 'use strict' ;
114
+ $templateCache . put ( 'directives/formly-field-checkbox.html' , '<div class=checkbox><label><input type=checkbox ng-required=options.required ng-disabled=options.disabled ng-model=value>{{options.label || \'Checkbox\'}} {{options.required ? \'*\' : \'\'}}</label></div>' ) ;
115
+ $templateCache . put ( 'directives/formly-field-email.html' , '<div class=form-group><label for={{id}}>{{options.label || \'Email\'}} {{options.required ? \'*\' : \'\'}}</label><input type=email class=form-control id={{id}} placeholder={{options.placeholder}} ng-required=options.required ng-disabled=options.disabled ng-model=value></div>' ) ;
116
+ $templateCache . put ( 'directives/formly-field-hidden.html' , '<input type=hidden ng-model=value>' ) ;
117
+ $templateCache . put ( 'directives/formly-field-number.html' , '<div class=form-group><label for={{id}}>{{options.label || \'Number\'}} {{options.required ? \'*\' : \'\'}}</label><input type=number class=form-control id={{id}} placeholder={{options.placeholder}} ng-required=options.required ng-disabled=options.disabled min={{options.min}} max={{options.max}} ng-minlength={{options.minlength}} ng-maxlength={{options.maxlength}} ng-model=value></div>' ) ;
118
+ $templateCache . put ( 'directives/formly-field-password.html' , '<div class=form-group><label for={{id}}>{{options.label || \'Password\'}} {{options.required ? \'*\' : \'\'}}</label><input type=password class=form-control id={{id}} ng-required=options.required ng-disabled=options.disabled ng-model=value></div>' ) ;
119
+ $templateCache . put ( 'directives/formly-field-radio.html' , '<div class=radio-group><label class=control-label>{{options.label}} {{options.required ? \'*\' : \'\'}}</label><div class=radio ng-repeat="(key, option) in options.options"><label><input type=radio name={{id}} id="{{id + \'_\'+ $index}}" ng-value=option.value ng-model=$parent.value>{{option.name}}</label></div></div>' ) ;
120
+ $templateCache . put ( 'directives/formly-field-select.html' , '<div class=form-group><label for={{id}}>{{options.label || \'Select\'}} {{options.required ? \'*\' : \'\'}}</label><select class=form-control id={{id}} ng-model=value ng-required=options.required ng-disabled=options.disabled ng-options="option.name group by option.group for option in options.options"></select></div>' ) ;
121
+ $templateCache . put ( 'directives/formly-field-text.html' , '<div class=form-group><label for={{id}}>{{options.label || \'Text\'}} {{options.required ? \'*\' : \'\'}}</label><input class=form-control id={{id}} placeholder={{options.placeholder}} ng-required=options.required ng-disabled=options.disabled ng-model=value></div>' ) ;
122
+ $templateCache . put ( 'directives/formly-field-textarea.html' , '<div class=form-group><label for={{id}}>{{options.label || \'Text\'}} {{options.required ? \'*\' : \'\'}}</label><textarea type=text class=form-control id={{id}} rows={{options.lines}} placeholder={{options.placeholder}} ng-required=options.required ng-disabled=options.disabled ng-model=value>\n' + '\t</textarea></div>' ) ;
123
+ $templateCache . put ( 'directives/formly-field.html' , '' ) ;
124
+ $templateCache . put ( 'directives/formly-form.html' , '<form class=formly role=form name={{options.uniqueFormId}}><formly-field ng-repeat="field in fields" options=field form-value=value form-id={{options.uniqueFormId}} index={{$index}}></formly-field><button type=submit ng-click=populateResult()>{{options.submitCopy || "Submit"}}</button></form>' ) ;
125
+ }
126
+ ] ) ;
0 commit comments