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 widgetsContainer = $('<div class="canvas-widgets" contenteditable="false">'),
128 container = $('<div document-element-content>');
130 dom.append(widgetsContainer, container);
131 // Make sure widgets aren't navigable with arrow keys
132 widgetsContainer.find('*').add(widgetsContainer).attr('tabindex', -1);
134 var element = this.fromHTMLElement(dom[0]);
136 element.setWlxmlTag(params.tag);
138 element.setWlxmlClass(params.klass);
140 _.keys(params.meta).forEach(function(key) {
141 element.setWlxmlMetaAttr(key, params.meta[key]);
144 element.data('other-attrs', params.others);
145 widgetsContainer.append(widgets.labelWidget(params.tag, params.klass));
146 if(params.rawChildren) {
147 container.append(params.rawChildren);
152 create: function(params, canvas) {
153 return this.fromHTMLElement(this.createDOM(params)[0]);
156 fromHTMLElement: function(htmlElement, canvas) {
157 return new this(htmlElement, canvas);
161 var manipulate = function(e, params, action) {
163 if(params instanceof DocumentElement) {
166 element = DocumentElement.create(params);
168 var target = action === 'append' ? e._container() : e.dom();
169 target[action](element.dom());
173 DocumentNodeElement.prototype = new DocumentElement();
176 $.extend(DocumentNodeElement.prototype, {
177 _container: function() {
178 return this.dom().children('[document-element-content]');
181 var dom = this.dom(),
182 args = Array.prototype.slice.call(arguments, 0);
183 if(args.length === 2 && args[1] === undefined)
184 return dom.removeData(args[0]);
185 return dom.data.apply(dom, arguments);
187 toXML: function(level) {
188 var node = $('<' + this.getWlxmlTag() + '>');
190 if(this.getWlxmlClass())
191 node.attr('class', this.getWlxmlClass());
192 var meta = this.getWlxmlMetaAttrs();
193 meta.forEach(function(attr) {
195 node.attr('meta-' + attr.name, attr.value);
197 _.keys(this.data('other-attrs') || {}).forEach(function(key) {
198 node.attr(key, this.data('other-attrs')[key]);
201 var addFormatting = function() {
202 var toret = $('<div>');
203 var formattings = {};
205 if(this.data('orig-before') !== undefined) {
206 if(this.data('orig-before')) {
207 toret.prepend(document.createTextNode(this.data('orig-before')));
209 } else if(level && this.getWlxmlTag() !== 'span') {
210 toret.append('\n' + (new Array(level * 2 + 1)).join(' '));
215 if(this.data('orig-after')) {
216 toret.append(document.createTextNode(this.data('orig-after')));
220 if(this.data('orig-begin')) {
221 node.prepend(this.data('orig-begin'));
222 formattings.begin = true;
225 if(this.data('orig-end') !== undefined) {
226 if(this.data('orig-end')) {
227 node.append(this.data('orig-end'));
229 } else if(this.getWlxmlTag() !== 'span' && children.length){
230 node.append('\n' + (new Array(level * 2 + 1)).join(' '));
233 return {parts: toret.contents(), formattings: formattings};
238 var children = this.children(),
241 var formatting = addFormatting(node);
243 for(var i = children.length - 1; i >= 0; i--) {
244 childParts = children[i].toXML(level + 1);
245 if(typeof childParts === 'string')
246 childParts = [document.createTextNode(childParts)];
248 if(formatting.formattings.begin) {
249 $(node.contents()[0]).after(childParts);
251 node.prepend(childParts);
253 return formatting.parts;
255 append: function(params) {
256 if(params.tag !== 'span')
257 this.data('orig-end', undefined);
258 return manipulate(this, params, 'append');
260 before: function(params) {
261 return manipulate(this, params, 'before');
264 after: function(params) {
265 return manipulate(this, params, 'after');
267 children: function() {
269 if(this instanceof DocumentTextElement)
273 var elementContent = this._container().contents();
275 elementContent.each(function(idx) {
276 var childElement = DocumentElement.fromHTMLElement(this, element.canvas);
277 if(childElement === undefined)
279 if(idx === 0 && elementContent.length > 1 && elementContent[1].nodeType === Node.ELEMENT_NODE && (childElement instanceof DocumentTextElement) && $.trim($(this).text()) === '')
281 if(idx > 0 && childElement instanceof DocumentTextElement) {
282 if(toret[toret.length-1] instanceof DocumentNodeElement && $.trim($(this).text()) === '')
285 toret.push(childElement);
289 childIndex: function(child) {
290 var children = this.children(),
292 children.forEach(function(c, idx) {
293 if(c.sameNode(child)) {
300 getWlxmlTag: function() {
301 return this._container().attr('wlxml-tag');
303 setWlxmlTag: function(tag) {
304 this._container().attr('wlxml-tag', tag);
305 this._updateDisplayStyle();
307 getWlxmlClass: function() {
308 var klass = this._container().attr('wlxml-class');
310 return klass.replace(/-/g, '.');
313 setWlxmlClass: function(klass) {
314 this.getWlxmlMetaAttrs().forEach(function(attr) {
315 if(!classAttributes.hasMetaAttr(klass, attr.name))
316 this.dom().removeAttr('wlxml-meta-' + attr.name);
320 this._container().attr('wlxml-class', klass.replace(/\./g, '-'));
322 this._container().removeAttr('wlxml-class');
323 this._updateDisplayStyle();
325 _updateDisplayStyle: function() {
326 var displayStyle = getDisplayStyle(this.getWlxmlTag(), this.getWlxmlClass());
327 this.dom().css('display', displayStyle);
328 this._container().css('display', displayStyle);
331 if(what === 'list' && _.contains(['list.items', 'list.items.enum'], this.getWlxmlClass()))
336 getWlxmlMetaAttr: function(attr) {
337 return this.dom().attr('wlxml-meta-'+attr);
340 getWlxmlMetaAttrs: function() {
342 var attrList = classAttributes.getMetaAttrsList(this.getWlxmlClass());
343 attrList.all.forEach(function(attr) {
344 toret.push({name: attr.name, value: this.getWlxmlMetaAttr(attr.name) || ''});
349 setWlxmlMetaAttr: function(attr, value) {
350 this.dom().attr('wlxml-meta-'+attr, value);
353 toggleLabel: function(toggle) {
354 var displayCss = toggle ? 'inline-block' : 'none';
355 var label = this.dom().children('.canvas-widgets').find('.canvas-widget-label');
356 label.css('display', displayCss);
357 this.toggleHighlight(toggle);
360 toggleHighlight: function(toogle) {
361 this._container().toggleClass('highlighted-element');
366 // DocumentNodeElement represents a text node from WLXML document rendered inside Canvas
367 var DocumentTextElement = function(htmlElement, canvas) {
368 DocumentElement.call(this, htmlElement, canvas);
371 $.extend(DocumentTextElement, {
372 createDOM: function(params) {
374 .attr('document-text-element', '')
375 .text(params.text || utils.unicode.ZWS);
378 create: function(params, canvas) {
379 return this.fromHTMLElement(this.createDOM(params)[0]);
382 fromHTMLElement: function(htmlElement, canvas) {
383 return new this(htmlElement, canvas);
385 isContentContainer: function(htmlElement) {
386 return htmlElement.nodeType === Node.TEXT_NODE && $(htmlElement).parent().is('[document-text-element]');
390 DocumentTextElement.prototype = new DocumentElement();
392 $.extend(DocumentTextElement.prototype, {
393 toXML: function(parent) {
394 return this.getText();
396 _setupDOMHandler: function(htmlElement) {
397 var $element = $(htmlElement);
398 if(htmlElement.nodeType === Node.TEXT_NODE)
399 this.$element = $element.parent();
401 this.$element = $element;
403 setText: function(text) {
404 this.dom().contents()[0].data = text;
406 getText: function() {
407 return this.dom().text().replace(utils.unicode.ZWS, '');
409 isEmpty: function() {
410 // Having at least Zero Width Space is guaranteed be Content Observer
411 return this.dom().contents()[0].data === utils.unicode.ZWS;
413 after: function(params) {
414 if(params instanceof DocumentTextElement || params.text)
417 if(params instanceof DocumentNodeElement) {
420 element = DocumentNodeElement.create(params);
422 this.dom().wrap('<div>');
423 this.dom().parent().after(element.dom());
427 before: function(params) {
428 if(params instanceof DocumentTextElement || params.text)
431 if(params instanceof DocumentNodeElement) {
434 element = DocumentNodeElement.create(params);
436 this.dom().wrap('<div>');
437 this.dom().parent().before(element.dom());
441 wrapWithNodeElement: function(wlxmlNode) {
442 if(typeof wlxmlNode.start === 'number' && typeof wlxmlNode.end === 'number') {
443 return this.canvas.wrapText({
444 inside: this.parent(),
445 textNodeIdx: this.parent().childIndex(this),
446 offsetStart: Math.min(wlxmlNode.start, wlxmlNode.end),
447 offsetEnd: Math.max(wlxmlNode.start, wlxmlNode.end),
448 _with: {tag: wlxmlNode.tag, klass: wlxmlNode.klass}
451 return DocumentElement.prototype.wrapWithNodeElement.call(this, wlxmlNode);
455 var parent = this.parent(),
457 if(parent.children().length === 1) {
458 toret = parent.parent();
459 var grandParent = parent.parent();
461 var grandParentChildren = grandParent.children(),
462 idx = grandParent.childIndex(parent),
463 prev = idx - 1 > -1 ? grandParentChildren[idx-1] : null,
464 next = idx + 1 < grandParentChildren.length ? grandParentChildren[idx+1] : null;
466 prev.setText(prev.getText() + this.getText() + next.getText());
468 } else if (prev || next) {
469 var target = prev ? prev : next;
470 target.setText(target.getText() + this.getText());
481 split: function(params) {
482 var parentElement = this.parent(),
483 myIdx = parentElement.childIndex(this),
484 myCanvas = this.canvas,
486 succeedingChildren = [],
488 prefix = this.getText().substr(0, params.offset),
489 suffix = this.getText().substr(params.offset);
491 parentElement.children().forEach(function(child) {
493 succeedingChildren.push(child);
494 if(child.sameNode(thisElement))
498 if(prefix.length > 0)
499 this.setText(prefix);
503 var newElement = DocumentNodeElement.create({tag: parentElement.getWlxmlTag(), klass: parentElement.getWlxmlClass()}, myCanvas);
504 parentElement.after(newElement);
506 if(suffix.length > 0)
507 newElement.append({text: suffix});
508 succeedingChildren.forEach(function(child) {
509 newElement.append(child);
512 return {first: parentElement, second: newElement};
514 divide: function(params) {
515 var myText = this.getText();
517 if(params.offset === myText.length)
518 return this.after(params);
519 if(params.offset === 0)
520 return this.before(params);
522 var lhsText = myText.substr(0, params.offset),
523 rhsText = myText.substr(params.offset),
524 newElement = DocumentNodeElement.create({tag: params.tag, klass: params.klass}, this.canvas),
525 rhsTextElement = DocumentTextElement.create({text: rhsText});
527 this.setText(lhsText);
528 this.after(newElement);
529 newElement.after(rhsTextElement);
533 toggleHighlight: function() {
534 // do nothing for now
539 DocumentElement: DocumentElement,
540 DocumentNodeElement: DocumentNodeElement,
541 DocumentTextElement: DocumentTextElement