summaryrefslogtreecommitdiff
path: root/public/javascripts/lazar-gui.js
blob: 18998869161a1a2581b9e28de8f6fe6bef6720ba (plain)
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
$(document).ready(function() {
  addExternalLinks();
  popOver();
});

addExternalLinks = function() {
  $('A[rel="external"]').each(function() {
    $(this).attr('alt', 'Link opens in new window.');
    $(this).attr('title', 'Link opens in new window.');
    $(this).attr('target', '_blank');
  });
};

popOver = function(){
  $('[data-toggle="popover"]').popover();
  $('.modal').on('hidden.bs.modal', function () {
    $(this).removeData('bs.modal');
  });
  $('.modal').on('show.bs.modal', function(e){
    var button = $(e.relatedTarget);
    var modal = $(this);
    modal.find('.modal-content').load(button.data("remote"));
  });
};

var HttpClient = function() {
  this.get = function(aUrl, aCallback) {
  var anHttpRequest = new XMLHttpRequest();
  anHttpRequest.onreadystatechange = function() {
  if (anHttpRequest.readyState == 4 && anHttpRequest.status == 200)
    aCallback(anHttpRequest.responseText);
  }
    anHttpRequest.open( "GET", aUrl, true );
    anHttpRequest.send( null );
  }
  this.post = function(aUrl, params, aCallback) {
  var anHttpRequest = new XMLHttpRequest();
  anHttpRequest.onreadystatechange = function() {
    //alert(anHttpRequest.status);
  if (anHttpRequest.readyState == 4 && anHttpRequest.status == 200)
    aCallback(anHttpRequest);
  }
  if (anHttpRequest.readyState == 4 && anHttpRequest.status == 400){
    aCallback(anHttpRequest);
  }
  anHttpRequest.open( "POST", aUrl, true );
  anHttpRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  anHttpRequest.send( params );
  }
};  

// functions used in predict.haml

// GET request
/*$(function() {
  $('a[data-toggle="tab"]').on('click', function (e) {
    localStorage.setItem('lastTab', $(e.target).attr('href'));
  });
  var lastTab = localStorage.getItem('lastTab');
  if (lastTab) {
    $('a[href="'+lastTab+'"]').click();
  }
});*/

// get and check input
function getInput(){
  identifier = document.getElementById("identifier").value.trim();
  fileselect = document.getElementById("fileselect").value;
  if (fileselect != ""){
    return 1;
  };
  if (identifier != ""){
    return 2;
  };
  return 0;
};

// display wait animation after click on predict button
// check form fields, input file or SMILES, endpoint selection
function showcircle() {
  switch (getInput()){
    case 0:
      alert("Please draw or insert a chemical structure.");
      return false;
      break;
    case 1:
      if (checkfile() && checkboxes()){
        button = document.getElementById("submit");
        image = document.getElementById("circle");
        button.parentNode.replaceChild(image, button);
        $("img.circle").show();
        return true;
      };
      return false;
      break;
    case 2:
      if (checksmiles() && checkboxes()){
        button = document.getElementById("submit");
        image = document.getElementById("circle");
        button.parentNode.replaceChild(image, button);
        $("img.circle").show();
        return true;
      };
      return false;
      break;
    default: false;
  };
  return false;
};

// check if a file was selected for upload
function checkfile() {
  var fileinput = document.getElementById("fileselect");
  if(fileinput.value != "") {
    return true;
  };
  alert("Please select a file (csv).");
  return false;
};

// check if a smiles string was entered
function checksmiles () {
  getsmiles();
  if (document.form.identifier.value == "") {
    alert("Please draw or insert a chemical structure.");
    document.form.identifier.focus();
    $("img.circle").hide();
    return false;
  };
  return true;
};

// check if an endpoint was selected
function checkboxes () {
  var checked = false;
  $('input[type="checkbox"]').each(function() {
    if ($(this).is(":checked")) {
      checked = true;
    };
  });
  if (checked == false){
    alert("Please select an endpoint.");
    $("img.circle").hide();
    return false;
  };
  return true;
};

// display jsme editor with option
function jsmeOnLoad() {
  jsmeApplet = new JSApplet.JSME("appletContainer", "380px", "340px", {
    //optional parameters
    "options" : "polarnitro"
  });
document.JME = jsmeApplet;
};

// get and take smiles from jsme editor for input field
function getsmiles() {
  if (document.JME.smiles() != '') {
    document.form.identifier.value = document.JME.smiles() ;
  };
};

