1
+ angular . module ( 'customPlaceholder' , [ ] ) . directive ( 'placeholder' , function ( ) {
2
+ // cn-placeholder directive definition object
3
+ return {
4
+ restrict : 'A' ,
5
+ require : 'ngModel' ,
6
+ link : function ( scope , elm , attrs , ctrl ) {
7
+ if ( ! Modernizr . placeholder ) {
8
+ // setup the label overlay for input
9
+ var computedStyle = window . getComputedStyle ( elm [ 0 ] , null ) ,
10
+ leftPadding = computedStyle . getPropertyValue ( 'padding-left' ) ,
11
+ topPadding = computedStyle . getPropertyValue ( 'padding-top' ) ,
12
+ placeholderLabel = angular . element ( elm . parent ( )
13
+ . css ( { 'position' :'relative' } )
14
+ . prepend ( '<span>' + elm . attr ( 'placeholder' ) + '</span>' )
15
+ . find ( 'span' ) [ 0 ] )
16
+ . css ( { 'color' :'#999' ,
17
+ 'position' :'absolute' ,
18
+ 'top' : topPadding ,
19
+ 'left' :leftPadding } ) ,
20
+ placeholderBindClick = function ( ) {
21
+ placeholderLabel . bind ( 'click' , function ( ) {
22
+ elm [ 0 ] . focus ( ) ;
23
+ } ) ;
24
+ } ;
25
+
26
+ placeholderBindClick ( ) ;
27
+
28
+ // removes the label overlay when a value is typed
29
+ var elemPlaceHandlePlaceholder = function ( ) {
30
+ if ( elm . val ( ) !== '' ) {
31
+ placeholderLabel . remove ( ) ;
32
+ } else {
33
+ elm . parent ( ) . prepend ( placeholderLabel ) ;
34
+ placeholderBindClick ( ) ;
35
+ }
36
+ } ;
37
+
38
+ scope . $watch ( attrs . ngModel , function ( newValue , oldValue ) {
39
+ elemPlaceHandlePlaceholder ( ) ;
40
+ } ) ;
41
+
42
+ elm . bind ( 'keyup keydown' , function ( ) {
43
+ elemPlaceHandlePlaceholder ( ) ;
44
+ } ) ;
45
+ }
46
+ }
47
+ } ;
48
+ } ) ;
0 commit comments