1
+ /*
2
+ * Filter/directive templates
3
+ */
4
+ angular . module ( 'ui.filters' )
5
+ . filter ( 'tmpl' , [ 'ui.config' , function ( uiConfig ) { // tmplFilter is a boilerplate setup for your coding pleasure
6
+ var config = { } ;
7
+ if ( uiConfig . tmplFilter ) {
8
+ angular . extend ( config , uiConfig . tmplFilter ) ;
9
+ }
10
+ return function ( value , kind ) {
11
+ var opts = { } , inputs = { } , out ;
12
+ // if an input is given then override
13
+ if ( kind ) {
14
+ inputs = { 'kind' : kind } ;
15
+ }
16
+ angular . extend ( opts , config , inputs ) ;
17
+ //
18
+ // do something more interesting to input value here
19
+ //
20
+ out = value ;
21
+ return out ;
22
+ } ;
23
+ } ] )
24
+ . filter ( 'wrap' , [ 'ui.config' , function ( uiConfig ) { // wrapFilter is a simple filter that wraps the incoming value, but only if value has a value.
25
+ var config = { } ;
26
+ if ( uiConfig . wrapFilter ) {
27
+ angular . extend ( config , uiConfig . wrapFilter ) ;
28
+ }
29
+ return function ( value , prefix , suffix ) {
30
+ var opts = { } , inputs = { } ;
31
+ // if an input is given then override
32
+ if ( prefix ) {
33
+ inputs = { 'prefix' : prefix , 'suffix' : suffix } ;
34
+ }
35
+ // build up our options
36
+ angular . extend ( opts , config , inputs ) ;
37
+ if ( ! value || value == '' ) {
38
+ return '' ;
39
+ }
40
+
41
+ if ( opts . prefix ) {
42
+ return opts . prefix + value + ( opts . suffix || '' ) ;
43
+ }
44
+ return value ;
45
+ } ;
46
+ } ] ) ;
47
+
48
+ angular . module ( 'ui.directives' )
49
+ . directive ( 'uiTmpl' , [ 'ui.config' , function ( uiConfig ) {
50
+ var config = { } ;
51
+ if ( uiConfig . tmpl ) {
52
+ angular . extend ( config , uiConfig . tmpl ) ;
53
+ }
54
+ return {
55
+ restrict : 'EAC' , // supports using directive as element, attribute and class
56
+ require : 'ngModel' , // requires ng-model controller so we get the two-way binding
57
+ link : function ( scope , element , attrs , ngModel ) {
58
+ var opts = { } ; // instance-specific options
59
+
60
+ // build up our options
61
+ angular . extend ( opts , config , scope . $eval ( attrs . uiTmpl ) ) ; // element attributes that override default options
62
+
63
+ // override the controllers render function and do something interesting in here
64
+ ngModel . $render = function ( ) {
65
+ element . text ( ngModel . $viewValue ) ; // boring
66
+ } ;
67
+ }
68
+ } ;
69
+ } ] )
70
+ . directive ( 'uiStylize' , [ 'ui.config' , function ( uiConfig ) { // uiStylize sets style based on if value is alpha or numeric
71
+ var config = { } ;
72
+ if ( uiConfig . stylize ) {
73
+ angular . extend ( config , uiConfig . stylize ) ;
74
+ }
75
+ return {
76
+ restrict : 'EAC' , // supports using directive as element, attribute and class
77
+ require : 'ngModel' , // requires ng-model controller so we get the two-way binding
78
+ link : function ( scope , element , attrs , ngModel ) {
79
+ var opts = { } ; // instance-specific options
80
+
81
+ // build up our options
82
+ angular . extend ( opts , config , scope . $eval ( attrs . uiStylize ) ) ; // element attributes that override default options
83
+
84
+ // override the controllers render function and do something interesting in here
85
+ ngModel . $render = function ( ) {
86
+ var numval ;
87
+ try {
88
+ // a different approach would be to use a regex, however this might be faster
89
+ numval = parseFloat ( ngModel . $viewValue ) ;
90
+ // javascript will parse a number with characters and come up with a number
91
+ // make sure that the result is the same as what was in model
92
+ if ( numval != ngModel . $viewValue ) {
93
+ numval = null ;
94
+ }
95
+ } catch ( err ) {
96
+ // don't do anything numval will be undefined
97
+ }
98
+ if ( numval ) {
99
+ element . addClass ( "ui-numeric" ) ;
100
+ } else {
101
+ element . addClass ( "ui-alpha" ) ;
102
+ }
103
+ element . text ( ngModel . $viewValue ) ; // boring
104
+ } ;
105
+ }
106
+ } ;
107
+ } ] ) ;
0 commit comments