forked from rodrigocfd/string-tension-calc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StringSet.js
161 lines (138 loc) · 4.14 KB
/
StringSet.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
/**
* String Tension Calculator
* @author Rodrigo Cesar de Freitas Dias
* @license MIT
* @see https://github.com/rodrigocfd/string-tension-calc
*/
class StringSet {
constructor() {
this.$tpl = $('#templates .stringSet').clone();
this.$tpl.data('obj', this);
this.weightUnit = 'kg';
this.onTensionChangeCB = null;
this.fillComboPacks();
this.fillComboScaleLengths();
this.fillComboTunings();
this.$tpl.find('.packs').on('change', ev => {
StringRow.deleteAllRows(this.$tpl);
let pack = $(ev.currentTarget).find(':selected').data('obj');
let scale = this.$tpl.find('.scaleLength :selected').data('obj');
let tuning = this.$tpl.find('.tuning :selected').data('obj');
pack.gauges.forEach((gau, i) => {
let newRow = new StringRow(i + 1);
newRow.setRowInfo(gau, tuning.notes[i])
.setScaleLength(StringSet.calcMultiScaleLength(scale.inches, i, pack.gauges.length))
.setUnit(this.weightUnit);
newRow.getBlock().appendTo(this.$tpl.find('.rowsArea'))
.hide().fadeIn(200);
newRow.onTensionChange(() => {
if (this.onTensionChangeCB) this.onTensionChangeCB();
});
});
if (this.onTensionChangeCB) this.onTensionChangeCB();
});
this.$tpl.find('.scaleLength').on('change', ev => {
let scale = $(ev.currentTarget).find(':selected').data('obj');
let pack = this.$tpl.find('.packs :selected').data('obj');
StringRow.getAllRows(this.$tpl).forEach((row, i) => {
row.setScaleLength(StringSet.calcMultiScaleLength(scale.inches, i, pack.gauges.length));
});
});
this.$tpl.find('.tuning').on('change', ev => {
let tuning = $(ev.currentTarget).find(':selected').data('obj');
StringRow.getAllRows(this.$tpl).forEach((row, i) => {
row.setRowInfo(null, tuning.notes[i]);
});
});
this.$tpl.find('.deleteSet').on('click', () => {
if (confirm('Delete this set?')) {
this.$tpl.fadeOut(200, () => {
this.$tpl.remove();
if (this.onTensionChangeCB) this.onTensionChangeCB();
});
}
});
this.$tpl.find('.moveLeft').on('click', () => {
if (this.$tpl.index() === 0) {
alert('First string set cannot be moved left.');
} else {
this.$tpl.insertBefore(this.$tpl.prev());
if (this.onTensionChangeCB) this.onTensionChangeCB();
}
});
this.setDefaultValues();
}
getBlock() {
return this.$tpl;
}
setColor(color) {
this.$tpl.css('border-top', `4px solid ${color}`);
return this;
}
setUnit(unit) {
this.weightUnit = unit;
for (let row of StringRow.getAllRows(this.$tpl)) {
row.setUnit(unit);
}
return this;
}
getTensions() {
let tensions = [];
for (let row of StringRow.getAllRows(this.$tpl)) {
tensions.push(row.getTension());
}
return tensions;
}
onTensionChange(callback) {
this.onTensionChangeCB = callback;
return this;
}
fillComboPacks() {
for (const pac of PACKS) {
let $newOpt = $(`<option>${pac.name}</option>`);
$newOpt.data('obj', pac);
this.$tpl.find(`.pack${pac.gauges.length}`).append($newOpt);
}
return this.$tpl.find('.packs');
}
fillComboScaleLengths() {
for (const sca of SCALES) {
let $newOpt = $(`<option>${sca.scale}</option>`);
$newOpt.data('obj', sca);
let simpleOrMulti = (sca.inches[0] == sca.inches[1]) ? 'S' : 'M';
this.$tpl.find(`.scale${simpleOrMulti}`).append($newOpt);
}
return this.$tpl.find('.scaleLength');
}
fillComboTunings() {
let $cmbTuning = this.$tpl.find('.tuning');
for (const tun of TUNINGS) {
let $newOpt = $(`<option>${tun.tuning}</option>`);
$newOpt.data('obj', tun);
$cmbTuning.append($newOpt);
}
return $cmbTuning;
}
setDefaultValues() {
for (const selec of ['.tuning', '.scaleLength', '.packs']) {
this.$tpl.find(`${selec} option`).each((i, elem) => {
let $opt = $(elem);
if ($opt.data('obj').defaultSel) {
$opt.prop('selected', true);
return false;
}
});
}
this.$tpl.find('.packs').trigger('change');
}
static calcMultiScaleLength(scales, num, totalStrings) {
return scales[0] - (scales[0] - scales[1]) * (num / (totalStrings - 1));
}
static getAllSets(container) {
let sets = [];
$(container).find('.stringSet').each((i, elem) => {
sets.push($(elem).data('obj'));
});
return sets;
}
}