-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathorbicular.js
161 lines (136 loc) · 4.86 KB
/
orbicular.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
/**
* CoTag Orbicular
* A more or less pure CSS, circular, progress bar
*
* Copyright (c) 2014 CoTag Media.
*
* @author Stephen von Takach <[email protected]>
* @copyright 2014 cotag.me
*
*
* References:
* * http://fromanegg.com/post/41302147556/100-pure-css-radial-progress-bar
* * https://medium.com/@andsens/radial-progress-indicator-using-css-a917b80c43f9
*
**/
(function () {
'use strict';
if (typeof define === 'function' && define.amd) {
define('Orbicular', ['angular'], angularOrbicular);
} else {
angularOrbicular(angular);
}
function angularOrbicular(angular) {
return angular.module('Orbicular', []).
// isolated circular progress bar
directive('orbicular', ['$window', function (windowRef) {
// Constants here reduce memory requirements of each circle
var $window = angular.element(windowRef),
// Cache event strings
resizeEvents = 'orientationchange resize',
// Set the rotation of the square
updateProgress = function (circles, pos, modifiers) {
var modifiedPos,
circle,
i;
// If total is 0 or negative it can lead to Infinity or NaN values
if (!isFinite(pos)) {
pos = 0;
}
for (i = 0; i < circles.length; i += 1) {
if (modifiers[i] === 0) {
continue;
}
modifiedPos = pos * modifiers[i];
circle = circles[i];
circle.css({
'-webkit-transform': 'rotate(' + modifiedPos + 'deg)',
'-moz-transform': 'rotate(' + modifiedPos + 'deg)',
'-ms-transform': 'rotate(' + modifiedPos + 'deg)',
'-o-transform': 'rotate(' + modifiedPos + 'deg)',
'transform': 'rotate(' + modifiedPos + 'deg)'
});
}
};
return {
template:
'<div class="co-circle co-full">' +
'<div class="co-fill"></div>' +
'</div>' +
'<div class="co-circle co-half">' +
'<div class="co-fill"></div>' +
'<div class="co-fill co-fix"></div>' +
'</div>' +
// optional shadow
'<div class="co-shadow"></div>' +
'<div class="co-content">' +
'<div>' +
'<div>' +
'<div ng-transclude></div>' +
'</div>' +
'</div>' +
'</div>',
transclude: true,
restrict: 'EA',
scope: {
current: '=progression',
total: '='
},
link: function (scope, element, attrs) {
var circles = [],
modifiers = attrs.hasOwnProperty('counterclockwise') ? [-1,1,-2,1,2] : [1,1,0,1,2],
// Width must be an even number of pixels for the effect to work.
setWidth = function () {
var width = element.prop('offsetWidth');
element.css('font-size', width - (width % 2) + 'px');
},
update = function () {
updateProgress(circles,
Math.min(1, scope.current / scope.total) * 180.0,
modifiers
);
};
/**
* circles are:
* 0: .co-full
* 1: .co-full > .co-fill
* 2: .co-half
* 3: .co-half > .co-fill
* 4: .co-half > .co-fix
**/
circles.push(angular.element(element.children()[0]));
circles.push(angular.element(
angular.element(
element.children()[0]
).children()[0]
));
circles.push(angular.element(element.children()[1]));
circles.push(angular.element(
angular.element(
element.children()[1]
).children()[0]
));
circles.push(angular.element(
angular.element(
element.children()[1]
).children()[1]
));
// we have to use em's for the clip function to work like a percentage
// so we have to manually perform the resize based on width
setWidth();
scope.$on('orb width', setWidth); // for programmatic resizing
// Optionally resize
if (attrs.hasOwnProperty('resize')) {
scope.$on('$destroy', function () {
$window.off(resizeEvents, setWidth);
});
$window.on(resizeEvents, setWidth);
}
// we watch for changes to the progress indicator of the parent scope
scope.$watch('current', update);
scope.$watch('total', update);
}
};
}]);
}
}());