4 'modules/documentCanvas/canvas/utils',
5 'modules/documentCanvas/canvas/widgets',
6 'modules/documentCanvas/canvas/wlxmlManagers'
7 ], function($, _, utils, widgets, wlxmlManagers) {
10 /* global Node:false, document:false */
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) {
19 this._setupDOMHandler(htmlElement);
23 var elementTypeFromWlxmlNode = function(wlxmlNode) {
24 return wlxmlNode.nodeType === Node.TEXT_NODE ? DocumentTextElement : DocumentNodeElement;
27 $.extend(DocumentElement, {
28 create: function(node, canvas) {
29 return elementTypeFromWlxmlNode(node).create(node, canvas);
32 createDOM: function(wlxmlNode) {
33 return elementTypeFromParams(wlxmlNode).createDOM(params);
36 fromHTMLElement: function(htmlElement, canvas) {
37 var $element = $(htmlElement);
38 if(htmlElement.nodeType === Node.ELEMENT_NODE && $element.attr('document-node-element') !== undefined) {
39 return DocumentNodeElement.fromHTMLElement(htmlElement, canvas);
41 if($element.attr('document-text-element') !== undefined || (htmlElement.nodeType === Node.TEXT_NODE && $element.parent().attr('document-text-element') !== undefined)) {
42 return DocumentTextElement.fromHTMLElement(htmlElement, canvas);
48 $.extend(DocumentElement.prototype, {
49 _setupDOMHandler: function(htmlElement) {
50 this.$element = $(htmlElement);
53 return $.contains(document.documentElement, this.dom()[0]);
60 args = Array.prototype.slice.call(arguments, 0);
61 if(args.length === 2 && args[1] === undefined) {
62 return dom.removeData(args[0]);
64 return dom.data.apply(dom, arguments);
67 var parents = this.$element.parents('[document-node-element]');
69 return DocumentElement.fromHTMLElement(parents[0], this.canvas);
76 parent = this.parent();
79 parent = parent.parent();
84 sameNode: function(other) {
85 return other && (typeof other === typeof this) && other.dom()[0] === this.dom()[0];
88 markAsCurrent: function() {
89 this.canvas.markAsCurrent(this);
92 getVerticallyFirstTextElement: function() {
94 this.children().some(function(child) {
95 if(!child.isVisible()) {
96 return false; // continue
98 if(child instanceof DocumentTextElement) {
100 return true; // break
102 toret = child.getVerticallyFirstTextElement();
104 return true; // break
111 getPreviousTextElement: function(includeInvisible) {
112 return this.getNearestTextElement('above', includeInvisible);
115 getNextTextElement: function(includeInvisible) {
116 return this.getNearestTextElement('below', includeInvisible);
119 getNearestTextElement: function(direction, includeInvisible) {
120 includeInvisible = includeInvisible !== undefined ? includeInvisible : false;
121 var selector = '[document-text-element]' + (includeInvisible ? '' : ':visible');
122 return this.canvas.getDocumentElement(utils.nearestInDocumentOrder(selector, direction, this.dom()[0]));
125 isVisible: function() {
126 return this instanceof DocumentTextElement || this.getWlxmlTag() !== 'metadata';
129 isInsideList: function() {
130 return this.parents().some(function(parent) {
131 return parent.is('list');
135 exec: function(method) {
136 var manager = this.data('_wlxmlManager');
137 if(manager[method]) {
138 return manager[method].apply(manager, Array.prototype.slice.call(arguments, 1));
144 // DocumentNodeElement represents an element node from WLXML document rendered inside Canvas
145 var DocumentNodeElement = function(htmlElement, canvas) {
146 DocumentElement.call(this, htmlElement, canvas);
149 $.extend(DocumentNodeElement, {
150 create: function(wlxmlNode, canvas) {
151 return this.fromHTMLElement(this.createDOM(wlxmlNode, canvas)[0], canvas);
154 fromHTMLElement: function(htmlElement, canvas) {
155 return new this(htmlElement, canvas);
158 createDOM: function(wlxmlNode, canvas) {
160 .attr('document-node-element', ''),
161 widgetsContainer = $('<div>')
162 .addClass('canvas-widgets')
163 .attr('contenteditable', false),
164 container = $('<div>')
165 .attr('document-element-content', '');
167 dom.append(widgetsContainer, container);
168 // Make sure widgets aren't navigable with arrow keys
169 widgetsContainer.find('*').add(widgetsContainer).attr('tabindex', -1);
171 var element = this.fromHTMLElement(dom[0], canvas);
173 element.data('wlxmlNode', wlxmlNode);
174 wlxmlNode.setData('canvasElement', element);
176 element.setWlxml({tag: wlxmlNode.getTagName(), klass: wlxmlNode.getClass()});
178 wlxmlNode.contents().forEach(function(node) {
179 container.append(DocumentElement.create(node).dom());
187 var manipulate = function(e, params, action) {
189 if(params instanceof DocumentElement) {
192 element = DocumentElement.create(params);
194 var target = (action === 'append' || action === 'prepend') ? e._container() : e.dom();
195 target[action](element.dom());
199 DocumentNodeElement.prototype = new DocumentElement();
202 $.extend(DocumentNodeElement.prototype, {
203 _container: function() {
204 return this.dom().children('[document-element-content]');
211 append: function(params) {
212 if(params.tag !== 'span') {
213 this.data('orig-end', undefined);
215 return manipulate(this, params, 'append');
217 prepend: function(params) {
218 return manipulate(this, params, 'prepend');
220 before: function(params) {
221 return manipulate(this, params, 'before');
224 after: function(params) {
225 return manipulate(this, params, 'after');
227 children: function() {
229 if(this instanceof DocumentTextElement) {
234 var elementContent = this._container().contents();
236 elementContent.each(function(idx) {
237 var childElement = DocumentElement.fromHTMLElement(this, element.canvas);
238 if(childElement === undefined) {
241 if(idx === 0 && elementContent.length > 1 && elementContent[1].nodeType === Node.ELEMENT_NODE && (childElement instanceof DocumentTextElement) && $.trim($(this).text()) === '') {
244 if(idx > 0 && childElement instanceof DocumentTextElement) {
245 if(toret[toret.length-1] instanceof DocumentNodeElement && $.trim($(this).text()) === '') {
249 toret.push(childElement);
253 childIndex: function(child) {
254 var children = this.children(),
256 children.forEach(function(c, idx) {
257 if(c.sameNode(child)) {
264 getWlxmlTag: function() {
265 return this._container().attr('wlxml-tag');
267 setWlxmlTag: function(tag) {
268 if(tag === this.getWlxmlTag()) {
272 this._container().attr('wlxml-tag', tag);
273 if(!this.__updatingWlxml) {
274 this._updateWlxmlManager();
277 getWlxmlClass: function() {
278 var klass = this._container().attr('wlxml-class');
280 return klass.replace(/-/g, '.');
284 setWlxmlClass: function(klass) {
285 if(klass === this.getWlxmlClass()) {
289 this._container().attr('wlxml-class', klass.replace(/\./g, '-'));
292 this._container().removeAttr('wlxml-class');
294 if(!this.__updatingWlxml) {
295 this._updateWlxmlManager();
298 setWlxml: function(params) {
299 this.__updatingWlxml = true;
300 if(params.tag !== undefined) {
301 this.setWlxmlTag(params.tag);
303 if(params.klass !== undefined) {
304 this.setWlxmlClass(params.klass);
306 this._updateWlxmlManager();
307 this.__updatingWlxml = false;
309 _updateWlxmlManager: function() {
310 var manager = wlxmlManagers.getFor(this);
311 this.data('_wlxmlManager', manager);
315 if(what === 'list' && _.contains(['list.items', 'list.items.enum'], this.getWlxmlClass())) {
320 toggleLabel: function(toggle) {
321 var displayCss = toggle ? 'inline-block' : 'none';
322 var label = this.dom().children('.canvas-widgets').find('.canvas-widget-label');
323 label.css('display', displayCss);
324 this.toggleHighlight(toggle);
327 toggleHighlight: function(toggle) {
328 this._container().toggleClass('highlighted-element', toggle);
331 toggle: function(toggle) {
332 var mng = this.data('_wlxmlManager');
340 // DocumentNodeElement represents a text node from WLXML document rendered inside Canvas
341 var DocumentTextElement = function(htmlElement, canvas) {
342 DocumentElement.call(this, htmlElement, canvas);
345 $.extend(DocumentTextElement, {
346 createDOM: function(wlxmlTextNode) {
348 .attr('document-text-element', '')
349 .text(wlxmlTextNode.getText() || utils.unicode.ZWS),
350 element = this.fromHTMLElement(dom[0], this);
351 element.data('wlxmlNode', wlxmlTextNode);
355 create: function(wlxmlTextNode, canvas) {
356 return this.fromHTMLElement(this.createDOM(wlxmlTextNode)[0]);
359 fromHTMLElement: function(htmlElement, canvas) {
360 return new this(htmlElement, canvas);
362 isContentContainer: function(htmlElement) {
363 return htmlElement.nodeType === Node.TEXT_NODE && $(htmlElement).parent().is('[document-text-element]');
367 DocumentTextElement.prototype = new DocumentElement();
369 $.extend(DocumentTextElement.prototype, {
370 _setupDOMHandler: function(htmlElement) {
371 var $element = $(htmlElement);
372 if(htmlElement.nodeType === Node.TEXT_NODE) {
373 this.$element = $element.parent();
376 this.$element = $element;
384 setText: function(text) {
385 this.dom().contents()[0].data = text;
387 getText: function(options) {
388 options = _.extend({raw: false}, options || {});
389 var toret = this.dom().text();
391 toret = toret.replace(utils.unicode.ZWS, '');
395 isEmpty: function() {
396 // Having at least Zero Width Space is guaranteed be Content Observer
397 return this.dom().contents()[0].data === utils.unicode.ZWS;
399 after: function(params) {
400 if(params instanceof DocumentTextElement || params.text) {
404 if(params instanceof DocumentNodeElement) {
407 element = DocumentElement.create(params, this.canvas);
409 this.dom().wrap('<div>');
410 this.dom().parent().after(element.dom());
414 before: function(params) {
415 if(params instanceof DocumentTextElement || params.text) {
419 if(params instanceof DocumentNodeElement) {
422 element = DocumentNodeElement.create(params, this.canvas);
424 this.dom().wrap('<div>');
425 this.dom().parent().before(element.dom());
430 divide: function(params) {
431 var myText = this.getText();
433 if(params.offset === myText.length) {
434 return this.after(params);
436 if(params.offset === 0) {
437 return this.before(params);
440 var lhsText = myText.substr(0, params.offset),
441 rhsText = myText.substr(params.offset),
442 newElement = DocumentNodeElement.create({tag: params.tag, klass: params.klass}, this.canvas),
443 rhsTextElement = DocumentTextElement.create({text: rhsText});
445 this.setText(lhsText);
446 this.after(newElement);
447 newElement.after(rhsTextElement);
451 toggleHighlight: function() {
452 // do nothing for now
457 DocumentElement: DocumentElement,
458 DocumentNodeElement: DocumentNodeElement,
459 DocumentTextElement: DocumentTextElement