-
-
Notifications
You must be signed in to change notification settings - Fork 160
/
ionic-timepicker.provider.js
163 lines (136 loc) · 5.08 KB
/
ionic-timepicker.provider.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
angular.module('ionic-timepicker.provider', [])
.provider('ionicTimePicker', function () {
var config = {
setLabel: 'Set',
closeLabel: 'Close',
inputTime: (((new Date()).getHours() * 60 * 60) + ((new Date()).getMinutes() * 60)),
format: 12,
step: 15
};
this.configTimePicker = function (inputObj) {
angular.extend(config, inputObj);
};
this.$get = ['$rootScope', '$ionicPopup', function ($rootScope, $ionicPopup) {
var provider = {};
var $scope = $rootScope.$new();
$scope.today = resetHMSM(new Date()).getTime();
$scope.time = {};
//Reset the hours, minutes, seconds and milli seconds
function resetHMSM(currentDate) {
currentDate.setHours(0);
currentDate.setMinutes(0);
currentDate.setSeconds(0);
currentDate.setMilliseconds(0);
return currentDate;
}
//Increasing the hours
$scope.increaseHours = function () {
$scope.time.hours = Number($scope.time.hours);
if ($scope.mainObj.format == 12) {
if ($scope.time.hours != 12) {
$scope.time.hours += 1;
} else {
$scope.time.hours = 1;
}
}
if ($scope.mainObj.format == 24) {
$scope.time.hours = ($scope.time.hours + 1) % 24;
}
$scope.time.hours = ($scope.time.hours < 10) ? ('0' + $scope.time.hours) : $scope.time.hours;
};
//Decreasing the hours
$scope.decreaseHours = function () {
$scope.time.hours = Number($scope.time.hours);
if ($scope.mainObj.format == 12) {
if ($scope.time.hours > 1) {
$scope.time.hours -= 1;
} else {
$scope.time.hours = 12;
}
}
if ($scope.mainObj.format == 24) {
$scope.time.hours = ($scope.time.hours + 23) % 24;
}
$scope.time.hours = ($scope.time.hours < 10) ? ('0' + $scope.time.hours) : $scope.time.hours;
};
//Increasing the minutes
$scope.increaseMinutes = function () {
$scope.time.minutes = Number($scope.time.minutes);
$scope.time.minutes = ($scope.time.minutes + $scope.mainObj.step) % 60;
$scope.time.minutes = ($scope.time.minutes < 10) ? ('0' + $scope.time.minutes) : $scope.time.minutes;
};
//Decreasing the minutes
$scope.decreaseMinutes = function () {
$scope.time.minutes = Number($scope.time.minutes);
$scope.time.minutes = ($scope.time.minutes + (60 - $scope.mainObj.step)) % 60;
$scope.time.minutes = ($scope.time.minutes < 10) ? ('0' + $scope.time.minutes) : $scope.time.minutes;
};
//Changing the meridian
$scope.changeMeridian = function () {
$scope.time.meridian = ($scope.time.meridian === "AM") ? "PM" : "AM";
};
function setMinSecs(ipTime, format) {
$scope.time.hours = Math.floor(ipTime / (60 * 60));
var rem = ipTime % (60 * 60);
if (format == 12) {
if ($scope.time.hours >= 12) {
$scope.time.meridian = 'PM';
if($scope.time.hours > 12){
$scope.time.hours -= 12;
}
}
else {
$scope.time.meridian = 'AM';
}
if ($scope.time.hours === 0) {
$scope.time.hours = 12;
}
}
$scope.time.minutes = rem / 60;
$scope.time.hours = Math.floor($scope.time.hours);
$scope.time.minutes = Math.floor($scope.time.minutes);
if ($scope.time.hours.toString().length == 1) {
$scope.time.hours = '0' + $scope.time.hours;
}
if ($scope.time.minutes.toString().length == 1) {
$scope.time.minutes = '0' + $scope.time.minutes;
}
$scope.time.format = $scope.mainObj.format;
}
provider.openTimePicker = function (ipObj) {
var buttons = [];
$scope.mainObj = angular.extend({}, config, ipObj);
setMinSecs($scope.mainObj.inputTime, $scope.mainObj.format);
buttons.push({
text: $scope.mainObj.setLabel,
type: 'button_set',
onTap: function (e) {
var totalSec = 0;
if ($scope.time.format == 12) {
$scope.time.hours = Number($scope.time.hours);
if ($scope.time.meridian == 'PM' && $scope.time.hours != 12) {
$scope.time.hours += 12;
} else if ($scope.time.meridian == 'AM' && $scope.time.hours == 12) {
$scope.time.hours -= 12;
}
totalSec = ($scope.time.hours * 60 * 60) + ($scope.time.minutes * 60);
} else {
totalSec = ($scope.time.hours * 60 * 60) + ($scope.time.minutes * 60);
}
$scope.mainObj.callback(totalSec);
}
});
buttons.push({
text: $scope.mainObj.closeLabel,
type: 'button_close'
});
$scope.popup = $ionicPopup.show({
templateUrl: 'ionic-timepicker.html',
scope: $scope,
cssClass: 'ionic_timepicker_popup',
buttons: buttons
});
};
return provider;
}];
});