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);
147 var textElement = canvas.getDocumentElement(mutation.target),
148 toSet = mutation.target.data !== utils.unicode.ZWS ? mutation.target.data : '';
150 //textElement.data('wlxmlNode').setText(toSet);
151 //textElement.data('wlxmlNode').document.transform('setText', {node: textElement.data('wlxmlNode'), text: toSet});
152 if(textElement.data('wlxmlNode').getText() !== toSet) {
153 canvas.textHandler.handle(textElement.data('wlxmlNode'), toSet);
158 var config = { attributes: false, childList: false, characterData: true, subtree: true, characterDataOldValue: true};
159 observer.observe(this.wrapper[0], config);
162 this.wrapper.on('mouseover', '[document-node-element], [document-text-element]', function(e) {
163 var el = canvas.getDocumentElement(e.currentTarget);
168 if(el instanceof documentElement.DocumentTextElement) {
171 el.toggleLabel(true);
173 this.wrapper.on('mouseout', '[document-node-element], [document-text-element]', function(e) {
174 var el = canvas.getDocumentElement(e.currentTarget);
179 if(el instanceof documentElement.DocumentTextElement) {
182 el.toggleLabel(false);
185 this.eventBus.on('elementToggled', function(toggle, element) {
187 canvas.setCurrentElement(element.getPreviousTextElement());
197 if(this.d === null) {
200 return documentElement.DocumentNodeElement.fromHTMLElement(this.d.get(0), this); //{wlxmlTag: this.d.prop('tagName')};
203 toggleElementHighlight: function(node, toggle) {
204 var element = utils.findCanvasElement(node);
205 element.toggleHighlight(toggle);
208 createNodeElement: function(params) {
209 return documentElement.DocumentNodeElement.create(params, this);
212 getDocumentElement: function(from) {
213 /* globals HTMLElement, Text */
214 if(from instanceof HTMLElement || from instanceof Text) {
215 return documentElement.DocumentElement.fromHTMLElement(from, this);
218 getCursor: function() {
219 return new Cursor(this);
223 getCurrentNodeElement: function() {
224 return this.getDocumentElement(this.wrapper.find('.current-node-element').parent()[0]);
227 getCurrentTextElement: function() {
228 return this.getDocumentElement(this.wrapper.find('.current-text-element')[0]);
233 setCurrentElement: function(element, params) {
234 if(!(element instanceof documentElement.DocumentElement)) {
235 element = utils.findCanvasElement(element);
238 params = _.extend({caretTo: 'end'}, params);
239 var findFirstDirectTextChild = function(e, nodeToLand) {
240 var byBrowser = this.getCursor().getPosition().element;
241 if(byBrowser && byBrowser.parent().sameNode(nodeToLand)) {
244 var children = e.children();
245 for(var i = 0; i < children.length; i++) {
246 if(children[i] instanceof documentElement.DocumentTextElement) {
252 var _markAsCurrent = function(element) {
253 if(element instanceof documentElement.DocumentTextElement) {
254 this.wrapper.find('.current-text-element').removeClass('current-text-element');
255 element.dom().addClass('current-text-element');
257 this.wrapper.find('.current-node-element').removeClass('current-node-element');
258 element._container().addClass('current-node-element');
263 var isTextElement = element instanceof documentElement.DocumentTextElement,
264 nodeElementToLand = isTextElement ? element.parent() : element,
265 textElementToLand = isTextElement ? element : findFirstDirectTextChild(element, nodeElementToLand),
266 currentTextElement = this.getCurrentTextElement(),
267 currentNodeElement = this.getCurrentNodeElement();
269 if(currentTextElement && !(currentTextElement.sameNode(textElementToLand))) {
270 this.wrapper.find('.current-text-element').removeClass('current-text-element');
273 if(textElementToLand) {
274 _markAsCurrent(textElementToLand);
275 if(params.caretTo || !textElementToLand.sameNode(this.getCursor().getPosition().element)) {
276 this._moveCaretToTextElement(textElementToLand, params.caretTo); // as method on element?
278 if(!(textElementToLand.sameNode(currentTextElement))) {
279 this.publisher('currentTextElementSet', textElementToLand.data('wlxmlNode'));
282 document.getSelection().removeAllRanges();
285 if(!(currentNodeElement && currentNodeElement.sameNode(nodeElementToLand))) {
286 _markAsCurrent(nodeElementToLand);
288 this.publisher('currentNodeElementSet', nodeElementToLand.data('wlxmlNode'));
292 _moveCaretToTextElement: function(element, where) {
293 var range = document.createRange(),
294 node = element.dom().contents()[0];
296 if(typeof where !== 'number') {
297 range.selectNodeContents(node);
299 range.setStart(node, where);
302 if(where !== 'whole') {
303 var collapseArg = true;
304 if(where === 'end') {
307 range.collapse(collapseArg);
309 var selection = document.getSelection();
311 selection.removeAllRanges();
312 selection.addRange(range);
313 this.wrapper.focus(); // FF requires this for caret to be put where range colllapses, Chrome doesn't.
316 setCursorPosition: function(position) {
317 if(position.element) {
318 this._moveCaretToTextElement(position.element, position.offset);
324 var Cursor = function(canvas) {
325 this.canvas = canvas;
328 $.extend(Cursor.prototype, {
329 isSelecting: function() {
330 var selection = window.getSelection();
331 return !selection.isCollapsed;
333 isSelectingWithinElement: function() {
334 return this.isSelecting() && this.getSelectionStart().element.sameNode(this.getSelectionEnd().element);
336 isWithinElement: function() {
337 return !this.isSelecting() || this.isSelectingWithinElement();
339 isSelectingSiblings: function() {
340 return this.isSelecting() && this.getSelectionStart().element.parent().sameNode(this.getSelectionEnd().element.parent());
342 getPosition: function() {
343 return this.getSelectionAnchor();
345 getSelectionStart: function() {
346 return this.getSelectionBoundry('start');
348 getSelectionEnd: function() {
349 return this.getSelectionBoundry('end');
351 getSelectionAnchor: function() {
352 return this.getSelectionBoundry('anchor');
354 getSelectionFocus: function() {
355 return this.getSelectionBoundry('focus');
357 getSelectionBoundry: function(which) {
359 var selection = window.getSelection(),
360 anchorElement = this.canvas.getDocumentElement(selection.anchorNode),
361 focusElement = this.canvas.getDocumentElement(selection.focusNode);
363 if((!anchorElement) || (anchorElement instanceof documentElement.DocumentNodeElement) || (!focusElement) || focusElement instanceof documentElement.DocumentNodeElement) {
367 if(which === 'anchor') {
369 element: anchorElement,
370 offset: selection.anchorOffset,
371 offsetAtBeginning: selection.anchorOffset === 0 || anchorElement.getText() === '',
372 offsetAtEnd: selection.anchorNode.data.length === selection.anchorOffset || anchorElement.getText() === ''
375 if(which === 'focus') {
377 element: focusElement,
378 offset: selection.focusOffset,
379 offsetAtBeginning: selection.focusOffset === 0 || focusElement.getText() === '',
380 offsetAtEnd: selection.focusNode.data.length === selection.focusOffset || focusElement.getText() === '',
384 var getPlaceData = function(anchorFirst) {
387 if(which === 'start') {
388 element = anchorElement;
389 offset = selection.anchorOffset;
391 else if(which === 'end') {
392 element = focusElement;
393 offset = selection.focusOffset;
396 if(which === 'start') {
397 element = focusElement;
398 offset = selection.focusOffset;
400 else if(which === 'end') {
401 element = anchorElement;
402 offset = selection.anchorOffset;
405 return {element: element, offset: offset};
408 var anchorFirst, placeData, parent;
410 if(anchorElement.parent().sameNode(focusElement.parent())) {
411 parent = anchorElement.parent();
412 if(selection.anchorNode === selection.focusNode) {
413 anchorFirst = selection.anchorOffset <= selection.focusOffset;
415 anchorFirst = parent.childIndex(anchorElement) < parent.childIndex(focusElement);
417 placeData = getPlaceData(anchorFirst);
419 /*jshint bitwise: false*/
420 anchorFirst = selection.anchorNode.compareDocumentPosition(selection.focusNode) & Node.DOCUMENT_POSITION_FOLLOWING;
421 placeData = getPlaceData(anchorFirst);
424 var nodeLen = (placeData.element.sameNode(focusElement) ? selection.focusNode : selection.anchorNode).length;
426 element: placeData.element,
427 offset: placeData.offset,
428 offsetAtBeginning: placeData.offset === 0 || focusElement.getText() === '',
429 offsetAtEnd: nodeLen === placeData.offset || focusElement.getText() === ''
435 fromXMLDocument: function(wlxmlDocument, publisher) {
436 return new Canvas(wlxmlDocument, publisher);