From a51c12e65747f50f8e46170893d49475c11b776e Mon Sep 17 00:00:00 2001
From: "Fuentes Serna  Juan Mariano (ID SIS)" <juanf@bs-mbpr28.d.ethz.ch>
Date: Thu, 8 Feb 2018 11:41:15 +0100
Subject: [PATCH] SSDM-6169 - Bugfix, CKEditor breaks when modifing layout

---
 .../html/js/controllers/LayoutManager.js      | 25 ++++++++++++++----
 .../webapps/eln-lims/html/js/util/FormUtil.js | 26 ++++++++++++++-----
 .../js/views/DataSetForm/DataSetFormView.js   |  4 ---
 .../ExperimentForm/ExperimentFormView.js      |  4 ---
 .../js/views/SampleForm/SampleFormView.js     |  4 ---
 5 files changed, 40 insertions(+), 23 deletions(-)

diff --git a/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/controllers/LayoutManager.js b/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/controllers/LayoutManager.js
index 95955113349..7e69a608a4c 100644
--- a/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/controllers/LayoutManager.js
+++ b/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/controllers/LayoutManager.js
@@ -23,7 +23,6 @@ var LayoutManager = {
 	thirdColumn : null,
 	isResizingColumn : false,
 	isLoadingView : false,
-	isBlocked : false,
 	_init : function(isFirstTime) {
 		var _this = this;
 		
@@ -339,8 +338,7 @@ var LayoutManager = {
 		}
 	},
 	canReload : function() {
-		return  this.isBlocked == false && 
-				this.isResizingColumn === false && 
+		return  this.isResizingColumn === false && 
 				this.isLoadingView === false && 
 				this.firstColumn.width() > 0;
 	},
@@ -384,6 +382,23 @@ var LayoutManager = {
 	}
 }
 
+var rtime;
+var timeout = false;
+var delta = 200;
+
 $(window).resize(function() {
-	LayoutManager.resize(mainController.views, true);
-});
\ No newline at end of file
+    rtime = new Date();
+    if (timeout === false) {
+        timeout = true;
+        setTimeout(resizeend, delta);
+    }
+});
+
+function resizeend() {
+    if (new Date() - rtime < delta) {
+        setTimeout(resizeend, delta);
+    } else {
+        timeout = false;
+        LayoutManager.resize(mainController.views, true);
+    }
+}
diff --git a/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/util/FormUtil.js b/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/util/FormUtil.js
index b01023d0775..83fd0a6ef04 100644
--- a/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/util/FormUtil.js
+++ b/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/util/FormUtil.js
@@ -816,12 +816,26 @@ var FormUtil = new function() {
 			$component.change(function(event) {
 				componentOnChange(event, $(this).val());
 			});
-		} else {
-			var editor = $component.ckeditor().editor;
-			editor.on('change', function(event) {
-				var value = event.editor.getData();
-				componentOnChange(event, value);
-			});
+		} else {			
+			var destroyAndRecreate = function($component) {
+				return function() {
+					if($component.editor) {
+						$component.editor.destroy();
+						delete $component.editor;
+					}
+					var editor = $component.ckeditor().editor;
+					editor.on('change', function(event) {
+						var value = event.editor.getData();
+						componentOnChange(event, value);
+					});
+				}
+			}
+			
+			var destroyAndRecreateFunc = destroyAndRecreate($component);
+			
+			destroyAndRecreateFunc();
+			
+			LayoutManager.addResizeEventHandler(destroyAndRecreateFunc);
 		}
 		
 		return $component;
diff --git a/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/views/DataSetForm/DataSetFormView.js b/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/views/DataSetForm/DataSetFormView.js
index f041c1b5252..ac938eb6f73 100644
--- a/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/views/DataSetForm/DataSetFormView.js
+++ b/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/views/DataSetForm/DataSetFormView.js
@@ -22,10 +22,6 @@ function DataSetFormView(dataSetFormController, dataSetFormModel) {
 		var $container = views.content;
 		var _this = this;
 		
-		if(!this._dataSetFormModel.isMini && this._dataSetFormModel.mode !== FormMode.VIEW) {
-			LayoutManager.isBlocked = true; // CKEDITOR owns hooks and variables that will break if the layout manager does its magic
-		}
-		
 		//Clean and prepare container
 		var $wrapper = $('<form>', { 'id' : 'mainDataSetForm', 'role' : 'form'});
 		if(this._dataSetFormModel.isMini) {
diff --git a/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/views/ExperimentForm/ExperimentFormView.js b/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/views/ExperimentForm/ExperimentFormView.js
index ba563f6106b..2963624187d 100644
--- a/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/views/ExperimentForm/ExperimentFormView.js
+++ b/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/views/ExperimentForm/ExperimentFormView.js
@@ -29,10 +29,6 @@ function ExperimentFormView(experimentFormController, experimentFormModel) {
 			'action' : 'javascript:void(0);'
 		});
 		
-		if(this._experimentFormModel.mode !== FormMode.VIEW) {
-			LayoutManager.isBlocked = true; // CKEDITOR owns hooks and variables that will break if the layout manager does its magic
-		}
-		
 		var $rightPanel = null;
 		if(this._experimentFormModel.mode === FormMode.VIEW) {
 			$rightPanel = views.auxContent;
diff --git a/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/views/SampleForm/SampleFormView.js b/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/views/SampleForm/SampleFormView.js
index c3b21666e7b..68e9fc8b2e7 100644
--- a/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/views/SampleForm/SampleFormView.js
+++ b/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/views/SampleForm/SampleFormView.js
@@ -32,10 +32,6 @@ function SampleFormView(sampleFormController, sampleFormModel) {
 			'action' : 'javascript:void(0);'
 		});
 		
-		if(this._sampleFormModel.mode !== FormMode.VIEW) {
-			LayoutManager.isBlocked = true; // CKEDITOR owns hooks and variables that will break if the layout manager does its magic
-		}
-		
 		var $rightPanel = null;
 		if(this._sampleFormModel.mode === FormMode.VIEW) {
 			$rightPanel = views.auxContent;
-- 
GitLab