2 'libs/jquery-1.9.1.min',
4 'modules/documentCanvas/classAttributes',
5 'modules/documentCanvas/canvas/utils',
6 'modules/documentCanvas/canvas/widgets'
7 ], function($, _, classAttributes, utils, widgets) {
12 // DocumentElement represents a text or an element node from WLXML document rendered inside Canvas
13 var DocumentElement = function(htmlElement, canvas) {
14 if(arguments.length === 0)
17 this._setupDOMHandler(htmlElement);
20 var elementTypeFromParams = function(params) {
21 return params.text !== undefined ? DocumentTextElement : DocumentNodeElement;
25 $.extend(DocumentElement, {
26 create: function(params, canvas) {
27 return elementTypeFromParams(params).create(params);
30 createDOM: function(params) {
31 return elementTypeFromParams(params).createDOM(params);
34 fromHTMLElement: function(htmlElement, canvas) {
35 var $element = $(htmlElement);
36 if(htmlElement.nodeType === Node.ELEMENT_NODE && $element.attr('document-node-element') !== undefined)
37 return DocumentNodeElement.fromHTMLElement(htmlElement, canvas);
38 if($element.attr('document-text-element') !== undefined || (htmlElement.nodeType === Node.TEXT_NODE && $element.parent().attr('document-text-element') !== undefined))
39 return DocumentTextElement.fromHTMLElement(htmlElement, canvas);
44 $.extend(DocumentElement.prototype, {
45 _setupDOMHandler: function(htmlElement) {
46 this.$element = $(htmlElement);
52 var parents = this.$element.parents('[document-node-element]');
54 return DocumentElement.fromHTMLElement(parents[0], this.canvas);
60 parent = this.parent();
63 parent = parent.parent();
68 sameNode: function(other) {
69 return other && (typeof other === typeof this) && other.dom()[0] === this.dom()[0];
72 wrapWithNodeElement: function(wlxmlNode) {
73 var wrapper = DocumentNodeElement.create({tag: wlxmlNode.tag, klass: wlxmlNode.klass});
74 this.dom().replaceWith(wrapper.dom());
84 markAsCurrent: function() {
85 this.canvas.markAsCurrent(this);
88 getVerticallyFirstTextElement: function() {
90 this.children().some(function(child) {
91 if(!child.isVisible())
92 return false; // continue
93 if(child instanceof DocumentTextElement) {
97 toret = child.getVerticallyFirstTextElement();
105 isVisible: function() {
106 return this instanceof DocumentTextElement || this.getWlxmlTag() !== 'metadata';
111 // DocumentNodeElement represents an element node from WLXML document rendered inside Canvas
112 var DocumentNodeElement = function(htmlElement, canvas) {
113 DocumentElement.call(this, htmlElement, canvas);
116 var getDisplayStyle = function(tag, klass) {
117 if(tag === 'metadata')
124 $.extend(DocumentNodeElement, {
125 createDOM: function(params) {
126 var dom = $('<div document-node-element>'),
127 container = $('<div document-element-content>');
129 container.attr('wlxml-tag', params.tag);
131 container.attr('wlxml-class', params.klass.replace(/\./g, '-'));
133 _.keys(params.meta).forEach(function(key) {
134 dom.attr('wlxml-meta-'+key, params.meta[key]);
137 dom.data('other-attrs', params.others);
140 var displayStyle = getDisplayStyle(params.tag, params.klass);
141 dom.css('display', displayStyle);
142 container.css('display', displayStyle);
144 var widgetsContainer = $('<div class="canvas-widgets" contenteditable="false">');
145 widgetsContainer.append(widgets.labelWidget(params.tag, params.klass));
146 dom.append(widgetsContainer);
148 // Make sure widgets aren't navigable with arrow keys
149 widgetsContainer.find('*').add(widgetsContainer).attr('tabindex', -1);
151 dom.append(container);
153 if(params.rawChildren) {
154 container.append(params.rawChildren);
159 create: function(params, canvas) {
160 return this.fromHTMLElement(this.createDOM(params)[0]);
163 fromHTMLElement: function(htmlElement, canvas) {
164 return new this(htmlElement, canvas);
168 var manipulate = function(e, params, action) {
170 if(params instanceof DocumentElement) {
173 element = DocumentElement.create(params);
175 var target = action === 'append' ? e._container() : e.dom();
176 target[action](element.dom());
180 DocumentNodeElement.prototype = new DocumentElement();
183 $.extend(DocumentNodeElement.prototype, {
184 _container: function() {
185 return this.dom().children('[document-element-content]');
188 var dom = this.dom(),
189 args = Array.prototype.slice.call(arguments, 0);
190 if(args.length === 2 && args[1] === undefined)
191 return dom.removeData(args[0]);
192 return dom.data.apply(dom, arguments);
194 toXML: function(level) {
195 var node = $('<' + this.getWlxmlTag() + '>');
197 if(this.getWlxmlClass())
198 node.attr('class', this.getWlxmlClass());
199 var meta = this.getWlxmlMetaAttrs();
200 meta.forEach(function(attr) {
202 node.attr('meta-' + attr.name, attr.value);
204 _.keys(this.data('other-attrs') || {}).forEach(function(key) {
205 node.attr(key, this.data('other-attrs')[key]);
208 var addFormatting = function() {
209 var toret = $('<div>');
210 var formattings = {};
212 if(this.data('orig-before') !== undefined) {
213 if(this.data('orig-before')) {
214 toret.prepend(document.createTextNode(this.data('orig-before')));
216 } else if(level && this.getWlxmlTag() !== 'span') {
217 toret.append('\n' + (new Array(level * 2 + 1)).join(' '));
222 if(this.data('orig-after')) {
223 toret.append(document.createTextNode(this.data('orig-after')));
227 if(this.data('orig-begin')) {
228 node.prepend(this.data('orig-begin'));
229 formattings.begin = true;
232 if(this.data('orig-end') !== undefined) {
233 if(this.data('orig-end')) {
234 node.append(this.data('orig-end'));
236 } else if(this.getWlxmlTag() !== 'span' && children.length){
237 node.append('\n' + (new Array(level * 2 + 1)).join(' '));
240 return {parts: toret.contents(), formattings: formattings};
245 var children = this.children(),
248 var formatting = addFormatting(node);
250 for(var i = children.length - 1; i >= 0; i--) {
251 childParts = children[i].toXML(level + 1);
252 if(typeof childParts === 'string')
253 childParts = [document.createTextNode(childParts)];
255 if(formatting.formattings.begin) {
256 $(node.contents()[0]).after(childParts);
258 node.prepend(childParts);
260 return formatting.parts;
262 append: function(params) {
263 if(params.tag !== 'span')
264 this.data('orig-end', undefined);
265 return manipulate(this, params, 'append');
267 before: function(params) {
268 return manipulate(this, params, 'before');
271 after: function(params) {
272 return manipulate(this, params, 'after');
274 children: function() {
276 if(this instanceof DocumentTextElement)
280 var elementContent = this._container().contents();
282 elementContent.each(function(idx) {
283 var childElement = DocumentElement.fromHTMLElement(this, element.canvas);
284 if(childElement === undefined)
286 if(idx === 0 && elementContent.length > 1 && elementContent[1].nodeType === Node.ELEMENT_NODE && (childElement instanceof DocumentTextElement) && $.trim($(this).text()) === '')
288 if(idx > 0 && childElement instanceof DocumentTextElement) {
289 if(toret[toret.length-1] instanceof DocumentNodeElement && $.trim($(this).text()) === '')
292 toret.push(childElement);
296 childIndex: function(child) {
297 var children = this.children(),
299 children.forEach(function(c, idx) {
300 if(c.sameNode(child)) {
307 getWlxmlTag: function() {
308 return this._container().attr('wlxml-tag');
310 setWlxmlTag: function(tag) {
311 this._container().attr('wlxml-tag', tag);
312 this._updateDisplayStyle();
314 getWlxmlClass: function() {
315 var klass = this._container().attr('wlxml-class');
317 return klass.replace(/-/g, '.');
320 setWlxmlClass: function(klass) {
321 this.getWlxmlMetaAttrs().forEach(function(attr) {
322 if(!classAttributes.hasMetaAttr(klass, attr.name))
323 this.dom().removeAttr('wlxml-meta-' + attr.name);
327 this._container().attr('wlxml-class', klass.replace(/\./g, '-'));
329 this._container().removeAttr('wlxml-class');
330 this._updateDisplayStyle();
332 _updateDisplayStyle: function() {
333 var displayStyle = getDisplayStyle(this.getWlxmlTag, this.getWlxmlClass);
334 this.dom().css('display', displayStyle);
335 this._container().css('display', displayStyle);
338 if(what === 'list' && _.contains(['list.items', 'list.items.enum'], this.getWlxmlClass()))
343 getWlxmlMetaAttr: function(attr) {
344 return this.dom().attr('wlxml-meta-'+attr);
347 getWlxmlMetaAttrs: function() {
349 var attrList = classAttributes.getMetaAttrsList(this.getWlxmlClass());
350 attrList.all.forEach(function(attr) {
351 toret.push({name: attr.name, value: this.getWlxmlMetaAttr(attr.name) || ''});
356 setWlxmlMetaAttr: function(attr, value) {
357 this.dom().attr('wlxml-meta-'+attr, value);
360 toggleLabel: function(toggle) {
361 var displayCss = toggle ? 'inline-block' : 'none';
362 var label = this.dom().children('.canvas-widgets').find('.canvas-widget-label');
363 label.css('display', displayCss);
364 this.toggleHighlight(toggle);
367 toggleHighlight: function(toogle) {
368 this._container().toggleClass('highlighted-element');
373 // DocumentNodeElement represents a text node from WLXML document rendered inside Canvas
374 var DocumentTextElement = function(htmlElement, canvas) {
375 DocumentElement.call(this, htmlElement, canvas);
378 $.extend(DocumentTextElement, {
379 createDOM: function(params) {
381 .attr('document-text-element', '')
382 .text(params.text || utils.unicode.ZWS);
385 create: function(params, canvas) {
386 return this.fromHTMLElement(this.createDOM(params)[0]);
389 fromHTMLElement: function(htmlElement, canvas) {
390 return new this(htmlElement, canvas);
392 isContentContainer: function(htmlElement) {
393 return htmlElement.nodeType === Node.TEXT_NODE && $(htmlElement).parent().is('[document-text-element]');
397 DocumentTextElement.prototype = new DocumentElement();
399 $.extend(DocumentTextElement.prototype, {
400 toXML: function(parent) {
401 return this.getText();
403 _setupDOMHandler: function(htmlElement) {
404 var $element = $(htmlElement);
405 if(htmlElement.nodeType === Node.TEXT_NODE)
406 this.$element = $element.parent();
408 this.$element = $element;
410 setText: function(text) {
411 this.dom().contents()[0].data = text;
413 getText: function() {
414 return this.dom().text().replace(utils.unicode.ZWS, '');
416 isEmpty: function() {
417 // Having at least Zero Width Space is guaranteed be Content Observer
418 return this.dom().contents()[0].data === utils.unicode.ZWS;
420 after: function(params) {
421 if(params instanceof DocumentTextElement || params.text)
424 if(params instanceof DocumentNodeElement) {
427 element = DocumentNodeElement.create(params);
429 this.dom().wrap('<div>');
430 this.dom().parent().after(element.dom());
434 before: function(params) {
435 if(params instanceof DocumentTextElement || params.text)
438 if(params instanceof DocumentNodeElement) {
441 element = DocumentNodeElement.create(params);
443 this.dom().wrap('<div>');
444 this.dom().parent().before(element.dom());
448 wrapWithNodeElement: function(wlxmlNode) {
449 if(typeof wlxmlNode.start === 'number' && typeof wlxmlNode.end === 'number') {
450 return this.canvas.wrapText({
451 inside: this.parent(),
452 textNodeIdx: this.parent().childIndex(this),
453 offsetStart: Math.min(wlxmlNode.start, wlxmlNode.end),
454 offsetEnd: Math.max(wlxmlNode.start, wlxmlNode.end),
455 _with: {tag: wlxmlNode.tag, klass: wlxmlNode.klass}
458 return DocumentElement.prototype.wrapWithNodeElement.call(this, wlxmlNode);
462 var parent = this.parent(),
464 if(parent.children().length === 1) {
465 toret = parent.parent();
466 var grandParent = parent.parent();
468 var grandParentChildren = grandParent.children(),
469 idx = grandParent.childIndex(parent),
470 prev = idx - 1 > -1 ? grandParentChildren[idx-1] : null,
471 next = idx + 1 < grandParentChildren.length ? grandParentChildren[idx+1] : null;
473 prev.setText(prev.getText() + this.getText() + next.getText());
475 } else if (prev || next) {
476 var target = prev ? prev : next;
477 target.setText(target.getText() + this.getText());
488 split: function(params) {
489 var parentElement = this.parent(),
490 myIdx = parentElement.childIndex(this),
491 myCanvas = this.canvas,
493 succeedingChildren = [],
495 prefix = this.getText().substr(0, params.offset),
496 suffix = this.getText().substr(params.offset);
498 parentElement.children().forEach(function(child) {
500 succeedingChildren.push(child);
501 if(child.sameNode(thisElement))
505 if(prefix.length > 0)
506 this.setText(prefix);
510 var newElement = DocumentNodeElement.create({tag: parentElement.getWlxmlTag(), klass: parentElement.getWlxmlClass()}, myCanvas);
511 parentElement.after(newElement);
513 if(suffix.length > 0)
514 newElement.append({text: suffix});
515 succeedingChildren.forEach(function(child) {
516 newElement.append(child);
519 return {first: parentElement, second: newElement};
521 divide: function(params) {
522 var myText = this.getText();
524 if(params.offset === myText.length)
525 return this.after(params);
526 if(params.offset === 0)
527 return this.before(params);
529 var lhsText = myText.substr(0, params.offset),
530 rhsText = myText.substr(params.offset),
531 newElement = DocumentNodeElement.create({tag: params.tag, klass: params.klass}, this.canvas),
532 rhsTextElement = DocumentTextElement.create({text: rhsText});
534 this.setText(lhsText);
535 this.after(newElement);
536 newElement.after(rhsTextElement);
540 toggleHighlight: function() {
541 // do nothing for now
546 DocumentElement: DocumentElement,
547 DocumentNodeElement: DocumentNodeElement,
548 DocumentTextElement: DocumentTextElement