linting
[fnpeditor.git] / src / editor / modules / sourceEditor / sourceEditor.js
1 define(['libs/jquery'], function($) {
2
3 'use strict';
4
5 return function(sandbox) {
6
7     var view = $(sandbox.getTemplate('main')()),
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     $('textarea', view).on('keyup', function() {
38         documentEditedHere = true;
39     });
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         },
57         getDocument: function() {
58             return editor.getValue();
59         }
60     };
61 };
62
63 });