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) {
127 .attr('document-node-element', ''),
128 widgetsContainer = $('<div>')
129 .addClass('canvas-widgets')
130 .attr('contenteditable', false),
131 container = $('<div>')
132 .attr('document-element-content', '');
134 dom.append(widgetsContainer, container);
135 // Make sure widgets aren't navigable with arrow keys
136 widgetsContainer.find('*').add(widgetsContainer).attr('tabindex', -1);
138 var element = this.fromHTMLElement(dom[0]);
140 element.setWlxmlTag(params.tag);
142 element.setWlxmlClass(params.klass);
144 _.keys(params.meta).forEach(function(key) {
145 element.setWlxmlMetaAttr(key, params.meta[key]);
148 element.data('other-attrs', params.others);
149 widgetsContainer.append(widgets.labelWidget(params.tag, params.klass));
150 if(params.rawChildren) {
151 container.append(params.rawChildren);
156 create: function(params, canvas) {
157 return this.fromHTMLElement(this.createDOM(params)[0]);
160 fromHTMLElement: function(htmlElement, canvas) {
161 return new this(htmlElement, canvas);
165 var manipulate = function(e, params, action) {
167 if(params instanceof DocumentElement) {
170 element = DocumentElement.create(params);
172 var target = action === 'append' ? e._container() : e.dom();
173 target[action](element.dom());
177 DocumentNodeElement.prototype = new DocumentElement();
180 $.extend(DocumentNodeElement.prototype, {
181 _container: function() {
182 return this.dom().children('[document-element-content]');
185 var dom = this.dom(),
186 args = Array.prototype.slice.call(arguments, 0);
187 if(args.length === 2 && args[1] === undefined)
188 return dom.removeData(args[0]);
189 return dom.data.apply(dom, arguments);
191 toXML: function(level) {
192 var node = $('<' + this.getWlxmlTag() + '>');
194 if(this.getWlxmlClass())
195 node.attr('class', this.getWlxmlClass());
196 var meta = this.getWlxmlMetaAttrs();
197 meta.forEach(function(attr) {
199 node.attr('meta-' + attr.name, attr.value);
201 _.keys(this.data('other-attrs') || {}).forEach(function(key) {
202 node.attr(key, this.data('other-attrs')[key]);
205 var addFormatting = function() {
206 var toret = $('<div>');
207 var formattings = {};
209 if(this.data('orig-before') !== undefined) {
210 if(this.data('orig-before')) {
211 toret.prepend(document.createTextNode(this.data('orig-before')));
213 } else if(level && this.getWlxmlTag() !== 'span') {
214 toret.append('\n' + (new Array(level * 2 + 1)).join(' '));
219 if(this.data('orig-after')) {
220 toret.append(document.createTextNode(this.data('orig-after')));
224 if(this.data('orig-begin')) {
225 node.prepend(this.data('orig-begin'));
226 formattings.begin = true;
229 if(this.data('orig-end') !== undefined) {
230 if(this.data('orig-end')) {
231 node.append(this.data('orig-end'));
233 } else if(this.getWlxmlTag() !== 'span' && children.length){
234 node.append('\n' + (new Array(level * 2 + 1)).join(' '));
237 return {parts: toret.contents(), formattings: formattings};
242 var children = this.children(),
245 var formatting = addFormatting(node);
247 for(var i = children.length - 1; i >= 0; i--) {
248 childParts = children[i].toXML(level + 1);
249 if(typeof childParts === 'string')
250 childParts = [document.createTextNode(childParts)];
252 if(formatting.formattings.begin) {
253 $(node.contents()[0]).after(childParts);
255 node.prepend(childParts);
257 return formatting.parts;
259 append: function(params) {
260 if(params.tag !== 'span')
261 this.data('orig-end', undefined);
262 return manipulate(this, params, 'append');
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 this._container().attr('wlxml-tag', tag);
309 this._updateDisplayStyle();
311 getWlxmlClass: function() {
312 var klass = this._container().attr('wlxml-class');
314 return klass.replace(/-/g, '.');
317 setWlxmlClass: function(klass) {
318 this.getWlxmlMetaAttrs().forEach(function(attr) {
319 if(!classAttributes.hasMetaAttr(klass, attr.name))
320 this.dom().removeAttr('wlxml-meta-' + attr.name);
324 this._container().attr('wlxml-class', klass.replace(/\./g, '-'));
326 this._container().removeAttr('wlxml-class');
327 this._updateDisplayStyle();
329 _updateDisplayStyle: function() {
330 var displayStyle = getDisplayStyle(this.getWlxmlTag(), this.getWlxmlClass());
331 this.dom().css('display', displayStyle);
332 this._container().css('display', displayStyle);
335 if(what === 'list' && _.contains(['list.items', 'list.items.enum'], this.getWlxmlClass()))
340 getWlxmlMetaAttr: function(attr) {
341 return this.dom().attr('wlxml-meta-'+attr);
344 getWlxmlMetaAttrs: function() {
346 var attrList = classAttributes.getMetaAttrsList(this.getWlxmlClass());
347 attrList.all.forEach(function(attr) {
348 toret.push({name: attr.name, value: this.getWlxmlMetaAttr(attr.name) || ''});
353 setWlxmlMetaAttr: function(attr, value) {
354 this.dom().attr('wlxml-meta-'+attr, value);
357 toggleLabel: function(toggle) {
358 var displayCss = toggle ? 'inline-block' : 'none';
359 var label = this.dom().children('.canvas-widgets').find('.canvas-widget-label');
360 label.css('display', displayCss);
361 this.toggleHighlight(toggle);
364 toggleHighlight: function(toogle) {
365 this._container().toggleClass('highlighted-element');
370 // DocumentNodeElement represents a text node from WLXML document rendered inside Canvas
371 var DocumentTextElement = function(htmlElement, canvas) {
372 DocumentElement.call(this, htmlElement, canvas);
375 $.extend(DocumentTextElement, {
376 createDOM: function(params) {
378 .attr('document-text-element', '')
379 .text(params.text || utils.unicode.ZWS);
382 create: function(params, canvas) {
383 return this.fromHTMLElement(this.createDOM(params)[0]);
386 fromHTMLElement: function(htmlElement, canvas) {
387 return new this(htmlElement, canvas);
389 isContentContainer: function(htmlElement) {
390 return htmlElement.nodeType === Node.TEXT_NODE && $(htmlElement).parent().is('[document-text-element]');
394 DocumentTextElement.prototype = new DocumentElement();
396 $.extend(DocumentTextElement.prototype, {
397 toXML: function(parent) {
398 return this.getText();
400 _setupDOMHandler: function(htmlElement) {
401 var $element = $(htmlElement);
402 if(htmlElement.nodeType === Node.TEXT_NODE)
403 this.$element = $element.parent();
405 this.$element = $element;
407 setText: function(text) {
408 this.dom().contents()[0].data = text;
410 getText: function() {
411 return this.dom().text().replace(utils.unicode.ZWS, '');
413 isEmpty: function() {
414 // Having at least Zero Width Space is guaranteed be Content Observer
415 return this.dom().contents()[0].data === utils.unicode.ZWS;
417 after: function(params) {
418 if(params instanceof DocumentTextElement || params.text)
421 if(params instanceof DocumentNodeElement) {
424 element = DocumentNodeElement.create(params);
426 this.dom().wrap('<div>');
427 this.dom().parent().after(element.dom());
431 before: function(params) {
432 if(params instanceof DocumentTextElement || params.text)
435 if(params instanceof DocumentNodeElement) {
438 element = DocumentNodeElement.create(params);
440 this.dom().wrap('<div>');
441 this.dom().parent().before(element.dom());
445 wrapWithNodeElement: function(wlxmlNode) {
446 if(typeof wlxmlNode.start === 'number' && typeof wlxmlNode.end === 'number') {
447 return this.canvas.wrapText({
448 inside: this.parent(),
449 textNodeIdx: this.parent().childIndex(this),
450 offsetStart: Math.min(wlxmlNode.start, wlxmlNode.end),
451 offsetEnd: Math.max(wlxmlNode.start, wlxmlNode.end),
452 _with: {tag: wlxmlNode.tag, klass: wlxmlNode.klass}
455 return DocumentElement.prototype.wrapWithNodeElement.call(this, wlxmlNode);
459 var parent = this.parent(),
461 if(parent.children().length === 1) {
462 toret = parent.parent();
463 var grandParent = parent.parent();
465 var grandParentChildren = grandParent.children(),
466 idx = grandParent.childIndex(parent),
467 prev = idx - 1 > -1 ? grandParentChildren[idx-1] : null,
468 next = idx + 1 < grandParentChildren.length ? grandParentChildren[idx+1] : null;
470 prev.setText(prev.getText() + this.getText() + next.getText());
472 } else if (prev || next) {
473 var target = prev ? prev : next;
474 target.setText(target.getText() + this.getText());
485 split: function(params) {
486 var parentElement = this.parent(),
487 myIdx = parentElement.childIndex(this),
488 myCanvas = this.canvas,
490 succeedingChildren = [],
492 prefix = this.getText().substr(0, params.offset),
493 suffix = this.getText().substr(params.offset);
495 parentElement.children().forEach(function(child) {
497 succeedingChildren.push(child);
498 if(child.sameNode(thisElement))
502 if(prefix.length > 0)
503 this.setText(prefix);
507 var newElement = DocumentNodeElement.create({tag: parentElement.getWlxmlTag(), klass: parentElement.getWlxmlClass()}, myCanvas);
508 parentElement.after(newElement);
510 if(suffix.length > 0)
511 newElement.append({text: suffix});
512 succeedingChildren.forEach(function(child) {
513 newElement.append(child);
516 return {first: parentElement, second: newElement};
518 divide: function(params) {
519 var myText = this.getText();
521 if(params.offset === myText.length)
522 return this.after(params);
523 if(params.offset === 0)
524 return this.before(params);
526 var lhsText = myText.substr(0, params.offset),
527 rhsText = myText.substr(params.offset),
528 newElement = DocumentNodeElement.create({tag: params.tag, klass: params.klass}, this.canvas),
529 rhsTextElement = DocumentTextElement.create({text: rhsText});
531 this.setText(lhsText);
532 this.after(newElement);
533 newElement.after(rhsTextElement);
537 toggleHighlight: function() {
538 // do nothing for now
543 DocumentElement: DocumentElement,
544 DocumentNodeElement: DocumentNodeElement,
545 DocumentTextElement: DocumentTextElement