-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
372 lines (327 loc) · 11.8 KB
/
app.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
$(function() {
/*
for the periodic table of elements
*/
$('li[class^="type-"]').mouseover(function() {
var currentClass = $(this).attr('class').split(' ')[0];
if (currentClass != 'empty') {
$('.main > li').addClass('deactivate');
$('.' + currentClass).removeClass('deactivate');
}
});
$('li[class^="cat-"]').mouseover(function() {
var currentClass = $(this).attr('class').split(' ')[0];
$('.main > li').addClass('deactivate');
$('.' + currentClass).removeClass('deactivate');
});
$('.main > li').mouseout(function() {
var currentClass = $(this).attr('class').split(' ')[0];
$('.main > li').removeClass('deactivate');
});
$('.main > li').click(function() {
var element = $.trim($(this).clone().children().remove().end().text());
var new_peaks_table_row = $($("#peaks_table tr")[1]).clone();
new_peaks_table_row.removeClass("hidden");
$("#peaks_table tbody").append(new_peaks_table_row);
$(new_peaks_table_row.find("input")[0]).val(element);
});
/*
required to initialize tooltips
*/
$('[data-toggle="tooltip"]').tooltip();
/*
add material when clicking 'ok' in modal dialog
*/
$("#material_modal_ok").click(function() {
add_material($("#material_modal_name").val());
});
/*
TODO: add default materials
*/
add_material('Air', ['C', 'N', 'O', 'Ar', 'Kr'], [0.000124, 0.75527, 0.23178, 0.012827, 3.2e-6], 0.0012048, 1);
add_material('Beryllium', ['Be'], [1.0], 1.848, 1);
add_material('Germanium', ['Ge'], [1.0], 5.323, 1);
add_material('Gold', ['Au'], [1.0], 19.37, 1);
add_material('Kapton', ['C', 'N', 'O'], [0.628772, 0.066659, 0.304569], 1.42, 1);
add_material('Mylar', ['H', 'C', 'O'], [0.0419590, 0.625017, 0.333025], 1.40, 1);
add_material('Silicon', ['Si'], [1.0], 2.33, 1);
add_material('Teflon', ['C', 'F'], [0.240183, 0.759817], 2.2, 1);
/* add_material('Water', ['H2O1'], [1.0], 1.0, 1); */
});
function add_material(name, compounds_list, mass_list, default_density, default_thickness) {
if (compounds_list == undefined) {
compounds_list = [];
};
if (mass_list == undefined) {
mass_list = [];
};
if (default_density == undefined) {
default_density = 1;
};
if (default_thickness == undefined) {
default_thickness = 1;
};
material_panels = $("#materials .panel");
var new_material = $(material_panels[material_panels.length - 1]).clone();
if ($(material_panels[0]).hasClass("placeholder")) {
$(material_panels[0]).remove();
new_material.removeClass("placeholder");
}
$("#materials").append(new_material);
var a = new_material.find(".panel-title").find("a")
a.text(name);
var new_id = "material_" + name;
a.attr("href", "#" + new_id);
new_material.find(".panel-collapse").attr("id", new_id);
var material_table = new_material.find("tbody");
var new_row = $(material_table.children()[0]).clone()
material_table.empty();
if (compounds_list.length > 0) {
for (var i = 0; i < compounds_list.length; i++) {
var inputs = new_row.find("input");
$(inputs[0]).val(compounds_list[i]);
$(inputs[1]).val(mass_list[i]);
material_table.append(new_row);
new_row = new_row.clone();
}
} else {
var inputs = new_row.find("input");
$(inputs[0]).val($(inputs[0]).attr("placeholder"));
$(inputs[1]).val($(inputs[0]).attr("placeholder"));
material_table.append(new_row);
}
var material_form = new_material.find("fieldset");
var inputs = material_form.find("input");
$(inputs[0]).val(default_density);
$(inputs[1]).val(default_thickness);
update_materials_in_tables();
};
function update_materials_in_tables() {
$("#multilayer ul").empty();
$("#attenuators ul").empty();
$("#detector ul").empty();
$("#materials .panel-title a").each(function() {
var material_name = $(this).html();
$("#multilayer ul").append($("<li><a href='#' onclick='material_selected(event)'>" + material_name + "</a></li>"));
$("#multilayer button").each(function() {
if ($.trim($(this).text()) == 'placeholder') {
$(this).html($(this).html().replace("placeholder", material_name))
};
});
$("#attenuators ul").append($("<li><a href='#' onclick='material_selected(event)'>" + $(this).html() + "</a></li>"));
$("#attenuators button").each(function() {
if ($.trim($(this).text()) == 'placeholder') {
$(this).html($(this).html().replace("placeholder", material_name))
};
});
$("#detector ul").append($("<li><a href='#' onclick='material_selected(event)'>" + $(this).html() + "</a></li>"));
$("#detector button").each(function() {
if ($.trim($(this).text()) == 'placeholder') {
$(this).html($(this).html().replace("placeholder", material_name))
};
});
});
};
function show_add_material(e) {
$("#material_modal").modal("show");
};
function peak_selected(e) {
var ref = e.target;
var selected_peak = $(ref).html();
var btn = $(ref).parent().parent().prev();
var current_peak = $.trim($(btn).text());
$(btn).html($(btn).html().replace(current_peak, selected_peak));
e.preventDefault();
};
function material_selected(e) {
var ref = e.target;
var selected_material = $(ref).html();
var btn = $(ref).parent().parent().prev();
var current_material = $.trim($(btn).text());
$(btn).html($(btn).html().replace(current_material, selected_material));
var density = 1;
var thickness;
var materials = get_materials();
for (var i=0; i<materials.length; i++) {
if (materials[i].name == selected_material) {
density = materials[i].density;
if (density == "") { density = "1" };
thickness = materials[i].thickness;
if (thickness == "") { thickness = "1" };
break;
}
}
$($(btn).parent().parent().parent().find("input")[0]).val(density);
var thickness_input = $($(btn).parent().parent().parent().find("input")[1]);
var thickness_current_value = $.trim(thickness_input.val());
if ((thickness_current_value == "") || (thickness_current_value=="1")) {
thickness_input.val(thickness);
}
e.preventDefault();
};
function add_row(e) {
var add_btn = e.target;
var table_row = $(add_btn).parent().parent();
var new_table_row = table_row.clone();
new_table_row.removeClass("hidden");
table_row.parent().append(new_table_row);
};
function del_row(e) {
var btn = e.target;
var table_row = $(btn).parent().parent();
$(table_row).remove();
};
function del_layer_row(e) {
var btn = e.target;
var table_row = $(btn).parent().parent();
if ($(table_row).parent().children().length > 1) {
$(table_row).remove();
}
};
function add_detector() {
if ($("#detector_table tbody tr").length == 1) {
var new_row = $($("#detector_table tbody tr")[0]).clone();
new_row.removeClass("hidden");
$("#detector_table tbody").append(new_row);
}
};
function add_layer(e) {
var new_row = $($("#multilayer_table tbody tr")[0]).clone();
new_row.removeClass("hidden");
$("#multilayer_table tbody").append(new_row);
};
function add_beam_filter(e) {
var new_row = $($("#beam_filters_table tbody tr")[0]).clone();
new_row.removeClass("hidden");
$("#beam_filters_table tbody").append(new_row);
};
function add_filter(e) {
var new_row = $($("#filters_table tbody tr")[0]).clone();
new_row.removeClass("hidden");
$("#filters_table tbody").append(new_row);
};
function filter_selected(e) {
material_selected(e);
};
function get_materials() {
var materials = [];
$("#materials .panel").each(function() {
var material = {};
material.name = $($(this).find(".panel-title a")[0]).text();
material.compounds = [];
material.mass = [];
var i = 0;
$(this).find("tbody input").each(function() {
if (i % 2 == 0) {
material.compounds.push($.trim($(this).val()));
} else {
material.mass.push($.trim($(this).val()));
}
i++;
});
var inputs = $(this).find("fieldset input");
material.density = $.trim($(inputs[0]).val());
material.thickness = $.trim($(inputs[1]).val());
materials.push(material);
});
return materials;
};
function get_multilayer() {
var multilayer = [];
$("#multilayer_table tbody tr").each(function() {
if (! $(this).hasClass("hidden")) {
var layer = {};
var input_cols = $(this).find("input");
layer.density = $(input_cols[0]).val();
layer.thickness = $(input_cols[1]).val();
layer.material = $.trim($($(this).find("button")[0]).text());
multilayer.push(layer);
}
});
return multilayer;
};
function get_attenuators() {
var attenuators = [];
$("#beam_filters_table tbody tr").each(function() {
if (! $(this).hasClass("hidden")) {
var att = {};
var input_cols = $(this).find("input");
var btns = $(this).find("button");
att.type = "Beam Filter"; //$.trim($(btns[0]).text());
att.density = $(input_cols[0]).val();
att.thickness = $(input_cols[1]).val();
att.funny = $(input_cols[2]).val();
att.material = $.trim($(btns[1]).text());
attenuators.push(att);
}
});
$("#filters_table tbody tr").each(function() {
if (! $(this).hasClass("hidden")) {
var att = {};
var input_cols = $(this).find("input");
var btns = $(this).find("button");
att.type = "Filter"; //$.trim($(btns[0]).text());
att.density = $(input_cols[0]).val();
att.thickness = $(input_cols[1]).val();
att.funny = $(input_cols[2]).val();
att.material = $.trim($(btns[1]).text());
attenuators.push(att);
}
});
return attenuators;
};
function get_detector() {
var detector = {};
$("#detector_table tbody tr").each(function() {
if (! $(this).hasClass("hidden")) {
var input_cols = $(this).find("input");
var btns = $(this).find("button");
detector.material = $.trim($(btns[0]).text());
detector.density = $(input_cols[0]).val();
detector.thickness = $(input_cols[1]).val();
detector.area = $(input_cols[2]).val();
detector.distance = $(input_cols[3]).val();
}
});
return detector;
};
function get_peaks() {
var peaks = [];
$("#peaks_table tbody tr").each(function() {
if (! $(this).hasClass("hidden")) {
var peak = {};
var input_cols = $(this).find("input");
var btns = $(this).find("button");
peak.element = $(input_cols[0]).val();
peak.family = $.trim($(btns[0]).text());
peak.layer = $(input_cols[1]).val();
peaks.push(peak);
}
});
return peaks;
};
function submit() {
user_input = {
"materials": get_materials(),
"attenuators": get_attenuators(),
"detector": get_detector(),
"multilayer": get_multilayer(),
"peaks": get_peaks(),
"beam_energy": $("#beam_energy").val(),
"incoming_angle": $("#incoming_angle").val(),
"outgoing_angle": $("#outgoing_angle").val()
};
$.ajax({
url: "calculate",
data: JSON.stringify(user_input),
dataType: "json",
type: "POST",
contentType: "application/json; charset=utf-8",
success: function(returned_data) {
/*
TODO: better handle result: are there errors? etc
*/
$("#result").text(returned_data.text);
}
});
};