1
- angular . module ( 'ui.directives' , [ 'ui.config' ] )
2
- . factory ( 'InputHelper ', [ '$compile' , '$http' , '$templateCache' , 'ui.config' , function ( $compile , $http , $templateCache , uiConfig ) {
1
+ angular . module ( 'ui.directives' )
2
+ . directive ( 'uiInput ', [ '$compile' , '$http' , '$templateCache' , 'ui.config' , function ( $compile , $http , $templateCache , uiConfig ) {
3
3
4
- uiConfig . uiinput = uiConfig . uiinput || { } ;
4
+ uiConfig . input = uiConfig . input || { } ;
5
5
6
6
var idUid = [ '0' , '0' , '0' ] ;
7
7
var nameUid = [ '0' , '0' , '0' ] ;
@@ -42,60 +42,41 @@ angular.module('ui.directives', ['ui.config'])
42
42
} ) ;
43
43
}
44
44
45
- var internalAttrs = [ 'family' , 'kind' , 'validation' ] ;
46
-
47
45
return {
48
- restrict :'E ' ,
46
+ restrict :'EA ' ,
49
47
priority :10000 ,
50
48
terminal :true ,
51
- compile :function compile ( tElement , tAttrs , transclude ) {
49
+ scope :true ,
50
+ require :'?uiForm' ,
51
+ compile :function compile ( tElement , tAttrs , transclude , uiForm ) {
52
52
53
- var control = {
54
- id :tAttrs . id || 'id ' + nextUid ( idUid )
53
+ var model = tAttrs . ngModel , input = {
54
+ id :tAttrs . id || 'input ' + nextUid ( idUid )
55
55
} ;
56
56
57
57
return function ( scope , element , attrs ) {
58
58
59
- var childScope = scope . $new ( ) ;
60
- var tplFamily = tAttrs . family || uiConfig . uiinput . family ;
61
- var tplKind = tAttrs . kind || uiConfig . uiinput . kind ;
62
-
63
59
//infer a field type from template's tag name (can be one of ui-input, ui-select, ui-textarea)
64
- var targetTagName = tElement [ 0 ] . tagName . substring ( 3 ) . toLowerCase ( ) ;
65
-
66
- $http . get ( targetTagName + '.' + tplFamily + '.' + tplKind + '.html' , { cache :$templateCache } ) . success ( function ( response ) {
60
+ $http . get ( scope . $eval ( attrs . src ) , { cache :$templateCache } ) . success ( function ( response ) {
67
61
68
62
element . html ( response ) ;
69
63
70
- var inputEl = angular . element ( element . find ( targetTagName ) [ 0 ] ) ;
64
+ var inputEl = element . find ( '[ng-transclude]' ) ;
71
65
angular . forEach ( tAttrs , function ( value , key ) {
72
- if ( key . charAt ( 0 ) !== '$' ) {
73
- if ( key . indexOf ( 'input' ) === 0 ) {
74
- control [ key . charAt ( 5 ) . toLowerCase ( ) + key . substr ( 6 ) ] = value ;
75
- } else {
76
- inputEl . attr ( snake_case ( key , '-' ) , value ) ;
77
- }
66
+ if ( key . charAt ( 0 ) !== '$' && [ 'src' , 'uiInput' ] . indexOf ( key ) === - 1 ) {
67
+ inputEl . attr ( snake_case ( key , '-' ) , value ) ;
78
68
}
79
69
} ) ;
80
70
81
71
//prepare validation messages
82
- control . validation = angular . extend ( { } , uiConfig . uiinput . validation , scope . $eval ( tAttrs . validation ) ) ;
72
+ input . validation = angular . extend ( { } , uiConfig . input . validation , scope . $eval ( tAttrs . validation ) ) ;
83
73
84
74
//expose model to a field's template
85
- childScope . $control = control ;
86
- $compile ( element . contents ( ) ) ( childScope ) ;
87
- childScope . $field = inputEl . controller ( 'ngModel' ) ;
75
+ scope . $input = input ;
76
+ $compile ( element . contents ( ) ) ( scope ) ;
77
+ scope . $field = inputEl . controller ( 'ngModel' ) ;
88
78
} ) ;
89
79
} ;
90
80
}
91
81
} ;
92
- } ] )
93
- . directive ( 'uiInput' , [ 'InputHelper' , function ( InputHelper ) {
94
- return InputHelper ;
95
- } ] )
96
- . directive ( 'uiTextarea' , [ 'InputHelper' , function ( InputHelper ) {
97
- return InputHelper ;
98
- } ] )
99
- . directive ( 'uiSelect' , [ 'InputHelper' , function ( InputHelper ) {
100
- return InputHelper ;
101
82
} ] ) ;
0 commit comments