aboutsummaryrefslogtreecommitdiff
path: root/bh20simplewebuploader/static/main.js
blob: a67d3dfac9e77c28ed3a6644cf9d20f2e3dc07e3 (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
function fetchAPI(apiEndPoint) {
  fetch(scriptRoot + apiEndPoint)
    .then(response => {
      return response.json();
    })
    .then(data => {
      document.getElementById("json").textContent = JSON.stringify(data, undefined, 2);
      document.getElementById("results").classList.remove("invisible");
      document.getElementById("loader").classList.add("invisible");
    });
  document.getElementById("results").classList.add("invisible");
  document.getElementById("loader").classList.remove("invisible");

}

let search = () => {
  let m =  document.getElementById('search-input').value;
  fetchAPI(scriptRoot + "/api/getDetailsForSeq?seq=" + encodeURIComponent(m));
}

let fetchSEQBySpecimen = () => {
  fetchAPI("/api/getSEQCountbySpecimenSource");
}

let fetchSEQByLocation = () => {
  fetchAPI("/api/getSEQCountbyLocation");
}

let fetchSEQByTech = () => {
  fetchAPI("/api/getSEQCountbytech");
}

let fetchAllaccessions = () => {
  fetchAPI("/api/getAllaccessions");
};

/**
 * Show form if checked
 */
let fillFormSpot = document.getElementById('metadata_fill_form_spot');
function displayForm() {
  if (document.getElementById('metadata_form').checked) {
    fillFormSpot.classList.remove("invisible");
    return;
  }
  fillFormSpot.classList.add("invisible");
}

/**
 * Add another form field to the group this button is part of.
 */
function addField(e) {
  // Find our parent field-group div
  let fieldGroup = this.parentElement
  
  // Get its keypath
  let keypath = fieldGroup.dataset.keypath
  
  // Find its last field child
  let existingFields = fieldGroup.getElementsByClassName('field')
  let templateField = existingFields[existingFields.length - 1]
  
  // Get its number
  let fieldNumber = templateField.dataset.number
  
  // Duplicate it
  let newField = templateField.cloneNode(true)
  
  // Increment the number and use the keypath and number to set IDs and cross
  // references.
  // TODO: Heavily dependent on the form field HTML. Maybe we want custom
  // elements for the labeled controlsd that know how to be list items?
  fieldNumber++
  newField.dataset.number = fieldNumber
  let newID = keypath + '[' + fieldNumber + ']'
  let newControl = newField.getElementsByClassName('control')[0]
  newControl.id = newID
  newControl.setAttribute('name', newID)
  let newLabel = newField.getElementsByTagName('label')[0]
  newLabel.setAttribute('for', newID)
  
  // Find the minus button
  let minusButton = fieldGroup.getElementsByClassName('remove-field')[0]
  
  // Put new field as a child before the minus button
  fieldGroup.insertBefore(newField, minusButton)
  
  // Enable the minus button
  minusButton.classList.remove('hidden')
}

/**
 * Remove the last form field from the group button is part of.
 */
function removeField(e) {
  // Find our parent field-group div
  let fieldGroup = this.parentElement
  
  // Find its field children
  let existingFields = fieldGroup.getElementsByClassName('field')
  
  if (existingFields.length > 1) {
    // There is a last field we can safely remove.
    let lastField = existingFields[existingFields.length - 1]
    fieldGroup.removeChild(lastField)
  }
  
  if (existingFields.length <= 1) {
    // Collection auto-updates. Now there's only one element. Don't let the
    // user remove it. If they don't want it, they can leave it empty.
    this.classList.add('hidden')
  }
}

// Find all the add and remove field buttons and hook up the listeners.
for (let button of document.getElementsByClassName('add-field')) {
  button.addEventListener('click', addField)
}
for (let button of document.getElementsByClassName('remove-field')) {
  button.addEventListener('click', removeField)
}