4 'modules/documentCanvas/canvas/utils',
5 'modules/documentCanvas/canvas/wlxmlManagers'
6 ], function($, _, utils, wlxmlManagers) {
9 /* global Node:false, document:false */
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) {
18 this._setupDOMHandler(htmlElement);
22 var elementTypeFromWlxmlNode = function(wlxmlNode) {
23 return wlxmlNode.nodeType === Node.TEXT_NODE ? DocumentTextElement : DocumentNodeElement;
26 $.extend(DocumentElement, {
27 create: function(node, canvas) {
28 return elementTypeFromWlxmlNode(node).create(node, canvas);
31 fromHTMLElement: function(htmlElement, canvas) {
32 var $element = $(htmlElement);
33 if(htmlElement.nodeType === Node.ELEMENT_NODE && $element.attr('document-node-element') !== undefined) {
34 return DocumentNodeElement.fromHTMLElement(htmlElement, canvas);
36 if($element.attr('document-text-element') !== undefined || (htmlElement.nodeType === Node.TEXT_NODE && $element.parent().attr('document-text-element') !== undefined)) {
37 return DocumentTextElement.fromHTMLElement(htmlElement, canvas);
43 $.extend(DocumentElement.prototype, {
44 _setupDOMHandler: function(htmlElement) {
45 this.$element = $(htmlElement);
48 return $.contains(document.documentElement, this.dom()[0]);
55 args = Array.prototype.slice.call(arguments, 0);
56 if(args.length === 2 && args[1] === undefined) {
57 return dom.removeData(args[0]);
59 return dom.data.apply(dom, arguments);
62 var parents = this.$element.parents('[document-node-element]');
64 return DocumentElement.fromHTMLElement(parents[0], this.canvas);
71 parent = this.parent();
74 parent = parent.parent();
79 sameNode: function(other) {
80 return other && (typeof other === typeof this) && other.dom()[0] === this.dom()[0];
83 markAsCurrent: function() {
84 this.canvas.markAsCurrent(this);
87 getVerticallyFirstTextElement: function() {
89 this.children().some(function(child) {
90 if(!child.isVisible()) {
91 return false; // continue
93 if(child instanceof DocumentTextElement) {
97 toret = child.getVerticallyFirstTextElement();
106 getPreviousTextElement: function(includeInvisible) {
107 return this.getNearestTextElement('above', includeInvisible);
110 getNextTextElement: function(includeInvisible) {
111 return this.getNearestTextElement('below', includeInvisible);
114 getNearestTextElement: function(direction, includeInvisible) {
115 includeInvisible = includeInvisible !== undefined ? includeInvisible : false;
116 var selector = '[document-text-element]' + (includeInvisible ? '' : ':visible');
117 return this.canvas.getDocumentElement(utils.nearestInDocumentOrder(selector, direction, this.dom()[0]));
120 isVisible: function() {
121 return this instanceof DocumentTextElement || this.getWlxmlTag() !== 'metadata';
124 isInsideList: function() {
125 return this.parents().some(function(parent) {
126 return parent.is('list');
130 exec: function(method) {
131 var manager = this.data('_wlxmlManager');
132 if(manager[method]) {
133 return manager[method].apply(manager, Array.prototype.slice.call(arguments, 1));
139 // DocumentNodeElement represents an element node from WLXML document rendered inside Canvas
140 var DocumentNodeElement = function(htmlElement, canvas) {
141 DocumentElement.call(this, htmlElement, canvas);
144 $.extend(DocumentNodeElement, {
145 create: function(wlxmlNode, canvas) {
146 return this.fromHTMLElement(this.createDOM(wlxmlNode, canvas)[0], canvas);
149 fromHTMLElement: function(htmlElement, canvas) {
150 return new this(htmlElement, canvas);
153 createDOM: function(wlxmlNode, canvas) {
155 .attr('document-node-element', ''),
156 widgetsContainer = $('<div>')
157 .addClass('canvas-widgets')
158 .attr('contenteditable', false),
159 container = $('<div>')
160 .attr('document-element-content', '');
162 dom.append(widgetsContainer, container);
163 // Make sure widgets aren't navigable with arrow keys
164 widgetsContainer.find('*').add(widgetsContainer).attr('tabindex', -1);
166 var element = this.fromHTMLElement(dom[0], canvas);
168 element.data('wlxmlNode', wlxmlNode);
169 wlxmlNode.setData('canvasElement', element);
171 element.setWlxml({tag: wlxmlNode.getTagName(), klass: wlxmlNode.getClass()});
173 wlxmlNode.contents().forEach(function(node) {
174 container.append(DocumentElement.create(node, canvas).dom());
182 var manipulate = function(e, params, action) {
184 if(params instanceof DocumentElement) {
187 element = DocumentElement.create(params);
189 var target = (action === 'append' || action === 'prepend') ? e._container() : e.dom();
190 target[action](element.dom());
194 DocumentNodeElement.prototype = new DocumentElement();
197 $.extend(DocumentNodeElement.prototype, {
198 _container: function() {
199 return this.dom().children('[document-element-content]');
206 append: function(params) {
207 return manipulate(this, params, 'append');
209 prepend: function(params) {
210 return manipulate(this, params, 'prepend');
212 before: function(params) {
213 return manipulate(this, params, 'before');
216 after: function(params) {
217 return manipulate(this, params, 'after');
219 children: function() {
221 if(this instanceof DocumentTextElement) {
226 var elementContent = this._container().contents();
228 elementContent.each(function() {
229 var childElement = DocumentElement.fromHTMLElement(this, element.canvas);
230 if(childElement === undefined) {
233 toret.push(childElement);
237 childIndex: function(child) {
238 var children = this.children(),
240 children.forEach(function(c, idx) {
241 if(c.sameNode(child)) {
248 getWlxmlTag: function() {
249 return this._container().attr('wlxml-tag');
251 setWlxmlTag: function(tag) {
252 if(tag === this.getWlxmlTag()) {
256 this._container().attr('wlxml-tag', tag);
257 if(!this.__updatingWlxml) {
258 this._updateWlxmlManager();
261 getWlxmlClass: function() {
262 var klass = this._container().attr('wlxml-class');
264 return klass.replace(/-/g, '.');
268 setWlxmlClass: function(klass) {
269 if(klass === this.getWlxmlClass()) {
273 this._container().attr('wlxml-class', klass.replace(/\./g, '-'));
276 this._container().removeAttr('wlxml-class');
278 if(!this.__updatingWlxml) {
279 this._updateWlxmlManager();
282 setWlxml: function(params) {
283 this.__updatingWlxml = true;
284 if(params.tag !== undefined) {
285 this.setWlxmlTag(params.tag);
287 if(params.klass !== undefined) {
288 this.setWlxmlClass(params.klass);
290 this._updateWlxmlManager();
291 this.__updatingWlxml = false;
293 _updateWlxmlManager: function() {
294 var manager = wlxmlManagers.getFor(this);
295 this.data('_wlxmlManager', manager);
299 if(what === 'list' && _.contains(['list.items', 'list.items.enum'], this.getWlxmlClass())) {
304 toggleLabel: function(toggle) {
305 var displayCss = toggle ? 'inline-block' : 'none';
306 var label = this.dom().children('.canvas-widgets').find('.canvas-widget-label');
307 label.css('display', displayCss);
308 this.toggleHighlight(toggle);
311 toggleHighlight: function(toggle) {
312 this._container().toggleClass('highlighted-element', toggle);
315 toggle: function(toggle) {
316 var mng = this.data('_wlxmlManager');
324 // DocumentNodeElement represents a text node from WLXML document rendered inside Canvas
325 var DocumentTextElement = function(htmlElement, canvas) {
326 DocumentElement.call(this, htmlElement, canvas);
329 $.extend(DocumentTextElement, {
330 createDOM: function(wlxmlTextNode, canvas) {
332 .attr('document-text-element', '')
333 .text(wlxmlTextNode.getText() || utils.unicode.ZWS),
334 element = this.fromHTMLElement(dom[0], canvas);
335 element.data('wlxmlNode', wlxmlTextNode);
339 create: function(wlxmlTextNode, canvas) {
340 return this.fromHTMLElement(this.createDOM(wlxmlTextNode, canvas)[0], canvas);
343 fromHTMLElement: function(htmlElement, canvas) {
344 return new this(htmlElement, canvas);
346 isContentContainer: function(htmlElement) {
347 return htmlElement.nodeType === Node.TEXT_NODE && $(htmlElement).parent().is('[document-text-element]');
351 DocumentTextElement.prototype = new DocumentElement();
353 $.extend(DocumentTextElement.prototype, {
354 _setupDOMHandler: function(htmlElement) {
355 var $element = $(htmlElement);
356 if(htmlElement.nodeType === Node.TEXT_NODE) {
357 this.$element = $element.parent();
360 this.$element = $element;
368 setText: function(text) {
369 this.dom().contents()[0].data = text;
371 getText: function(options) {
372 options = _.extend({raw: false}, options || {});
373 var toret = this.dom().text();
375 toret = toret.replace(utils.unicode.ZWS, '');
379 isEmpty: function() {
380 // Having at least Zero Width Space is guaranteed be Content Observer
381 return this.dom().contents()[0].data === utils.unicode.ZWS;
383 after: function(params) {
384 if(params instanceof DocumentTextElement || params.text) {
388 if(params instanceof DocumentNodeElement) {
391 element = DocumentElement.create(params, this.canvas);
393 this.dom().wrap('<div>');
394 this.dom().parent().after(element.dom());
398 before: function(params) {
399 if(params instanceof DocumentTextElement || params.text) {
403 if(params instanceof DocumentNodeElement) {
406 element = DocumentNodeElement.create(params, this.canvas);
408 this.dom().wrap('<div>');
409 this.dom().parent().before(element.dom());
414 divide: function(params) {
415 var myText = this.getText();
417 if(params.offset === myText.length) {
418 return this.after(params);
420 if(params.offset === 0) {
421 return this.before(params);
424 var lhsText = myText.substr(0, params.offset),
425 rhsText = myText.substr(params.offset),
426 newElement = DocumentNodeElement.create({tag: params.tag, klass: params.klass}, this.canvas),
427 rhsTextElement = DocumentTextElement.create({text: rhsText});
429 this.setText(lhsText);
430 this.after(newElement);
431 newElement.after(rhsTextElement);
435 toggleHighlight: function() {
436 // do nothing for now
441 DocumentElement: DocumentElement,
442 DocumentNodeElement: DocumentNodeElement,
443 DocumentTextElement: DocumentTextElement