@@ -512,6 +512,7 @@ function($compile, $parse, $document, $rootScope, $position, dateFilter, datePar
512
512
onOpenFocus = angular . isDefined ( attrs . onOpenFocus ) ? scope . $parent . $eval ( attrs . onOpenFocus ) : datepickerPopupConfig . onOpenFocus ,
513
513
datepickerPopupTemplateUrl = angular . isDefined ( attrs . datepickerPopupTemplateUrl ) ? attrs . datepickerPopupTemplateUrl : datepickerPopupConfig . datepickerPopupTemplateUrl ,
514
514
datepickerTemplateUrl = angular . isDefined ( attrs . datepickerTemplateUrl ) ? attrs . datepickerTemplateUrl : datepickerPopupConfig . datepickerTemplateUrl ,
515
+ timelessJsonMode = angular . isDefined ( attrs . timelessJsonMode ) ? attrs . timelessJsonMode : datepickerPopupConfig . timelessJsonMode ,
515
516
cache = { } ;
516
517
517
518
scope . showButtonBar = angular . isDefined ( attrs . showButtonBar ) ? scope . $parent . $eval ( attrs . showButtonBar ) : datepickerPopupConfig . showButtonBar ;
@@ -533,6 +534,19 @@ function($compile, $parse, $document, $rootScope, $position, dateFilter, datePar
533
534
return ( new Date ( date1 . getFullYear ( ) , date1 . getMonth ( ) , date1 . getDate ( ) ) - new Date ( date2 . getFullYear ( ) , date2 . getMonth ( ) , date2 . getDate ( ) ) ) ;
534
535
} ;
535
536
537
+ scope . jsonToDate = function ( date ) {
538
+ if ( date && angular . isString ( date ) && date . indexOf ( '-' ) > - 1 ) {
539
+ var update = date . indexOf ( 'T' ) > - 1 ;
540
+ date = date . substring ( 0 , 10 ) . split ( '-' ) ;
541
+ date = new Date ( + date [ 0 ] , + date [ 1 ] - 1 , + date [ 2 ] ) ;
542
+ if ( update ) {
543
+ ngModel . $setViewValue ( dateFilter ( date , dateFormat ) ) ;
544
+ ngModel . $render ( ) ;
545
+ }
546
+ }
547
+ return date ;
548
+ } ;
549
+
536
550
var isHtml5DateInput = false ;
537
551
if ( datepickerPopupConfig . html5Types [ attrs . type ] ) {
538
552
dateFormat = datepickerPopupConfig . html5Types [ attrs . type ] ;
@@ -647,15 +661,15 @@ function($compile, $parse, $document, $rootScope, $position, dateFilter, datePar
647
661
if ( isNaN ( date ) ) {
648
662
return undefined ;
649
663
} else {
650
- return date ;
664
+ return timelessJsonMode ? dateFilter ( date , 'yyyy-MM-dd' ) : date ;
651
665
}
652
666
} else {
653
667
return undefined ;
654
668
}
655
669
}
656
670
657
671
function validator ( modelValue , viewValue ) {
658
- var value = modelValue || viewValue ;
672
+ var value = ( timelessJsonMode ? scope . jsonToDate ( modelValue ) : modelValue ) || viewValue ;
659
673
660
674
if ( ! attrs . ngRequired && ! value ) {
661
675
return true ;
@@ -682,11 +696,17 @@ function($compile, $parse, $document, $rootScope, $position, dateFilter, datePar
682
696
ngModel . $validators . date = validator ;
683
697
ngModel . $parsers . unshift ( parseDate ) ;
684
698
ngModel . $formatters . push ( function ( value ) {
699
+ if ( timelessJsonMode ) {
700
+ value = scope . jsonToDate ( value ) ;
701
+ }
685
702
scope . date = value ;
686
703
return ngModel . $isEmpty ( value ) ? value : dateFilter ( value , dateFormat ) ;
687
704
} ) ;
688
705
} else {
689
706
ngModel . $formatters . push ( function ( value ) {
707
+ if ( timelessJsonMode ) {
708
+ value = scope . jsonToDate ( value ) ;
709
+ }
690
710
scope . date = value ;
691
711
return value ;
692
712
} ) ;
@@ -820,4 +840,20 @@ function($compile, $parse, $document, $rootScope, $position, dateFilter, datePar
820
840
return attrs . templateUrl || 'template/datepicker/popup.html' ;
821
841
}
822
842
} ;
843
+ } )
844
+
845
+ . directive ( 'datepickerJsonUtils' , function ( ) {
846
+ return {
847
+ restrict : 'A' ,
848
+ link : function ( scope ) {
849
+ scope . jsonToTicks = function ( date ) {
850
+ if ( date && angular . isString ( date ) && date . indexOf ( '-' ) > - 1 ) {
851
+ date = date . substring ( 0 , 10 ) . split ( '-' ) ;
852
+ date = new Date ( + date [ 0 ] , + date [ 1 ] - 1 , + date [ 2 ] ) ;
853
+ }
854
+ return date . getTime ? date . getTime ( ) : null ;
855
+ } ;
856
+ scope . oneDay = 1000 * 60 * 60 * 24 ;
857
+ }
858
+ } ;
823
859
} ) ;
0 commit comments