2 'libs/jquery-1.9.1.min',
4 'modules/documentCanvas/canvas/documentElement',
5 'modules/documentCanvas/canvas/utils'
6 ], function($, _, documentElement, utils) {
10 var Canvas = function(wlxml, publisher) {
11 this.loadWlxml(wlxml);
12 this.publisher = publisher ? publisher : function() {};
15 $.extend(Canvas.prototype, {
17 loadWlxml: function(wlxml) {
18 var d = wlxml ? $($.trim(wlxml)) : null;
20 this.wrapper = $('<div>').addClass('canvas-wrapper').attr('contenteditable', true);
21 this.wrapper.append(d);
23 this.wrapper.find('*').replaceWith(function() {
24 var currentTag = $(this);
25 if(currentTag.attr('wlxml-tag'))
28 var meta = {}, others = {};
29 for(var i = 0; i < this.attributes.length; i++) {
30 var attr = this.attributes[i];
31 if(attr.name.substr(0, 5) === 'meta-')
32 meta[attr.name.substr(5)] = attr.value;
33 else if(attr.name !== 'class')
34 others[attr.name] = attr.value;
37 var element = documentElement.DocumentNodeElement.create({
38 tag: currentTag.prop('tagName').toLowerCase(),
39 klass: currentTag.attr('class'),
44 element.dom().append(currentTag.contents());
45 ['orig-before', 'orig-after', 'orig-begin', 'orig-end'].forEach(function(attr) {
46 element.data(attr, '');
51 this.wrapper.find(':not(iframe)').addBack().contents()
52 .filter(function() {return this.nodeType === Node.TEXT_NODE})
54 // TODO: use DocumentElement API
57 text = {original: el.text(), trimmed: $.trim(el.text())},
58 elParent = el.parent(),
59 hasSpanParent = elParent.attr('wlxml-tag') === 'span',
60 hasSpanBefore = el.prev().length > 0 && $(el.prev()[0]).attr('wlxml-tag') === 'span',
61 hasSpanAfter = el.next().length > 0 && $(el.next()[0]).attr('wlxml-tag') === 'span';
64 var addInfo = function(toAdd, where) {
65 var parentContents = elParent.contents(),
66 idx = parentContents.index(el[0]),
67 prev = idx > 0 ? parentContents[idx-1] : null,
68 next = idx < parentContents.length - 1 ? parentContents[idx+1] : null,
71 if(where === 'above') {
72 target = prev ? $(prev) : elParent;
73 key = prev ? 'orig-after' : 'orig-begin';
74 } else if(where === 'below') {
75 target = next ? $(next) : elParent;
76 key = next ? 'orig-before' : 'orig-end';
77 } else { throw new Object;}
79 target.data(key, toAdd);
82 text.transformed = text.trimmed;
84 if(hasSpanParent || hasSpanBefore || hasSpanAfter) {
85 var startSpace = /\s/g.test(text.original.substr(0,1)),
86 endSpace = /\s/g.test(text.original.substr(-1)) && text.original.length > 1;
87 text.transformed = (startSpace && (hasSpanParent || hasSpanBefore) ? ' ' : '')
89 + (endSpace && (hasSpanParent || hasSpanAfter) ? ' ' : '');
91 if(text.trimmed.length === 0 && text.original.length > 0 && elParent.contents().length === 1)
92 text.transformed = ' ';
95 if(!text.transformed) {
96 addInfo(text.original, 'below');
98 return true; // continue
101 if(text.transformed !== text.original) {
103 addInfo(text.original, 'below');
105 var startingMatch = text.original.match(/^\s+/g),
106 endingMatch = text.original.match(/\s+$/g),
107 startingWhiteSpace = startingMatch ? startingMatch[0] : null,
108 endingWhiteSpace = endingMatch ? endingMatch[0] : null;
110 if(endingWhiteSpace) {
111 if(text.transformed[text.transformed.length - 1] === ' ' && endingWhiteSpace[0] === ' ')
112 endingWhiteSpace = endingWhiteSpace.substr(1);
113 addInfo(endingWhiteSpace, 'below');
116 if(startingWhiteSpace) {
117 if(text.transformed[0] === ' ' && startingWhiteSpace[startingWhiteSpace.length-1] === ' ')
118 startingWhiteSpace = startingWhiteSpace.substr(0, startingWhiteSpace.length -1);
119 addInfo(startingWhiteSpace, 'above');
124 var element = documentElement.DocumentTextElement.create({text: text.transformed});
125 el.replaceWith(element.dom());
128 this.d = this.wrapper.children(0);
131 this.wrapper.on('keydown', function(e) {
134 var cursor = canvas.getCursor();
135 if(!cursor.isSelecting()) {
136 var position = cursor.getPosition(),
137 elements = position.element.split({offset: position.offset}),
141 if(position.offsetAtBeginning)
142 newEmpty = elements.first;
143 else if(position.offsetAtEnd)
144 newEmpty = elements.second;
146 goto = newEmpty.append(documentElement.DocumentTextElement.create({text: ''}, this));
147 canvas.setCurrentElement(goto);
161 this.wrapper.on('keyup', function(e) {
162 if(e.which >= 37 && e.which <= 40)
163 canvas.setCurrentElement(canvas.getCursor().getPosition().element, {caretTo: false})
166 this.wrapper.on('keydown', function(e) {
167 if(e.which >= 37 && e.which <= 40) {
168 var position = canvas.getCursor().getPosition(),
169 element = position.element;
170 if(element && (element instanceof documentElement.DocumentTextElement)) {
171 if(element.isEmpty()) {
172 var direction, caretTo;
173 if(e.which === KEYS.ARROW_LEFT || e.which === KEYS.ARROW_UP) {
180 var el = canvas.getDocumentElement(utils.nearestInDocumentOrder('[document-text-element]', direction, window.getSelection().focusNode))
181 canvas.setCurrentElement(element, {caretTo: caretTo});
183 var txt = element.dom().contents()[0].data;
184 if(e.which === KEYS.ARROW_LEFT && position.offset > 1 && txt.charAt(position.offset-2) === utils.unicode.ZWS) {
186 canvas._moveCaretToTextElement(element, position.offset-2);
188 if(e.which === KEYS.ARROW_RIGHT && position.offset < txt.length - 1 && txt.charAt(position.offset+1) === utils.unicode.ZWS) {
190 canvas._moveCaretToTextElement(element, position.offset+2);
199 this.wrapper.on('click', '[wlxml-tag], [wlxml-text]', function(e) {
201 canvas.setCurrentElement(canvas.getDocumentElement(e.target), {caretTo: false});
205 var observer = new MutationObserver(function(mutations) {
206 mutations.forEach(function(mutation) {
207 if(documentElement.DocumentTextElement.isContentContainer(mutation.target) && mutation.target.data === '')
208 mutation.target.data = utils.unicode.ZWS;
211 var config = { attributes: false, childList: false, characterData: true, subtree: true, characterDataOldValue: true};
212 observer.observe(this.d[0], config);
226 return documentElement.DocumentNodeElement.fromHTMLElement(this.d.get(0), this); //{wlxmlTag: this.d.prop('tagName')};
229 wrapText: function(params) {
230 params = _.extend({textNodeIdx: 0}, params);
231 if(typeof params.textNodeIdx === 'number')
232 params.textNodeIdx = [params.textNodeIdx];
234 var childrenInside = params.inside.children(),
235 idx1 = Math.min.apply(Math, params.textNodeIdx),
236 idx2 = Math.max.apply(Math, params.textNodeIdx),
237 textNode1 = childrenInside[idx1],
238 textNode2 = childrenInside[idx2],
239 sameNode = textNode1.sameNode(textNode2),
240 prefixOutside = textNode1.getText().substr(0, params.offsetStart),
241 prefixInside = textNode1.getText().substr(params.offsetStart),
242 suffixInside = textNode2.getText().substr(0, params.offsetEnd),
243 suffixOutside = textNode2.getText().substr(params.offsetEnd)
246 var wrapperElement = documentElement.DocumentNodeElement.create({tag: params._with.tag, klass: params._with.klass});
247 textNode1.after(wrapperElement);
250 if(prefixOutside.length > 0)
251 wrapperElement.before({text:prefixOutside});
253 var core = textNode1.getText().substr(params.offsetStart, params.offsetEnd - params.offsetStart);
254 wrapperElement.append({text: core});
257 if(prefixInside.length > 0)
258 wrapperElement.append({text: prefixInside});
259 for(var i = idx1 + 1; i < idx2; i++) {
260 wrapperElement.append(childrenInside[i]);
262 if(suffixInside.length > 0)
263 wrapperElement.append({text: suffixInside});
265 if(suffixOutside.length > 0)
266 wrapperElement.after({text: suffixOutside});
267 return wrapperElement;
269 getDocumentElement: function(from) {
270 if(from instanceof HTMLElement || from instanceof Text) {
271 return documentElement.DocumentElement.fromHTMLElement(from, this);
274 getCursor: function() {
275 return new Cursor(this);
282 highlightElement: function(element) {
283 this.wrapper.find('.highlighted-element').removeClass('highlighted-element');
284 element.dom().addClass('highlighted-element');
287 dimElement: function(element) {
288 element.dom().removeClass('highlighted-element');
291 getCurrentNodeElement: function() {
292 return this.getDocumentElement(this.wrapper.find('.current-node-element')[0]);
295 getCurrentTextElement: function() {
296 return this.getDocumentElement(this.wrapper.find('.current-text-element')[0]);
301 setCurrentElement: function(element, params) {
302 params = _.extend({caretTo: 'end'}, params);
303 var findFirstDirectTextChild = function(e, nodeToLand) {
304 var byBrowser = this.getCursor().getPosition().element;
305 if(byBrowser && byBrowser.parent().sameNode(nodeToLand))
307 var children = e.children();
308 for(var i = 0; i < children.length; i++) {
309 if(children[i] instanceof documentElement.DocumentTextElement)
314 var _markAsCurrent = function(element) {
315 if(element instanceof documentElement.DocumentTextElement) {
316 this.wrapper.find('.current-text-element').removeClass('current-text-element');
317 element.dom().addClass('current-text-element');
319 this.wrapper.find('.current-node-element').removeClass('current-node-element')
320 element.dom().addClass('current-node-element');
321 this.publisher('currentElementChanged', element);
326 var isTextElement = element instanceof documentElement.DocumentTextElement,
327 nodeElementToLand = isTextElement ? element.parent() : element,
328 textElementToLand = isTextElement ? element : findFirstDirectTextChild(element, nodeElementToLand),
329 currentTextElement = this.getCurrentTextElement(),
330 currentNodeElement = this.getCurrentNodeElement();
332 if(currentTextElement && !(currentTextElement.sameNode(textElementToLand)))
333 this.wrapper.find('.current-text-element').removeClass('current-text-element');
335 if(textElementToLand) {
336 _markAsCurrent(textElementToLand);
337 if(params.caretTo || !textElementToLand.sameNode(this.getCursor().getPosition().element))
338 this._moveCaretToTextElement(textElementToLand, params.caretTo); // as method on element?
339 if(!(textElementToLand.sameNode(currentTextElement)))
340 this.publisher('currentTextElementSet', textElementToLand);
342 document.getSelection().removeAllRanges();
345 if(!(currentNodeElement && currentNodeElement.sameNode(nodeElementToLand))) {
346 _markAsCurrent(nodeElementToLand);
348 this.publisher('currentNodeElementSet', nodeElementToLand);
352 _moveCaretToTextElement: function(element, where) {
353 var range = document.createRange(),
354 node = element.dom().contents()[0];
356 if(typeof where !== 'number') {
357 range.selectNodeContents(node);
359 range.setStart(node, where);
362 var collapseArg = true;
365 range.collapse(collapseArg);
367 var selection = document.getSelection();
369 selection.removeAllRanges();
370 selection.addRange(range);
371 this.wrapper.focus(); // FF requires this for caret to be put where range colllapses, Chrome doesn't.
374 setCursorPosition: function(position) {
376 this._moveCaretToTextElement(position.element, position.offset);
380 var parent = $('<div>'),
381 parts = this.doc().toXML(0);
382 parent.append(parts);
383 return parent.html();
387 $.extend(Canvas.prototype.list, {
388 create: function(params) {
389 if(!(params.element1.parent().sameNode(params.element2.parent())))
392 var parent = params.element1.parent();
394 if(parent.childIndex(params.element1) > parent.childIndex(params.element2)) {
395 var tmp = params.element1;
396 params.element1 = params.element2;
397 params.element2 = tmp;
400 var elementsToWrap = [];
402 var place = 'before';
404 parent.children().some(function(element) {
406 if(element.sameNode(params.element1))
408 if(place === 'inside') {
409 if(element instanceof documentElement.DocumentTextElement) {
410 element = element.wrapWithNodeElement({tag: 'div', klass: 'list.item'});
411 if(element.children()[0].sameNode(params.element1))
412 params.element1 = element;
414 element.setWlxmlClass('item');
415 elementsToWrap.push(element);
417 if(_e.sameNode(params.element2))
421 var listElement = documentElement.DocumentNodeElement.create({tag: 'div', klass: 'list-items' + (params.type === 'enum' ? '-enum' : '')});
424 if(parent.is('list')) {
425 listElement.wrapWithNodeElement({tag: 'div', klass: 'item'});
426 toret = listElement.parent();
431 params.element1.before(toret);
433 elementsToWrap.forEach(function(element) {
435 listElement.append(element);
438 extractItems: function(params) {
439 params = _.extend({merge: true}, params);
440 var list = params.element1.parent();
441 if(!list.is('list') || !(list.sameNode(params.element2.parent())))
444 var idx1 = list.childIndex(params.element1),
445 idx2 = list.childIndex(params.element2),
448 succeedingItems = [],
449 items = list.children(),
450 listIsNested = list.parent().getWlxmlClass() === 'item',
454 var tmp = idx1; idx1 = idx2; idx2 = tmp;
457 items.forEach(function(item, idx) {
459 precedingItems.push(item);
460 else if(idx >= idx1 && idx <= idx2) {
461 extractedItems.push(item);
464 succeedingItems.push(item);
468 var reference = listIsNested ? list.parent() : list;
469 if(succeedingItems.length === 0) {
470 var reference_orig = reference;
471 extractedItems.forEach(function(item) {
472 reference.after(item);
475 item.setWlxmlClass(null);
477 if(precedingItems.length === 0)
478 reference_orig.detach();
479 } else if(precedingItems.length === 0) {
480 extractedItems.forEach(function(item) {
481 reference.before(item);
483 item.setWlxmlClass(null);
486 extractedItems.forEach(function(item) {
487 reference.after(item);
489 item.setWlxmlClass(null);
492 var secondList = documentElement.DocumentNodeElement.create({tag: 'div', klass:'list-items'}, this),
496 toAdd = secondList.wrapWithNodeElement({tag: 'div', klass:'item'});
498 succeedingItems.forEach(function(item) {
499 secondList.append(item);
502 reference.after(toAdd);
504 if(!params.merge && listIsNested) {
505 return this.extractItems({element1: extractedItems[0], element2: extractedItems[extractedItems.length-1]});
509 areItemsOfTheSameList: function(params) {
510 var e1 = params.element1,
511 e2 = params.element2;
512 return e1.parent().sameNode(e2.parent())
513 && e1.parent().is('list');
518 var Cursor = function(canvas) {
519 this.canvas = canvas;
522 $.extend(Cursor.prototype, {
523 isSelecting: function() {
524 var selection = window.getSelection();
525 return !selection.isCollapsed;
527 isSelectingWithinElement: function() {
528 return this.isSelecting() && this.getSelectionStart().element.sameNode(this.getSelectionEnd().element);
530 isSelectingSiblings: function() {
531 return this.isSelecting() && this.getSelectionStart().element.parent().sameNode(this.getSelectionEnd().element.parent());
533 getPosition: function() {
534 return this.getSelectionAnchor();
536 getSelectionStart: function() {
537 return this.getSelectionBoundry('start');
539 getSelectionEnd: function() {
540 return this.getSelectionBoundry('end');
542 getSelectionAnchor: function() {
543 return this.getSelectionBoundry('anchor');
545 getSelectionFocus: function() {
546 return this.getSelectionBoundry('focus');
548 getSelectionBoundry: function(which) {
549 var selection = window.getSelection(),
550 anchorElement = this.canvas.getDocumentElement(selection.anchorNode),
551 focusElement = this.canvas.getDocumentElement(selection.focusNode);
553 if(anchorElement instanceof documentElement.DocumentNodeElement || focusElement instanceof documentElement.DocumentNodeElement)
556 if(which === 'anchor') {
558 element: anchorElement,
559 offset: selection.anchorOffset,
560 offsetAtBeginning: selection.anchorOffset === 0,
561 offsetAtEnd: anchorElement && anchorElement.getText().length === selection.anchorOffset
564 if(which === 'focus') {
566 element: focusElement,
567 offset: selection.focusOffset,
568 offsetAtBeginning: selection.focusOffset === 0,
569 offsetAtEnd: focusElement && focusElement.getText().length === selection.focusOffset
576 if(anchorElement.parent().sameNode(focusElement.parent())) {
577 var parent = anchorElement.parent(),
578 anchorFirst = parent.childIndex(anchorElement) < parent.childIndex(focusElement);
580 if(which === 'start') {
581 element = anchorElement;
582 offset = selection.anchorOffset;
584 else if(which === 'end') {
585 element = focusElement,
586 offset = selection.focusOffset;
589 if(which === 'start') {
590 element = focusElement,
591 offset = selection.focusOffset
593 else if(which === 'end') {
594 element = anchorElement;
595 offset = selection.anchorOffset;
599 // TODO: Handle order via https://developer.mozilla.org/en-US/docs/Web/API/Node.compareDocumentPosition
600 if(which === 'start') {
601 element = anchorElement;
602 offset = selection.anchorOffset
604 element = focusElement;
605 offset = selection.focusOffset
612 offsetAtBeginning: offset === 0,
613 offsetAtEnd: element.getText().length === offset
619 fromXML: function(xml, publisher) {
620 return new Canvas(xml, publisher);