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, Node: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.setupEventHandling();
56 this.publisher = publisher ? publisher : function() {};
57 this.textHandler = new TextHandler(this);
60 $.extend(Canvas.prototype, {
62 loadWlxmlDocument: function(wlxmlDocument) {
67 this.wlxmlListener.listenTo(wlxmlDocument);
68 this.wlxmlDocument = wlxmlDocument;
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);
93 this.wrapper.on('mousedown', '[document-node-element], [document-text-element]', function(e) {
97 this.wrapper.on('click', '[document-node-element], [document-text-element]', function(e) {
99 if(e.originalEvent.detail === 3) {
101 canvas._moveCaretToTextElement(canvas.getDocumentElement(e.currentTarget), 'whole');
103 if(mouseDown === e.target) {
104 canvas.setCurrentElement(canvas.getDocumentElement(e.currentTarget), {caretTo: false});
109 this.wrapper.on('paste', function(e) {
112 var clipboardData = e.originalEvent.clipboardData;
113 if(!clipboardData || !clipboardData.getData) {
114 return; // TODO: alert
117 var text = clipboardData.getData('text/plain').replace(/\r?\n|\r/g, ' '),
118 cursor = canvas.getCursor(),
119 element = cursor.getPosition().element,
122 if(element && cursor.isWithinElement()) {
123 lhs = element.getText().substr(0, cursor.getSelectionStart().offset);
124 rhs = element.getText().substr(cursor.getSelectionEnd().offset);
125 element.setText(lhs+text+rhs);
126 canvas.setCurrentElement(element, {caretTo: lhs.length + text.length});
128 /* jshint noempty:false */
133 /* globals MutationObserver */
134 var observer = new MutationObserver(function(mutations) {
135 mutations.forEach(function(mutation) {
136 if(documentElement.DocumentTextElement.isContentContainer(mutation.target)) {
137 observer.disconnect();
138 if(mutation.target.data === '') {
139 mutation.target.data = utils.unicode.ZWS;
141 else if(mutation.oldValue === utils.unicode.ZWS) {
142 mutation.target.data = mutation.target.data.replace(utils.unicode.ZWS, '');
143 canvas._moveCaretToTextElement(canvas.getDocumentElement(mutation.target), 'end');
145 observer.observe(canvas.wrapper[0], config);
146 canvas.publisher('contentChanged');
148 var textElement = canvas.getDocumentElement(mutation.target),
149 toSet = mutation.target.data !== utils.unicode.ZWS ? mutation.target.data : '';
151 //textElement.data('wlxmlNode').setText(toSet);
152 //textElement.data('wlxmlNode').document.transform('setText', {node: textElement.data('wlxmlNode'), text: toSet});
153 if(textElement.data('wlxmlNode').getText() !== toSet) {
154 canvas.textHandler.handle(textElement.data('wlxmlNode'), toSet);
159 var config = { attributes: false, childList: false, characterData: true, subtree: true, characterDataOldValue: true};
160 observer.observe(this.wrapper[0], config);
163 this.wrapper.on('mouseover', '[document-node-element], [document-text-element]', function(e) {
164 var el = canvas.getDocumentElement(e.currentTarget);
169 if(el instanceof documentElement.DocumentTextElement) {
172 el.toggleLabel(true);
174 this.wrapper.on('mouseout', '[document-node-element], [document-text-element]', function(e) {
175 var el = canvas.getDocumentElement(e.currentTarget);
180 if(el instanceof documentElement.DocumentTextElement) {
183 el.toggleLabel(false);
186 this.eventBus.on('elementToggled', function(toggle, element) {
188 canvas.setCurrentElement(element.getPreviousTextElement());
198 if(this.d === null) {
201 return documentElement.DocumentNodeElement.fromHTMLElement(this.d.get(0), this); //{wlxmlTag: this.d.prop('tagName')};
204 toggleElementHighlight: function(node, toggle) {
205 var element = utils.findCanvasElement(node);
206 element.toggleHighlight(toggle);
209 createNodeElement: function(params) {
210 return documentElement.DocumentNodeElement.create(params, this);
213 getDocumentElement: function(from) {
214 /* globals HTMLElement, Text */
215 if(from instanceof HTMLElement || from instanceof Text) {
216 return documentElement.DocumentElement.fromHTMLElement(from, this);
219 getCursor: function() {
220 return new Cursor(this);
224 getCurrentNodeElement: function() {
225 return this.getDocumentElement(this.wrapper.find('.current-node-element').parent()[0]);
228 getCurrentTextElement: function() {
229 return this.getDocumentElement(this.wrapper.find('.current-text-element')[0]);
234 setCurrentElement: function(element, params) {
235 if(!(element instanceof documentElement.DocumentElement)) {
236 element = utils.findCanvasElement(element);
239 params = _.extend({caretTo: 'end'}, params);
240 var findFirstDirectTextChild = function(e, nodeToLand) {
241 var byBrowser = this.getCursor().getPosition().element;
242 if(byBrowser && byBrowser.parent().sameNode(nodeToLand)) {
245 var children = e.children();
246 for(var i = 0; i < children.length; i++) {
247 if(children[i] instanceof documentElement.DocumentTextElement) {
253 var _markAsCurrent = function(element) {
254 if(element instanceof documentElement.DocumentTextElement) {
255 this.wrapper.find('.current-text-element').removeClass('current-text-element');
256 element.dom().addClass('current-text-element');
258 this.wrapper.find('.current-node-element').removeClass('current-node-element');
259 element._container().addClass('current-node-element');
260 this.publisher('currentElementChanged', element);
265 var isTextElement = element instanceof documentElement.DocumentTextElement,
266 nodeElementToLand = isTextElement ? element.parent() : element,
267 textElementToLand = isTextElement ? element : findFirstDirectTextChild(element, nodeElementToLand),
268 currentTextElement = this.getCurrentTextElement(),
269 currentNodeElement = this.getCurrentNodeElement();
271 if(currentTextElement && !(currentTextElement.sameNode(textElementToLand))) {
272 this.wrapper.find('.current-text-element').removeClass('current-text-element');
275 if(textElementToLand) {
276 _markAsCurrent(textElementToLand);
277 if(params.caretTo || !textElementToLand.sameNode(this.getCursor().getPosition().element)) {
278 this._moveCaretToTextElement(textElementToLand, params.caretTo); // as method on element?
280 if(!(textElementToLand.sameNode(currentTextElement))) {
281 this.publisher('currentTextElementSet', textElementToLand.data('wlxmlNode'));
284 document.getSelection().removeAllRanges();
287 if(!(currentNodeElement && currentNodeElement.sameNode(nodeElementToLand))) {
288 _markAsCurrent(nodeElementToLand);
290 this.publisher('currentNodeElementSet', nodeElementToLand.data('wlxmlNode'));
294 _moveCaretToTextElement: function(element, where) {
295 var range = document.createRange(),
296 node = element.dom().contents()[0];
298 if(typeof where !== 'number') {
299 range.selectNodeContents(node);
301 range.setStart(node, where);
304 if(where !== 'whole') {
305 var collapseArg = true;
306 if(where === 'end') {
309 range.collapse(collapseArg);
311 var selection = document.getSelection();
313 selection.removeAllRanges();
314 selection.addRange(range);
315 this.wrapper.focus(); // FF requires this for caret to be put where range colllapses, Chrome doesn't.
318 setCursorPosition: function(position) {
319 if(position.element) {
320 this._moveCaretToTextElement(position.element, position.offset);
326 var Cursor = function(canvas) {
327 this.canvas = canvas;
330 $.extend(Cursor.prototype, {
331 isSelecting: function() {
332 var selection = window.getSelection();
333 return !selection.isCollapsed;
335 isSelectingWithinElement: function() {
336 return this.isSelecting() && this.getSelectionStart().element.sameNode(this.getSelectionEnd().element);
338 isWithinElement: function() {
339 return !this.isSelecting() || this.isSelectingWithinElement();
341 isSelectingSiblings: function() {
342 return this.isSelecting() && this.getSelectionStart().element.parent().sameNode(this.getSelectionEnd().element.parent());
344 getPosition: function() {
345 return this.getSelectionAnchor();
347 getSelectionStart: function() {
348 return this.getSelectionBoundry('start');
350 getSelectionEnd: function() {
351 return this.getSelectionBoundry('end');
353 getSelectionAnchor: function() {
354 return this.getSelectionBoundry('anchor');
356 getSelectionFocus: function() {
357 return this.getSelectionBoundry('focus');
359 getSelectionBoundry: function(which) {
361 var selection = window.getSelection(),
362 anchorElement = this.canvas.getDocumentElement(selection.anchorNode),
363 focusElement = this.canvas.getDocumentElement(selection.focusNode);
365 if((!anchorElement) || (anchorElement instanceof documentElement.DocumentNodeElement) || (!focusElement) || focusElement instanceof documentElement.DocumentNodeElement) {
369 if(which === 'anchor') {
371 element: anchorElement,
372 offset: selection.anchorOffset,
373 offsetAtBeginning: selection.anchorOffset === 0 || anchorElement.getText() === '',
374 offsetAtEnd: selection.anchorNode.data.length === selection.anchorOffset || anchorElement.getText() === ''
377 if(which === 'focus') {
379 element: focusElement,
380 offset: selection.focusOffset,
381 offsetAtBeginning: selection.focusOffset === 0 || focusElement.getText() === '',
382 offsetAtEnd: selection.focusNode.data.length === selection.focusOffset || focusElement.getText() === '',
386 var getPlaceData = function(anchorFirst) {
389 if(which === 'start') {
390 element = anchorElement;
391 offset = selection.anchorOffset;
393 else if(which === 'end') {
394 element = focusElement;
395 offset = selection.focusOffset;
398 if(which === 'start') {
399 element = focusElement;
400 offset = selection.focusOffset;
402 else if(which === 'end') {
403 element = anchorElement;
404 offset = selection.anchorOffset;
407 return {element: element, offset: offset};
410 var anchorFirst, placeData, parent;
412 if(anchorElement.parent().sameNode(focusElement.parent())) {
413 parent = anchorElement.parent();
414 if(selection.anchorNode === selection.focusNode) {
415 anchorFirst = selection.anchorOffset <= selection.focusOffset;
417 anchorFirst = parent.childIndex(anchorElement) < parent.childIndex(focusElement);
419 placeData = getPlaceData(anchorFirst);
421 /*jshint bitwise: false*/
422 anchorFirst = selection.anchorNode.compareDocumentPosition(selection.focusNode) & Node.DOCUMENT_POSITION_FOLLOWING;
423 placeData = getPlaceData(anchorFirst);
426 var nodeLen = (placeData.element.sameNode(focusElement) ? selection.focusNode : selection.anchorNode).length;
428 element: placeData.element,
429 offset: placeData.offset,
430 offsetAtBeginning: placeData.offset === 0 || focusElement.getText() === '',
431 offsetAtEnd: nodeLen === placeData.offset || focusElement.getText() === ''
437 fromXMLDocument: function(wlxmlDocument, publisher) {
438 return new Canvas(wlxmlDocument, publisher);