Newer
Older
anttil
committed
define([
"base/js/dialog",
"./common",
"./state"
],
function (dialog, common, state) {
function writeMetaData(data) {
var notebook = IPython.notebook
if (typeof notebook.metadata.datasets === 'undefined') {
notebook.metadata.datasets = {}
anttil
committed
}
// store metadata about the downloaded files into the notebook-metadata
if (data.permId) {
notebook.metadata.datasets[data.permId] = {
anttil
committed
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
"permId": data.permId,
"path": data.path,
"dataStore": data.dataStore,
"location": data.location,
"size": data.size,
"status": data.statusText
}
}
}
function show_datasets_table(data, datasets_table) {
var table = document.createElement("TABLE")
table.className = "table-bordered table-striped table-condensed text-nowrap"
var thead = table.createTHead()
var t_row = thead.insertRow()
var titles = ['', 'permId', 'Type', 'Experiment', 'Registration Date', 'Status', 'Size']
titles.forEach(function (title) {
t_row.insertCell().textContent = title
})
var tbody = table.createTBody()
for (dataSet of data.dataSets) {
var permId = document.createElement("INPUT")
permId.type = "checkbox"
permId.name = "permId"
permId.value = dataSet.permId
var row = tbody.insertRow()
row.insertCell().appendChild(permId)
row.insertCell().textContent = dataSet.permId
row.insertCell().textContent = dataSet.type
row.insertCell().textContent = dataSet.experiment
row.insertCell().textContent = dataSet.registrationDate
row.insertCell().textContent = dataSet.status
row.insertCell().textContent = dataSet.size
}
while (datasets_table.firstChild) {
datasets_table.removeChild(datasets_table.firstChild);
}
datasets_table.appendChild(table)
}
return {
help: 'Download openBIS datasets to your local harddrive',
icon: 'fa-download',
help_index: '',
handler: function (env) {
conn_table = document.createElement("DIV")
conn_table.id = "openbis_connections"
var showDataSets = document.createElement("DIV")
var title = document.createElement("STRONG")
Swen Vermeul
committed
title.textContent = "Sample or Experiment identifier/permId: "
anttil
committed
showDataSets.appendChild(title)
showDataSets.style.marginTop = '10px'
Swen Vermeul
committed
var entityIdentifier = document.createElement("INPUT")
entityIdentifier.type = "text"
entityIdentifier.name = "entityIdentifier"
entityIdentifier.size = 40
entityIdentifier.placeholder = "Sample or Experiment identifier/permId"
entityIdentifier.value = ''
anttil
committed
var datasets_table = document.createElement("DIV")
datasets_table.id = "dataset_table"
datasets_table.className = "output output_scroll"
datasets_table.style.maxHeight = "10em"
var show_datasets_btn = document.createElement("BUTTON")
show_datasets_btn.className = "btn-info btn-xs"
show_datasets_btn.textContent = "show datasets"
show_datasets_btn.onclick = function () {
var selected_conn = state.connection.name
if (!selected_conn) {
alert('Please choose a connection')
return false
}
connection_name = state.connection.name
currentConnection = connection_name
Swen Vermeul
committed
currentEntityIdentifier = entityIdentifier.value
if (!currentEntityIdentifier) {
alert('Please specify a Sample or Experiment identifier/permId')
anttil
committed
return false
}
Swen Vermeul
committed
var url = env.notebook.base_url + 'openbis/sample/' + connection_name + '/' + encodeURIComponent(currentEntityIdentifier)
anttil
committed
fetch(url)
.then(function (response) {
if (response.ok) {
response.json()
.then(function (data) {
show_datasets_table(data, datasets_table)
})
} else {
response.json()
.then(function (error) {
console.log(error.reason)
alert("Error: " + error.reason)
})
}
})
.catch(function (error) {
console.error('A serious network problem occured:', error)
})
}
Swen Vermeul
committed
showDataSets.appendChild(entityIdentifier)
anttil
committed
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
showDataSets.appendChild(show_datasets_btn)
showDataSets.appendChild(datasets_table)
// dataSetPermId only
var dataset_direct = document.createElement("P")
dataset_direct.style.marginTop = '10px'
dataset_direct.innerHTML = '<strong>enter DataSet permId directly: </strong>'
//var dataset = $('<p>')
// .css('margin-top', '10px')
// .append($('<b>').text('... or enter DataSet permId directly: '))
var datasetPermId = document.createElement("INPUT")
datasetPermId.type = "text"
datasetPermId.name = "datasetPermId"
datasetPermId.size = "40"
datasetPermId.placeholder = "dataSet permId"
dataset_direct.appendChild(datasetPermId)
var downloadPath = document.createElement("INPUT")
downloadPath.type = "text"
downloadPath.name = "downloadPath"
downloadPath.size = "90"
downloadPath.value = ''
var path = document.createElement("DIV")
path.innerHTML = "<strong>download data to path: </strong>"
path.appendChild(downloadPath)
var download_dialog_box = document.createElement("DIV")
download_dialog_box.appendChild(conn_table)
download_dialog_box.appendChild(showDataSets)
download_dialog_box.appendChild(dataset_direct)
download_dialog_box.appendChild(path)
function downloadDataset(connection_name, selectedPermIds, downloadPath) {
for (permId of selectedPermIds) {
var downloadUrl = env.notebook.base_url + 'openbis/dataset/' +
connection_name + '/' + permId + '/' + encodeURIComponent(downloadPath)
fetch(downloadUrl)
.then(function (response) {
if (response.ok) {
response.json()
.then(function (data) {
common.createFeedback('success', data.statusText)
// successful download:
// write statusText from returned data to notebooks metadata
writeMetaData(data)
// keep current download path for later use
currentDownloadPath = downloadPath
})
} else {
response.json()
.then(function (error) {
console.log(error.reason)
alert("Error: " + error.reason)
})
}
})
.catch(function (error) {
console.error('A serious network problem occured:', error)
})
}
}
function onDownloadClick() {
var selected_conn = state.connection.name
if (!selected_conn) {
alert('please choose a connection')
return false
}
var selectedPermIds = []
for (row of document.querySelectorAll('input[name=permId]:checked')) {
selectedPermIds.push(row.value)
}
if (datasetPermId.value) {
selectedPermIds.push(datasetPermId.value)
}
if (!selectedPermIds) {
alert('please select a dataset or provide a permId')
return false
}
if (!downloadPath.value) {
alert('Please specify where you would like to download your files!')
return false
}
downloadDataset(selected_conn, selectedPermIds, downloadPath.value)
}
dialog.modal({
body: download_dialog_box,
title: 'Download openBIS DataSets',
buttons: {
'Cancel': {},
'Download': {
class: 'btn-primary btn-large',
click: onDownloadClick,
}
},
notebook: env.notebook,
keyboard_manager: env.notebook.keyboard_manager
})
}
}
}
)