@@ -516,16 +516,18 @@ angular.module('ui.bootstrap.datepicker', ['ui.bootstrap.dateparser', 'ui.bootst
516
516
closeOnDateSelection : true ,
517
517
appendToBody : false ,
518
518
showButtonBar : true ,
519
- onOpenFocus : true
519
+ onOpenFocus : true ,
520
+ timelessJsonMode : false
520
521
} )
521
522
522
- . controller ( 'UibDatepickerPopupController' , [ '$scope' , '$element' , '$attrs' , '$compile' , '$parse' , '$document' , '$rootScope' , '$uibPosition' , 'dateFilter' , 'uibDateParser' , 'uibDatepickerPopupConfig' , '$timeout' ,
523
- function ( scope , element , attrs , $compile , $parse , $document , $rootScope , $position , dateFilter , dateParser , datepickerPopupConfig , $timeout ) {
523
+ . controller ( 'UibDatepickerPopupController' , [ '$scope' , '$element' , '$attrs' , '$compile' , '$parse' , '$document' , '$rootScope' , '$uibPosition' , 'dateFilter' , 'uibDateParser' , 'uibDatepickerPopupConfig' , '$timeout' , 'timelessJsonUtils' ,
524
+ function ( scope , element , attrs , $compile , $parse , $document , $rootScope , $position , dateFilter , dateParser , datepickerPopupConfig , $timeout , timelessJsonUtils ) {
524
525
var self = this ;
525
526
var cache = { } ,
526
527
isHtml5DateInput = false ;
527
528
var dateFormat , closeOnDateSelection , appendToBody , onOpenFocus ,
528
529
datepickerPopupTemplateUrl , datepickerTemplateUrl , popupEl , datepickerEl ,
530
+ timelessJsonMode , addTimelessJsonUtils ,
529
531
ngModel , $popup ;
530
532
531
533
scope . watchData = { } ;
@@ -537,6 +539,8 @@ function(scope, element, attrs, $compile, $parse, $document, $rootScope, $positi
537
539
onOpenFocus = angular . isDefined ( attrs . onOpenFocus ) ? scope . $parent . $eval ( attrs . onOpenFocus ) : datepickerPopupConfig . onOpenFocus ;
538
540
datepickerPopupTemplateUrl = angular . isDefined ( attrs . datepickerPopupTemplateUrl ) ? attrs . datepickerPopupTemplateUrl : datepickerPopupConfig . datepickerPopupTemplateUrl ;
539
541
datepickerTemplateUrl = angular . isDefined ( attrs . datepickerTemplateUrl ) ? attrs . datepickerTemplateUrl : datepickerPopupConfig . datepickerTemplateUrl ;
542
+ timelessJsonMode = angular . isDefined ( attrs . timelessJsonMode ) ? attrs . timelessJsonMode : datepickerPopupConfig . timelessJsonMode ;
543
+ addTimelessJsonUtils = timelessJsonMode && timelessJsonMode . toString ( ) . toLowerCase ( ) == 'withutils' ;
540
544
541
545
scope . showButtonBar = angular . isDefined ( attrs . showButtonBar ) ? scope . $parent . $eval ( attrs . showButtonBar ) : datepickerPopupConfig . showButtonBar ;
542
546
@@ -599,6 +603,10 @@ function(scope, element, attrs, $compile, $parse, $document, $rootScope, $positi
599
603
} ) ;
600
604
}
601
605
606
+ if ( addTimelessJsonUtils ) {
607
+ angular . extend ( scope . $parent , timelessJsonUtils ) ;
608
+ }
609
+
602
610
angular . forEach ( [ 'minMode' , 'maxMode' , 'minDate' , 'maxDate' , 'datepickerMode' , 'initDate' , 'shortcutPropagation' ] , function ( key ) {
603
611
if ( attrs [ key ] ) {
604
612
var getAttribute = $parse ( attrs [ key ] ) ;
@@ -639,11 +647,22 @@ function(scope, element, attrs, $compile, $parse, $document, $rootScope, $positi
639
647
ngModel . $validators . date = validator ;
640
648
ngModel . $parsers . unshift ( parseDate ) ;
641
649
ngModel . $formatters . push ( function ( value ) {
650
+ if ( timelessJsonMode ) {
651
+ value = timelessJsonUtils . jsonToDate ( value , ngModel , dateFilter , dateFormat ) ;
652
+ }
642
653
scope . date = value ;
643
654
return ngModel . $isEmpty ( value ) ? value : dateFilter ( value , dateFormat ) ;
644
655
} ) ;
645
656
} else {
657
+ if ( timelessJsonMode ) {
658
+ ngModel . $parsers . push ( function ( viewValue ) {
659
+ return dateFilter ( viewValue , 'yyyy-MM-dd' ) ;
660
+ } ) ;
661
+ }
646
662
ngModel . $formatters . push ( function ( value ) {
663
+ if ( timelessJsonMode ) {
664
+ value = timelessJsonUtils . jsonToDate ( value , ngModel , dateFilter , dateFormat ) ;
665
+ }
647
666
scope . date = value ;
648
667
return value ;
649
668
} ) ;
@@ -772,6 +791,8 @@ function(scope, element, attrs, $compile, $parse, $document, $rootScope, $positi
772
791
var date = dateParser . parse ( viewValue , dateFormat , scope . date ) ;
773
792
if ( isNaN ( date ) ) {
774
793
return undefined ;
794
+ } else if ( timelessJsonMode ) {
795
+ return dateFilter ( date , 'yyyy-MM-dd' ) ;
775
796
} else {
776
797
return date ;
777
798
}
@@ -781,6 +802,9 @@ function(scope, element, attrs, $compile, $parse, $document, $rootScope, $positi
781
802
}
782
803
783
804
function validator ( modelValue , viewValue ) {
805
+ if ( timelessJsonMode ) {
806
+ modelValue = timelessJsonUtils . jsonToDate ( modelValue , ngModel , dateFilter , dateFormat ) ;
807
+ }
784
808
var value = modelValue || viewValue ;
785
809
786
810
if ( ! attrs . ngRequired && ! value ) {
@@ -1004,4 +1028,42 @@ angular.module('ui.bootstrap.datepicker')
1004
1028
}
1005
1029
}
1006
1030
} ;
1007
- } ] ) ;
1031
+ } ] )
1032
+
1033
+ . constant ( 'timelessJsonUtils' , ( function ( ) {
1034
+ return {
1035
+ jsonToDate : jsonToDate ,
1036
+ jsonDateToTicks : jsonDateToTicks ,
1037
+ jsonDateAddDays : jsonDateToTicks
1038
+ } ;
1039
+ function jsonToDate ( date , ngModel , dateFilter , dateFormat ) {
1040
+ if ( date && angular . isString ( date ) && date . indexOf ( '-' ) > - 1 ) {
1041
+ var update = date . indexOf ( 'T' ) > - 1 ;
1042
+ date = date . substring ( 0 , 10 ) . split ( '-' ) ;
1043
+ date = new Date ( + date [ 0 ] , + date [ 1 ] - 1 , + date [ 2 ] ) ;
1044
+ if ( update && ngModel ) {
1045
+ ngModel . $setViewValue ( dateFilter ( date , dateFormat ) ) ;
1046
+ ngModel . $render ( ) ;
1047
+ }
1048
+ }
1049
+ return date ;
1050
+ }
1051
+ function jsonDateToTicks ( date , additionalDays ) {
1052
+ if ( ! date ) {
1053
+ return null ;
1054
+ }
1055
+ if ( angular . isString ( date ) && date . indexOf ( '-' ) > - 1 ) {
1056
+ date = date . substring ( 0 , 10 ) . split ( '-' ) ;
1057
+ date = new Date ( + date [ 0 ] , + date [ 1 ] - 1 , + date [ 2 ] ) ;
1058
+ }
1059
+ if ( ! date . getTime ) {
1060
+ return null ;
1061
+ }
1062
+ var ticks = date . getTime ( ) ;
1063
+ if ( additionalDays ) {
1064
+ ticks += ( + additionalDays * 1000 * 60 * 60 * 24 ) ;
1065
+ }
1066
+ return ticks ;
1067
+ }
1068
+ } ) ( )
1069
+ ) ;
0 commit comments