editor: minor refactoring
[fnpeditor.git] / src / editor / modules / sourceEditor / sourceEditor.js
1 define(['libs/jquery', 'libs/text!./template.html'], function($, template) {
2
3 'use strict';
4
5 return function(sandbox) {
6
7     var view = $(template),
8         documentIsDirty = true,
9         documentEditedHere = false,
10         wlxmlDocument;
11
12     view.onShow = function() {
13         if(documentIsDirty) {
14             editor.setValue(wlxmlDocument.toXML());
15             editor.gotoLine(0);
16             documentEditedHere = false;
17
18             sandbox.publish('documentSet');
19             documentIsDirty = false;
20         }
21     };
22
23     view.onHide = function() {
24         if(documentEditedHere) {
25             documentEditedHere = false;
26             wlxmlDocument.loadXML(editor.getValue());
27         }
28     };
29     
30     /* globals ace */
31     var editor = ace.edit(view.find('#rng-sourceEditor-editor')[0]),
32         session = editor.getSession();
33     editor.setTheme('ace/theme/chrome');
34     session.setMode('ace/mode/xml');
35     session.setUseWrapMode(true);
36     
37     editor.getSession().on('change', function() {
38         documentEditedHere = true;
39     });
40     return {
41         start: function() {
42             sandbox.publish('ready');
43         },
44         getView: function() {
45             return view;
46         },
47         setDocument: function(document) {
48             wlxmlDocument = document;
49             wlxmlDocument.on('change', function() {
50                 documentIsDirty = true;
51             });
52             wlxmlDocument.on('contentSet', function() {
53                 documentIsDirty = true;
54             });
55         },
56         getDocument: function() {
57             return editor.getValue();
58         }
59     };
60 };
61
62 });