From 453937d89b04a0a35fa182e706d4aa7948044246 Mon Sep 17 00:00:00 2001
From: srosse <none@none>
Date: Mon, 26 Aug 2013 12:14:31 +0200
Subject: [PATCH] OO-696: fix some issues with add/remove from tinymce editors

---
 .../richText/RichTextConfiguration.java        |  2 ++
 .../richText/RichTextElementRenderer.java      | 13 ++++++++-----
 .../webapp/static/js/tinymce4/BTinyHelper.js   | 18 +++++++++---------
 3 files changed, 19 insertions(+), 14 deletions(-)

diff --git a/src/main/java/org/olat/core/gui/components/form/flexible/impl/elements/richText/RichTextConfiguration.java b/src/main/java/org/olat/core/gui/components/form/flexible/impl/elements/richText/RichTextConfiguration.java
index 65f41ac64f2..0d62e80a358 100644
--- a/src/main/java/org/olat/core/gui/components/form/flexible/impl/elements/richText/RichTextConfiguration.java
+++ b/src/main/java/org/olat/core/gui/components/form/flexible/impl/elements/richText/RichTextConfiguration.java
@@ -1584,6 +1584,8 @@ public class RichTextConfiguration implements Disposable {
 		copyValues.remove("theme_advanced_toolbar_align");
 		copyValues.remove("dialog_type");
 		copyValues.remove("mode");
+		copyValues.remove("elements");
+		
 		
 		//update value from 3 to 4
 		String tabfocus = copyValues.remove("tab_focus");
diff --git a/src/main/java/org/olat/core/gui/components/form/flexible/impl/elements/richText/RichTextElementRenderer.java b/src/main/java/org/olat/core/gui/components/form/flexible/impl/elements/richText/RichTextElementRenderer.java
index 0fac0f0e552..7db5ba4d7af 100644
--- a/src/main/java/org/olat/core/gui/components/form/flexible/impl/elements/richText/RichTextElementRenderer.java
+++ b/src/main/java/org/olat/core/gui/components/form/flexible/impl/elements/richText/RichTextElementRenderer.java
@@ -116,7 +116,7 @@ class RichTextElementRenderer implements ComponentRenderer {
 		List<String> onInit = config.getOnInit();
 
 		// Read write view
-		renderTextarea(sb, domID + "_TEXTAREA", teC);
+		renderTextarea(sb, domID, teC);
 
 		StringOutput configurations = new StringOutput();
 		config.appendConfigToTinyJSArray_4(configurations);
@@ -126,16 +126,19 @@ class RichTextElementRenderer implements ComponentRenderer {
 
 		sb.append("<script type='text/javascript'>/* <![CDATA[ */ ");
 		//file browser url
-		sb.append("BTinyHelper.editorMediaUris.put('").append(domID).append("_TEXTAREA','");
+		sb.append("  BTinyHelper.editorMediaUris.put('").append(domID).append("','");
 		ubu.buildURI(sb, null, null);
 		sb.append("');");
+		//remove if a instance is there
+		sb.append("  BTinyHelper.stopFormDirtyObserver('" + te.getRootForm().getDispatchFieldId() + "','" + domID + "');");
+		sb.append("  BTinyHelper.removeEditorInstance('").append(domID).append("');");
 		
-		sb.append("  jQuery('#").append(domID).append("_TEXTAREA').tinymce({\n")
-		  .append("    selector: '#").append(domID).append("_TEXTAREA',\n")
+		sb.append("  jQuery('#").append(domID).append("').tinymce({\n")
+		  .append("    selector: '#").append(domID).append("',\n")
 		  .append("    script_url: '").append(baseUrl.toString()).append("',\n")
 		  .append("    setup: function(ed){\n")
 		  .append("      ed.on('init', function(e) {\n")
-		  .append("        ").append(onInit.get(0)).append(";\n")
+		  .append("        ").append(onInit.get(0).replace(".curry(", "(")).append(";\n")
 		  .append("      });\n")
 		  .append("      ed.on('change', function(e) {\n")
 		  .append("        BTinyHelper.triggerOnChange('").append(domID).append("');\n")
diff --git a/src/main/webapp/static/js/tinymce4/BTinyHelper.js b/src/main/webapp/static/js/tinymce4/BTinyHelper.js
index 21f3aed86ce..63e16dffa3e 100644
--- a/src/main/webapp/static/js/tinymce4/BTinyHelper.js
+++ b/src/main/webapp/static/js/tinymce4/BTinyHelper.js
@@ -71,7 +71,7 @@ var BTinyHelper = {
 	// - absolute links: media an links to external sites
 	linkConverter : function (url, node, on_save) {
 		var orig = url + '';
-		var editor = tinyMCE.activeEditor;
+		var editor = top.tinymce.activeEditor;
 		var settings = editor.settings;
 		if (!settings.convert_urls || (node && node.nodeName == 'LINK') || url.indexOf('file:') === 0) {
 			// Don't convert link href since thats the CSS files that gets loaded into the editor also skip local file URLs
@@ -118,16 +118,16 @@ var BTinyHelper = {
 		var observerKey = formId + '-' + elementId;
 		
 		// Check for dirtyness and mark buttons accordingly, each second
-		var newExecutor = jQuery.periodic({period: 500, decay:1.0, max_period: Number.MAX_VALUE}, function(executor) {
+		var newExecutor = jQuery.periodic({period: 500, decay:1.0, max_period: Number.MAX_VALUE}, function() {
 			// first check if the html editor still exists on this page, otherwhise stop executing this code
 			var elem = jQuery('#' + elementId);
-			if (!elem) {
-				executor.cancel();
+			if (elem.length == 0) {
+				newExecutor.cancel();
 				BTinyHelper.formDirtyObservers.remove(observerKey);
 				return;
 			}
-			if (tinyMCE && tinyMCE.activeEditor) {
-				if (tinyMCE.activeEditor.isDirty()) {
+			if (top.tinymce && top.tinymce.activeEditor) {
+				if (top.tinymce.activeEditor.isDirty()) {
 					setFlexiFormDirty(formId);
 				}
 			}		
@@ -139,8 +139,8 @@ var BTinyHelper = {
 	// Remove the editor instance for the given DOM node ID if such an editor exists.
 	// Remove all event handlers and release the memory
 	removeEditorInstance : function (elementId) {
-		if (tinyMCE) {
-			var oldE = tinyMCE.get(elementId);
+		if (top.tinymce) {
+			var oldE = top.tinymce.get(elementId);
 			if (oldE != null) { 
 				try { 					
 					// first try to remove and cleanup editor instance itself
@@ -152,7 +152,7 @@ var BTinyHelper = {
 				}
 				try { 					
 					// second remove editor instance from tiny editorManager 
-					tinyMCE.remove(oldE); 
+					top.tinymce.remove(oldE); 
 				} catch(e) {
 					// IE (of course) has some issues here, need to silently catch those 
 					//console.log('could not removeEditorInstance::' + e.message)
-- 
GitLab