list fixes: headers, invalid elements, easier ending
[fnpeditor.git] / src / editor / modules / sourceEditor / sourceEditor.js
1 define(['libs/jquery', 'libs/ace/ace', 'libs/text!./template.html'], function($, ace, 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             documentIsDirty = false;
19         }
20     };
21
22     view.onHide = function() {
23         if(documentEditedHere) {
24             commitDocument();
25         }
26     };
27
28     var commitDocument = function() {
29         documentEditedHere = false;
30         wlxmlDocument.loadXML(editor.getValue());
31     };
32
33     var editor = ace.edit(view.find('#rng-sourceEditor-editor')[0]),
34         session = editor.getSession();
35     session.setMode('ace/mode/xml');
36     session.setUseWrapMode(true);
37     
38     editor.getSession().on('change', function() {
39         documentEditedHere = true;
40     });
41     return {
42         start: function() {
43             sandbox.publish('ready');
44         },
45         getView: function() {
46             return view;
47         },
48         setDocument: function(document) {
49             wlxmlDocument = document;
50             wlxmlDocument.on('change', function() {
51                 documentIsDirty = true;
52             });
53             wlxmlDocument.on('contentSet', function() {
54                 documentIsDirty = true;
55             });
56         },
57         changesCommited: function() {
58             return !documentEditedHere;
59         },
60         commitChanges: commitDocument,
61         getDocument: function() {
62             return editor.getValue();
63         }
64     };
65 };
66
67 });