2 'libs/jquery-1.9.1.min',
4 'modules/documentCanvas/canvas/documentElement'
5 ], function($, _, documentElement) {
9 var Canvas = function(wlxml, publisher) {
10 this.loadWlxml(wlxml);
11 this.publisher = publisher ? publisher : function() {};
14 $.extend(Canvas.prototype, {
16 loadWlxml: function(wlxml) {
17 var d = wlxml ? $($.trim(wlxml)) : null;
19 this.wrapper = $('<div>').addClass('canvas-wrapper').attr('contenteditable', true);
20 this.wrapper.append(d);
22 this.wrapper.find('*').replaceWith(function() {
23 var currentTag = $(this);
24 if(currentTag.attr('wlxml-tag'))
28 for(var i = 0; i < this.attributes.length; i++) {
29 var attr = this.attributes[i];
30 if(attr.name.substr(0, 5) === 'meta-')
31 meta[attr.name.substr(5)] = attr.value;
34 var element = documentElement.DocumentNodeElement.create({
35 tag: currentTag.prop('tagName').toLowerCase(),
36 klass: currentTag.attr('class'),
40 element.dom().append(currentTag.contents());
41 ['orig-before', 'orig-append'].forEach(function(attr) {
42 element.data(attr, '');
47 this.wrapper.find(':not(iframe)').addBack().contents()
48 .filter(function() {return this.nodeType === Node.TEXT_NODE})
53 // TODO: use DocumentElement API
54 var spanParent = el.parent().attr('wlxml-tag') === 'span',
55 spanBefore = el.prev().length > 0 && $(el.prev()[0]).attr('wlxml-tag') === 'span',
56 spanAfter = el.next().length > 0 && $(el.next()[0]).attr('wlxml-tag') === 'span';
58 var oldText = this.data,
59 oldLength = this.data.length;
60 var parent = el.parent(),
61 parentContents = parent.contents(),
62 idx = parentContents.index(this),
63 next = idx < parentContents.length - 1 ? parentContents[idx+1] : null;
65 var addInfo = function() {
67 $(next).data('orig-before', oldText);
69 parent.data('orig-append', oldText);
73 if(spanParent || spanBefore || spanAfter) {
74 var startSpace = /\s/g.test(this.data.substr(0,1));
75 var endSpace = /\s/g.test(this.data.substr(-1)) && this.data.length > 1;
76 var trimmed = $.trim(this.data);
77 var newText = (startSpace && (spanParent || spanBefore) ? ' ' : '')
79 + (endSpace && (spanParent || spanAfter) ? ' ' : '');
80 if(newText !== oldText) {
86 this.data = $.trim(this.data);
87 if(this.data.length === 0 && oldLength > 0 && el.parent().contents().length === 1)
89 if(this.data.length === 0) {
93 return true; // continue
97 var element = documentElement.DocumentTextElement.create({text: this.data});
98 $(this).replaceWith(element.dom());
101 this.d = this.wrapper.children(0);
104 this.wrapper.on('keydown', function(e) {
107 var cursor = canvas.getCursor();
108 if(!cursor.isSelecting()) {
109 var position = cursor.getPosition(),
110 elements = position.element.split({offset: position.offset}),
114 if(position.offsetAtBeginning)
115 newEmpty = elements.first;
116 else if(position.offsetAtEnd)
117 newEmpty = elements.second;
119 goto = newEmpty.append(documentElement.DocumentTextElement.create({text: '\u200B'}, this));
120 canvas.setCurrentElement(goto);
126 this.wrapper.on('keyup', function(e) {
127 if(e.which >= 37 && e.which <= 40)
128 canvas.setCurrentElement(canvas.getCursor().getPosition().element, {caretTo: false})
131 this.wrapper.on('click', '[wlxml-tag], [wlxml-text]', function(e) {
133 canvas.setCurrentElement(canvas.getDocumentElement(e.target), {caretTo: false});
148 return documentElement.DocumentNodeElement.fromHTMLElement(this.d.get(0), this); //{wlxmlTag: this.d.prop('tagName')};
151 wrapText: function(params) {
152 params = _.extend({textNodeIdx: 0}, params);
153 if(typeof params.textNodeIdx === 'number')
154 params.textNodeIdx = [params.textNodeIdx];
156 var childrenInside = params.inside.children(),
157 idx1 = Math.min.apply(Math, params.textNodeIdx),
158 idx2 = Math.max.apply(Math, params.textNodeIdx),
159 textNode1 = childrenInside[idx1],
160 textNode2 = childrenInside[idx2],
161 sameNode = textNode1.sameNode(textNode2),
162 prefixOutside = textNode1.getText().substr(0, params.offsetStart),
163 prefixInside = textNode1.getText().substr(params.offsetStart),
164 suffixInside = textNode2.getText().substr(0, params.offsetEnd),
165 suffixOutside = textNode2.getText().substr(params.offsetEnd)
168 var wrapperElement = documentElement.DocumentNodeElement.create({tag: params._with.tag, klass: params._with.klass});
169 textNode1.after(wrapperElement);
172 if(prefixOutside.length > 0)
173 wrapperElement.before({text:prefixOutside});
175 var core = textNode1.getText().substr(params.offsetStart, params.offsetEnd - params.offsetStart);
176 wrapperElement.append({text: core});
179 if(prefixInside.length > 0)
180 wrapperElement.append({text: prefixInside});
181 for(var i = idx1 + 1; i < idx2; i++) {
182 wrapperElement.append(childrenInside[i]);
184 if(suffixInside.length > 0)
185 wrapperElement.append({text: suffixInside});
187 if(suffixOutside.length > 0)
188 wrapperElement.after({text: suffixOutside});
189 return wrapperElement;
191 getDocumentElement: function(from) {
192 if(from instanceof HTMLElement || from instanceof Text) {
193 return documentElement.DocumentElement.fromHTMLElement(from, this);
196 getCursor: function() {
197 return new Cursor(this);
204 highlightElement: function(element) {
205 this.wrapper.find('.highlighted-element').removeClass('highlighted-element');
206 element.dom().addClass('highlighted-element');
209 dimElement: function(element) {
210 element.dom().removeClass('highlighted-element');
213 getCurrentNodeElement: function() {
214 return this.getDocumentElement(this.wrapper.find('.current-node-element')[0]);
217 getCurrentTextElement: function() {
218 return this.getDocumentElement(this.wrapper.find('.current-text-element')[0]);
223 setCurrentElement: function(element, params) {
224 params = _.extend({caretTo: 'end'}, params);
225 var findFirstDirectTextChild = function(e, nodeToLand) {
226 var byBrowser = this.getCursor().getPosition().element;
227 if(byBrowser && byBrowser.parent().sameNode(nodeToLand))
229 var children = e.children();
230 for(var i = 0; i < children.length; i++) {
231 if(children[i] instanceof documentElement.DocumentTextElement)
236 var _markAsCurrent = function(element) {
237 if(element instanceof documentElement.DocumentTextElement) {
238 this.wrapper.find('.current-text-element').removeClass('current-text-element');
239 element.dom().addClass('current-text-element');
241 this.wrapper.find('.current-node-element').removeClass('current-node-element')
242 element.dom().addClass('current-node-element');
243 this.publisher('currentElementChanged', element);
248 var isTextElement = element instanceof documentElement.DocumentTextElement,
249 nodeElementToLand = isTextElement ? element.parent() : element,
250 textElementToLand = isTextElement ? element : findFirstDirectTextChild(element, nodeElementToLand),
251 currentTextElement = this.getCurrentTextElement(),
252 currentNodeElement = this.getCurrentNodeElement();
254 if(currentTextElement && !(currentTextElement.sameNode(textElementToLand)))
255 this.wrapper.find('.current-text-element').removeClass('current-text-element');
257 if(textElementToLand) {
258 _markAsCurrent(textElementToLand);
259 if(params.caretTo || !textElementToLand.sameNode(this.getCursor().getPosition().element))
260 this._moveCaretToTextElement(textElementToLand, params.caretTo); // as method on element?
261 if(!(textElementToLand.sameNode(currentTextElement)))
262 this.publisher('currentTextElementSet', textElementToLand);
264 document.getSelection().removeAllRanges();
267 if(!(currentNodeElement && currentNodeElement.sameNode(nodeElementToLand))) {
268 _markAsCurrent(nodeElementToLand);
270 this.publisher('currentNodeElementSet', nodeElementToLand);
274 _moveCaretToTextElement: function(element, where) {
275 var range = document.createRange(),
276 node = element.dom().contents()[0];
278 if(typeof where !== 'number') {
279 range.selectNodeContents(node);
281 range.setStart(node, where);
284 var collapseArg = true;
287 range.collapse(collapseArg);
289 var selection = document.getSelection();
291 selection.removeAllRanges();
292 selection.addRange(range);
293 this.wrapper.focus(); // FF requires this for caret to be put where range colllapses, Chrome doesn't.
296 setCursorPosition: function(position) {
298 this._moveCaretToTextElement(position.element, position.offset);
302 var d = function(element, parent, level) {
303 console.log(element.getText ? 'text: ' + element.getText() : 'node: ' + element.getWlxmlTag());
304 var isElementNode = element instanceof documentElement.DocumentNodeElement;
305 parent.prepend(element.toXML(level));
307 var dom = $(parent.children()[0]),
308 elementChildren = element.children();
309 for(var i = elementChildren.length - 1; i >= 0; i--) {
310 d(elementChildren[i], dom, level + 1);
314 var parent = $('<div>');
315 d(this.doc(), parent, 0);
316 return parent.html();
320 $.extend(Canvas.prototype.list, {
321 create: function(params) {
322 if(!(params.element1.parent().sameNode(params.element2.parent())))
325 var parent = params.element1.parent();
327 if(parent.childIndex(params.element1) > parent.childIndex(params.element2)) {
328 var tmp = params.element1;
329 params.element1 = params.element2;
330 params.element2 = tmp;
333 var elementsToWrap = [];
335 var place = 'before';
337 parent.children().some(function(element) {
339 if(element.sameNode(params.element1))
341 if(place === 'inside') {
342 if(element instanceof documentElement.DocumentTextElement) {
343 element = element.wrapWithNodeElement({tag: 'div', klass: 'list.item'});
344 if(element.children()[0].sameNode(params.element1))
345 params.element1 = element;
347 element.setWlxmlClass('item');
348 elementsToWrap.push(element);
350 if(_e.sameNode(params.element2))
354 var listElement = documentElement.DocumentNodeElement.create({tag: 'div', klass: 'list-items' + (params.type === 'enum' ? '-enum' : '')});
357 if(parent.is('list')) {
358 listElement.wrapWithNodeElement({tag: 'div', klass: 'item'});
359 toret = listElement.parent();
364 params.element1.before(toret);
366 elementsToWrap.forEach(function(element) {
368 listElement.append(element);
371 extractItems: function(params) {
372 params = _.extend({merge: true}, params);
373 var list = params.element1.parent();
374 if(!list.is('list') || !(list.sameNode(params.element2.parent())))
377 var idx1 = list.childIndex(params.element1),
378 idx2 = list.childIndex(params.element2),
381 succeedingItems = [],
382 items = list.children(),
383 listIsNested = list.parent().getWlxmlClass() === 'item',
387 var tmp = idx1; idx1 = idx2; idx2 = tmp;
390 items.forEach(function(item, idx) {
392 precedingItems.push(item);
393 else if(idx >= idx1 && idx <= idx2) {
394 extractedItems.push(item);
397 succeedingItems.push(item);
401 var reference = listIsNested ? list.parent() : list;
402 if(succeedingItems.length === 0) {
403 var reference_orig = reference;
404 extractedItems.forEach(function(item) {
405 reference.after(item);
408 item.setWlxmlClass(null);
410 if(precedingItems.length === 0)
411 reference_orig.detach();
412 } else if(precedingItems.length === 0) {
413 extractedItems.forEach(function(item) {
414 reference.before(item);
416 item.setWlxmlClass(null);
419 extractedItems.forEach(function(item) {
420 reference.after(item);
422 item.setWlxmlClass(null);
425 var secondList = documentElement.DocumentNodeElement.create({tag: 'div', klass:'list-items'}, this),
429 toAdd = secondList.wrapWithNodeElement({tag: 'div', klass:'item'});
431 succeedingItems.forEach(function(item) {
432 secondList.append(item);
435 reference.after(toAdd);
437 if(!params.merge && listIsNested) {
438 return this.extractItems({element1: extractedItems[0], element2: extractedItems[extractedItems.length-1]});
442 areItemsOfTheSameList: function(params) {
443 var e1 = params.element1,
444 e2 = params.element2;
445 return e1.parent().sameNode(e2.parent())
446 && e1.parent().is('list');
451 var Cursor = function(canvas) {
452 this.canvas = canvas;
455 $.extend(Cursor.prototype, {
456 isSelecting: function() {
457 var selection = window.getSelection();
458 return !selection.isCollapsed;
460 isSelectingWithinElement: function() {
461 return this.isSelecting() && this.getSelectionStart().element.sameNode(this.getSelectionEnd().element);
463 isSelectingSiblings: function() {
464 return this.isSelecting() && this.getSelectionStart().element.parent().sameNode(this.getSelectionEnd().element.parent());
466 getPosition: function() {
467 return this.getSelectionAnchor();
469 getSelectionStart: function() {
470 return this.getSelectionBoundry('start');
472 getSelectionEnd: function() {
473 return this.getSelectionBoundry('end');
475 getSelectionAnchor: function() {
476 return this.getSelectionBoundry('anchor');
478 getSelectionFocus: function() {
479 return this.getSelectionBoundry('focus');
481 getSelectionBoundry: function(which) {
482 var selection = window.getSelection(),
483 anchorElement = this.canvas.getDocumentElement(selection.anchorNode),
484 focusElement = this.canvas.getDocumentElement(selection.focusNode);
486 if(anchorElement instanceof documentElement.DocumentNodeElement || focusElement instanceof documentElement.DocumentNodeElement)
489 if(which === 'anchor') {
491 element: anchorElement,
492 offset: selection.anchorOffset,
493 offsetAtBeginning: selection.anchorOffset === 0,
494 offsetAtEnd: anchorElement && anchorElement.getText().length === selection.anchorOffset
497 if(which === 'focus') {
499 element: focusElement,
500 offset: selection.focusOffset,
501 offsetAtBeginning: selection.focusOffset === 0,
502 offsetAtEnd: focusElement && focusElement.getText().length === selection.focusOffset
509 if(anchorElement.parent().sameNode(focusElement.parent())) {
510 var parent = anchorElement.parent(),
511 anchorFirst = parent.childIndex(anchorElement) < parent.childIndex(focusElement);
513 if(which === 'start') {
514 element = anchorElement;
515 offset = selection.anchorOffset;
517 else if(which === 'end') {
518 element = focusElement,
519 offset = selection.focusOffset;
522 if(which === 'start') {
523 element = focusElement,
524 offset = selection.focusOffset
526 else if(which === 'end') {
527 element = anchorElement;
528 offset = selection.anchorOffset;
532 // TODO: Handle order via https://developer.mozilla.org/en-US/docs/Web/API/Node.compareDocumentPosition
533 if(which === 'start') {
534 element = anchorElement;
535 offset = selection.anchorOffset
537 element = focusElement;
538 offset = selection.focusOffset
545 offsetAtBeginning: offset === 0,
546 offsetAtEnd: element.getText().length === offset
552 fromXML: function(xml, publisher) {
553 return new Canvas(xml, publisher);