From ea7b9c66660ca83ce0f122341e90a752ba8d18f1 Mon Sep 17 00:00:00 2001
From: Antti Luomi <antti.luomi@id.ethz.ch>
Date: Wed, 20 Mar 2019 15:41:38 +0100
Subject: [PATCH] Remember checkbox states in upload dialog

---
 jupyter-openbis-extension/static/state.js        |  6 ++++--
 jupyter-openbis-extension/static/uploadDIalog.js | 15 +++++++++------
 2 files changed, 13 insertions(+), 8 deletions(-)

diff --git a/jupyter-openbis-extension/static/state.js b/jupyter-openbis-extension/static/state.js
index b638056..e5008e9 100644
--- a/jupyter-openbis-extension/static/state.js
+++ b/jupyter-openbis-extension/static/state.js
@@ -8,8 +8,10 @@ define([],
             uploadDataSetType: null,
             uploadDataSetTypes: {},
             uploadEntityIdentifier: '',
-            getSelectedDataSets: () => [],
-            getSelectedFiles: () => []
+            datasetCheckboxes: [],
+            fileCheckboxes: [],
+            selectedFiles: [],
+            unselectedDatasets: []
         }
     }
 )
\ No newline at end of file
diff --git a/jupyter-openbis-extension/static/uploadDIalog.js b/jupyter-openbis-extension/static/uploadDIalog.js
index 55e73c0..c32a163 100644
--- a/jupyter-openbis-extension/static/uploadDIalog.js
+++ b/jupyter-openbis-extension/static/uploadDIalog.js
@@ -40,7 +40,7 @@ define([
                             .then(function(data){
                                 var values = Object.keys(data.files)
                                 values.sort()
-                                state.getSelectedFiles = createSelectTable(values, container, false)
+                                state.fileCheckboxes = createSelectTable(values, container, false, state.selectedFiles)
                             })
                     }
                     else {
@@ -55,7 +55,7 @@ define([
             if (datasets != null) {
                 var values = Object.keys(datasets)
                 values.sort()
-                state.getSelectedDataSets = createSelectTable(values, container, true)
+                state.datasetCheckboxes = createSelectTable(values, container, true, state.unselectedDatasets)
             }
         }
 
@@ -156,7 +156,7 @@ define([
                 })
         }
 
-        function createSelectTable(values, container, checked) {
+        function createSelectTable(values, container, checked, overrides) {
 
             var table = document.createElement("TABLE")
             table.className = 'table-bordered table-striped table-condensed'
@@ -170,7 +170,7 @@ define([
                 var checkbox = document.createElement("INPUT")
                 checkbox.type = "checkbox"
                 checkbox.value = value
-                checkbox.checked = checked
+                checkbox.checked = overrides.includes(value) ? !checked :  checked
                 checkboxes.push(checkbox)
                 row.insertCell().appendChild(checkbox)
                 var valueCell = row.insertCell()
@@ -179,7 +179,7 @@ define([
             })
             container.appendChild(table)
 
-            return () => checkboxes.filter(cb => cb.checked).map(cb => cb.value)
+            return checkboxes
         }
 
         return {
@@ -255,6 +255,8 @@ define([
                         }
                     }
                     state.uploadEntityIdentifier = sampleIdentifier.value
+                    state.unselectedDatasets = state.datasetCheckboxes.filter(cb => !cb.checked).map(cb => cb.value)
+                    state.selectedFiles = state.fileCheckboxes.filter(cb => cb.checked).map(cb => cb.value)
                 }
 
                 function onOk() {
@@ -268,7 +270,7 @@ define([
                     var uploadUrl = env.notebook.base_url + 'openbis/dataset/' + connection_name
 
                     var notebook = IPython.notebook
-                    var files = state.getSelectedFiles()
+                    var files = state.fileCheckboxes.filter(cb => cb.checked).map(cb => cb.value)
                     var re = /\/notebooks\/(.*?)$/
                     var filepath = window.location.pathname.match(re)[1]
                     files.push(filepath)
@@ -281,6 +283,7 @@ define([
                     var dataSetInfo = {
                         "type": dataset_types.value,
                         "files": files,
+                        "parents": state.datasetCheckboxes.filter(cb => cb.checked).map(cb => cb.value),
                         "sampleIdentifier": sampleIdentifier.value,
                         "props": props
                     }
-- 
GitLab