5 'modules/documentCanvas/canvas/documentElement',
6 'modules/documentCanvas/canvas/keyboard',
7 'modules/documentCanvas/canvas/utils',
8 'modules/documentCanvas/canvas/wlxmlListener'
9 ], function($, _, Backbone, documentElement, keyboard, utils, wlxmlListener) {
12 /* global document:false, window:false */
15 var TextHandler = function(canvas) {this.canvas = canvas; this.buffer = null;};
16 $.extend(TextHandler.prototype, {
17 handle: function(node, text) {
18 this.setText(text, node);
23 // if(this.node.sameNode(node)) {
31 _ping: _.throttle(function(text) {
36 if(this.buffer !== null) {
37 this.setText(this.buffer, this.node);
41 setText: function(text, node) {
42 //this.canvas.wlxmlDocument.transform('setText', {node:node, text: text});
50 var Canvas = function(wlxmlDocument, publisher) {
51 this.eventBus = _.extend({}, Backbone.Events);
52 this.wrapper = $('<div>').addClass('canvas-wrapper').attr('contenteditable', true);
53 this.wlxmlListener = wlxmlListener.create(this);
54 this.loadWlxmlDocument(wlxmlDocument);
55 this.publisher = publisher ? publisher : function() {};
56 this.textHandler = new TextHandler(this);
59 $.extend(Canvas.prototype, {
61 loadWlxmlDocument: function(wlxmlDocument) {
66 this.wlxmlListener.listenTo(wlxmlDocument);
67 this.wlxmlDocument = wlxmlDocument;
69 this.setupEventHandling();
72 reloadRoot: function() {
73 var canvasDOM = this.generateCanvasDOM(this.wlxmlDocument.root);
74 //var canvasDOM = this.wlxmlDocument.root.getData('canvasElement') ? this.wlxmlDocument.root.getData('canvasElement').dom() : this.generateCanvasDOM(this.wlxmlDocument.root);
77 this.wrapper.append(canvasDOM);
78 this.d = this.wrapper.children(0);
81 generateCanvasDOM: function(wlxmlNode) {
82 var element = documentElement.DocumentNodeElement.create(wlxmlNode, this);
86 setupEventHandling: function() {
88 this.wrapper.on('keyup keydown keypress', function(e) {
89 keyboard.handleKey(e, this);
92 this.wrapper.on('click', '[document-node-element], [document-text-element]', function(e) {
94 canvas.setCurrentElement(canvas.getDocumentElement(e.currentTarget), {caretTo: false});
98 /* globals MutationObserver */
99 var observer = new MutationObserver(function(mutations) {
100 mutations.forEach(function(mutation) {
101 if(documentElement.DocumentTextElement.isContentContainer(mutation.target)) {
102 observer.disconnect();
103 if(mutation.target.data === '') {
104 mutation.target.data = utils.unicode.ZWS;
106 else if(mutation.oldValue === utils.unicode.ZWS) {
107 mutation.target.data = mutation.target.data.replace(utils.unicode.ZWS, '');
108 canvas._moveCaretToTextElement(canvas.getDocumentElement(mutation.target), 'end');
110 observer.observe(canvas.wrapper[0], config);
111 canvas.publisher('contentChanged');
113 var textElement = canvas.getDocumentElement(mutation.target),
114 toSet = mutation.target.data !== utils.unicode.ZWS ? mutation.target.data : '';
116 //textElement.data('wlxmlNode').setText(toSet);
117 //textElement.data('wlxmlNode').document.transform('setText', {node: textElement.data('wlxmlNode'), text: toSet});
118 if(textElement.data('wlxmlNode').getText() !== toSet) {
119 canvas.textHandler.handle(textElement.data('wlxmlNode'), toSet);
124 var config = { attributes: false, childList: false, characterData: true, subtree: true, characterDataOldValue: true};
125 observer.observe(this.wrapper[0], config);
128 this.wrapper.on('mouseover', '[document-node-element], [document-text-element]', function(e) {
129 var el = canvas.getDocumentElement(e.currentTarget);
134 if(el instanceof documentElement.DocumentTextElement) {
137 el.toggleLabel(true);
139 this.wrapper.on('mouseout', '[document-node-element], [document-text-element]', function(e) {
140 var el = canvas.getDocumentElement(e.currentTarget);
145 if(el instanceof documentElement.DocumentTextElement) {
148 el.toggleLabel(false);
151 this.eventBus.on('elementToggled', function(toggle, element) {
153 canvas.setCurrentElement(element.getPreviousTextElement());
163 if(this.d === null) {
166 return documentElement.DocumentNodeElement.fromHTMLElement(this.d.get(0), this); //{wlxmlTag: this.d.prop('tagName')};
169 toggleElementHighlight: function(node, toggle) {
170 var element = utils.findCanvasElement(node);
171 element.toggleHighlight(toggle);
174 createNodeElement: function(params) {
175 return documentElement.DocumentNodeElement.create(params, this);
178 getDocumentElement: function(from) {
179 /* globals HTMLElement, Text */
180 if(from instanceof HTMLElement || from instanceof Text) {
181 return documentElement.DocumentElement.fromHTMLElement(from, this);
184 getCursor: function() {
185 return new Cursor(this);
189 getCurrentNodeElement: function() {
190 return this.getDocumentElement(this.wrapper.find('.current-node-element').parent()[0]);
193 getCurrentTextElement: function() {
194 return this.getDocumentElement(this.wrapper.find('.current-text-element')[0]);
199 setCurrentElement: function(element, params) {
200 if(!(element instanceof documentElement.DocumentElement)) {
201 element = utils.findCanvasElement(element);
204 params = _.extend({caretTo: 'end'}, params);
205 var findFirstDirectTextChild = function(e, nodeToLand) {
206 var byBrowser = this.getCursor().getPosition().element;
207 if(byBrowser && byBrowser.parent().sameNode(nodeToLand)) {
210 var children = e.children();
211 for(var i = 0; i < children.length; i++) {
212 if(children[i] instanceof documentElement.DocumentTextElement) {
218 var _markAsCurrent = function(element) {
219 if(element instanceof documentElement.DocumentTextElement) {
220 this.wrapper.find('.current-text-element').removeClass('current-text-element');
221 element.dom().addClass('current-text-element');
223 this.wrapper.find('.current-node-element').removeClass('current-node-element');
224 element._container().addClass('current-node-element');
225 this.publisher('currentElementChanged', element);
230 var isTextElement = element instanceof documentElement.DocumentTextElement,
231 nodeElementToLand = isTextElement ? element.parent() : element,
232 textElementToLand = isTextElement ? element : findFirstDirectTextChild(element, nodeElementToLand),
233 currentTextElement = this.getCurrentTextElement(),
234 currentNodeElement = this.getCurrentNodeElement();
236 if(currentTextElement && !(currentTextElement.sameNode(textElementToLand))) {
237 this.wrapper.find('.current-text-element').removeClass('current-text-element');
240 if(textElementToLand) {
241 _markAsCurrent(textElementToLand);
242 if(params.caretTo || !textElementToLand.sameNode(this.getCursor().getPosition().element)) {
243 this._moveCaretToTextElement(textElementToLand, params.caretTo); // as method on element?
245 if(!(textElementToLand.sameNode(currentTextElement))) {
246 this.publisher('currentTextElementSet', textElementToLand.data('wlxmlNode'));
249 document.getSelection().removeAllRanges();
252 if(!(currentNodeElement && currentNodeElement.sameNode(nodeElementToLand))) {
253 _markAsCurrent(nodeElementToLand);
255 this.publisher('currentNodeElementSet', nodeElementToLand.data('wlxmlNode'));
259 _moveCaretToTextElement: function(element, where) {
260 var range = document.createRange(),
261 node = element.dom().contents()[0];
263 if(typeof where !== 'number') {
264 range.selectNodeContents(node);
266 range.setStart(node, where);
269 var collapseArg = true;
270 if(where === 'end') {
273 range.collapse(collapseArg);
275 var selection = document.getSelection();
277 selection.removeAllRanges();
278 selection.addRange(range);
279 this.wrapper.focus(); // FF requires this for caret to be put where range colllapses, Chrome doesn't.
282 setCursorPosition: function(position) {
283 if(position.element) {
284 this._moveCaretToTextElement(position.element, position.offset);
290 var Cursor = function(canvas) {
291 this.canvas = canvas;
294 $.extend(Cursor.prototype, {
295 isSelecting: function() {
296 var selection = window.getSelection();
297 return !selection.isCollapsed;
299 isSelectingWithinElement: function() {
300 return this.isSelecting() && this.getSelectionStart().element.sameNode(this.getSelectionEnd().element);
302 isSelectingSiblings: function() {
303 return this.isSelecting() && this.getSelectionStart().element.parent().sameNode(this.getSelectionEnd().element.parent());
305 getPosition: function() {
306 return this.getSelectionAnchor();
308 getSelectionStart: function() {
309 return this.getSelectionBoundry('start');
311 getSelectionEnd: function() {
312 return this.getSelectionBoundry('end');
314 getSelectionAnchor: function() {
315 return this.getSelectionBoundry('anchor');
317 getSelectionFocus: function() {
318 return this.getSelectionBoundry('focus');
320 getSelectionBoundry: function(which) {
322 var selection = window.getSelection(),
323 anchorElement = this.canvas.getDocumentElement(selection.anchorNode),
324 focusElement = this.canvas.getDocumentElement(selection.focusNode);
326 if((!anchorElement) || (anchorElement instanceof documentElement.DocumentNodeElement) || (!focusElement) || focusElement instanceof documentElement.DocumentNodeElement) {
330 if(which === 'anchor') {
332 element: anchorElement,
333 offset: selection.anchorOffset,
334 offsetAtBeginning: selection.anchorOffset === 0,
335 offsetAtEnd: selection.anchorNode.data.length === selection.anchorOffset
338 if(which === 'focus') {
340 element: focusElement,
341 offset: selection.focusOffset,
342 offsetAtBeginning: selection.focusOffset === 0,
343 offsetAtEnd: selection.focusNode.data.length === selection.focusOffset
350 if(anchorElement.parent().sameNode(focusElement.parent())) {
351 var parent = anchorElement.parent(),
352 anchorFirst = parent.childIndex(anchorElement) < parent.childIndex(focusElement);
354 if(which === 'start') {
355 element = anchorElement;
356 offset = selection.anchorOffset;
358 else if(which === 'end') {
359 element = focusElement;
360 offset = selection.focusOffset;
363 if(which === 'start') {
364 element = focusElement;
365 offset = selection.focusOffset;
367 else if(which === 'end') {
368 element = anchorElement;
369 offset = selection.anchorOffset;
373 // TODO: Handle order via https://developer.mozilla.org/en-US/docs/Web/API/Node.compareDocumentPosition
374 if(which === 'start') {
375 element = anchorElement;
376 offset = selection.anchorOffset;
378 element = focusElement;
379 offset = selection.focusOffset;
383 var nodeLen = (element.sameNode(focusElement) ? selection.focusNode : selection.anchorNode).length;
387 offsetAtBeginning: offset === 0,
388 offsetAtEnd: nodeLen === offset
394 fromXMLDocument: function(wlxmlDocument, publisher) {
395 return new Canvas(wlxmlDocument, publisher);