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(wlxmlNode, canvas) {
14 if(arguments.length === 0) {
17 this.wlxmlNode = wlxmlNode;
21 $.extend(DocumentElement.prototype, {
22 _setupDOMHandler: function(htmlElement) {
23 this.$element = $(htmlElement);
24 this.$element.data('canvas-element', this);
27 return $.contains(document.documentElement, this.dom()[0]);
34 args = Array.prototype.slice.call(arguments, 0);
35 if(args.length === 2 && args[1] === undefined) {
36 return dom.removeData(args[0]);
38 return dom.data.apply(dom, arguments);
41 var parents = this.$element.parents('[document-node-element]');
43 return this.canvas.getDocumentElement(parents[0]);
50 parent = this.parent();
53 parent = parent.parent();
58 sameNode: function(other) {
59 return other && (typeof other === typeof this) && other.dom()[0] === this.dom()[0];
62 markAsCurrent: function() {
63 this.canvas.markAsCurrent(this);
66 getVerticallyFirstTextElement: function() {
68 this.children().some(function(child) {
69 if(!child.isVisible()) {
70 return false; // continue
72 if(child instanceof DocumentTextElement) {
76 toret = child.getVerticallyFirstTextElement();
85 getPreviousTextElement: function(includeInvisible) {
86 return this.getNearestTextElement('above', includeInvisible);
89 getNextTextElement: function(includeInvisible) {
90 return this.getNearestTextElement('below', includeInvisible);
93 getNearestTextElement: function(direction, includeInvisible) {
94 includeInvisible = includeInvisible !== undefined ? includeInvisible : false;
95 var selector = '[document-text-element]' + (includeInvisible ? '' : ':visible');
96 return this.canvas.getDocumentElement(utils.nearestInDocumentOrder(selector, direction, this.dom()[0]));
99 isVisible: function() {
100 return this instanceof DocumentTextElement || this.getWlxmlTag() !== 'metadata';
103 isInsideList: function() {
104 return this.parents().some(function(parent) {
105 return parent.is('list');
109 exec: function(method) {
110 var manager = this.data('_wlxmlManager');
111 if(manager[method]) {
112 return manager[method].apply(manager, Array.prototype.slice.call(arguments, 1));
118 // DocumentNodeElement represents an element node from WLXML document rendered inside Canvas
119 var DocumentNodeElement = function(wlxmlNode, canvas) {
120 DocumentElement.call(this, wlxmlNode, canvas);
123 .attr('document-node-element', ''),
124 widgetsContainer = $('<div>')
125 .addClass('canvas-widgets')
126 .attr('contenteditable', false),
127 container = $('<div>')
128 .attr('document-element-content', '');
130 dom.append(widgetsContainer, container);
131 // Make sure widgets aren't navigable with arrow keys
132 widgetsContainer.find('*').add(widgetsContainer).attr('tabindex', -1);
133 this._setupDOMHandler(dom);
136 this.data('wlxmlNode', wlxmlNode);
137 wlxmlNode.setData('canvasElement', this);
139 this.setWlxml({tag: wlxmlNode.getTagName(), klass: wlxmlNode.getClass()});
141 wlxmlNode.contents().forEach(function(node) {
142 container.append(canvas.createElement(node).dom());
147 var manipulate = function(e, params, action) {
149 if(params instanceof DocumentElement) {
152 element = e.canvas.createElement(params);
154 var target = (action === 'append' || action === 'prepend') ? e._container() : e.dom();
155 target[action](element.dom());
159 DocumentNodeElement.prototype = new DocumentElement();
162 $.extend(DocumentNodeElement.prototype, {
163 _container: function() {
164 return this.dom().children('[document-element-content]');
171 append: function(params) {
172 return manipulate(this, params, 'append');
174 prepend: function(params) {
175 return manipulate(this, params, 'prepend');
177 before: function(params) {
178 return manipulate(this, params, 'before');
181 after: function(params) {
182 return manipulate(this, params, 'after');
184 children: function() {
186 if(this instanceof DocumentTextElement) {
191 var elementContent = this._container().contents();
193 elementContent.each(function() {
194 var childElement = element.canvas.getDocumentElement(this);
195 if(childElement === undefined) {
198 toret.push(childElement);
202 childIndex: function(child) {
203 var children = this.children(),
205 children.forEach(function(c, idx) {
206 if(c.sameNode(child)) {
213 getWlxmlTag: function() {
214 return this._container().attr('wlxml-tag');
216 setWlxmlTag: function(tag) {
217 if(tag === this.getWlxmlTag()) {
221 this._container().attr('wlxml-tag', tag);
222 if(!this.__updatingWlxml) {
223 this._updateWlxmlManager();
226 getWlxmlClass: function() {
227 var klass = this._container().attr('wlxml-class');
229 return klass.replace(/-/g, '.');
233 setWlxmlClass: function(klass) {
234 if(klass === this.getWlxmlClass()) {
238 this._container().attr('wlxml-class', klass.replace(/\./g, '-'));
241 this._container().removeAttr('wlxml-class');
243 if(!this.__updatingWlxml) {
244 this._updateWlxmlManager();
247 setWlxml: function(params) {
248 this.__updatingWlxml = true;
249 if(params.tag !== undefined) {
250 this.setWlxmlTag(params.tag);
252 if(params.klass !== undefined) {
253 this.setWlxmlClass(params.klass);
255 this._updateWlxmlManager();
256 this.__updatingWlxml = false;
258 _updateWlxmlManager: function() {
259 var manager = wlxmlManagers.getFor(this);
260 this.data('_wlxmlManager', manager);
264 if(what === 'list' && _.contains(['list.items', 'list.items.enum'], this.getWlxmlClass())) {
269 toggleLabel: function(toggle) {
270 var displayCss = toggle ? 'inline-block' : 'none';
271 var label = this.dom().children('.canvas-widgets').find('.canvas-widget-label');
272 label.css('display', displayCss);
273 this.toggleHighlight(toggle);
276 toggleHighlight: function(toggle) {
277 this._container().toggleClass('highlighted-element', toggle);
280 toggle: function(toggle) {
281 var mng = this.data('_wlxmlManager');
289 // DocumentNodeElement represents a text node from WLXML document rendered inside Canvas
290 var DocumentTextElement = function(wlxmlTextNode, canvas) {
291 DocumentElement.call(this, wlxmlTextNode, canvas);
293 .attr('document-text-element', '')
294 .text(wlxmlTextNode.getText() || utils.unicode.ZWS);
295 this._setupDOMHandler(dom);
296 this.data('wlxmlNode', wlxmlTextNode);
299 $.extend(DocumentTextElement, {
300 isContentContainer: function(htmlElement) {
301 return htmlElement.nodeType === Node.TEXT_NODE && $(htmlElement).parent().is('[document-text-element]');
305 DocumentTextElement.prototype = new DocumentElement();
307 $.extend(DocumentTextElement.prototype, {
313 setText: function(text) {
314 this.dom().contents()[0].data = text;
316 getText: function(options) {
317 options = _.extend({raw: false}, options || {});
318 var toret = this.dom().text();
320 toret = toret.replace(utils.unicode.ZWS, '');
324 isEmpty: function() {
325 // Having at least Zero Width Space is guaranteed be Content Observer
326 return this.dom().contents()[0].data === utils.unicode.ZWS;
328 after: function(params) {
329 if(params instanceof DocumentTextElement || params.text) {
333 if(params instanceof DocumentNodeElement) {
336 element = this.canvas.createElement(params);
338 this.dom().wrap('<div>');
339 this.dom().parent().after(element.dom());
343 before: function(params) {
344 if(params instanceof DocumentTextElement || params.text) {
348 if(params instanceof DocumentNodeElement) {
351 element = this.canvas.createElement(params);
353 this.dom().wrap('<div>');
354 this.dom().parent().before(element.dom());
359 toggleHighlight: function() {
360 // do nothing for now
365 DocumentElement: DocumentElement,
366 DocumentNodeElement: DocumentNodeElement,
367 DocumentTextElement: DocumentTextElement