-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtune.js
181 lines (181 loc) · 7.6 KB
/
tune.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
"use strict";
exports.__esModule = true;
var interfaces_1 = require("./interfaces");
var common_1 = require("./common");
var circle_1 = require("./circle");
var needle_1 = require("./needle");
var edges_1 = require("./edges");
var icon_1 = require("./icon");
var Tune = /** @class */ (function () {
function Tune(element, options) {
this.element = element;
this.common = new common_1.Common();
// set default options
var defaultOptions = this.getDefaultOptions();
// override defaults with user options
this.options = this.common.extend(defaultOptions, options);
this.fixOptions();
this.init();
}
Tune.prototype.fixOptions = function () {
// this.options.title = this.common.setInnerTextDefaults(this.options.title);
this.options.strokeWidth = this.common.fixStrokeWidth(this.options.strokeWidth);
this.options.radius = this.common.fixRadius(this.options.radius);
// if we're hiding bottom we should take thoes values in to considuration
if (this.options.hideBottom) {
var portionToHide = 0.3334;
this.options.needleOptions.minMaxVal.max = this.common.normalizeByPercentage(this.options.needleOptions.minMaxVal.max, portionToHide);
this.options.needleOptions.minMaxVal.min = this.common.normalizeByPercentage(this.options.needleOptions.minMaxVal.min, portionToHide);
this.options.needleOptions.minMaxVal.value = this.common.normalizeByPercentage(this.options.needleOptions.minMaxVal.value, portionToHide);
if (this.options.iconOptions && typeof this.options.iconOptions.degree === 'number')
this.options.iconOptions.degree = this.common.normalizeByPercentage(this.options.iconOptions.degree, portionToHide);
}
};
Tune.prototype.init = function () {
var obj = {
type: 'div',
attrs: {
'data-tune': ''
}
};
var innerElem = this.common.jsonToHtml(obj);
this.updateOptions(false);
innerElem.appendChild(this.circle.getElement());
innerElem.appendChild(this.needle.getElement());
innerElem.appendChild(this.edges.getLeftElement());
innerElem.appendChild(this.edges.getRightElement());
innerElem.appendChild(this.icon.getElement());
this.element.appendChild(innerElem);
this.updateOptions(true);
};
Tune.prototype.update = function (options) {
this.options = this.common.extend(this.options, options);
this.fixOptions();
this.updateOptions(true);
};
Tune.prototype.updateOptions = function (setWrap) {
if (setWrap)
this.setWrap(this.options);
this.setCircle();
this.setNeedle();
this.setEdges();
this.setIcon();
};
Tune.prototype.setWrap = function (options) {
var wrap = this.element;
var dim = (options.radius * 2) + 'px';
wrap.style.width = dim;
wrap.style.height = dim;
wrap.style.position = 'relative';
};
Tune.prototype.getDefaultOptions = function () {
var colors = this.common.getDefaultColors();
var defRadius = 88, animationDuration = 500;
return {
needleOptions: {
minMaxVal: {
min: 30,
max: 70,
value: 55
},
color: colors.active,
scale: 1.125,
radius: defRadius,
animationDuration: animationDuration,
disabled: false
},
iconOptions: {
animationDuration: animationDuration,
degree: 50,
radius: defRadius,
radiusOffset: 0,
src: '',
dimensions: {
width: 25,
height: 25
},
top: 0,
left: 0,
opacity: 1
},
colors: colors,
strokeWidth: 6,
animationDuration: animationDuration,
radius: defRadius,
showEdges: true,
showIcon: true,
hollowEdges: interfaces_1.SideState.None,
// title: this.common.setInnerTextDefaults(),
hideBottom: true,
backgroundColor: '#ffffff',
hollowEdgesBgColor: '#ffffff'
};
};
Tune.prototype.setCircle = function () {
this.circleOptions = this.common.extend(this.options, this.circleOptions);
this.circleOptions.fromDegree = this.options.needleOptions.minMaxVal.min;
this.circleOptions.toDegree = this.options.needleOptions.minMaxVal.max;
this.circleOptions.backgroundColor = this.common.getComputedStyleByParentRec(this.element, 'backgroundColor');
if (!this.circleOptions.backgroundColor)
this.circleOptions.backgroundColor = '#fff';
if (this.circle)
this.circle.update(this.circleOptions);
else
this.circle = new circle_1.Circle(this.circleOptions);
};
Tune.prototype.setNeedle = function () {
this.needleOptions = this.common.extend(this.options.needleOptions, this.needleOptions);
if (!this.options.needleOptions.color)
this.needleOptions.color = this.common.isInRange(this.options.needleOptions.minMaxVal, this.options.hollowEdges) ? this.options.colors.active : this.options.colors["default"];
if (this.options.needleOptions.minMaxVal.value > 100 || this.options.needleOptions.minMaxVal.value < 0)
this.needleOptions.color = this.options.colors.inactive;
else if (this.options.hideBottom) {
if (this.options.needleOptions.minMaxVal.value >= 83.34 || this.options.needleOptions.minMaxVal.value <= 16.67)
this.needleOptions.color = this.options.colors.inactive;
}
if (this.needle) {
this.needle.update(this.needleOptions);
}
else
this.needle = new needle_1.Needle(this.needleOptions);
};
Tune.prototype.setEdges = function () {
this.edgesOptions = this.common.extend(this.options.needleOptions, this.edgesOptions);
this.edgesOptions.strokeWidth = this.options.strokeWidth;
this.edgesOptions.color = this.options.colors.active;
this.edgesOptions.hollowEdges = this.options.hollowEdges;
this.edgesOptions.backgroundColor = this.options.hollowEdgesBgColor;
if (this.edges)
this.edges.update(this.edgesOptions);
else
this.edges = new edges_1.Edges(this.edgesOptions);
var left = this.element.querySelector('[data-left-edge]');
var right = this.element.querySelector('[data-right-edge]');
if (left && right) {
if (!this.options.showEdges) {
left.style.display = 'none';
right.style.display = 'none';
}
else {
left.style.display = 'inline-block';
right.style.display = 'inline-block';
}
}
};
Tune.prototype.setIcon = function () {
this.iconOptions = this.common.extend(this.options.iconOptions, this.iconOptions);
if (this.icon)
this.icon.update(this.iconOptions);
else
this.icon = new icon_1.Icon(this.iconOptions);
var image = this.element.querySelector('[data-icon]');
if (image) {
if (!this.options.showIcon || !this.iconOptions.src)
image.style.display = 'none';
else
image.style.display = 'inline-block';
}
};
return Tune;
}());
exports.Tune = Tune;