From fb6884154a50de80e6d67024f5c794586dd0f4a9 Mon Sep 17 00:00:00 2001
From: Juan Fuentes <juanf@bs-mbpr28.d.ethz.ch>
Date: Mon, 20 May 2019 15:30:38 +0200
Subject: [PATCH] setting history id when is available on the jupyter notebook
 and the selected dataset type

---
 .../static/uploadDialog.js                    | 28 ++++++++++++++++++-
 1 file changed, 27 insertions(+), 1 deletion(-)

diff --git a/jupyter-openbis-extension/static/uploadDialog.js b/jupyter-openbis-extension/static/uploadDialog.js
index 9f848ee..6b0ac72 100644
--- a/jupyter-openbis-extension/static/uploadDialog.js
+++ b/jupyter-openbis-extension/static/uploadDialog.js
@@ -58,6 +58,30 @@ define([
             }
         }
 
+        function getOpenBISHistoryId() {
+            var resultDatasetHistoryId = "";
+            if(Jupyter && 
+                Jupyter.notebook && 
+                Jupyter.notebook.metadata && 
+                Jupyter.notebook.metadata.openbisHistoryId) {
+                resultDatasetHistoryId = Jupyter.notebook.metadata.openbisHistoryId;
+            }
+            if (!resultDatasetHistoryId) {
+                var resultDatasetHistoryIdIdx = 0;
+                while(Jupyter.notebook.get_cell(resultDatasetHistoryIdIdx) != null) {
+                    var cell = Jupyter.notebook.get_cell(resultDatasetHistoryIdIdx);
+                    var cellText = cell.get_text();
+                    if(cell.get_text().startsWith("resultDatasetHistoryId='")) {
+                        var firstIndexOf = cell.get_text().indexOf("'");
+                        var lastIndexOf = cell.get_text().indexOf("'", firstIndexOf + 1);
+                        resultDatasetHistoryId = cell.get_text().substring(firstIndexOf + 1, lastIndexOf);
+                    }
+                    resultDatasetHistoryIdIdx++;
+                }
+            }
+            return resultDatasetHistoryId;
+        }
+
         function getDatasetTypes(env, connection_name, dataset_types, input_fields) {
             // get all DatasetTypes of a given connection
 
@@ -104,7 +128,9 @@ define([
 
                                         var mem = state.uploadDataSetTypes[dts[dataset_types.selectedIndex].code]
                                         if (mem == null) {
-                                            mem = {}
+                                            mem = {
+                                                "$HISTORY_ID" : getOpenBISHistoryId()
+                                            }
                                         }
                                         input_field.value = pa.code in mem ? mem[pa.code] : ""
 
-- 
GitLab