-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathctrl.js
334 lines (328 loc) · 9.98 KB
/
ctrl.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
function Ctrl() {
this.rcWorker=null;
this.valSet=$rc.E12Series; // the chosen value set
this.rSeriesVal="E12";
var owner=this; // to have it as a variable in closures
this.updateData=function(data, done) {
var numCombs=$("#numCombs");
numCombs.html("# within "+Math.round(this.tolerance*1000)/10+"% of target: "+
(null!=data ? data.combs : 0)
);
if(done) {
$("#cancelSupport").empty();
$("#cancelSupport").html("Done!");
}
var resTBody=$("#resTable tbody");
resTBody.empty();
var ownerCtrl=this;
if(null!==data && "undefined"!=typeof(data.vals) && $rc.util.isArray(data.vals)) {
data.vals.forEach(
function(e) {
var minTxt=
e.min+"<br>("+Math.round((e.min-ownerCtrl.target)/ownerCtrl.target*1000000)/10000+"%)";
;
var maxTxt=
e.max+"<br>("+Math.round((e.max-ownerCtrl.target)/ownerCtrl.target*1000000)/10000+"%)";
;
var valTxt=(new $rc.Val(e.v)).toString()+"<br>("+Math.round(e.v*1000)/1000+")";
var eps=(e.v-ownerCtrl.target)/ownerCtrl.target;
eps=Math.round(eps*1000000)/10000;
resTBody.append(
"<tr><td>"+e.c+"</td>"+
"<td>"+valTxt+"</td>"+
"<td>"+eps+"</td>"+
"<td>"+minTxt+"</td>"+
"<td>"+maxTxt+"</td>"+
"</tr>"
);
}
);
}
}
function showCustomSubsetDialog(
ctrlOwner,
subset,
doneCallback, cancelCallback
) {
var d=$("#custom-choice-dialog");
var nRows=subset.nRows;
var nCols=subset.v.length/nRows;
function valueIx(rIx, cIx) {
return cIx*nRows+rIx;
};
function valueId(rIx, cIx) {
return "cvId"+valueIx(rIx,cIx);
};
var str="<table style='border:none;width:100%'><tr>";
for(var cIx=0;cIx<nCols;cIx++) {
var has=false;
for(var rIx=0; rIx<nRows && !has; rIx++) {
var valObj=subset.v[valueIx(rIx, cIx)];
has=valObj.s;
}
str+=
"<td align='right'>↓ <input type='checkbox' value='"+cIx
+ "' id='reset"+cIx+"'"
;
if(has) {
str+="checked='checked'";
}
str+="/></td>";
}
str+="</tr>";
for(var rIx=0; rIx<nRows;rIx++) {
str+="<tr>";
for(var cIx=0; cIx<nCols;cIx++) {
var vIx=valueIx(rIx,cIx);
var vId=valueId(rIx, cIx);
var valObj=subset.v[vIx];
str+="<td align='right'><label for='"+vId+"'>"+valObj.v.toString()+"</label>";
str+="<input type='checkbox' id='"+vId+"' value='"+vIx+"'";
if(valObj.s) {
str+=" checked='checked'";
}
str+="/></td>";
}
str+="</tr>";
}
str+="</table>";
d.html(str);
for(var cIx=0;cIx<nCols;cIx++) {
d.find("#reset"+cIx).change(function(e) {
var enAll=$(this).is(':checked');
var cIx=$(this).val();
for(var i=0;i<nRows;i++) {
d.find("#"+valueId(i,cIx)).attr("checked",enAll);
// Darn, changing it programmatically doesn't trigger an event.
// Well, adjust the corresponding value manually
var vIx=valueIx(i,cIx);
subset.v[vIx].s=enAll;
}
});
for(var rIx=0;rIx<nRows;rIx++) {
var chBox=d.find("#"+valueId(rIx,cIx));
chBox.change(function(e) {
var sel=$(this).is(":checked");
var vIx=$(this).val()-0; // make it number
subset.v[vIx].s=sel;
});
// chBox.checkboxradio();
}
}
d.dialog(
{
resizable: false,
height: "auto",
width: 460,
maxHeight : 520,
modal: true,
autoOpen: false,
close : function(e,ui) {
// This will get called after the Ok or cancel buttons are pressed
// As such, one can expect the rSeriesVal to be already set up correctly
$("#rSeries").find("[value="+ctrlOwner.rSeriesVal+"]").prop("selected", true);
},
buttons: {
"OK": function() {
$("custom-choice-dialog").empty();
var valSet=[];
subset.v.forEach(valObj=>{
if(valObj.s) {
valSet.push(valObj.v.getValue());
}
});
doneCallback(valSet);
$( this ).dialog( "close" );
},
Cancel: function() {
$("#custom-choice-dialog").empty();
if(typeof(cancelCallback)=="function") {
cancelCallback();
}
$( this ).dialog( "close" );
}
}
}
);
d.dialog("open");
}
this.showE12subset=function() {
var resSet=[];
var saved=window.sessionStorage['E12_subset'];
saved=
(typeof(saved)==="undefined" || null===saved)
? $rc.E12Series
: saved.split(',')
;
$rc.E12Series.forEach(function(e) {
var ix=saved.binarySearch(e);
resSet.push({v:new $rc.Val(e),s:(ix>=0)});
});
var subset={nRows:12,v:resSet};
var ctrlOwner=this;
showCustomSubsetDialog(
this,
subset,
function(valSet) {
ctrlOwner.valSet=valSet;
ctrlOwner.rSeriesVal=$("#rSeries").val(); // save it
ctrlOwner.adjustMaxNum();
window.sessionStorage['E12_subset']=valSet;
}
);
}
this.showE24subset=function() {
var resSet=[];
var saved=window.sessionStorage['E24_subset'];
saved=
(typeof(saved)==="undefined" || null===saved)
? $rc.E24Series
: saved.split(',')
;
$rc.E24Series.forEach(function(e) {
var ix=saved.binarySearch(e);
resSet.push({v:new $rc.Val(e),s:(ix>=0)});
});
var subset={nRows:24,v:resSet};
var ctrlOwner=this;
showCustomSubsetDialog(
this,
subset,
function(valSet) {
ctrlOwner.valSet=valSet;
ctrlOwner.rSeriesVal=$("#rSeries").val(); // save it
ctrlOwner.adjustMaxNum();
window.sessionStorage['E24_subset']=valSet;
}
);
}
this.compute=function() {
if(null!=this.rcWorker) {
this.rcWorker.terminate();
this.rcWorker=null;
}
this.updateData(null, false);
var target=$("#resistorValue").val();
if(false==$rc.util.isNum(target)) {
alert("Expecting a number as the 'Desired value'");
return;
}
this.target=target-0;
this.rcWorker=new Worker("worker.js");
var ownerCtrl=this;
this.rcWorker.onmessage=function(e) {
if(typeof(e.data)!="undefined") {
var type=e.data.type;
if("progress"==type || "done"==type) {
ownerCtrl.updateData(e.data, "done"==type);
}
if("done"==type) {
this.rcWorker.terminate();
this.rcWorker=null;
}
}
}
this.rcWorker.onerror=function(e) {
// TODO handle it better
alert(e.message);
}
var series=$("#rSeries").val();
this.tolerance=(series.startsWith("E24") ? 0.05 : 0.1);
series=this.valSet;
var combType=$('#combType').val();
var owner=this;
$("#cancelSupport").html("<button id='cancelBtn'>Cancel</button>");
$("#cancelSupport").find("#cancelBtn").click(
function(e) {
if(null!=owner.rcWorker) {
owner.rcWorker.terminate();
owner.rcWorker=null;
setTimeout(
function() {
$("#cancelSupport").html("Interrupted!");
},850
);
}
}
);
this.rcWorker.postMessage({
cmd:"start",
numRes:$("#numComponents").val(),
target: this.target,
tolerance:this.tolerance,
srcVals:series,
combType:combType
});
}
this.adjustMaxNum=function() {
if(typeof(this.valSet)!="undefined") {
var maxNum=6;
if(this.valSet.length<=24) {
maxNum=6;
}
else if(this.valSet.length<=36) {
maxNum=5;
}
else if(this.valSet.length<=72) {
maxNum=4;
}
else {
maxNum=3;
}
var numSelected=$("#numComponents").find(":selected").val();
if(numSelected>maxNum) {
numSelected=maxNum;
}
for(i=1; i<=maxNum; i++) {
var opt=$("#numComponents").find("[value="+i+"]");
opt.prop("disabled",false);
if(i==numSelected) {
opt.prop("selected", true);
}
}
for(i=maxNum+1; i<=6; i++) {
$("#numComponents").find("[value="+i+"]").prop("disabled",true);
}
}
}
$("#resistorValue").on("input",
function(e) {
var valStr=$("#resistorValue").val();
var validVal=$rc.util.isNum(valStr) && (valStr-0)>0;
$("#computeButton").prop('disabled', !validVal);
}
);
var owner=this;
$("#rSeries").find("option").on("click",
function(e) {
var v=$("#rSeries").val();
if("E24"==v) {
owner.valSet=$rc.E24Series;
owner.rSeriesVal=v;
owner.adjustMaxNum();
}
else if("E12"==v) {
owner.valSet=$rc.E12Series;
owner.rSeriesVal=v;
owner.adjustMaxNum();
}
else if("E12_subset"==v) {
owner.showE12subset();
}
else if("E24_subset"==v) {
owner.showE24subset();
}
}
);
$("#computeButton").prop('disabled', true);
$("#resistorValue").val("");
$("#numComponents").find("[value=3]").prop("selected", true);
$("#rSeries").find("[value=E12]").prop("selected", true);
this.adjustMaxNum();
var owner=this;
$("#computeButton").click(
function(){
owner.compute();
}
);
}