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);
21 $.extend(DocumentElement.prototype, {
22 _setupDOMHandler: function(htmlElement) {
23 this.$element = $(htmlElement);
26 return $.contains(document.documentElement, this.dom()[0]);
33 args = Array.prototype.slice.call(arguments, 0);
34 if(args.length === 2 && args[1] === undefined) {
35 return dom.removeData(args[0]);
37 return dom.data.apply(dom, arguments);
40 var parents = this.$element.parents('[document-node-element]');
42 return this.canvas.getDocumentElement(parents[0]);
49 parent = this.parent();
52 parent = parent.parent();
57 sameNode: function(other) {
58 return other && (typeof other === typeof this) && other.dom()[0] === this.dom()[0];
61 markAsCurrent: function() {
62 this.canvas.markAsCurrent(this);
65 getVerticallyFirstTextElement: function() {
67 this.children().some(function(child) {
68 if(!child.isVisible()) {
69 return false; // continue
71 if(child instanceof DocumentTextElement) {
75 toret = child.getVerticallyFirstTextElement();
84 getPreviousTextElement: function(includeInvisible) {
85 return this.getNearestTextElement('above', includeInvisible);
88 getNextTextElement: function(includeInvisible) {
89 return this.getNearestTextElement('below', includeInvisible);
92 getNearestTextElement: function(direction, includeInvisible) {
93 includeInvisible = includeInvisible !== undefined ? includeInvisible : false;
94 var selector = '[document-text-element]' + (includeInvisible ? '' : ':visible');
95 return this.canvas.getDocumentElement(utils.nearestInDocumentOrder(selector, direction, this.dom()[0]));
98 isVisible: function() {
99 return this instanceof DocumentTextElement || this.getWlxmlTag() !== 'metadata';
102 isInsideList: function() {
103 return this.parents().some(function(parent) {
104 return parent.is('list');
108 exec: function(method) {
109 var manager = this.data('_wlxmlManager');
110 if(manager[method]) {
111 return manager[method].apply(manager, Array.prototype.slice.call(arguments, 1));
117 // DocumentNodeElement represents an element node from WLXML document rendered inside Canvas
118 var DocumentNodeElement = function(htmlElement, canvas) {
119 DocumentElement.call(this, htmlElement, canvas);
122 $.extend(DocumentNodeElement, {
123 create: function(wlxmlNode, canvas) {
125 .attr('document-node-element', ''),
126 widgetsContainer = $('<div>')
127 .addClass('canvas-widgets')
128 .attr('contenteditable', false),
129 container = $('<div>')
130 .attr('document-element-content', '');
132 dom.append(widgetsContainer, container);
133 // Make sure widgets aren't navigable with arrow keys
134 widgetsContainer.find('*').add(widgetsContainer).attr('tabindex', -1);
136 var element = canvas.getDocumentElement(dom[0]);
138 element.data('wlxmlNode', wlxmlNode);
139 wlxmlNode.setData('canvasElement', element);
141 element.setWlxml({tag: wlxmlNode.getTagName(), klass: wlxmlNode.getClass()});
143 wlxmlNode.contents().forEach(function(node) {
144 container.append(canvas.createElement(node).dom());
151 var manipulate = function(e, params, action) {
153 if(params instanceof DocumentElement) {
156 element = e.canvas.createElement(params);
158 var target = (action === 'append' || action === 'prepend') ? e._container() : e.dom();
159 target[action](element.dom());
163 DocumentNodeElement.prototype = new DocumentElement();
166 $.extend(DocumentNodeElement.prototype, {
167 _container: function() {
168 return this.dom().children('[document-element-content]');
175 append: function(params) {
176 return manipulate(this, params, 'append');
178 prepend: function(params) {
179 return manipulate(this, params, 'prepend');
181 before: function(params) {
182 return manipulate(this, params, 'before');
185 after: function(params) {
186 return manipulate(this, params, 'after');
188 children: function() {
190 if(this instanceof DocumentTextElement) {
195 var elementContent = this._container().contents();
197 elementContent.each(function() {
198 var childElement = element.canvas.getDocumentElement(this);
199 if(childElement === undefined) {
202 toret.push(childElement);
206 childIndex: function(child) {
207 var children = this.children(),
209 children.forEach(function(c, idx) {
210 if(c.sameNode(child)) {
217 getWlxmlTag: function() {
218 return this._container().attr('wlxml-tag');
220 setWlxmlTag: function(tag) {
221 if(tag === this.getWlxmlTag()) {
225 this._container().attr('wlxml-tag', tag);
226 if(!this.__updatingWlxml) {
227 this._updateWlxmlManager();
230 getWlxmlClass: function() {
231 var klass = this._container().attr('wlxml-class');
233 return klass.replace(/-/g, '.');
237 setWlxmlClass: function(klass) {
238 if(klass === this.getWlxmlClass()) {
242 this._container().attr('wlxml-class', klass.replace(/\./g, '-'));
245 this._container().removeAttr('wlxml-class');
247 if(!this.__updatingWlxml) {
248 this._updateWlxmlManager();
251 setWlxml: function(params) {
252 this.__updatingWlxml = true;
253 if(params.tag !== undefined) {
254 this.setWlxmlTag(params.tag);
256 if(params.klass !== undefined) {
257 this.setWlxmlClass(params.klass);
259 this._updateWlxmlManager();
260 this.__updatingWlxml = false;
262 _updateWlxmlManager: function() {
263 var manager = wlxmlManagers.getFor(this);
264 this.data('_wlxmlManager', manager);
268 if(what === 'list' && _.contains(['list.items', 'list.items.enum'], this.getWlxmlClass())) {
273 toggleLabel: function(toggle) {
274 var displayCss = toggle ? 'inline-block' : 'none';
275 var label = this.dom().children('.canvas-widgets').find('.canvas-widget-label');
276 label.css('display', displayCss);
277 this.toggleHighlight(toggle);
280 toggleHighlight: function(toggle) {
281 this._container().toggleClass('highlighted-element', toggle);
284 toggle: function(toggle) {
285 var mng = this.data('_wlxmlManager');
293 // DocumentNodeElement represents a text node from WLXML document rendered inside Canvas
294 var DocumentTextElement = function(htmlElement, canvas) {
295 DocumentElement.call(this, htmlElement, canvas);
298 $.extend(DocumentTextElement, {
299 create: function(wlxmlTextNode, canvas) {
301 .attr('document-text-element', '')
302 .text(wlxmlTextNode.getText() || utils.unicode.ZWS),
303 element = canvas.getDocumentElement(dom[0]);
304 element.data('wlxmlNode', wlxmlTextNode);
308 isContentContainer: function(htmlElement) {
309 return htmlElement.nodeType === Node.TEXT_NODE && $(htmlElement).parent().is('[document-text-element]');
313 DocumentTextElement.prototype = new DocumentElement();
315 $.extend(DocumentTextElement.prototype, {
316 _setupDOMHandler: function(htmlElement) {
317 var $element = $(htmlElement);
318 if(htmlElement.nodeType === Node.TEXT_NODE) {
319 this.$element = $element.parent();
322 this.$element = $element;
330 setText: function(text) {
331 this.dom().contents()[0].data = text;
333 getText: function(options) {
334 options = _.extend({raw: false}, options || {});
335 var toret = this.dom().text();
337 toret = toret.replace(utils.unicode.ZWS, '');
341 isEmpty: function() {
342 // Having at least Zero Width Space is guaranteed be Content Observer
343 return this.dom().contents()[0].data === utils.unicode.ZWS;
345 after: function(params) {
346 if(params instanceof DocumentTextElement || params.text) {
350 if(params instanceof DocumentNodeElement) {
353 element = this.canvas.createElement(params);
355 this.dom().wrap('<div>');
356 this.dom().parent().after(element.dom());
360 before: function(params) {
361 if(params instanceof DocumentTextElement || params.text) {
365 if(params instanceof DocumentNodeElement) {
368 element = this.canvas.createElement(params);
370 this.dom().wrap('<div>');
371 this.dom().parent().before(element.dom());
376 toggleHighlight: function() {
377 // do nothing for now
382 DocumentElement: DocumentElement,
383 DocumentNodeElement: DocumentNodeElement,
384 DocumentTextElement: DocumentTextElement