comparison src/common.js @ 1:7e3085fc60c1 draft default tip

master branch Updating
author lain
date Wed, 30 Aug 2023 14:21:18 +0000
parents
children
comparison
equal deleted inserted replaced
0:b58b229c4cbf 1:7e3085fc60c1
1
2 this.ctx = () => context;
3 var all_contexts = [] ;
4 var inchidata = {} ;
5 var all_mix_data = {} ;
6 var already_sent_data = {} ;
7
8 function no_success(tab_index, inchi, data) {
9 console.log(`inchikey ${inchi} not found...`) ;
10 console.log(`disabling ${data} ...`)
11 disable_tab(
12 tab_index,
13 "red",
14 "This compound has not been found on peakforest"
15 ) ;
16 }
17
18 function disable_tab(tab_index, color, title) {
19 var element = $(`#open_tab_${tab_index}`) ;
20 element.attr("disabled", "disabled").off("click");
21 element.attr("onclick", null) ;
22 element.attr("title", title) ;
23 element.attr("data-toggle", null) ;
24 element.attr("href", null) ;
25 element.css("color", color) ;
26 element.css("cursor", "not-allowed") ;
27 }
28
29 function successfull_send_to_pf(bundle, json, index) {
30 for (var i = 0 ; i < all_contexts.length ; i += 1) {
31 if (all_contexts[i] === null || all_contexts[i] === undefined) {
32 continue ;
33 }
34 if (all_contexts[i].inchikey == bundle["sample"]["inchikey"]) {
35 disable_tab(
36 all_contexts[i].tab_index,
37 "green",
38 "This compound has already been sent to peakforest"
39 )
40 }
41 }
42 if (already_sent_data[index] == null) {
43 already_sent_data[index] = [] ;
44 }
45 json.forEach((id) => {
46 already_sent_data[index].push(id)
47 $("#send_buttons_div").prepend(`
48 <div class="row">
49 <div class="col-lg-3">
50 <div class="alert alert-success alert-dismissible" role="alert">
51 <button type="button" class="close" data-dismiss="alert">
52 <span aria-hidden="true">×</span>
53 <span class="sr-only">
54 <spring:message code="alert.close" text="Close"></spring:message>
55 </span>
56 </button>
57 <strong>
58 <spring:message code="alert.strong.info" text="Success!"></spring:message>
59 </strong> Spectrum has been sent:
60 <a
61 href="${ctx().peakforest_url}/webapp/home?PFs=${id.slice(3)}"
62 target="_blank"
63 >${id}</a>
64 </div>
65 </div>
66 </div>`)
67 })
68 }
69
70 function failed_send_to_pf(err, bundle) {
71 console.log(err) ;
72 $("#send_buttons_div").prepend(`
73 <div class="row">
74 <div class="col-lg-3">
75 <div class="alert alert-error alert-dismissible" role="alert">
76 <button type="button" class="close" data-dismiss="alert">
77 <span aria-hidden="true">×</span>
78 <span class="sr-only">
79 <spring:message code="alert.close" text="Close"></spring:message>
80 </span>
81 </button>
82 <strong>
83 <spring:message code="alert.strong.info" text="Error!"></spring:message>
84 </strong> Spectrum has not been sent!
85 </div>
86 </div>
87 </div>`)
88 }
89
90 function already_sent(bundle, index) {
91 return already_sent_data[index] != null ;
92 }
93
94 function failed_already_sent(bundle, index) {
95 already_sent_data[index].forEach((id) => {
96 $("#send_buttons_div").prepend(`
97 <div class="row">
98 <div class="col-lg-3">
99 <div class="alert alert-warning alert-dismissible" role="alert">
100 <button type="button" class="close" data-dismiss="alert">
101 <span aria-hidden="true">×</span>
102 <span class="sr-only">
103 <spring:message code="alert.close" text="Close"></spring:message>
104 </span>
105 </button>
106 <strong>
107 <spring:message code="alert.strong.info" text="warning!"></spring:message>
108 </strong> Spectrum has already been sent:
109 <a
110 href="${ctx().peakforest_url}/webapp/home?PFs=${id.slice(3)}"
111 target="_blank"
112 >${id}</a>
113 </div>
114 </div>
115 </div>`)
116 }) ;
117 }
118
119 function send_everything_to_peakforest(url, token) {
120 var bundles ;
121 if (context.is_mix()) {
122 bundles = [all_mix_data] ;
123 } else {
124 bundles = create_dataset_bundles() ;
125 }
126 bundles.forEach((bundle, index) => {
127 if (already_sent(bundle, index)) {
128 return failed_already_sent(bundle, index) ;
129 }
130 console.log(bundle)
131 $.ajax({
132 type: "post",
133 url: `${url}/rest/v2/spectrum?token=${token}`,
134 data: JSON.stringify(bundle),
135 contentType: "application/json",
136 success: function(json) {
137 successfull_send_to_pf(bundle, json, index)
138 }, error: function(err) {
139 failed_send_to_pf(err, bundle)
140 }
141 })
142 $.ajax({
143 type: "post",
144 url: "/",
145 data: `{"index": ${index}, "object": ${JSON.stringify(bundle)}}`,
146 contentType: "application/json",
147 error: function(err) {
148 console.log(`Error: JSON will not be produced by the application.`)
149 }
150 })
151 })
152 // console.log(bundles)
153 }
154
155 function create_dataset_bundles() {
156 var bundles = [] ;
157 var data ;
158 all_contexts.forEach((context, index) => {
159 if ((data = context.sent_json) === null) {
160 return ;
161 }
162 if (bundles.length === 0) {
163 // console.log("First metadata!")
164 return bundles.push(data) ;
165 }
166 if (merge_in_bundle(bundles, data)) {
167 // console.log("Merged!")
168 } else {
169 // console.log("New metadata!")
170 }
171 }) ;
172 return bundles ;
173 }
174
175 function merge_in_bundle(bundles, data) {
176 for(var i = 0 ; i < bundles.length ; i += 1) {
177 if (
178 identical(bundles[i]["sample"], data["sample"])
179 && identical(bundles[i]["chromatography"], data["chromatography"])
180 && identical(bundles[i]["analyzer"], data["analyzer"])
181 && identical(bundles[i]["ionization_mode_positive"], data["ionization_mode_positive"])
182 && identical(bundles[i]["ionization_mode_negative"], data["ionization_mode_negative"])
183 && identical(bundles[i]["other_metadata"], data["other_metadata"])
184 ) {
185 bundles[i]["peaklists"].push(data["peaklists"][0]) ;
186 return true ;
187 } else {
188 continue ;
189 console.log(
190 "sample: "
191 + identical(bundles[i]["sample"], data["sample"])
192 )
193 console.log(
194 "chromatography: "
195 + identical(bundles[i]["chromatography"], data["chromatography"])
196 )
197 console.log(
198 "analyzer: "
199 + identical(bundles[i]["analyzer"], data["analyzer"])
200 )
201 console.log(
202 "ionization_mode_positive: "
203 + identical(bundles[i]["ionization_mode_positive"], data["ionization_mode_positive"])
204 )
205 console.log(
206 "ionization_mode_negative: "
207 + identical(bundles[i]["ionization_mode_negative"], data["ionization_mode_negative"])
208 )
209 console.log(
210 "other_metadata: "
211 + identical(bundles[i]["other_metadata"], data["other_metadata"])
212 )
213 }
214 }
215 bundles.push(data) ;
216 return false ;
217 }
218
219 function identical(left, right) {
220 if (typeof left !== typeof right) {
221 return false ;
222 }
223 if (left === null) {
224 return right === null ;
225 }
226 if (right === null) {
227 return left === null ;
228 }
229 switch (typeof left) {
230 case "array":
231 return identical_array(left, right) ;
232 break ;
233 case "object":
234 return identical_object(left, right) ;
235 break ;
236 default:
237 break ;
238 }
239 return (left === right)
240 }
241
242 function identical_array(left, right) {
243 if (right.length !== left.length) {
244 return false ;
245 }
246 for(var i = 0 ; i < left.length ; i += 1) {
247 if (!identical(left[i], right[i])) {
248 return false ;
249 }
250 }
251 return true ;
252 }
253
254 function identical_object(left, right) {
255 var left_keys ;
256
257 if (!(
258 share_keys(left, right)
259 && share_keys(right, left)
260 )) {
261 return true ;
262 }
263 left_keys = Object.keys(left) ;
264 for (var i = 0 ; i < left_keys.length ; i += 1){
265 if (!identical(left[left_keys[i]], right[left_keys[i]])) {
266 return false ;
267 }
268 }
269 return true ;
270 }
271
272 function share_keys(left, right) {
273 var left_keys ;
274 var right_keys ;
275
276 left_keys = Object.keys(left) ;
277 right_keys = Object.keys(right) ;
278 for(var i = 0 ; i < left_keys.length ; i += 1) {
279 if (!right_keys.includes(left_keys[i])) {
280 return false ;
281 }
282 }
283 return true ;
284 }
285
286 var set_inchi_data = function(data, tab_index) {
287 inchidata[data.inchikey] = data ;
288 $(`#add1spectrum-sample-inchi-${tab_index}`).val(data.inchi);
289 $(`#add1spectrum-sample-inchi-${tab_index}`).change();
290 $(`#add1spectrum-sample-commonName-${tab_index}`).val(data.name);
291 $(`#add1spectrum-sample-commonName-${tab_index}`).change();
292 $(`#sample-bonus-display-${tab_index}`).html(
293 `<img
294 class=""
295 src="{{ PF_URL_PLACEHOLDER }}/webapp/image/${data.type}/${data.inchikey}"
296 alt="${data.name}"
297 >`
298 );
299 }
300
301 var lightgrayRenderer = function(instance, td, row, col, prop, value, cellProperties) {
302 Handsontable.renderers.TextRenderer.apply(this, arguments);
303 td.style.backgroundColor = "#EEE";
304 }