-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathangular-hx-utils.js
201 lines (162 loc) · 8.18 KB
/
angular-hx-utils.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
angular.module('hxUtils', [])
.filter('millisecondsToTimeString', function () {
return function (millseconds) {
var neg = false;
if (millseconds < 0) {
millseconds = -millseconds;
neg = true;
}
var seconds = Math.floor(millseconds / 1000);
var days = Math.floor(seconds / 86400);
var hours = Math.floor((seconds % 86400) / 3600);
var minutes = Math.floor(((seconds % 86400) % 3600) / 60);
var secondsRemaining = seconds - minutes * 60 - hours * 3600 - days * 86400;
var timeString = '';
// if (days > 0) timeString += (days > 1) ? (days + " days ") : (days + " day ");
// if (hours > 0) timeString += (hours > 1) ? (hours + " hours ") : (hours + " hour ");
// if (minutes >= 0) timeString += (minutes > 1) ? (minutes + " minutes ") : (minutes + " minute ");
// if (secondsRemaining >= 0) timeString += (secondsRemaining > 1) ? (secondsRemaining + " seconds ") : (secondsRemaining + " second ");
if (days > 0) timeString = days + "d ";
if (hours > 0) timeString += hours + "h ";
if (minutes > 0) timeString += minutes + "m ";
if (secondsRemaining >= 0) timeString += secondsRemaining + "s ";
if (neg)
timeString = "-" + timeString;
return timeString;
};
})
.filter('millisecondsToCronString', function () {
return function (millseconds) {
var neg = false;
if (millseconds < 0) {
millseconds = -millseconds;
neg = true;
}
var seconds = Math.floor(millseconds / 1000);
var hours = Math.floor(seconds / 3600);
var minutes = Math.floor((seconds % 3600) / 60);
var secondsRemaining = Math.floor(seconds % 60);
function pad(n, width, z) {
z = z || '0';
n = n + '';
return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;
}
var timeString = pad(hours, 2) + ":" + pad(minutes, 2) + ":" + pad(secondsRemaining, 2);
if (neg)
timeString = "-" + timeString;
return timeString;
};
})
.filter('millisecondsToHxDonuts', ['$filter', function ($filter) {
return function (millseconds) {
var neg = false;
if (millseconds < 0) {
millseconds = -millseconds;
neg = true;
}
var seconds = Math.floor(millseconds / 1000);
var days = Math.floor(seconds / 86400);
var hours = Math.floor((seconds % 86400) / 3600);
var minutes = Math.floor(((seconds % 86400) % 3600) / 60);
var secondsRemaining = seconds - minutes * 60 - hours * 3600 - days * 86400;
var hxDonuts = {
firstValue: "",
firstUnit: "",
rest: ""
};
var firstCollapsed = "";
if (secondsRemaining >= 0) {
hxDonuts.firstValue = secondsRemaining;
hxDonuts.firstUnit = secondsRemaining != 1 ? $filter("translate")("SECONDS") : $filter("translate")("SECOND");
firstCollapsed = secondsRemaining + "s";
hxDonuts.rest = "";
}
if (minutes > 0) {
hxDonuts.firstValue = minutes;
hxDonuts.firstUnit = minutes != 1 ? $filter("translate")("MINUTES") : $filter("translate")("MINUTE");
hxDonuts.rest = firstCollapsed;
firstCollapsed = minutes + "m";
}
if (hours !== 0) {
hxDonuts.firstValue = hours;
hxDonuts.firstUnit = hours != 1 ? $filter("translate")("HOURS") : $filter("translate")("HOUR");
hxDonuts.rest = firstCollapsed + " " + hxDonuts.rest;
firstCollapsed = hours + "h";
}
if (days !== 0) {
hxDonuts.firstValue = days;
hxDonuts.firstUnit = days != 1 ? $filter("translate")("DAYS") : $filter("translate")("DAY");
hxDonuts.rest = firstCollapsed + " " + hxDonuts.rest;
}
if (neg)
hxDonuts.firstValue = "-" + hxDonuts.firstValue;
return hxDonuts;
};
}])
.filter("emptyDateFilter", function () {
return function (input) {
if (!input)
return "--/--/----";
return input;
};
})
.filter("emptyFilter", function () {
return function (input) {
if (!input)
return "-";
return input;
};
})
.directive("hxDonuts", function () {
return {
restrict: "C",
scope: {
options: "=?",
value: "=",
title: "=",
subtitle: "=",
name: "="
},
link: function ($scope, element, attr) {
var _defaultOptions = {
frontStrokeColors: ["#11c1f3"],
frontStrokeWidth: 4,
backStrokeColor: "#141718",
backStrokeWidth: 4,
borderStrokeColor: "#51545a",
borderStrokeWidth: 1
};
var loadOptions = function () {
if (!$scope.options) {
$scope.options = {};
}
$scope.frontStrokeColors = $scope.options.frontStrokeColors ? $scope.options.frontStrokeColors : _defaultOptions.frontStrokeColors;
$scope.frontStrokeWidth = $scope.options.frontStrokeWidth ? $scope.options.frontStrokeWidth : _defaultOptions.frontStrokeWidth;
$scope.backStrokeColor = $scope.options.backStrokeColor ? $scope.options.backStrokeColor : _defaultOptions.backStrokeColor;
$scope.backStrokeWidth = $scope.options.backStrokeWidth ? $scope.options.backStrokeWidth : _defaultOptions.backStrokeWidth;
$scope.borderStrokeColor = $scope.options.borderStrokeColor ? $scope.options.borderStrokeColor : _defaultOptions.borderStrokeColor;
$scope.borderStrokeWidth = $scope.options.borderStrokeWidth ? $scope.options.borderStrokeWidth : _defaultOptions.borderStrokeWidth;
};
loadOptions();
$scope.dashArray = 264;
$scope.dashOffset = 264;
$scope.$watch("value", function () {
var j = Math.ceil($scope.frontStrokeColors.length * $scope.value / 100) - 1;
$scope.frontStrokeColor = $scope.frontStrokeColors[j];
$scope.dashOffset = $scope.dashArray - $scope.value * $scope.dashArray / 100;
$scope.deg = 90 - ($scope.value / 100 * 360) / 2;
//because if it is less than zero it means that is over 100%
if ($scope.dashOffset < 0) {
$scope.dashOffset = 0;
$scope.deg = 0;
$scope.frontStrokeColor = $scope.frontStrokeColors[$scope.frontStrokeColors.length - 1];
}
});
$scope.$watch("options", function () {
loadOptions();
});
},
template: '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" perserveAspectRatio="xMinYMid" style="transform: rotate({{deg}}deg);"> <g> <circle style="fill:none;" r="49" cy="50%" cx="50%" stroke="{{borderStrokeColor}}" stroke-width="{{borderStrokeWidth}}"></circle> <circle style="fill:none;" r="42" cy="50%" cx="50%" stroke="{{backStrokeColor}}" stroke-width="{{backStrokeWidth}}"></circle> <circle style="fill:none;" id="circle" r="42" cy="50%" cx="50%" stroke-dasharray="{{dashArray}}" stroke-dashoffset="{{dashOffset}}" stroke="{{frontStrokeColor}}" stroke-width="{{frontStrokeWidth}}"></circle> </g> </svg><h1>{{title}}</h1> <h2>{{name}}</h2> <h3>{{subtitle}}</h3>'
}
})
;