8f0c83368005baeffc3b8e476eb9887899f888e3
[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             commitDocument();
26         }
27     };
28
29     var commitDocument = function() {
30         documentEditedHere = false;
31         wlxmlDocument.loadXML(editor.getValue());
32     };
33     
34     /* globals ace */
35     var editor = ace.edit(view.find('#rng-sourceEditor-editor')[0]),
36         session = editor.getSession();
37     editor.setTheme('ace/theme/chrome');
38     session.setMode('ace/mode/xml');
39     session.setUseWrapMode(true);
40     
41     editor.getSession().on('change', function() {
42         documentEditedHere = true;
43     });
44     return {
45         start: function() {
46             sandbox.publish('ready');
47         },
48         getView: function() {
49             return view;
50         },
51         setDocument: function(document) {
52             wlxmlDocument = document;
53             wlxmlDocument.on('change', function() {
54                 documentIsDirty = true;
55             });
56             wlxmlDocument.on('contentSet', function() {
57                 documentIsDirty = true;
58             });
59         },
60         changesCommited: function() {
61             return !documentEditedHere;
62         },
63         commitChanges: commitDocument,
64         getDocument: function() {
65             return editor.getValue();
66         }
67     };
68 };
69
70 });