editor: fix canvas wlxml listener nodeMoved event handling
[fnpeditor.git] / src / editor / modules / sourceEditor / sourceEditor.js
index 2e2e6ab..1930939 100644 (file)
@@ -1,23 +1,41 @@
-define(function() {
+define(['libs/jquery', 'libs/text!./template.html'], function($, template) {
 
 'use strict';
 
 return function(sandbox) {
 
-    var view = $(sandbox.getTemplate('main')());
+    var view = $(template),
+        documentIsDirty = true,
+        documentEditedHere = false,
+        wlxmlDocument;
+
+    view.onShow = function() {
+        if(documentIsDirty) {
+            editor.setValue(wlxmlDocument.toXML());
+            editor.gotoLine(0);
+            documentEditedHere = false;
+
+            sandbox.publish('documentSet');
+            documentIsDirty = false;
+        }
+    };
+
+    view.onHide = function() {
+        if(documentEditedHere) {
+            documentEditedHere = false;
+            wlxmlDocument.loadXML(editor.getValue());
+        }
+    };
     
+    /* globals ace */
     var editor = ace.edit(view.find('#rng-sourceEditor-editor')[0]),
         session = editor.getSession();
-    editor.setTheme("ace/theme/chrome");
-    session.setMode("ace/mode/xml")
+    editor.setTheme('ace/theme/chrome');
+    session.setMode('ace/mode/xml');
     session.setUseWrapMode(true);
     
-    $('textarea', view).on('keyup', function() {
-        sandbox.publish('xmlChanged');
-    });
-    
     editor.getSession().on('change', function() {
-        sandbox.publish('xmlChanged');
+        documentEditedHere = true;
     });
     return {
         start: function() {
@@ -27,9 +45,13 @@ return function(sandbox) {
             return view;
         },
         setDocument: function(document) {
-            editor.setValue(document.toXML());
-            editor.gotoLine(0);
-            sandbox.publish('documentSet');
+            wlxmlDocument = document;
+            wlxmlDocument.on('change', function() {
+                documentIsDirty = true;
+            });
+            wlxmlDocument.on('contentSet', function() {
+                documentIsDirty = true;
+            });
         },
         getDocument: function() {
             return editor.getValue();