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 return manipulate(this, params, 'append');
210 prepend: function(params) {
211 return manipulate(this, params, 'prepend');
213 before: function(params) {
214 return manipulate(this, params, 'before');
217 after: function(params) {
218 return manipulate(this, params, 'after');
220 children: function() {
222 if(this instanceof DocumentTextElement) {
227 var elementContent = this._container().contents();
229 elementContent.each(function() {
230 var childElement = DocumentElement.fromHTMLElement(this, element.canvas);
231 if(childElement === undefined) {
234 toret.push(childElement);
238 childIndex: function(child) {
239 var children = this.children(),
241 children.forEach(function(c, idx) {
242 if(c.sameNode(child)) {
249 getWlxmlTag: function() {
250 return this._container().attr('wlxml-tag');
252 setWlxmlTag: function(tag) {
253 if(tag === this.getWlxmlTag()) {
257 this._container().attr('wlxml-tag', tag);
258 if(!this.__updatingWlxml) {
259 this._updateWlxmlManager();
262 getWlxmlClass: function() {
263 var klass = this._container().attr('wlxml-class');
265 return klass.replace(/-/g, '.');
269 setWlxmlClass: function(klass) {
270 if(klass === this.getWlxmlClass()) {
274 this._container().attr('wlxml-class', klass.replace(/\./g, '-'));
277 this._container().removeAttr('wlxml-class');
279 if(!this.__updatingWlxml) {
280 this._updateWlxmlManager();
283 setWlxml: function(params) {
284 this.__updatingWlxml = true;
285 if(params.tag !== undefined) {
286 this.setWlxmlTag(params.tag);
288 if(params.klass !== undefined) {
289 this.setWlxmlClass(params.klass);
291 this._updateWlxmlManager();
292 this.__updatingWlxml = false;
294 _updateWlxmlManager: function() {
295 var manager = wlxmlManagers.getFor(this);
296 this.data('_wlxmlManager', manager);
300 if(what === 'list' && _.contains(['list.items', 'list.items.enum'], this.getWlxmlClass())) {
305 toggleLabel: function(toggle) {
306 var displayCss = toggle ? 'inline-block' : 'none';
307 var label = this.dom().children('.canvas-widgets').find('.canvas-widget-label');
308 label.css('display', displayCss);
309 this.toggleHighlight(toggle);
312 toggleHighlight: function(toggle) {
313 this._container().toggleClass('highlighted-element', toggle);
316 toggle: function(toggle) {
317 var mng = this.data('_wlxmlManager');
325 // DocumentNodeElement represents a text node from WLXML document rendered inside Canvas
326 var DocumentTextElement = function(htmlElement, canvas) {
327 DocumentElement.call(this, htmlElement, canvas);
330 $.extend(DocumentTextElement, {
331 createDOM: function(wlxmlTextNode, canvas) {
333 .attr('document-text-element', '')
334 .text(wlxmlTextNode.getText() || utils.unicode.ZWS),
335 element = this.fromHTMLElement(dom[0], canvas);
336 element.data('wlxmlNode', wlxmlTextNode);
340 create: function(wlxmlTextNode, canvas) {
341 return this.fromHTMLElement(this.createDOM(wlxmlTextNode, canvas)[0], canvas);
344 fromHTMLElement: function(htmlElement, canvas) {
345 return new this(htmlElement, canvas);
347 isContentContainer: function(htmlElement) {
348 return htmlElement.nodeType === Node.TEXT_NODE && $(htmlElement).parent().is('[document-text-element]');
352 DocumentTextElement.prototype = new DocumentElement();
354 $.extend(DocumentTextElement.prototype, {
355 _setupDOMHandler: function(htmlElement) {
356 var $element = $(htmlElement);
357 if(htmlElement.nodeType === Node.TEXT_NODE) {
358 this.$element = $element.parent();
361 this.$element = $element;
369 setText: function(text) {
370 this.dom().contents()[0].data = text;
372 getText: function(options) {
373 options = _.extend({raw: false}, options || {});
374 var toret = this.dom().text();
376 toret = toret.replace(utils.unicode.ZWS, '');
380 isEmpty: function() {
381 // Having at least Zero Width Space is guaranteed be Content Observer
382 return this.dom().contents()[0].data === utils.unicode.ZWS;
384 after: function(params) {
385 if(params instanceof DocumentTextElement || params.text) {
389 if(params instanceof DocumentNodeElement) {
392 element = DocumentElement.create(params, this.canvas);
394 this.dom().wrap('<div>');
395 this.dom().parent().after(element.dom());
399 before: function(params) {
400 if(params instanceof DocumentTextElement || params.text) {
404 if(params instanceof DocumentNodeElement) {
407 element = DocumentNodeElement.create(params, this.canvas);
409 this.dom().wrap('<div>');
410 this.dom().parent().before(element.dom());
415 divide: function(params) {
416 var myText = this.getText();
418 if(params.offset === myText.length) {
419 return this.after(params);
421 if(params.offset === 0) {
422 return this.before(params);
425 var lhsText = myText.substr(0, params.offset),
426 rhsText = myText.substr(params.offset),
427 newElement = DocumentNodeElement.create({tag: params.tag, klass: params.klass}, this.canvas),
428 rhsTextElement = DocumentTextElement.create({text: rhsText});
430 this.setText(lhsText);
431 this.after(newElement);
432 newElement.after(rhsTextElement);
436 toggleHighlight: function() {
437 // do nothing for now
442 DocumentElement: DocumentElement,
443 DocumentNodeElement: DocumentNodeElement,
444 DocumentTextElement: DocumentTextElement