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;
20 this.$element = this.createDOM();
21 this.$element.data('canvas-element', this);
22 this.data('wlxmlNode', wlxmlNode);
25 $.extend(DocumentElement.prototype, {
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);
121 wlxmlNode.setData('canvasElement', this);
125 var manipulate = function(e, params, action) {
127 if(params instanceof DocumentElement) {
130 element = e.canvas.createElement(params);
132 var target = (action === 'append' || action === 'prepend') ? e._container() : e.dom();
133 target[action](element.dom());
137 DocumentNodeElement.prototype = new DocumentElement();
140 $.extend(DocumentNodeElement.prototype, {
141 createDOM: function() {
143 .attr('document-node-element', ''),
144 widgetsContainer = $('<div>')
145 .addClass('canvas-widgets')
146 .attr('contenteditable', false),
147 container = $('<div>')
148 .attr('document-element-content', '');
150 dom.append(widgetsContainer, container);
151 // Make sure widgets aren't navigable with arrow keys
152 widgetsContainer.find('*').add(widgetsContainer).attr('tabindex', -1);
153 this.$element = dom; //@!!!
154 this.setWlxml({tag: this.wlxmlNode.getTagName(), klass: this.wlxmlNode.getClass()});
156 this.wlxmlNode.contents().forEach(function(node) {
157 container.append(this.canvas.createElement(node).dom());
161 _container: function() {
162 return this.dom().children('[document-element-content]');
169 append: function(params) {
170 return manipulate(this, params, 'append');
172 prepend: function(params) {
173 return manipulate(this, params, 'prepend');
175 before: function(params) {
176 return manipulate(this, params, 'before');
179 after: function(params) {
180 return manipulate(this, params, 'after');
182 children: function() {
184 if(this instanceof DocumentTextElement) {
189 var elementContent = this._container().contents();
191 elementContent.each(function() {
192 var childElement = element.canvas.getDocumentElement(this);
193 if(childElement === undefined) {
196 toret.push(childElement);
200 childIndex: function(child) {
201 var children = this.children(),
203 children.forEach(function(c, idx) {
204 if(c.sameNode(child)) {
211 getWlxmlTag: function() {
212 return this._container().attr('wlxml-tag');
214 setWlxmlTag: function(tag) {
215 if(tag === this.getWlxmlTag()) {
219 this._container().attr('wlxml-tag', tag);
220 if(!this.__updatingWlxml) {
221 this._updateWlxmlManager();
224 getWlxmlClass: function() {
225 var klass = this._container().attr('wlxml-class');
227 return klass.replace(/-/g, '.');
231 setWlxmlClass: function(klass) {
232 if(klass === this.getWlxmlClass()) {
236 this._container().attr('wlxml-class', klass.replace(/\./g, '-'));
239 this._container().removeAttr('wlxml-class');
241 if(!this.__updatingWlxml) {
242 this._updateWlxmlManager();
245 setWlxml: function(params) {
246 this.__updatingWlxml = true;
247 if(params.tag !== undefined) {
248 this.setWlxmlTag(params.tag);
250 if(params.klass !== undefined) {
251 this.setWlxmlClass(params.klass);
253 this._updateWlxmlManager();
254 this.__updatingWlxml = false;
256 _updateWlxmlManager: function() {
257 var manager = wlxmlManagers.getFor(this);
258 this.data('_wlxmlManager', manager);
262 if(what === 'list' && _.contains(['list.items', 'list.items.enum'], this.getWlxmlClass())) {
267 toggleLabel: function(toggle) {
268 var displayCss = toggle ? 'inline-block' : 'none';
269 var label = this.dom().children('.canvas-widgets').find('.canvas-widget-label');
270 label.css('display', displayCss);
271 this.toggleHighlight(toggle);
274 toggleHighlight: function(toggle) {
275 this._container().toggleClass('highlighted-element', toggle);
278 toggle: function(toggle) {
279 var mng = this.data('_wlxmlManager');
287 // DocumentNodeElement represents a text node from WLXML document rendered inside Canvas
288 var DocumentTextElement = function(wlxmlTextNode, canvas) {
289 DocumentElement.call(this, wlxmlTextNode, canvas);
292 $.extend(DocumentTextElement, {
293 isContentContainer: function(htmlElement) {
294 return htmlElement.nodeType === Node.TEXT_NODE && $(htmlElement).parent().is('[document-text-element]');
298 DocumentTextElement.prototype = new DocumentElement();
300 $.extend(DocumentTextElement.prototype, {
301 createDOM: function() {
303 .attr('document-text-element', '')
304 .text(this.wlxmlNode.getText() || utils.unicode.ZWS);
311 setText: function(text) {
312 this.dom().contents()[0].data = text;
314 getText: function(options) {
315 options = _.extend({raw: false}, options || {});
316 var toret = this.dom().text();
318 toret = toret.replace(utils.unicode.ZWS, '');
322 isEmpty: function() {
323 // Having at least Zero Width Space is guaranteed be Content Observer
324 return this.dom().contents()[0].data === utils.unicode.ZWS;
326 after: function(params) {
327 if(params instanceof DocumentTextElement || params.text) {
331 if(params instanceof DocumentNodeElement) {
334 element = this.canvas.createElement(params);
336 this.dom().wrap('<div>');
337 this.dom().parent().after(element.dom());
341 before: function(params) {
342 if(params instanceof DocumentTextElement || params.text) {
346 if(params instanceof DocumentNodeElement) {
349 element = this.canvas.createElement(params);
351 this.dom().wrap('<div>');
352 this.dom().parent().before(element.dom());
357 toggleHighlight: function() {
358 // do nothing for now
363 DocumentElement: DocumentElement,
364 DocumentNodeElement: DocumentNodeElement,
365 DocumentTextElement: DocumentTextElement