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 fromHTMLElement: function(htmlElement, canvas) {
33 var $element = $(htmlElement);
34 if(htmlElement.nodeType === Node.ELEMENT_NODE && $element.attr('document-node-element') !== undefined) {
35 return DocumentNodeElement.fromHTMLElement(htmlElement, canvas);
37 if($element.attr('document-text-element') !== undefined || (htmlElement.nodeType === Node.TEXT_NODE && $element.parent().attr('document-text-element') !== undefined)) {
38 return DocumentTextElement.fromHTMLElement(htmlElement, canvas);
44 $.extend(DocumentElement.prototype, {
45 _setupDOMHandler: function(htmlElement) {
46 this.$element = $(htmlElement);
49 return $.contains(document.documentElement, this.dom()[0]);
56 args = Array.prototype.slice.call(arguments, 0);
57 if(args.length === 2 && args[1] === undefined) {
58 return dom.removeData(args[0]);
60 return dom.data.apply(dom, arguments);
63 var parents = this.$element.parents('[document-node-element]');
65 return DocumentElement.fromHTMLElement(parents[0], this.canvas);
72 parent = this.parent();
75 parent = parent.parent();
80 sameNode: function(other) {
81 return other && (typeof other === typeof this) && other.dom()[0] === this.dom()[0];
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
94 if(child instanceof DocumentTextElement) {
98 toret = child.getVerticallyFirstTextElement();
100 return true; // break
107 getPreviousTextElement: function(includeInvisible) {
108 return this.getNearestTextElement('above', includeInvisible);
111 getNextTextElement: function(includeInvisible) {
112 return this.getNearestTextElement('below', includeInvisible);
115 getNearestTextElement: function(direction, includeInvisible) {
116 includeInvisible = includeInvisible !== undefined ? includeInvisible : false;
117 var selector = '[document-text-element]' + (includeInvisible ? '' : ':visible');
118 return this.canvas.getDocumentElement(utils.nearestInDocumentOrder(selector, direction, this.dom()[0]));
121 isVisible: function() {
122 return this instanceof DocumentTextElement || this.getWlxmlTag() !== 'metadata';
125 isInsideList: function() {
126 return this.parents().some(function(parent) {
127 return parent.is('list');
131 exec: function(method) {
132 var manager = this.data('_wlxmlManager');
133 if(manager[method]) {
134 return manager[method].apply(manager, Array.prototype.slice.call(arguments, 1));
140 // DocumentNodeElement represents an element node from WLXML document rendered inside Canvas
141 var DocumentNodeElement = function(htmlElement, canvas) {
142 DocumentElement.call(this, htmlElement, canvas);
145 $.extend(DocumentNodeElement, {
146 create: function(wlxmlNode, canvas) {
147 return this.fromHTMLElement(this.createDOM(wlxmlNode, canvas)[0], canvas);
150 fromHTMLElement: function(htmlElement, canvas) {
151 return new this(htmlElement, canvas);
154 createDOM: function(wlxmlNode, canvas) {
156 .attr('document-node-element', ''),
157 widgetsContainer = $('<div>')
158 .addClass('canvas-widgets')
159 .attr('contenteditable', false),
160 container = $('<div>')
161 .attr('document-element-content', '');
163 dom.append(widgetsContainer, container);
164 // Make sure widgets aren't navigable with arrow keys
165 widgetsContainer.find('*').add(widgetsContainer).attr('tabindex', -1);
167 var element = this.fromHTMLElement(dom[0], canvas);
169 element.data('wlxmlNode', wlxmlNode);
170 wlxmlNode.setData('canvasElement', element);
172 element.setWlxml({tag: wlxmlNode.getTagName(), klass: wlxmlNode.getClass()});
174 wlxmlNode.contents().forEach(function(node) {
175 container.append(DocumentElement.create(node, canvas).dom());
183 var manipulate = function(e, params, action) {
185 if(params instanceof DocumentElement) {
188 element = DocumentElement.create(params);
190 var target = (action === 'append' || action === 'prepend') ? e._container() : e.dom();
191 target[action](element.dom());
195 DocumentNodeElement.prototype = new DocumentElement();
198 $.extend(DocumentNodeElement.prototype, {
199 _container: function() {
200 return this.dom().children('[document-element-content]');
207 append: function(params) {
208 if(params.tag !== 'span') {
209 this.data('orig-end', undefined);
211 return manipulate(this, params, 'append');
213 prepend: function(params) {
214 return manipulate(this, params, 'prepend');
216 before: function(params) {
217 return manipulate(this, params, 'before');
220 after: function(params) {
221 return manipulate(this, params, 'after');
223 children: function() {
225 if(this instanceof DocumentTextElement) {
230 var elementContent = this._container().contents();
232 elementContent.each(function() {
233 var childElement = DocumentElement.fromHTMLElement(this, element.canvas);
234 if(childElement === undefined) {
237 toret.push(childElement);
241 childIndex: function(child) {
242 var children = this.children(),
244 children.forEach(function(c, idx) {
245 if(c.sameNode(child)) {
252 getWlxmlTag: function() {
253 return this._container().attr('wlxml-tag');
255 setWlxmlTag: function(tag) {
256 if(tag === this.getWlxmlTag()) {
260 this._container().attr('wlxml-tag', tag);
261 if(!this.__updatingWlxml) {
262 this._updateWlxmlManager();
265 getWlxmlClass: function() {
266 var klass = this._container().attr('wlxml-class');
268 return klass.replace(/-/g, '.');
272 setWlxmlClass: function(klass) {
273 if(klass === this.getWlxmlClass()) {
277 this._container().attr('wlxml-class', klass.replace(/\./g, '-'));
280 this._container().removeAttr('wlxml-class');
282 if(!this.__updatingWlxml) {
283 this._updateWlxmlManager();
286 setWlxml: function(params) {
287 this.__updatingWlxml = true;
288 if(params.tag !== undefined) {
289 this.setWlxmlTag(params.tag);
291 if(params.klass !== undefined) {
292 this.setWlxmlClass(params.klass);
294 this._updateWlxmlManager();
295 this.__updatingWlxml = false;
297 _updateWlxmlManager: function() {
298 var manager = wlxmlManagers.getFor(this);
299 this.data('_wlxmlManager', manager);
303 if(what === 'list' && _.contains(['list.items', 'list.items.enum'], this.getWlxmlClass())) {
308 toggleLabel: function(toggle) {
309 var displayCss = toggle ? 'inline-block' : 'none';
310 var label = this.dom().children('.canvas-widgets').find('.canvas-widget-label');
311 label.css('display', displayCss);
312 this.toggleHighlight(toggle);
315 toggleHighlight: function(toggle) {
316 this._container().toggleClass('highlighted-element', toggle);
319 toggle: function(toggle) {
320 var mng = this.data('_wlxmlManager');
328 // DocumentNodeElement represents a text node from WLXML document rendered inside Canvas
329 var DocumentTextElement = function(htmlElement, canvas) {
330 DocumentElement.call(this, htmlElement, canvas);
333 $.extend(DocumentTextElement, {
334 createDOM: function(wlxmlTextNode, canvas) {
336 .attr('document-text-element', '')
337 .text(wlxmlTextNode.getText() || utils.unicode.ZWS),
338 element = this.fromHTMLElement(dom[0], canvas);
339 element.data('wlxmlNode', wlxmlTextNode);
343 create: function(wlxmlTextNode, canvas) {
344 return this.fromHTMLElement(this.createDOM(wlxmlTextNode, canvas)[0], canvas);
347 fromHTMLElement: function(htmlElement, canvas) {
348 return new this(htmlElement, canvas);
350 isContentContainer: function(htmlElement) {
351 return htmlElement.nodeType === Node.TEXT_NODE && $(htmlElement).parent().is('[document-text-element]');
355 DocumentTextElement.prototype = new DocumentElement();
357 $.extend(DocumentTextElement.prototype, {
358 _setupDOMHandler: function(htmlElement) {
359 var $element = $(htmlElement);
360 if(htmlElement.nodeType === Node.TEXT_NODE) {
361 this.$element = $element.parent();
364 this.$element = $element;
372 setText: function(text) {
373 this.dom().contents()[0].data = text;
375 getText: function(options) {
376 options = _.extend({raw: false}, options || {});
377 var toret = this.dom().text();
379 toret = toret.replace(utils.unicode.ZWS, '');
383 isEmpty: function() {
384 // Having at least Zero Width Space is guaranteed be Content Observer
385 return this.dom().contents()[0].data === utils.unicode.ZWS;
387 after: function(params) {
388 if(params instanceof DocumentTextElement || params.text) {
392 if(params instanceof DocumentNodeElement) {
395 element = DocumentElement.create(params, this.canvas);
397 this.dom().wrap('<div>');
398 this.dom().parent().after(element.dom());
402 before: function(params) {
403 if(params instanceof DocumentTextElement || params.text) {
407 if(params instanceof DocumentNodeElement) {
410 element = DocumentNodeElement.create(params, this.canvas);
412 this.dom().wrap('<div>');
413 this.dom().parent().before(element.dom());
418 divide: function(params) {
419 var myText = this.getText();
421 if(params.offset === myText.length) {
422 return this.after(params);
424 if(params.offset === 0) {
425 return this.before(params);
428 var lhsText = myText.substr(0, params.offset),
429 rhsText = myText.substr(params.offset),
430 newElement = DocumentNodeElement.create({tag: params.tag, klass: params.klass}, this.canvas),
431 rhsTextElement = DocumentTextElement.create({text: rhsText});
433 this.setText(lhsText);
434 this.after(newElement);
435 newElement.after(rhsTextElement);
439 toggleHighlight: function() {
440 // do nothing for now
445 DocumentElement: DocumentElement,
446 DocumentNodeElement: DocumentNodeElement,
447 DocumentTextElement: DocumentTextElement