1 // Module that implements main WYSIWIG edit area
\r
4 'libs/underscore-min',
\r
5 './transformations',
\r
7 'libs/text!./template.html'], function(_, transformations, wlxmlNode, template) {
\r
11 return function(sandbox) {
\r
14 node: $(_.template(template)()),
\r
19 this.node.find('#rng-module-documentCanvas-content').on('keyup', function() {
\r
21 sandbox.publish('contentChanged');
\r
24 this.node.on('mouseover', '[wlxml-tag]', function(e) {
\r
25 e.stopPropagation();
\r
26 sandbox.publish('nodeHovered', new wlxmlNode.Node($(e.target)));
\r
28 this.node.on('mouseout', '[wlxml-tag]', function(e) {
\r
29 e.stopPropagation();
\r
30 sandbox.publish('nodeBlured', new wlxmlNode.Node($(e.target)));
\r
32 this.node.on('click', '[wlxml-tag]', function(e) {
\r
33 e.stopPropagation();
\r
34 console.log('clicked node type: '+e.target.nodeType);
\r
35 view.selectNode(new wlxmlNode.Node($(e.target)));
\r
38 this.node.on('keyup', '#rng-module-documentCanvas-contentWrapper', function(e) {
\r
39 var anchor = $(window.getSelection().anchorNode);
\r
40 if(anchor[0].nodeType === Node.TEXT_NODE)
\r
41 anchor = anchor.parent();
\r
42 if(!anchor.is('[wlxml-tag]'))
\r
44 view.selectNode(new wlxmlNode.Node(anchor));
\r
47 this.node.on('keydown', '#rng-module-documentCanvas-contentWrapper', function(e) {
\r
48 if(e.which === 13) {
\r
50 view.insertNewNode(null, null);
\r
53 this.gridToggled = false;
\r
55 _createNode: function(wlxmlTag, wlxmlClass) {
\r
56 var toBlock = ['div', 'document', 'section', 'header'];
\r
57 var htmlTag = _.contains(toBlock, wlxmlTag) ? 'div' : 'span';
\r
58 var toret = $('<' + htmlTag + '>');
\r
59 toret.attr('wlxml-tag', wlxmlTag);
\r
61 toret.attr('wlxml-class', wlxmlClass);
\r
62 toret.attr('id', 'xxxxxxxx-xxxx-xxxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {var r = Math.random()*16|0,v=c=='x'?r:r&0x3|0x8;return v.toString(16);}));
\r
65 insertNewNode: function(wlxmlTag, wlxmlClass) {
\r
66 //TODO: Insert inline
\r
67 var anchor = $(window.getSelection().anchorNode);
\r
68 var anchorOffset = window.getSelection().anchorOffset;
\r
69 if(anchor[0].nodeType === Node.TEXT_NODE)
\r
70 anchor = anchor.parent();
\r
71 if(anchor.text() === '') {
\r
73 anchor = anchor.parent();
\r
76 if(anchorOffset > 0 && anchorOffset < anchor.text().length) {
\r
77 if(wlxmlTag === null && wlxmlClass === null) {
\r
78 return this.splitWithNewNode(anchor);
\r
80 return this.wrapSelectionWithNewNode(wlxmlTag, wlxmlClass);
\r
82 var newNode = this._createNode(wlxmlTag || anchor.attr('wlxml-tag'), wlxmlClass || anchor.attr('wlxml-class'));
\r
83 if(anchorOffset === 0)
\r
84 anchor.before(newNode)
\r
86 anchor.after(newNode);
\r
87 this.selectNode(newNode);
\r
89 sandbox.publish('contentChanged');
\r
91 wrapSelectionWithNewNode: function(wlxmlTag, wlxmlClass) {
\r
92 var selection = window.getSelection();
\r
93 if(selection.anchorNode === selection.focusNode && selection.anchorNode.nodeType === Node.TEXT_NODE) {
\r
94 var startOffset = selection.anchorOffset;
\r
95 var endOffset = selection.focusOffset;
\r
96 if(startOffset > endOffset) {
\r
97 var tmp = startOffset;
\r
98 startOffset = endOffset;
\r
101 var node = selection.anchorNode;
\r
102 var prefix = node.data.substr(0, startOffset);
\r
103 var suffix = node.data.substr(endOffset);
\r
104 var core = node.data.substr(startOffset, endOffset - startOffset);
\r
105 var newNode = this._createNode(wlxmlTag, wlxmlClass);
\r
106 newNode.text(core || 'test');
\r
107 $(node).replaceWith(newNode);
\r
108 newNode.before(prefix);
\r
109 newNode.after(suffix);
\r
111 this.selectNode(newNode);
\r
113 sandbox.publish('contentChanged');
\r
116 splitWithNewNode: function(node) {
\r
117 var selection = window.getSelection();
\r
118 if(selection.anchorNode === selection.focusNode && selection.anchorNode.nodeType === Node.TEXT_NODE) {
\r
119 var startOffset = selection.anchorOffset;
\r
120 var endOffset = selection.focusOffset;
\r
121 if(startOffset > endOffset) {
\r
122 var tmp = startOffset;
\r
123 startOffset = endOffset;
\r
126 var anchor = selection.anchorNode;
\r
127 var prefix = anchor.data.substr(0, startOffset);
\r
128 var suffix = anchor.data.substr(endOffset);
\r
129 var prefixNode = this._createNode(node.attr('wlxml-tag'), node.attr('wlxml-class'));
\r
130 var newNode = this._createNode(node.attr('wlxml-tag'), node.attr('wlxml-class'));
\r
131 var suffixNode = this._createNode(node.attr('wlxml-tag'), node.attr('wlxml-class'));
\r
132 prefixNode.text(prefix);
\r
133 suffixNode.text(suffix);
\r
134 node.replaceWith(newNode);
\r
135 newNode.before(prefixNode);
\r
136 newNode.after(suffixNode);
\r
138 this.selectNode(newNode);
\r
140 sandbox.publish('contentChanged');
\r
143 setBody: function(HTMLTree) {
\r
144 this.node.find('#rng-module-documentCanvas-content').html(HTMLTree);
\r
146 getBody: function() {
\r
147 return this.node.find('#rng-module-documentCanvas-content').html();
\r
149 selectNode: function(wlxmlNode, options) {
\r
150 options = options || {};
\r
151 var nodeElement = this.getNodeElement(wlxmlNode)
\r
153 this.dimNode(wlxmlNode);
\r
155 this.node.find('.rng-module-documentCanvas-currentNode').removeClass('rng-module-documentCanvas-currentNode');
\r
156 nodeElement.addClass('rng-module-documentCanvas-currentNode');
\r
158 if(options.moveCarret) {
\r
159 var range = document.createRange();
\r
160 range.selectNodeContents(nodeElement[0]);
\r
161 range.collapse(false);
\r
162 var selection = document.getSelection();
\r
163 selection.removeAllRanges()
\r
164 selection.addRange(range);
\r
167 this.currentNode = wlxmlNode;
\r
168 sandbox.publish('nodeSelected', wlxmlNode);
\r
170 highlightNode: function(wlxmlNode) {
\r
171 var nodeElement = this.getNodeElement(wlxmlNode);
\r
172 if(!this.gridToggled) {
\r
173 nodeElement.addClass('rng-common-hoveredNode');
\r
174 var label = nodeElement.attr('wlxml-tag');
\r
175 if(nodeElement.attr('wlxml-class'))
\r
176 label += ' / ' + nodeElement.attr('wlxml-class');
\r
177 var tag = $('<div>').addClass('rng-module-documentCanvas-hoveredNodeTag').text(label);
\r
178 nodeElement.append(tag);
\r
181 dimNode: function(wlxmlNode) {
\r
182 var nodeElement = this.getNodeElement(wlxmlNode);
\r
183 if(!this.gridToggled) {
\r
184 nodeElement.removeClass('rng-common-hoveredNode');
\r
185 nodeElement.find('.rng-module-documentCanvas-hoveredNodeTag').remove();
\r
188 selectFirstNode: function() {
\r
189 var firstNodeWithText = this.node.find('[wlxml-tag]').filter(function() {
\r
190 return $(this).clone().children().remove().end().text().trim() !== '';
\r
193 if(firstNodeWithText.length)
\r
194 node = $(firstNodeWithText[0])
\r
196 node = this.node.find('[wlxml-class|="p"]')
\r
198 this.selectNode(new wlxmlNode.Node(node));
\r
200 toggleGrid: function(toggle) {
\r
201 this.node.find('[wlxml-tag]').toggleClass('rng-common-hoveredNode', toggle);
\r
202 this.gridToggled = toggle;
\r
204 getNodeElement: function(wlxmlNode) {
\r
205 return this.node.find('#'+wlxmlNode.id);
\r
213 start: function() { sandbox.publish('ready'); },
\r
214 getView: function() { return view.node; },
\r
215 setDocument: function(xml) {
\r
216 var transformed = transformations.fromXML.getDocumentDescription(xml);
\r
217 view.setBody(transformed.HTMLTree);
\r
218 view.selectFirstNode();
\r
219 sandbox.publish('documentSet');
\r
221 getDocument: function() {
\r
222 return transformations.toXML.getXML(view.getBody());
\r
224 modifyCurrentNode: function(attr, value) {
\r
225 if(view.currentNode) {
\r
226 view.getNodeElement(view.currentNode).attr('wlxml-'+attr, value);
\r
227 sandbox.publish('contentChanged');
\r
230 highlightNode: function(wlxmlNode) {
\r
231 view.highlightNode(wlxmlNode);
\r
233 dimNode: function(wlxmlNode) {
\r
234 view.dimNode(wlxmlNode);
\r
236 selectNode: function(wlxmlNode) {
\r
237 if(!wlxmlNode.is(view.currentNode))
\r
238 view.selectNode(wlxmlNode, {moveCarret: true});
\r
240 toggleGrid: function(toggle) {
\r
241 view.toggleGrid(toggle);
\r
243 insertNewNode: function(wlxmlTag, wlxmlClass) {
\r
244 view.insertNewNode(wlxmlTag, wlxmlClass);
\r
246 wrapSelectionWithNewNode: function(wlxmlTag, wlxmlClass) {
\r
247 view.wrapSelectionWithNewNode(wlxmlTag, wlxmlClass);
\r