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
+ '$templateCache' ,
10
+ 'formlyTemplate' ,
11
+ function formlyField ( $http , $compile , $templateCache , formlyTemplate ) {
12
+ return {
13
+ restrict : 'AE' ,
14
+ transclude : true ,
15
+ scope : {
16
+ optionsData : '&options' ,
17
+ formId : '=formId' ,
18
+ index : '=index' ,
19
+ value : '=formValue'
20
+ } ,
21
+ link : function fieldLink ( $scope , $element , $attr ) {
22
+ var template = $scope . options . template ;
23
+ if ( template ) {
24
+ setElementTemplate ( template ) ;
25
+ } else {
26
+ var templateUrl = $scope . options . templateUrl || formlyTemplate . getTemplateUrl ( $scope . options . type ) ;
27
+ if ( templateUrl ) {
28
+ $http . get ( templateUrl , { cache : $templateCache } ) . then ( function ( response ) {
29
+ setElementTemplate ( response . data ) ;
30
+ } , function ( error ) {
31
+ console . log ( 'Formly Error: Problem loading template for ' + templateUrl , error ) ;
32
+ } ) ;
33
+ } else {
34
+ console . log ( 'Formly Error: template type \'' + $scope . options . type + '\' not supported.' ) ;
35
+ }
36
+ }
37
+ function setElementTemplate ( templateData ) {
38
+ $element . html ( templateData ) ;
39
+ $compile ( $element . contents ( ) ) ( $scope ) ;
40
+ }
41
+ } ,
42
+ controller : [
43
+ '$scope' ,
44
+ function fieldController ( $scope ) {
45
+ $scope . options = $scope . optionsData ( ) ;
46
+ if ( typeof $scope . options . default !== 'undefined' ) {
47
+ $scope . value = $scope . options . default ;
48
+ }
49
+ var type = $scope . options . type ;
50
+ if ( ! type && $scope . options . template ) {
51
+ type = 'template' ;
52
+ } else if ( ! type && $scope . options . templateUrl ) {
53
+ type = 'templateUrl' ;
54
+ }
55
+ // set field id to link labels and fields
56
+ $scope . id = $scope . formId + type + $scope . index ;
57
+ }
58
+ ]
59
+ } ;
60
+ }
61
+ ] ) ;
62
+ 'use strict' ;
63
+ angular . module ( 'formly.render' ) . directive ( 'formlyForm' , [
64
+ 'formlyOptions' ,
65
+ '$compile' ,
66
+ function formlyForm ( formlyOptions , $compile ) {
67
+ return {
68
+ restrict : 'AE' ,
69
+ templateUrl : 'directives/formly-form.html' ,
70
+ replace : true ,
71
+ scope : {
72
+ fields : '=' ,
73
+ options : '=?' ,
74
+ result : '=' ,
75
+ formOnParentScope : '=name'
76
+ } ,
77
+ compile : function ( scope , iElement , iAttrs , controller , transcludeFn ) {
78
+ return {
79
+ post : function ( scope , ele , attr , controller ) {
80
+ scope . options = angular . extend ( formlyOptions . getOptions ( ) , scope . options ) ;
81
+ if ( scope . options . submitButtonTemplate ) {
82
+ ele . find ( 'button' ) . replaceWith ( $compile ( scope . options . submitButtonTemplate ) ( scope ) ) ;
83
+ }
84
+ //Post gets called after angular has created the FormController
85
+ //Now pass the FormController back up to the parent scope
86
+ scope . formOnParentScope = scope [ attr . name ] ;
87
+ }
88
+ } ;
89
+ } ,
90
+ controller : [
91
+ '$scope' ,
92
+ '$element' ,
93
+ '$parse' ,
94
+ function ( $scope , $element , $parse ) {
95
+ // setup watches for watchExpressions
96
+ angular . forEach ( $scope . fields , function ( field , index ) {
97
+ if ( angular . isDefined ( field . watch ) ) {
98
+ var watchExpression = field . watch . expression ;
99
+ if ( angular . isFunction ( watchExpression ) ) {
100
+ // wrap the field's watch expression so we can call it with the field as the first arg as a helper
101
+ watchExpression = function ( ) {
102
+ var args = Array . prototype . slice . call ( arguments , 0 ) ;
103
+ args . unshift ( field ) ;
104
+ return field . watch . expression . apply ( this , args ) ;
105
+ } ;
106
+ }
107
+ $scope . $watch ( watchExpression , function ( ) {
108
+ // wrap the field's watch listener so we can call it with the field as the first arg as a helper
109
+ var args = Array . prototype . slice . call ( arguments , 0 ) ;
110
+ args . unshift ( field ) ;
111
+ return field . watch . listener . apply ( this , args ) ;
112
+ } ) ;
113
+ }
114
+ } ) ;
115
+ $scope . $watch ( 'result' , function ( newValue ) {
116
+ angular . forEach ( $scope . fields , function ( field , index ) {
117
+ if ( field . hideExpression ) {
118
+ field . hide = $parse ( field . hideExpression ) ( $scope . result ) ;
119
+ }
120
+ } ) ;
121
+ } , true ) ;
122
+ }
123
+ ]
124
+ } ;
125
+ }
126
+ ] ) ;
127
+ 'use strict' ;
128
+ angular . module ( 'formly.render' ) . provider ( 'formlyOptions' , function ( ) {
129
+ var options = {
130
+ uniqueFormId : null ,
131
+ submitCopy : 'Submit' ,
132
+ hideSubmit : false ,
133
+ submitButtonTemplate : null
134
+ } ;
135
+ function setOption ( name , value ) {
136
+ if ( typeof name === 'string' ) {
137
+ options [ name ] = value ;
138
+ } else {
139
+ angular . forEach ( name , function ( val , name ) {
140
+ setOption ( name , val ) ;
141
+ } ) ;
142
+ }
143
+ }
144
+ function getOptions ( ) {
145
+ // copy to avoid third-parties manipulating the options outside of the api.
146
+ return angular . copy ( options ) ;
147
+ }
148
+ this . setOption = setOption ;
149
+ this . getOptions = getOptions ;
150
+ this . $get = function formlyOptions ( ) {
151
+ return this ;
152
+ } ;
153
+ } ) ;
154
+ 'use strict' ;
155
+ angular . module ( 'formly.render' ) . provider ( 'formlyTemplate' , function ( ) {
156
+ var templateMap = {
157
+ textarea : 'directives/formly-field-textarea.html' ,
158
+ radio : 'directives/formly-field-radio.html' ,
159
+ select : 'directives/formly-field-select.html' ,
160
+ number : 'directives/formly-field-number.html' ,
161
+ checkbox : 'directives/formly-field-checkbox.html' ,
162
+ password : 'directives/formly-field-password.html' ,
163
+ hidden : 'directives/formly-field-hidden.html' ,
164
+ email : 'directives/formly-field-email.html' ,
165
+ text : 'directives/formly-field-text.html'
166
+ } ;
167
+ function setTemplateUrl ( name , templateUrl ) {
168
+ if ( typeof name === 'string' ) {
169
+ templateMap [ name ] = templateUrl ;
170
+ } else {
171
+ angular . forEach ( name , function ( templateUrl , name ) {
172
+ setTemplateUrl ( name , templateUrl ) ;
173
+ } ) ;
174
+ }
175
+ }
176
+ function getTemplateUrl ( type ) {
177
+ return templateMap [ type ] ;
178
+ }
179
+ ;
180
+ this . setTemplateUrl = setTemplateUrl ;
181
+ this . getTemplateUrl = getTemplateUrl ;
182
+ this . $get = function formlyTemplate ( ) {
183
+ return this ;
184
+ } ;
185
+ } ) ;
186
+ angular . module ( 'formly.render' ) . run ( [
187
+ '$templateCache' ,
188
+ function ( $templateCache ) {
189
+ 'use strict' ;
190
+ $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>' ) ;
191
+ $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>' ) ;
192
+ $templateCache . put ( 'directives/formly-field-hidden.html' , '<input type=hidden ng-model=value>' ) ;
193
+ $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>' ) ;
194
+ $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>' ) ;
195
+ $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-required=options.required ng-model=$parent.value> {{option.name}}</label></div></div>' ) ;
196
+ $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-init="value = options.options[options.default]" ng-options="option.name group by option.group for option in options.options"></select></div>' ) ;
197
+ $templateCache . put ( 'directives/formly-field-text.html' , '<div class=form-group><label for={{id}}>{{options.label || \'Text\'}} {{options.required ? \'*\' : \'\'}}</label><input type=text class=form-control id={{id}} placeholder={{options.placeholder}} ng-required=options.required ng-disabled=options.disabled ng-model=value></div>' ) ;
198
+ $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>' ) ;
199
+ $templateCache . put ( 'directives/formly-field.html' , '' ) ;
200
+ $templateCache . put ( 'directives/formly-form.html' , '<form class=formly role=form><formly-field ng-repeat="field in fields" options=field form-value=result[field.key||$index] class=formly-field form-id=options.uniqueFormId index=$index ng-hide=field.hide></formly-field><button type=submit class="btn btn-primary" ng-hide=options.hideSubmit>{{options.submitCopy || "Submit"}}</button></form>' ) ;
201
+ }
202
+ ] ) ;
0 commit comments