1
- angular . module ( "angular-datepicker" , [ ] ) . directive ( "datepicker" , [ '$timeout' , 'padFilter' , '$filter' , function ( $timeout , padFilter , $filter ) {
1
+ angular . module ( "angular-datepicker" , [ ] ) . directive ( "datepicker" , [ '$timeout' , 'padFilter' , '$filter' , '$compile' , function ( $timeout , padFilter , $filter , $compile ) {
2
2
return {
3
3
restrict : "A" ,
4
4
scope : {
@@ -12,52 +12,51 @@ angular.module("angular-datepicker", []).directive("datepicker", ['$timeout', 'p
12
12
require : 'ngModel' ,
13
13
link : function ( $scope , elem , attrs , ngModel ) {
14
14
15
- function init ( value ) {
15
+ var first = true ;
16
16
17
+ ngModel . $render = function ( ) {
18
+ var html = elem . contents ( ) ;
19
+ elem . empty ( ) ;
20
+ if ( ! ngModel . $modelValue ) {
21
+ elem . html ( $scope . dateFormat ) ;
22
+ } else {
23
+ elem . html ( $filter ( "date" ) ( ngModel . $modelValue , $scope . dateFormat ) ) ;
24
+ }
25
+ elem . on ( "click" , function ( ) {
26
+ $scope . $applyAsync ( function ( ) {
27
+ $scope . opened = true ;
28
+ $scope . daySelectionOpened = true ;
29
+ } ) ;
30
+ } ) ;
31
+ if ( first ) {
32
+ first = false ;
33
+ elem . after ( $compile ( html ) ( $scope ) ) ;
34
+ }
35
+ } ;
36
+
37
+ ngModel . $parsers . push ( function ( value ) {
38
+ return Date . parse ( value ) ;
39
+ } ) ;
40
+
41
+ ngModel . $formatters . push ( function ( value ) {
17
42
if ( / ^ - ? \d + $ / . test ( value ) ) {
18
- $scope . modelDate = new Date ( value ) ;
19
- $scope . generateMonth ( $scope . modelDate ) ;
43
+ $scope . generateMonth ( new Date ( value ) ) ;
44
+ return $filter ( "date" ) ( value , $scope . dateFormat ) ;
20
45
} else if ( angular . isDate ( value ) ) {
21
- $scope . modelDate = value ;
22
46
$scope . generateMonth ( value ) ;
47
+ return $filter ( "date" ) ( value , $scope . dateFormat ) ;
23
48
} else if ( value ) {
24
49
throw "Model format is wrong, only timestamp or javascript date object are supported" ;
25
50
} else {
26
- $scope . modelDate = value ;
51
+ //has to be a date for now
27
52
$scope . generateMonth ( $scope . standardDate ) ;
53
+ return $filter ( "date" ) ( $scope . standardDate , $scope . dateFormat ) ;
28
54
}
29
- }
30
-
31
- ngModel . $render = function ( ) {
32
- init ( ngModel . $modelValue ) ;
33
- }
34
-
35
- ngModel . $parsers . push ( function ( value ) {
36
- return value ? value . getTime ( ) : null ;
37
- } ) ;
38
-
39
- ngModel . $formatters . push ( function ( value ) {
40
- return $filter ( 'date' ) ( value , $scope . dateFormat ) ;
41
55
} ) ;
42
56
43
57
$scope . opened = false ;
44
58
$scope . daySelectionOpened = false ;
45
59
46
- // if ($scope.fromDate) {
47
- // $scope.fromDate.setUTCHours(0);
48
- // $scope.fromDate.setUTCMinutes(0);
49
- // $scope.fromDate.setUTCSeconds(0);
50
- // $scope.fromDate.setUTCMilliseconds(0);
51
- // }
52
-
53
-
54
- // if ($scope.toDate) {
55
- // $scope.toDate.setUTCHours(0);
56
- // $scope.toDate.setUTCMinutes(0);
57
- // $scope.toDate.setUTCSeconds(0);
58
- // $scope.toDate.setUTCMilliseconds(0);
59
- // }
60
-
61
60
var defaultMonths = [ "January" , "February" , "March" , "April" , "May" , "June" , "July" , "August" , "Settember" , "October" , "November" , "December" ] ;
62
61
var defaultDays = [ "Monday" , "Tuesday" , "Wednesday" , "Thursday" , "Friday" , "Saturday" , "Sunday" ] ;
63
62
var defaultCleanName = "Clean" ;
@@ -103,11 +102,19 @@ angular.module("angular-datepicker", []).directive("datepicker", ['$timeout', 'p
103
102
} ;
104
103
$scope . generatedTimes . push ( tmpObj ) ;
105
104
106
- if ( $scope . modelDate && $scope . modelDate . getHours ( ) == tmpObj . hour && $scope . modelDate . getMinutes ( ) == tmpObj . minute && $scope . modelDate . getSeconds ( ) == tmpObj . second ) {
105
+ var selectedDate = new Date ( ngModel . $modelValue ) ;
106
+
107
+ if ( selectedDate && selectedDate . getHours ( ) == tmpObj . hour && selectedDate . getMinutes ( ) == tmpObj . minute && selectedDate . getSeconds ( ) == tmpObj . second ) {
107
108
$scope . selectedTime = tmpObj ;
108
109
}
109
110
}
110
111
112
+ $scope . closeWrapper = function ( ) {
113
+ $scope . opened = false ;
114
+ $scope . daySelectionOpened = true ;
115
+ $scope . yearSelectionOpened = false ;
116
+ }
117
+
111
118
112
119
$scope . generateMonth = function ( selectedDate ) {
113
120
// selectedDate.setUTCHours(0);
@@ -157,7 +164,7 @@ angular.module("angular-datepicker", []).directive("datepicker", ['$timeout', 'p
157
164
158
165
var jsonDay = {
159
166
number : tmpDay . getUTCDate ( ) ,
160
- isSelectedDate : $scope . modelDate && tmpDay . getTime ( ) === $scope . modelDate . getTime ( ) ,
167
+ isSelectedDate : selectedDate && tmpDay . getTime ( ) === selectedDate . getTime ( ) ,
161
168
isToday : tmpDay . getTime ( ) === today . getTime ( ) ,
162
169
date : tmpDay ,
163
170
differentMonth : tmpDay . getUTCMonth ( ) !== selectedDate . getUTCMonth ( ) ,
@@ -174,19 +181,19 @@ angular.module("angular-datepicker", []).directive("datepicker", ['$timeout', 'p
174
181
175
182
$scope . years = [ ] ;
176
183
177
- var count = 0 ;
178
- for ( var num = $scope . month . selectedDate . getUTCFullYear ( ) - 17 ; num <= $scope . month . selectedDate . getUTCFullYear ( ) + 17 ; num ++ ) {
179
- if ( ! $scope . years [ count ] ) {
180
- $scope . years [ count ] = [ ] ;
184
+ for ( var num = $scope . month . selectedDate . getUTCFullYear ( ) - 2 ; num <= $scope . month . selectedDate . getUTCFullYear ( ) + 1 ; num ++ ) {
185
+ var months = [ ] ;
186
+ for ( var i = 0 ; i < 12 ; i ++ ) {
187
+ months . push ( {
188
+ num : i ,
189
+ isActualMonth : today . getUTCMonth ( ) === i && today . getUTCFullYear ( ) === num ,
190
+ isSelectedMonth : selectedDate && selectedDate . getUTCMonth ( ) === i && selectedDate . getUTCFullYear ( ) === num
191
+ } ) ;
181
192
}
182
- $scope . years [ count ] . push ( {
193
+ $scope . years . push ( {
183
194
num : num ,
184
- isActualYear : today . getUTCFullYear ( ) === num ,
185
- isSelectedYear : $scope . modelDate && $scope . modelDate . getUTCFullYear ( ) === num
195
+ months : months
186
196
} ) ;
187
- if ( $scope . years [ count ] . length === 5 ) {
188
- count ++ ;
189
- }
190
197
}
191
198
192
199
} ;
@@ -198,9 +205,9 @@ angular.module("angular-datepicker", []).directive("datepicker", ['$timeout', 'p
198
205
199
206
200
207
$scope . selectDay = function ( date ) {
201
- $scope . modelDate = date ;
202
- ! $scope . timepicker && $scope . close ( ) ;
208
+ // $scope.modelDate = date;
203
209
$scope . generateMonth ( angular . copy ( date ) ) ;
210
+ ! $scope . timepicker && $scope . close ( date ) ;
204
211
} ;
205
212
206
213
$scope . addMonth = function ( selectedDate ) {
@@ -230,10 +237,11 @@ angular.module("angular-datepicker", []).directive("datepicker", ['$timeout', 'p
230
237
}
231
238
} ;
232
239
233
- $scope . selectYear = function ( year ) {
240
+ $scope . selectYear = function ( year , month ) {
234
241
$scope . toggleYearSelection ( ) ;
235
242
var newDate = angular . copy ( $scope . month . selectedDate ) ;
236
243
newDate . setUTCFullYear ( year ) ;
244
+ newDate . setUTCMonth ( month ) ;
237
245
$scope . generateMonth ( newDate ) ;
238
246
} ;
239
247
@@ -245,45 +253,41 @@ angular.module("angular-datepicker", []).directive("datepicker", ['$timeout', 'p
245
253
246
254
$scope . precYears = function ( ) {
247
255
var newDate = angular . copy ( $scope . month . selectedDate ) ;
248
- newDate . setUTCFullYear ( newDate . getUTCFullYear ( ) - 21 ) ;
256
+ newDate . setUTCFullYear ( newDate . getUTCFullYear ( ) - 3 ) ;
249
257
$scope . generateMonth ( newDate ) ;
250
258
} ;
251
259
252
260
$scope . succYears = function ( ) {
253
261
var newDate = angular . copy ( $scope . month . selectedDate ) ;
254
- newDate . setUTCFullYear ( newDate . getUTCFullYear ( ) + 21 ) ;
262
+ newDate . setUTCFullYear ( newDate . getUTCFullYear ( ) + 3 ) ;
255
263
$scope . generateMonth ( newDate ) ;
256
264
} ;
257
265
258
266
$scope . clear = function ( ) {
259
267
260
268
ngModel . $setViewValue ( null ) ;
261
- // ngModel.$modelValue = null;
262
- // $scope.selectedTime = null;
263
- $scope . modelDate = null ;
269
+ ngModel . $render ( ) ;
270
+ // $scope.modelDate = null;
264
271
$scope . opened = false ;
265
272
$scope . generateMonth ( $scope . standardDate ) ;
266
273
} ;
267
274
268
275
$scope . changeTime = function ( time ) {
269
- $scope . modelDate . setHours ( time . hour ) ;
270
- $scope . modelDate . setMinutes ( time . minute ) ;
271
- $scope . modelDate . setSeconds ( time . second ) ;
276
+ ngModel . $modelValue . setHours ( time . hour ) ;
277
+ ngModel . $modelValue . setMinutes ( time . minute ) ;
278
+ ngModel . $modelValue . setSeconds ( time . second ) ;
272
279
}
273
280
274
- $scope . close = function ( ) {
281
+ $scope . close = function ( date ) {
275
282
$scope . opened = false ;
276
- ngModel . $setViewValue ( $scope . modelDate ) ;
283
+ ngModel . $setViewValue ( $filter ( "date" ) ( date , $scope . dateFormat ) ) ;
284
+ ngModel . $render ( ) ;
277
285
$timeout ( function ( ) {
278
286
$scope . events . onDaySelected && $scope . events . onDaySelected ( ) ;
279
287
} ) ;
280
- }
281
- // init(ngModel.$modelValue);
282
-
283
-
284
- //functions
288
+ } ;
285
289
} ,
286
- template : '<div class="datepicker"> <div data-ng-click="opened=!disabled;daySelectionOpened=!disabled" class="selectable"><i class="fa fa-calendar"></i> {{modelDate | date: dateFormat | datepickerEmptyFilter: dateFormat}}</div> <div class="datepicker-wrapper" data-ng-show="opened" data-ng-click="opened=!opened;daySelectionOpened=true;yearSelectionOpened=false;"></div> <div class="datepicker-close" data-ng-show="opened" data-ng-click="opened=!opened;daySelectionOpened=true;yearSelectionOpened=false;"> <button type="button"></button> </div> <div class="datepicker-table-wrapper table" data-ng-show="daySelectionOpened && opened"> <table> <thead> <tr class="month-header"> <th><a data-ng-click="subMonth(month.selectedDate)">❮ {{getPreviousMonthName()}}</a></th> <th colspan="5"> {{monthNames[month.selectedDate.getMonth()]}} <a data-ng-click="toggleYearSelection()" class="button-month" type="button"> {{month.selectedDate.getFullYear()}}</a></th> <th><a data-ng-click="addMonth(month.selectedDate)">{{getSuccessiveMonthName()}} ❯</a></th> </tr> <tr> <th data-ng-repeat="d in dayNames">{{d | substring}}</th> </tr> </thead> <tbody> <tr data-ng-repeat="week in month.weeks"> <td data-ng-click="!day.isDisabled && selectDay(day.date)" data-ng-repeat="day in week" data-ng-class="{\'disabled\': day.isDisabled,\'selected-date\': day.isSelectedDate, \'today\': day.isToday, \'different-month\': !day.isDisabled && day.differentMonth}"><a>{{day.number}}</a></td> </tr> <tr> <td colspan="3"><select class="timeselect" data-ng-if="timepicker" data-ng-disabled="!modelDate" data-ng-model="selectedTime" ng-options="t.label for t in generatedTimes" data-ng-change="changeTime(selectedTime)"></select></td> <td colspan="4"> <button style="margin-left: 5px" class="datepicker-button-clear" data-ng-if="timepicker" data-ng-click="close()">Select</button> <button type="button" class="datepicker-button-clear" data-ng-click="clear()">{{cleanName}}</button> </td> </tr> </tbody> </table> </div> <div class="datepicker-table-wrapper table" data-ng-show="yearSelectionOpened && opened"> <table> <thead> <tr class="month-header"> <th><a data-ng-click="precYears()">prev</a></th> <th colspan="3">{{years[0][0].num}}-{{years[years.length - 1][years[years.length - 1].length - 1].num}}</th> <th><a data-ng-click="succYears()">next</a></th> </tr> </thead> <tbody> <tr data-ng-repeat="list in years"> <td data-ng-repeat="year in list" data-ng-click="selectYear(year.num)" data-ng-class="{\'selected-date\': year.isSelectedYear, \'today\': year.isActualYear}"> <a>{{year.num}}</a> </td> </tr> <tr></tr> </tbody> </table> </div></div>'
290
+ template : '<div class="datepicker"> <div class="datepicker-wrapper" data-ng-show="opened" data-ng-click="closeWrapper()"></div> <div class="datepicker-close" data-ng-show="opened" data-ng-click="closeWrapper()"> <button type="button"></button> </div> <div class="datepicker-table-wrapper table" data-ng-show="daySelectionOpened && opened"> <table> <thead> <tr class="month-header"> <th style="width: 15%"><a data-ng-click="subMonth(month.selectedDate)">{{getPreviousMonthName()}}</a></th> <th style="width: 70%" colspan="5"> {{monthNames[month.selectedDate.getMonth()]}} <a data-ng-click="toggleYearSelection($event)" class="button-month" type="button"> {{month.selectedDate.getFullYear()}}</a></th> <th style="width: 15%"><a data-ng-click="addMonth(month.selectedDate)">{{getSuccessiveMonthName()}}</a></th> </tr> <tr> <th data-ng-repeat="d in dayNames">{{d | substring}}</th> </tr> </thead> <tbody> <tr data-ng-repeat="week in month.weeks"> <td class="focus" data-ng-click="!day.isDisabled && selectDay(day.date)" data-ng-repeat="day in week" data-ng-class="{\'disabled\': day.isDisabled,\'selected-date\': day.isSelectedDate, \'today\': day.isToday, \'different-month\': !day.isDisabled && day.differentMonth}"><a>{{day.number}}</a></td> </tr> <tr> <td colspan="3"><select class="timeselect" data-ng-if="timepicker" data-ng-disabled="!modelDate" data-ng-model="selectedTime" ng-options="t.label for t in generatedTimes" data-ng-change="changeTime(selectedTime)"></select></td> <td colspan="4"> <button style="margin-left: 5px" class="datepicker-button-clear" data-ng-if="timepicker" data-ng-click="close()">Select</button> <button type="button" class="datepicker-button-clear" data-ng-click="clear()">{{cleanName}}</button> </td> </tr> </tbody> </table> </div> <div class="datepicker-table-wrapper table" data-ng-show="yearSelectionOpened && opened"> <table> <thead> <tr class="month-header"> <th style="width: 25%"><a data-ng-click="precYears()">prev</a></th> <th style="width: 50%" colspan="3">{{years[0].num}}-{{years[years.length - 1].num}}</th> <th style="width: 25%"><a data-ng-click="succYears()">next</a></th> </tr> </thead> <tbody> <tr data-ng-repeat="y in years"> <td>{{y.num}}</td> <td colspan="4"> <table class="month-in-year-selection"> <tr> <td class="focus" data-ng-repeat="m in y.months | limitTo: 6" data-ng-click="selectYear(y.num, m.num)" data-ng-class="{\'selected-date\': m.isSelectedMonth, \'today\': m.isActualMonth}">{{monthNames[m.num] | substring}}</td> </tr> <tr> <td class="focus" data-ng-repeat="m in y.months | limitTo: 12 : 6" data-ng-click="selectYear(y.num, m.num)" data-ng-class="{\'selected-date\': m.isSelectedMonth, \'today\': m.isActualMonth}">{{monthNames[m.num] | substring}}</td> </tr> </table> </td> </tr> <tr></tr> </tbody> </table> </div></div>'
287
291
} ;
288
292
} ] ) . filter ( "datepickerEmptyFilter" , function ( ) {
289
293
return function ( input , placeholder ) {
0 commit comments