5 'fnpjs/logging/logging',
6 'modules/documentCanvas/canvas/documentElement',
7 'modules/documentCanvas/canvas/keyboard',
8 'modules/documentCanvas/canvas/utils',
9 'modules/documentCanvas/canvas/wlxmlListener'
10 ], function($, _, Backbone, logging, documentElement, keyboard, utils, wlxmlListener) {
13 /* global document:false, window:false, Node:false */
15 var logger = logging.getLogger('canvas');
17 var TextHandler = function(canvas) {this.canvas = canvas; this.buffer = null;};
18 $.extend(TextHandler.prototype, {
19 handle: function(node, text) {
20 this.setText(text, node);
25 // if(this.node.sameNode(node)) {
33 _ping: _.throttle(function(text) {
38 if(this.buffer !== null) {
39 this.setText(this.buffer, this.node);
43 setText: function(text, node) {
44 //this.canvas.wlxmlDocument.transform('setText', {node:node, text: text});
52 var Canvas = function(wlxmlDocument, publisher) {
53 this.eventBus = _.extend({}, Backbone.Events);
54 this.wrapper = $('<div>').addClass('canvas-wrapper').attr('contenteditable', true);
55 this.wlxmlListener = wlxmlListener.create(this);
56 this.loadWlxmlDocument(wlxmlDocument);
57 this.setupEventHandling();
58 this.publisher = publisher ? publisher : function() {};
59 this.textHandler = new TextHandler(this);
62 $.extend(Canvas.prototype, {
64 loadWlxmlDocument: function(wlxmlDocument) {
69 this.wlxmlListener.listenTo(wlxmlDocument);
70 this.wlxmlDocument = wlxmlDocument;
74 createElement: function(wlxmlNode) {
75 var Factory = wlxmlNode.nodeType === Node.TEXT_NODE ? documentElement.DocumentTextElement : documentElement.DocumentNodeElement;
76 return Factory.create(wlxmlNode, this);
79 getDocumentElement: function(htmlElement) {
80 /* globals HTMLElement, Text */
81 if(!htmlElement || !(htmlElement instanceof HTMLElement || htmlElement instanceof Text)) {
84 var $element = $(htmlElement);
85 if(htmlElement.nodeType === Node.ELEMENT_NODE && $element.attr('document-node-element') !== undefined) {
86 return new documentElement.DocumentNodeElement(htmlElement, this);
88 if($element.attr('document-text-element') !== undefined || (htmlElement.nodeType === Node.TEXT_NODE && $element.parent().attr('document-text-element') !== undefined)) {
89 //return DocumentTextElement.fromHTMLElement(htmlElement, canvas);
90 return new documentElement.DocumentTextElement(htmlElement, this);
94 reloadRoot: function() {
95 var canvasDOM = this.generateCanvasDOM(this.wlxmlDocument.root);
96 //var canvasDOM = this.wlxmlDocument.root.getData('canvasElement') ? this.wlxmlDocument.root.getData('canvasElement').dom() : this.generateCanvasDOM(this.wlxmlDocument.root);
99 this.wrapper.append(canvasDOM);
100 this.d = this.wrapper.children(0);
103 generateCanvasDOM: function(wlxmlNode) {
104 var element = documentElement.DocumentNodeElement.create(wlxmlNode, this);
105 return element.dom();
108 setupEventHandling: function() {
110 this.wrapper.on('keyup keydown keypress', function(e) {
111 keyboard.handleKey(e, this);
115 this.wrapper.on('mousedown', '[document-node-element], [document-text-element]', function(e) {
116 mouseDown = e.target;
119 this.wrapper.on('click', '[document-node-element], [document-text-element]', function(e) {
121 if(e.originalEvent.detail === 3) {
123 canvas._moveCaretToTextElement(canvas.getDocumentElement(e.currentTarget), 'whole');
125 if(mouseDown === e.target) {
126 canvas.setCurrentElement(canvas.getDocumentElement(e.currentTarget), {caretTo: false});
131 this.wrapper.on('paste', function(e) {
134 var clipboardData = e.originalEvent.clipboardData;
135 if(!clipboardData || !clipboardData.getData) {
136 return; // TODO: alert
139 var text = clipboardData.getData('text/plain').replace(/\r?\n|\r/g, ' '),
140 cursor = canvas.getCursor(),
141 element = cursor.getPosition().element,
144 if(element && cursor.isWithinElement()) {
145 lhs = element.getText().substr(0, cursor.getSelectionStart().offset);
146 rhs = element.getText().substr(cursor.getSelectionEnd().offset);
147 element.setText(lhs+text+rhs);
148 canvas.setCurrentElement(element, {caretTo: lhs.length + text.length});
150 /* jshint noempty:false */
155 /* globals MutationObserver */
156 var observer = new MutationObserver(function(mutations) {
157 mutations.forEach(function(mutation) {
158 if(documentElement.DocumentTextElement.isContentContainer(mutation.target)) {
159 observer.disconnect();
160 if(mutation.target.data === '') {
161 mutation.target.data = utils.unicode.ZWS;
163 else if(mutation.oldValue === utils.unicode.ZWS) {
164 mutation.target.data = mutation.target.data.replace(utils.unicode.ZWS, '');
165 canvas._moveCaretToTextElement(canvas.getDocumentElement(mutation.target), 'end');
167 observer.observe(canvas.wrapper[0], config);
169 var textElement = canvas.getDocumentElement(mutation.target),
170 toSet = mutation.target.data !== utils.unicode.ZWS ? mutation.target.data : '';
172 //textElement.data('wlxmlNode').setText(toSet);
173 //textElement.data('wlxmlNode').document.transform('setText', {node: textElement.data('wlxmlNode'), text: toSet});
174 if(textElement.data('wlxmlNode').getText() !== toSet) {
175 canvas.textHandler.handle(textElement.data('wlxmlNode'), toSet);
180 var config = { attributes: false, childList: false, characterData: true, subtree: true, characterDataOldValue: true};
181 observer.observe(this.wrapper[0], config);
184 this.wrapper.on('mouseover', '[document-node-element], [document-text-element]', function(e) {
185 var el = canvas.getDocumentElement(e.currentTarget);
190 if(el instanceof documentElement.DocumentTextElement) {
193 el.toggleLabel(true);
195 this.wrapper.on('mouseout', '[document-node-element], [document-text-element]', function(e) {
196 var el = canvas.getDocumentElement(e.currentTarget);
201 if(el instanceof documentElement.DocumentTextElement) {
204 el.toggleLabel(false);
207 this.eventBus.on('elementToggled', function(toggle, element) {
209 canvas.setCurrentElement(element.getPreviousTextElement());
219 if(this.d === null) {
222 return this.getDocumentElement(this.d[0]);
225 toggleElementHighlight: function(node, toggle) {
226 var element = utils.findCanvasElement(node);
227 element.toggleHighlight(toggle);
230 getCursor: function() {
231 return new Cursor(this);
235 getCurrentNodeElement: function() {
236 var htmlElement = this.wrapper.find('.current-node-element').parent()[0];
238 return this.getDocumentElement(htmlElement);
242 getCurrentTextElement: function() {
243 var htmlElement = this.wrapper.find('.current-text-element')[0];
245 return this.getDocumentElement(htmlElement);
249 contains: function(element) {
250 return element.dom().parents().index(this.wrapper) !== -1;
253 setCurrentElement: function(element, params) {
254 if(!(element instanceof documentElement.DocumentElement)) {
255 element = utils.findCanvasElement(element);
258 if(!element || !this.contains(element)) {
259 logger.warning('Cannot set current element: element doesn\'t exist on canvas');
263 params = _.extend({caretTo: 'end'}, params);
264 var findFirstDirectTextChild = function(e, nodeToLand) {
265 var byBrowser = this.getCursor().getPosition().element;
266 if(byBrowser && byBrowser.parent().sameNode(nodeToLand)) {
269 var children = e.children();
270 for(var i = 0; i < children.length; i++) {
271 if(children[i] instanceof documentElement.DocumentTextElement) {
277 var _markAsCurrent = function(element) {
278 if(element instanceof documentElement.DocumentTextElement) {
279 this.wrapper.find('.current-text-element').removeClass('current-text-element');
280 element.dom().addClass('current-text-element');
282 this.wrapper.find('.current-node-element').removeClass('current-node-element');
283 element._container().addClass('current-node-element');
288 var isTextElement = element instanceof documentElement.DocumentTextElement,
289 nodeElementToLand = isTextElement ? element.parent() : element,
290 textElementToLand = isTextElement ? element : findFirstDirectTextChild(element, nodeElementToLand),
291 currentTextElement = this.getCurrentTextElement(),
292 currentNodeElement = this.getCurrentNodeElement();
294 if(currentTextElement && !(currentTextElement.sameNode(textElementToLand))) {
295 this.wrapper.find('.current-text-element').removeClass('current-text-element');
298 if(textElementToLand) {
299 _markAsCurrent(textElementToLand);
300 if(params.caretTo || !textElementToLand.sameNode(this.getCursor().getPosition().element)) {
301 this._moveCaretToTextElement(textElementToLand, params.caretTo); // as method on element?
303 if(!(textElementToLand.sameNode(currentTextElement))) {
304 this.publisher('currentTextElementSet', textElementToLand.data('wlxmlNode'));
307 document.getSelection().removeAllRanges();
310 if(!(currentNodeElement && currentNodeElement.sameNode(nodeElementToLand))) {
311 _markAsCurrent(nodeElementToLand);
313 this.publisher('currentNodeElementSet', nodeElementToLand.data('wlxmlNode'));
317 _moveCaretToTextElement: function(element, where) {
318 var range = document.createRange(),
319 node = element.dom().contents()[0];
321 if(typeof where !== 'number') {
322 range.selectNodeContents(node);
324 range.setStart(node, where);
327 if(where !== 'whole') {
328 var collapseArg = true;
329 if(where === 'end') {
332 range.collapse(collapseArg);
334 var selection = document.getSelection();
336 selection.removeAllRanges();
337 selection.addRange(range);
338 this.wrapper.focus(); // FF requires this for caret to be put where range colllapses, Chrome doesn't.
341 setCursorPosition: function(position) {
342 if(position.element) {
343 this._moveCaretToTextElement(position.element, position.offset);
349 var Cursor = function(canvas) {
350 this.canvas = canvas;
353 $.extend(Cursor.prototype, {
354 isSelecting: function() {
355 var selection = window.getSelection();
356 return !selection.isCollapsed;
358 isSelectingWithinElement: function() {
359 return this.isSelecting() && this.getSelectionStart().element.sameNode(this.getSelectionEnd().element);
361 isWithinElement: function() {
362 return !this.isSelecting() || this.isSelectingWithinElement();
364 isSelectingSiblings: function() {
365 return this.isSelecting() && this.getSelectionStart().element.parent().sameNode(this.getSelectionEnd().element.parent());
367 getPosition: function() {
368 return this.getSelectionAnchor();
370 getSelectionStart: function() {
371 return this.getSelectionBoundry('start');
373 getSelectionEnd: function() {
374 return this.getSelectionBoundry('end');
376 getSelectionAnchor: function() {
377 return this.getSelectionBoundry('anchor');
379 getSelectionFocus: function() {
380 return this.getSelectionBoundry('focus');
382 getSelectionBoundry: function(which) {
384 var selection = window.getSelection(),
385 anchorElement = this.canvas.getDocumentElement(selection.anchorNode),
386 focusElement = this.canvas.getDocumentElement(selection.focusNode);
388 if((!anchorElement) || (anchorElement instanceof documentElement.DocumentNodeElement) || (!focusElement) || focusElement instanceof documentElement.DocumentNodeElement) {
392 if(which === 'anchor') {
394 element: anchorElement,
395 offset: selection.anchorOffset,
396 offsetAtBeginning: selection.anchorOffset === 0 || anchorElement.getText() === '',
397 offsetAtEnd: selection.anchorNode.data.length === selection.anchorOffset || anchorElement.getText() === ''
400 if(which === 'focus') {
402 element: focusElement,
403 offset: selection.focusOffset,
404 offsetAtBeginning: selection.focusOffset === 0 || focusElement.getText() === '',
405 offsetAtEnd: selection.focusNode.data.length === selection.focusOffset || focusElement.getText() === '',
409 var getPlaceData = function(anchorFirst) {
412 if(which === 'start') {
413 element = anchorElement;
414 offset = selection.anchorOffset;
416 else if(which === 'end') {
417 element = focusElement;
418 offset = selection.focusOffset;
421 if(which === 'start') {
422 element = focusElement;
423 offset = selection.focusOffset;
425 else if(which === 'end') {
426 element = anchorElement;
427 offset = selection.anchorOffset;
430 return {element: element, offset: offset};
433 var anchorFirst, placeData, parent;
435 if(anchorElement.parent().sameNode(focusElement.parent())) {
436 parent = anchorElement.parent();
437 if(selection.anchorNode === selection.focusNode) {
438 anchorFirst = selection.anchorOffset <= selection.focusOffset;
440 anchorFirst = parent.childIndex(anchorElement) < parent.childIndex(focusElement);
442 placeData = getPlaceData(anchorFirst);
444 /*jshint bitwise: false*/
445 anchorFirst = selection.anchorNode.compareDocumentPosition(selection.focusNode) & Node.DOCUMENT_POSITION_FOLLOWING;
446 placeData = getPlaceData(anchorFirst);
449 var nodeLen = (placeData.element.sameNode(focusElement) ? selection.focusNode : selection.anchorNode).length;
451 element: placeData.element,
452 offset: placeData.offset,
453 offsetAtBeginning: placeData.offset === 0 || focusElement.getText() === '',
454 offsetAtEnd: nodeLen === placeData.offset || focusElement.getText() === ''
460 fromXMLDocument: function(wlxmlDocument, publisher) {
461 return new Canvas(wlxmlDocument, publisher);