1- ngGridDirectives . directive ( 'ngInput' , [ '$parse' , function ( $parse ) {
2- return function ( $scope , elm , attrs ) {
3-
4- // Strip the $eval off of the ng-input tag, we want WHERE to put the edited values, not the actual edited/displayed value itself
5- var inputter = attrs . ngInput . replace ( / .+ ?\( ( .+ ?) \) / , '$1' ) ;
6-
7- var evaled = $scope . $eval ( inputter ) ;
8- var getter = $parse ( evaled ) ;
9- var setter = getter . assign ;
10- var oldCellValue = getter ( $scope ) ;
11-
12- elm . val ( oldCellValue ) ;
13-
14- elm . bind ( 'keyup' , function ( ) {
15- var newVal = elm . val ( ) ;
16- // dump(newVal);
17- if ( ! $scope . $root . $$phase ) {
18- $scope . $apply ( function ( ) {
19- setter ( $scope , newVal ) ;
20- } ) ;
21- }
22- } ) ;
1+ ngGridDirectives . directive ( 'ngInput' , [ function ( ) {
2+ return {
3+ require : 'ngModel' ,
4+ link : function ( scope , elm , attrs , ngModel ) {
5+ // Store the initial cell value so we can reset to it if need be
6+ var oldCellValue ;
7+ var dereg = scope . $watch ( 'ngModel' , function ( ) {
8+ oldCellValue = ngModel . $modelValue ;
9+ dereg ( ) ; // only run this watch once, we don't want to overwrite our stored value when the input changes
10+ } ) ;
11+
12+ elm . bind ( 'keydown' , function ( evt ) {
13+ switch ( evt . keyCode ) {
14+ case 37 : // Left arrow
15+ case 38 : // Up arrow
16+ case 39 : // Right arrow
17+ case 40 : // Down arrow
18+ evt . stopPropagation ( ) ;
19+ break ;
20+ case 27 : // Esc (reset to old value)
21+ if ( ! scope . $$phase ) {
22+ scope . $apply ( function ( ) {
23+ ngModel . $setViewValue ( oldCellValue ) ;
24+ elm . blur ( ) ;
25+ } ) ;
26+ }
27+ break ;
28+ case 13 : // Enter (Leave Field)
29+ elm . blur ( ) ;
30+ break ;
31+ }
2332
24- elm . bind ( 'keydown' , function ( evt ) {
25- switch ( evt . keyCode ) {
26- case 37 : // Left arrow
27- case 38 : // Up arrow
28- case 39 : // Right arrow
29- case 40 : // Down arrow
30- evt . stopPropagation ( ) ;
31- break ;
32- case 27 : // Esc (reset to old value)
33- if ( ! $scope . $root . $$phase ) {
34- $scope . $apply ( function ( ) {
35- setter ( $scope , oldCellValue ) ;
36- elm . val ( oldCellValue ) ;
37- elm . blur ( ) ;
38- } ) ;
39- }
40- case 13 : // Enter (apply new value)
41- if ( ! $scope . $root . $$phase ) {
42- $scope . $apply ( function ( ) {
43- setter ( $scope , elm . val ( ) ) ;
44- elm . blur ( ) ;
33+ return true ;
4534 } ) ;
46- }
47- default :
48- break ;
49- }
50- return true ;
51- } ) ;
52- } ;
35+ }
36+ } ;
5337} ] ) ;
0 commit comments