Skip to content
This repository was archived by the owner on May 29, 2019. It is now read-only.

Commit 261f207

Browse files
bekospkozlowski-opensource
authored andcommitted
feat(progressbar): add progressbar directive
Closes #187
1 parent e22f28b commit 261f207

File tree

7 files changed

+508
-0
lines changed

7 files changed

+508
-0
lines changed

src/progressbar/docs/demo.html

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<div ng-controller="ProgressDemoCtrl" class="well">
2+
<h2>Static</h2>
3+
<div class="row-fluid">
4+
<div class="span4"><progress value="55"></progress></div>
5+
<div class="span4"><progress value="22" class="progress-warning progress-striped"></progress></div>
6+
<div class="span4"><progress value="88" class="progress-danger progress-striped active"></div>
7+
</div>
8+
9+
<h2>Dynamic <button class="btn btn-primary" type="button" ng-click="random()">Randomize</button></h2>
10+
<pre>Value: {{dynamic}}</pre>
11+
<progress value="dynamic"></progress>
12+
13+
<small><em>No animation</em></small>
14+
<progress value="dynamic" class="progress-success" animate="false"></progress>
15+
16+
<small><em>Object (changes type based on value)</em></small>
17+
<progress value="dynamicObject" class="progress-striped active"></progress>
18+
19+
<h2>Stacked <button class="btn btn-primary" type="button" ng-click="randomStacked()">Randomize</button></h2>
20+
<small><em>Array values with automatic types</em></small>
21+
<pre>Value: {{stackedArray}}</pre>
22+
<progress value="stackedArray" auto-type="true"></progress>
23+
24+
<small><em>Objects</em></small>
25+
<pre>Value: {{stacked}}</pre>
26+
<progress value="stacked"></progress>
27+
</div>

src/progressbar/docs/demo.js

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
var ProgressDemoCtrl = function ($scope) {
2+
3+
$scope.random = function() {
4+
var value = Math.floor((Math.random()*100)+1);
5+
var type;
6+
7+
if (value < 25) {
8+
type = 'success';
9+
} else if (value < 50) {
10+
type = 'info';
11+
} else if (value < 75) {
12+
type = 'warning';
13+
} else {
14+
type = 'danger';
15+
}
16+
17+
$scope.dynamic = value;
18+
$scope.dynamicObject = {
19+
value: value,
20+
type: type
21+
};
22+
};
23+
$scope.random();
24+
25+
var types = ['success', 'info', 'warning', 'danger'];
26+
$scope.randomStacked = function() {
27+
$scope.stackedArray = [];
28+
$scope.stacked = [];
29+
30+
var n = Math.floor((Math.random()*4)+1);
31+
32+
for (var i=0; i < n; i++) {
33+
var value = Math.floor((Math.random()*30)+1);
34+
$scope.stackedArray.push(value);
35+
36+
var index = Math.floor((Math.random()*4));
37+
$scope.stacked.push({
38+
value: value,
39+
type: types[index]
40+
});
41+
}
42+
};
43+
$scope.randomStacked();
44+
};

src/progressbar/docs/readme.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
A lightweight progress bar directive that is focused on providing progress visualization!
2+
3+
The progress bar directive supports multiple (stacked) bars into the same element, optional transition animation, event handler for full & empty state and many more.

src/progressbar/progressbar.js

+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
angular.module('ui.bootstrap.progressbar', ['ui.bootstrap.transition'])
2+
3+
.constant('progressConfig', {
4+
animate: true,
5+
autoType: false,
6+
stackedTypes: ['success', 'info', 'warning', 'danger']
7+
})
8+
9+
.controller('ProgressBarController', ['$scope', '$attrs', 'progressConfig', function($scope, $attrs, progressConfig) {
10+
11+
// Whether bar transitions should be animated
12+
var animate = angular.isDefined($attrs.animate) ? $scope.$eval($attrs.animate) : progressConfig.animate;
13+
var autoType = angular.isDefined($attrs.autoType) ? $scope.$eval($attrs.autoType) : progressConfig.autoType;
14+
var stackedTypes = angular.isDefined($attrs.stackedTypes) ? $scope.$eval('[' + $attrs.stackedTypes + ']') : progressConfig.stackedTypes;
15+
16+
// Create bar object
17+
this.makeBar = function(newBar, oldBar, index) {
18+
var newValue = (angular.isObject(newBar)) ? newBar.value : (newBar || 0);
19+
var oldValue = (angular.isObject(oldBar)) ? oldBar.value : (oldBar || 0);
20+
var type = (angular.isObject(newBar) && angular.isDefined(newBar.type)) ? newBar.type : (autoType) ? getStackedType(index || 0) : null;
21+
22+
return {
23+
from: oldValue,
24+
to: newValue,
25+
type: type,
26+
animate: animate
27+
};
28+
};
29+
30+
function getStackedType(index) {
31+
return stackedTypes[index];
32+
}
33+
34+
this.addBar = function(bar) {
35+
$scope.bars.push(bar);
36+
$scope.totalPercent += bar.to;
37+
};
38+
39+
this.clearBars = function() {
40+
$scope.bars = [];
41+
$scope.totalPercent = 0;
42+
};
43+
this.clearBars();
44+
}])
45+
46+
.directive('progress', function() {
47+
return {
48+
restrict: 'EA',
49+
replace: true,
50+
controller: 'ProgressBarController',
51+
scope: {
52+
value: '=',
53+
onFull: '&',
54+
onEmpty: '&'
55+
},
56+
templateUrl: 'template/progressbar/progress.html',
57+
link: function(scope, element, attrs, controller) {
58+
scope.$watch('value', function(newValue, oldValue) {
59+
controller.clearBars();
60+
61+
if (angular.isArray(newValue)) {
62+
// Stacked progress bar
63+
for (var i=0, n=newValue.length; i < n; i++) {
64+
controller.addBar(controller.makeBar(newValue[i], oldValue[i], i));
65+
}
66+
} else {
67+
// Simple bar
68+
controller.addBar(controller.makeBar(newValue, oldValue));
69+
}
70+
}, true);
71+
72+
// Total percent listeners
73+
scope.$watch('totalPercent', function(value) {
74+
if (value >= 100) {
75+
scope.onFull();
76+
} else if (value <= 0) {
77+
scope.onEmpty();
78+
}
79+
}, true);
80+
}
81+
};
82+
})
83+
84+
.directive('progressbar', ['$transition', function($transition) {
85+
return {
86+
restrict: 'EA',
87+
replace: true,
88+
scope: {
89+
width: '=',
90+
old: '=',
91+
type: '=',
92+
animate: '='
93+
},
94+
templateUrl: 'template/progressbar/bar.html',
95+
link: function(scope, element) {
96+
scope.$watch('width', function(value) {
97+
if (scope.animate) {
98+
element.css('width', scope.old + '%');
99+
$transition(element, {width: value + '%'});
100+
} else {
101+
element.css('width', value + '%');
102+
}
103+
});
104+
}
105+
};
106+
}]);

0 commit comments

Comments
 (0)