progress.js
4.79 KB
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
// Base object for different progress bar shapes
var Path = require('./path');
var utils = require('./utils');
var DESTROYED_ERROR = 'Object is destroyed';
var CONSTRUCTOR_CALL_ERROR = 'Constructor was called without new keyword';
var Progress = function Progress(container, opts) {
// Throw a better error if progress bars are not initialized with `new`
// keyword
if (!(this instanceof Progress)) {
throw new Error(CONSTRUCTOR_CALL_ERROR);
}
// Prevent calling constructor without parameters so inheritance
// works correctly. To understand, this is how Progress is inherited:
//
// Line.prototype = new Progress();
//
// We just want to set the prototype for Line.
if (arguments.length === 0) return;
var svgView = this._createSvgView(opts);
var element;
if (utils.isString(container)) {
element = document.querySelector(container);
} else {
element = container;
}
if (!element) {
throw new Error('Container does not exist: ' + container);
}
element.appendChild(svgView.svg);
var newOpts = utils.extend({
attachment: this
}, opts);
this._progressPath = new Path(svgView.path, newOpts);
// Expose public attributes
this.svg = svgView.svg;
this.path = svgView.path;
this.trail = svgView.trail;
};
Progress.prototype.animate = function animate(progress, opts, cb) {
if (this._progressPath === null) throw new Error(DESTROYED_ERROR);
this._progressPath.animate(progress, opts, cb);
};
Progress.prototype.stop = function stop() {
if (this._progressPath === null) throw new Error(DESTROYED_ERROR);
this._progressPath.stop();
};
Progress.prototype.destroy = function destroy() {
if (this._progressPath === null) throw new Error(DESTROYED_ERROR);
this.stop();
this.svg.parentNode.removeChild(this.svg);
this.svg = null;
this.path = null;
this.trail = null;
this._progressPath = null;
};
Progress.prototype.set = function set(progress) {
if (this._progressPath === null) throw new Error(DESTROYED_ERROR);
this._progressPath.set(progress);
};
Progress.prototype.value = function value() {
if (this._progressPath === null) throw new Error(DESTROYED_ERROR);
return this._progressPath.value();
};
Progress.prototype._createSvgView = function _createSvgView(opts) {
// Default parameters for progress bar creation
opts = utils.extend({
color: '#555',
strokeWidth: 1.0,
trailColor: null,
trailWidth: null,
fill: null
}, opts);
var svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
this._initializeSvg(svg, opts);
var trailPath = null;
// Each option listed in the if condition are 'triggers' for creating
// the trail path
if (opts.trailColor || opts.trailWidth) {
trailPath = this._createTrail(opts);
svg.appendChild(trailPath);
}
var path = this._createPath(opts);
svg.appendChild(path);
return {
svg: svg,
path: path,
trail: trailPath
};
};
Progress.prototype._initializeSvg = function _initializeSvg(svg, opts) {
svg.setAttribute('viewBox', '0 0 100 100');
};
Progress.prototype._createPath = function _createPath(opts) {
var pathString = this._pathString(opts);
return this._createPathElement(pathString, opts);
};
Progress.prototype._createTrail = function _createTrail(opts) {
// Create path string with original passed options
var pathString = this._trailString(opts);
// Prevent modifying original
var newOpts = utils.extend({}, opts);
// Defaults for parameters which modify trail path
if (!newOpts.trailColor) newOpts.trailColor = '#eee';
if (!newOpts.trailWidth) newOpts.trailWidth = newOpts.strokeWidth;
newOpts.color = newOpts.trailColor;
newOpts.strokeWidth = newOpts.trailWidth;
// When trail path is set, fill must be set for it instead of the
// actual path to prevent trail stroke from clipping
newOpts.fill = null;
return this._createPathElement(pathString, newOpts);
};
Progress.prototype._createPathElement =
function _createPathElement(pathString, opts) {
var path = document.createElementNS('http://www.w3.org/2000/svg', 'path');
path.setAttribute('d', pathString);
path.setAttribute('stroke', opts.color);
path.setAttribute('stroke-width', opts.strokeWidth);
if (opts.fill) {
path.setAttribute('fill', opts.fill);
} else {
path.setAttribute('fill-opacity', '0');
}
return path;
};
Progress.prototype._pathString = function _pathString(opts) {
throw new Error('Override this function for each progress bar');
};
Progress.prototype._trailString = function _trailString(opts) {
throw new Error('Override this function for each progress bar');
};
module.exports = Progress;