2 'libs/jquery-1.9.1.min',
4 'modules/documentCanvas/classAttributes',
5 'modules/documentCanvas/canvas/utils',
6 'modules/documentCanvas/canvas/widgets',
7 'modules/documentCanvas/canvas/wlxmlManagers'
8 ], function($, _, classAttributes, utils, widgets, wlxmlManagers) {
13 // DocumentElement represents a text or an element node from WLXML document rendered inside Canvas
14 var DocumentElement = function(htmlElement, canvas) {
15 if(arguments.length === 0)
18 this._setupDOMHandler(htmlElement);
21 var elementTypeFromParams = function(params) {
22 return params.text !== undefined ? DocumentTextElement : DocumentNodeElement;
26 $.extend(DocumentElement, {
27 create: function(params, canvas) {
28 return elementTypeFromParams(params).create(params);
31 createDOM: function(params) {
32 return elementTypeFromParams(params).createDOM(params);
35 fromHTMLElement: function(htmlElement, canvas) {
36 var $element = $(htmlElement);
37 if(htmlElement.nodeType === Node.ELEMENT_NODE && $element.attr('document-node-element') !== undefined)
38 return DocumentNodeElement.fromHTMLElement(htmlElement, canvas);
39 if($element.attr('document-text-element') !== undefined || (htmlElement.nodeType === Node.TEXT_NODE && $element.parent().attr('document-text-element') !== undefined))
40 return DocumentTextElement.fromHTMLElement(htmlElement, canvas);
45 $.extend(DocumentElement.prototype, {
46 _setupDOMHandler: function(htmlElement) {
47 this.$element = $(htmlElement);
50 return this.canvas !== undefined;
56 var parents = this.$element.parents('[document-node-element]');
58 return DocumentElement.fromHTMLElement(parents[0], this.canvas);
64 parent = this.parent();
67 parent = parent.parent();
72 sameNode: function(other) {
73 return other && (typeof other === typeof this) && other.dom()[0] === this.dom()[0];
76 wrapWithNodeElement: function(wlxmlNode) {
77 var wrapper = DocumentNodeElement.create({tag: wlxmlNode.tag, klass: wlxmlNode.klass});
78 this.dom().replaceWith(wrapper.dom());
89 markAsCurrent: function() {
90 this.canvas.markAsCurrent(this);
93 getVerticallyFirstTextElement: function() {
95 this.children().some(function(child) {
96 if(!child.isVisible())
97 return false; // continue
98 if(child instanceof DocumentTextElement) {
100 return true; // break
102 toret = child.getVerticallyFirstTextElement();
104 return true; // break
110 isVisible: function() {
111 return this instanceof DocumentTextElement || this.getWlxmlTag() !== 'metadata';
116 // DocumentNodeElement represents an element node from WLXML document rendered inside Canvas
117 var DocumentNodeElement = function(htmlElement, canvas) {
118 DocumentElement.call(this, htmlElement, canvas);
121 $.extend(DocumentNodeElement, {
122 createDOM: function(params, canvas) {
124 .attr('document-node-element', ''),
125 widgetsContainer = $('<div>')
126 .addClass('canvas-widgets')
127 .attr('contenteditable', false),
128 container = $('<div>')
129 .attr('document-element-content', '');
131 dom.append(widgetsContainer, container);
132 // Make sure widgets aren't navigable with arrow keys
133 widgetsContainer.find('*').add(widgetsContainer).attr('tabindex', -1);
135 var element = this.fromHTMLElement(dom[0], canvas);
137 element.setWlxmlTag(params.tag);
139 element.setWlxmlClass(params.klass);
141 _.keys(params.meta).forEach(function(key) {
142 element.setWlxmlMetaAttr(key, params.meta[key]);
145 element.data('other-attrs', params.others);
147 if(params.rawChildren) {
148 container.append(params.rawChildren);
153 create: function(params, canvas) {
154 return this.fromHTMLElement(this.createDOM(params, canvas)[0], canvas);
157 fromHTMLElement: function(htmlElement, canvas) {
158 return new this(htmlElement, canvas);
162 var manipulate = function(e, params, action) {
164 if(params instanceof DocumentElement) {
167 element = DocumentElement.create(params);
169 var target = (action === 'append' || action === 'prepend') ? e._container() : e.dom();
170 target[action](element.dom());
174 DocumentNodeElement.prototype = new DocumentElement();
177 $.extend(DocumentNodeElement.prototype, {
178 _container: function() {
179 return this.dom().children('[document-element-content]');
182 var dom = this.dom(),
183 args = Array.prototype.slice.call(arguments, 0);
184 if(args.length === 2 && args[1] === undefined)
185 return dom.removeData(args[0]);
186 return dom.data.apply(dom, arguments);
188 toXML: function(level) {
189 var node = $('<' + this.getWlxmlTag() + '>');
191 if(this.getWlxmlClass())
192 node.attr('class', this.getWlxmlClass());
193 var meta = this.getWlxmlMetaAttrs();
194 meta.forEach(function(attr) {
196 node.attr('meta-' + attr.name, attr.value);
198 _.keys(this.data('other-attrs') || {}).forEach(function(key) {
199 node.attr(key, this.data('other-attrs')[key]);
202 var addFormatting = function() {
203 var toret = $('<div>');
204 var formattings = {};
206 if(this.data('orig-before') !== undefined) {
207 if(this.data('orig-before')) {
208 toret.prepend(document.createTextNode(this.data('orig-before')));
210 } else if(level && this.getWlxmlTag() !== 'span') {
211 toret.append('\n' + (new Array(level * 2 + 1)).join(' '));
216 if(this.data('orig-after')) {
217 toret.append(document.createTextNode(this.data('orig-after')));
221 if(this.data('orig-begin')) {
222 node.prepend(this.data('orig-begin'));
223 formattings.begin = true;
226 if(this.data('orig-end') !== undefined) {
227 if(this.data('orig-end')) {
228 node.append(this.data('orig-end'));
230 } else if(this.getWlxmlTag() !== 'span' && children.length){
231 node.append('\n' + (new Array(level * 2 + 1)).join(' '));
234 return {parts: toret.contents(), formattings: formattings};
239 var children = this.children(),
242 var formatting = addFormatting(node);
244 for(var i = children.length - 1; i >= 0; i--) {
245 childParts = children[i].toXML(level + 1);
246 if(typeof childParts === 'string')
247 childParts = [document.createTextNode(childParts)];
249 if(formatting.formattings.begin) {
250 $(node.contents()[0]).after(childParts);
252 node.prepend(childParts);
254 return formatting.parts;
256 append: function(params) {
257 if(params.tag !== 'span')
258 this.data('orig-end', undefined);
259 return manipulate(this, params, 'append');
261 prepend: function(params) {
262 return manipulate(this, params, 'prepend');
264 before: function(params) {
265 return manipulate(this, params, 'before');
268 after: function(params) {
269 return manipulate(this, params, 'after');
271 children: function() {
273 if(this instanceof DocumentTextElement)
277 var elementContent = this._container().contents();
279 elementContent.each(function(idx) {
280 var childElement = DocumentElement.fromHTMLElement(this, element.canvas);
281 if(childElement === undefined)
283 if(idx === 0 && elementContent.length > 1 && elementContent[1].nodeType === Node.ELEMENT_NODE && (childElement instanceof DocumentTextElement) && $.trim($(this).text()) === '')
285 if(idx > 0 && childElement instanceof DocumentTextElement) {
286 if(toret[toret.length-1] instanceof DocumentNodeElement && $.trim($(this).text()) === '')
289 toret.push(childElement);
293 childIndex: function(child) {
294 var children = this.children(),
296 children.forEach(function(c, idx) {
297 if(c.sameNode(child)) {
304 getWlxmlTag: function() {
305 return this._container().attr('wlxml-tag');
307 setWlxmlTag: function(tag) {
308 if(tag === this.getWlxmlTag())
311 this._container().attr('wlxml-tag', tag);
312 this._updateWlxmlManager();
314 getWlxmlClass: function() {
315 var klass = this._container().attr('wlxml-class');
317 return klass.replace(/-/g, '.');
320 setWlxmlClass: function(klass) {
321 if(klass === this.getWlxmlClass())
324 this.getWlxmlMetaAttrs().forEach(function(attr) {
325 if(!classAttributes.hasMetaAttr(klass, attr.name))
326 this.dom().removeAttr('wlxml-meta-' + attr.name);
330 this._container().attr('wlxml-class', klass.replace(/\./g, '-'));
332 this._container().removeAttr('wlxml-class');
333 this._updateWlxmlManager();
335 _updateWlxmlManager: function() {
336 var manager = wlxmlManagers.getFor(this);
337 this.data('_wlxmlManager', manager);
341 if(what === 'list' && _.contains(['list.items', 'list.items.enum'], this.getWlxmlClass()))
346 getWlxmlMetaAttr: function(attr) {
347 return this.dom().attr('wlxml-meta-'+attr);
350 getWlxmlMetaAttrs: function() {
352 var attrList = classAttributes.getMetaAttrsList(this.getWlxmlClass());
353 attrList.all.forEach(function(attr) {
354 toret.push({name: attr.name, value: this.getWlxmlMetaAttr(attr.name) || ''});
359 setWlxmlMetaAttr: function(attr, value) {
360 this.dom().attr('wlxml-meta-'+attr, value);
363 toggleLabel: function(toggle) {
364 var displayCss = toggle ? 'inline-block' : 'none';
365 var label = this.dom().children('.canvas-widgets').find('.canvas-widget-label');
366 label.css('display', displayCss);
367 this.toggleHighlight(toggle);
370 toggleHighlight: function(toogle) {
371 this._container().toggleClass('highlighted-element');
374 toggle: function(toggle) {
375 var mng = this.data('_wlxmlManager');
383 // DocumentNodeElement represents a text node from WLXML document rendered inside Canvas
384 var DocumentTextElement = function(htmlElement, canvas) {
385 DocumentElement.call(this, htmlElement, canvas);
388 $.extend(DocumentTextElement, {
389 createDOM: function(params) {
391 .attr('document-text-element', '')
392 .text(params.text || utils.unicode.ZWS);
395 create: function(params, canvas) {
396 return this.fromHTMLElement(this.createDOM(params)[0]);
399 fromHTMLElement: function(htmlElement, canvas) {
400 return new this(htmlElement, canvas);
402 isContentContainer: function(htmlElement) {
403 return htmlElement.nodeType === Node.TEXT_NODE && $(htmlElement).parent().is('[document-text-element]');
407 DocumentTextElement.prototype = new DocumentElement();
409 $.extend(DocumentTextElement.prototype, {
410 toXML: function(parent) {
411 return this.getText();
413 _setupDOMHandler: function(htmlElement) {
414 var $element = $(htmlElement);
415 if(htmlElement.nodeType === Node.TEXT_NODE)
416 this.$element = $element.parent();
418 this.$element = $element;
420 setText: function(text) {
421 this.dom().contents()[0].data = text;
423 getText: function() {
424 return this.dom().text().replace(utils.unicode.ZWS, '');
426 isEmpty: function() {
427 // Having at least Zero Width Space is guaranteed be Content Observer
428 return this.dom().contents()[0].data === utils.unicode.ZWS;
430 after: function(params) {
431 if(params instanceof DocumentTextElement || params.text)
434 if(params instanceof DocumentNodeElement) {
437 element = DocumentNodeElement.create(params, this.canvas);
439 this.dom().wrap('<div>');
440 this.dom().parent().after(element.dom());
444 before: function(params) {
445 if(params instanceof DocumentTextElement || params.text)
448 if(params instanceof DocumentNodeElement) {
451 element = DocumentNodeElement.create(params, this.canvas);
453 this.dom().wrap('<div>');
454 this.dom().parent().before(element.dom());
458 wrapWithNodeElement: function(wlxmlNode) {
459 if(typeof wlxmlNode.start === 'number' && typeof wlxmlNode.end === 'number') {
460 return this.canvas.wrapText({
461 inside: this.parent(),
462 textNodeIdx: this.parent().childIndex(this),
463 offsetStart: Math.min(wlxmlNode.start, wlxmlNode.end),
464 offsetEnd: Math.max(wlxmlNode.start, wlxmlNode.end),
465 _with: {tag: wlxmlNode.tag, klass: wlxmlNode.klass}
468 return DocumentElement.prototype.wrapWithNodeElement.call(this, wlxmlNode);
472 var parent = this.parent(),
474 if(parent.children().length === 1) {
475 toret = parent.parent();
476 var grandParent = parent.parent();
478 var grandParentChildren = grandParent.children(),
479 idx = grandParent.childIndex(parent),
480 prev = idx - 1 > -1 ? grandParentChildren[idx-1] : null,
481 next = idx + 1 < grandParentChildren.length ? grandParentChildren[idx+1] : null;
483 prev = (prev instanceof DocumentTextElement) ? prev : null;
484 next = (next instanceof DocumentTextElement) ? next : null;
487 prev.setText(prev.getText() + this.getText() + next.getText());
489 } else if (prev || next) {
490 var target = prev ? prev : next;
491 target.setText(target.getText() + this.getText());
502 split: function(params) {
503 var parentElement = this.parent(),
504 myIdx = parentElement.childIndex(this),
505 myCanvas = this.canvas,
507 succeedingChildren = [],
509 prefix = this.getText().substr(0, params.offset),
510 suffix = this.getText().substr(params.offset);
512 parentElement.children().forEach(function(child) {
514 succeedingChildren.push(child);
515 if(child.sameNode(thisElement))
519 if(prefix.length > 0)
520 this.setText(prefix);
524 var newElement = DocumentNodeElement.create({tag: parentElement.getWlxmlTag(), klass: parentElement.getWlxmlClass()}, myCanvas);
525 parentElement.after(newElement);
527 if(suffix.length > 0)
528 newElement.append({text: suffix});
529 succeedingChildren.forEach(function(child) {
530 newElement.append(child);
533 return {first: parentElement, second: newElement};
535 divide: function(params) {
536 var myText = this.getText();
538 if(params.offset === myText.length)
539 return this.after(params);
540 if(params.offset === 0)
541 return this.before(params);
543 var lhsText = myText.substr(0, params.offset),
544 rhsText = myText.substr(params.offset),
545 newElement = DocumentNodeElement.create({tag: params.tag, klass: params.klass}, this.canvas),
546 rhsTextElement = DocumentTextElement.create({text: rhsText});
548 this.setText(lhsText);
549 this.after(newElement);
550 newElement.after(rhsTextElement);
554 toggleHighlight: function() {
555 // do nothing for now
560 DocumentElement: DocumentElement,
561 DocumentNodeElement: DocumentNodeElement,
562 DocumentTextElement: DocumentTextElement