// show model details
function loadDetails(id, model_url) {
  button = document.getElementById("link"+id);
  span = button.childNodes[1];
  if (span.className == "fa fa-caret-right"){
    span.className = "fa fa-caret-down";
  } else if (span.className = "fa fa-caret-down"){
    span.className = "fa fa-caret-right";
  };
  image = document.getElementById("circle"+id);
  if ($('modeldetails'+id).length == 0) {
    $(button).hide();
    $(image).show();
    aClient = new HttpClient();
    aClient.get(model_url, function(response) {
      var details = document.createElement("modeldetails"+id);
      details.innerHTML = response;
      document.getElementById("details"+id).appendChild(details);
      $(button).show();
      $(image).hide();
      addExternalLinks();
    });
  }
}


// functions used in batch.haml

var markers = [];

/*progress = function(value,id) {
  var percent = Math.round(value);
  var bar = document.getElementById("bar_"+id);
  var est = document.getElementById("est_"+id);
  var prog = document.getElementById("progress_"+id);
  bar.style.width = value + '%';
  if (percent == 100){
    prog.style.display = "none";
    est.style.display = "none";
  };
};*/

/*remaining = function(id,approximate) {
  var est = document.getElementById("est_"+id);
  var now = new Date().getTime();
  var remain = approximate - now;
  var minutes = Math.floor((remain % (1000 * 60 * 60)) / (1000 * 60));
  var seconds = Math.floor((remain % (1000 * 60)) / 1000);
  if ( minutes <= 0 && seconds <= 0 ) {
    var newtime = "0m " + "00s ";
  } else {
    var newtime = minutes + "m " + seconds + "s ";
  }
  est.innerHTML = newtime;
};*/

uploadDataset = function(task_uri) {
  var uri = task_uri;
  var aClient = new HttpClient();
  aClient.get(uri, function(res) {
    var response = JSON.parse(res);
    if (response['percent'] == 100){
      clearInterval(uploadInterval);
      var element = document.getElementById("uploadDataset");
      element.parentNode.removeChild(element);
      // unset js var
      element = undefined;
      // delete js var
      delete(element);
    };
  });
};

renderTask = function(task_uri,id) {
  var uri = task_uri;
  var aClient = new HttpClient();
  aClient.get(uri, function(res) {
    var response = JSON.parse(res);
    //progress(response['percent'],id);
    if (response['percent'] == 100){
      clearInterval(markers[id]);
      $("a#downbutton_"+id).removeClass("disabled");
      $("a#detailsbutton_"+id).removeClass("disabled");
      $("a#downbutton_"+id).removeClass("btn-outline-info");
      $("a#detailsbutton_"+id).removeClass("btn-outline-info");
      $("a#downbutton_"+id).addClass("btn-info");
      $("a#detailsbutton_"+id).addClass("btn-info");
      $("#circle_"+id).hide();
    };
  });
};

taskProgress = function(idx,timer,task_uri){
  // wait until previous task is completed
  if (idx > 0){
    markers[idx] = setInterval(function(){
      var button = document.getElementById("detailsbutton_"+(idx-1));
      if(!button.classList.contains('disabled')){
        renderTask(task_uri,idx);
        $("#est_"+idx).hide();
        $("#circle_"+idx).show();
      }
    }, timer );
  }else{
    if ( $("#est_"+idx).is(":visible") ){
      $("#est_"+idx).hide();
      $("#circle_"+idx).show();
    }
    renderTask(task_uri,idx);
    markers[idx] = setInterval(function(){
      renderTask(task_uri,idx);
    }, timer );
  };
};

simpleTemplating = function(data) {
  var html = '<ul class=pagination>';
  $.each(data, function(index, item){
    html += '<li>'+ item +'</li>'+'</br>';
  });
  html += '</ul>';
  return html;
};

pagePredictions = function(task_uri,model_id,id, compoundsSize){
  button = document.getElementById("detailsbutton_"+id);
  span = button.childNodes[1];
  if (span.className == "fa fa-caret-right"){
    span.className = "fa fa-caret-down";
    $('#data-container_'+id).removeClass("d-none");
    $('#data-container_'+id).show();
    $('#pager_'+id).show();
    $('#pager_'+id).pagination({
      dataSource: task_uri,
      locator: 'prediction',
      totalNumber: compoundsSize,
      pageSize: 1,
      showPageNumbers: true,
      showGoInput: true,
      formatGoInput: 'go to <%= input %>',
      formatAjaxError: function(jqXHR, textStatus, errorThrown) {
        $('#data-container_'+id).html(errorThrown);
      },
      ajax: {
        beforeSend: function() {
          $('#data-container_'+id).html('Loading content ...');
        }
      },
      callback: function(data, pagination) {
        var html = simpleTemplating(data);
        $('#data-container_'+id).html(html);
        //$('#data-container_'+id).css("min-height", $(window).height() + "px" );
      }
    });
  } else if (span.className = "fa fa-caret-down"){
    span.className = "fa fa-caret-right";
    $('#data-container_'+id).hide();
    $('#pager_'+id).hide();
  };
};