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 fromHTMLElement: function(htmlElement, canvas) {
146 return new this(htmlElement, canvas);
149 create: function(wlxmlNode, canvas) {
151 .attr('document-node-element', ''),
152 widgetsContainer = $('<div>')
153 .addClass('canvas-widgets')
154 .attr('contenteditable', false),
155 container = $('<div>')
156 .attr('document-element-content', '');
158 dom.append(widgetsContainer, container);
159 // Make sure widgets aren't navigable with arrow keys
160 widgetsContainer.find('*').add(widgetsContainer).attr('tabindex', -1);
162 var element = this.fromHTMLElement(dom[0], canvas);
164 element.data('wlxmlNode', wlxmlNode);
165 wlxmlNode.setData('canvasElement', element);
167 element.setWlxml({tag: wlxmlNode.getTagName(), klass: wlxmlNode.getClass()});
169 wlxmlNode.contents().forEach(function(node) {
170 container.append(DocumentElement.create(node, canvas).dom());
177 var manipulate = function(e, params, action) {
179 if(params instanceof DocumentElement) {
182 element = DocumentElement.create(params);
184 var target = (action === 'append' || action === 'prepend') ? e._container() : e.dom();
185 target[action](element.dom());
189 DocumentNodeElement.prototype = new DocumentElement();
192 $.extend(DocumentNodeElement.prototype, {
193 _container: function() {
194 return this.dom().children('[document-element-content]');
201 append: function(params) {
202 return manipulate(this, params, 'append');
204 prepend: function(params) {
205 return manipulate(this, params, 'prepend');
207 before: function(params) {
208 return manipulate(this, params, 'before');
211 after: function(params) {
212 return manipulate(this, params, 'after');
214 children: function() {
216 if(this instanceof DocumentTextElement) {
221 var elementContent = this._container().contents();
223 elementContent.each(function() {
224 var childElement = DocumentElement.fromHTMLElement(this, element.canvas);
225 if(childElement === undefined) {
228 toret.push(childElement);
232 childIndex: function(child) {
233 var children = this.children(),
235 children.forEach(function(c, idx) {
236 if(c.sameNode(child)) {
243 getWlxmlTag: function() {
244 return this._container().attr('wlxml-tag');
246 setWlxmlTag: function(tag) {
247 if(tag === this.getWlxmlTag()) {
251 this._container().attr('wlxml-tag', tag);
252 if(!this.__updatingWlxml) {
253 this._updateWlxmlManager();
256 getWlxmlClass: function() {
257 var klass = this._container().attr('wlxml-class');
259 return klass.replace(/-/g, '.');
263 setWlxmlClass: function(klass) {
264 if(klass === this.getWlxmlClass()) {
268 this._container().attr('wlxml-class', klass.replace(/\./g, '-'));
271 this._container().removeAttr('wlxml-class');
273 if(!this.__updatingWlxml) {
274 this._updateWlxmlManager();
277 setWlxml: function(params) {
278 this.__updatingWlxml = true;
279 if(params.tag !== undefined) {
280 this.setWlxmlTag(params.tag);
282 if(params.klass !== undefined) {
283 this.setWlxmlClass(params.klass);
285 this._updateWlxmlManager();
286 this.__updatingWlxml = false;
288 _updateWlxmlManager: function() {
289 var manager = wlxmlManagers.getFor(this);
290 this.data('_wlxmlManager', manager);
294 if(what === 'list' && _.contains(['list.items', 'list.items.enum'], this.getWlxmlClass())) {
299 toggleLabel: function(toggle) {
300 var displayCss = toggle ? 'inline-block' : 'none';
301 var label = this.dom().children('.canvas-widgets').find('.canvas-widget-label');
302 label.css('display', displayCss);
303 this.toggleHighlight(toggle);
306 toggleHighlight: function(toggle) {
307 this._container().toggleClass('highlighted-element', toggle);
310 toggle: function(toggle) {
311 var mng = this.data('_wlxmlManager');
319 // DocumentNodeElement represents a text node from WLXML document rendered inside Canvas
320 var DocumentTextElement = function(htmlElement, canvas) {
321 DocumentElement.call(this, htmlElement, canvas);
324 $.extend(DocumentTextElement, {
325 create: function(wlxmlTextNode, canvas) {
327 .attr('document-text-element', '')
328 .text(wlxmlTextNode.getText() || utils.unicode.ZWS),
329 element = this.fromHTMLElement(dom[0], canvas);
330 element.data('wlxmlNode', wlxmlTextNode);
334 fromHTMLElement: function(htmlElement, canvas) {
335 return new this(htmlElement, canvas);
338 isContentContainer: function(htmlElement) {
339 return htmlElement.nodeType === Node.TEXT_NODE && $(htmlElement).parent().is('[document-text-element]');
343 DocumentTextElement.prototype = new DocumentElement();
345 $.extend(DocumentTextElement.prototype, {
346 _setupDOMHandler: function(htmlElement) {
347 var $element = $(htmlElement);
348 if(htmlElement.nodeType === Node.TEXT_NODE) {
349 this.$element = $element.parent();
352 this.$element = $element;
360 setText: function(text) {
361 this.dom().contents()[0].data = text;
363 getText: function(options) {
364 options = _.extend({raw: false}, options || {});
365 var toret = this.dom().text();
367 toret = toret.replace(utils.unicode.ZWS, '');
371 isEmpty: function() {
372 // Having at least Zero Width Space is guaranteed be Content Observer
373 return this.dom().contents()[0].data === utils.unicode.ZWS;
375 after: function(params) {
376 if(params instanceof DocumentTextElement || params.text) {
380 if(params instanceof DocumentNodeElement) {
383 element = DocumentElement.create(params, this.canvas);
385 this.dom().wrap('<div>');
386 this.dom().parent().after(element.dom());
390 before: function(params) {
391 if(params instanceof DocumentTextElement || params.text) {
395 if(params instanceof DocumentNodeElement) {
398 element = DocumentNodeElement.create(params, this.canvas);
400 this.dom().wrap('<div>');
401 this.dom().parent().before(element.dom());
406 divide: function(params) {
407 var myText = this.getText();
409 if(params.offset === myText.length) {
410 return this.after(params);
412 if(params.offset === 0) {
413 return this.before(params);
416 var lhsText = myText.substr(0, params.offset),
417 rhsText = myText.substr(params.offset),
418 newElement = DocumentNodeElement.create({tag: params.tag, klass: params.klass}, this.canvas),
419 rhsTextElement = DocumentTextElement.create({text: rhsText});
421 this.setText(lhsText);
422 this.after(newElement);
423 newElement.after(rhsTextElement);
427 toggleHighlight: function() {
428 // do nothing for now
433 DocumentElement: DocumentElement,
434 DocumentNodeElement: DocumentNodeElement,
435 DocumentTextElement: DocumentTextElement