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);
24 $.extend(DocumentElement.prototype, {
26 return $.contains(document.documentElement, this.dom()[0]);
32 var parents = this.$element.parents('[document-node-element]');
34 return this.canvas.getDocumentElement(parents[0]);
41 parent = this.parent();
44 parent = parent.parent();
49 sameNode: function(other) {
50 return other && (typeof other === typeof this) && other.dom()[0] === this.dom()[0];
53 markAsCurrent: function() {
54 this.canvas.markAsCurrent(this);
57 getVerticallyFirstTextElement: function() {
59 this.children().some(function(child) {
60 if(!child.isVisible()) {
61 return false; // continue
63 if(child instanceof DocumentTextElement) {
67 toret = child.getVerticallyFirstTextElement();
76 getPreviousTextElement: function(includeInvisible) {
77 return this.getNearestTextElement('above', includeInvisible);
80 getNextTextElement: function(includeInvisible) {
81 return this.getNearestTextElement('below', includeInvisible);
84 getNearestTextElement: function(direction, includeInvisible) {
85 includeInvisible = includeInvisible !== undefined ? includeInvisible : false;
86 var selector = '[document-text-element]' + (includeInvisible ? '' : ':visible');
87 return this.canvas.getDocumentElement(utils.nearestInDocumentOrder(selector, direction, this.dom()[0]));
90 isVisible: function() {
91 return this instanceof DocumentTextElement || this.getWlxmlTag() !== 'metadata';
94 isInsideList: function() {
95 return this.parents().some(function(parent) {
96 return parent.is('list');
100 exec: function(method) {
101 if(this.manager && this.manager[method]) {
102 return this.manager[method].apply(this.manager, Array.prototype.slice.call(arguments, 1));
108 // DocumentNodeElement represents an element node from WLXML document rendered inside Canvas
109 var DocumentNodeElement = function(wlxmlNode, canvas) {
110 DocumentElement.call(this, wlxmlNode, canvas);
111 wlxmlNode.setData('canvasElement', this);
115 var manipulate = function(e, params, action) {
117 if(params instanceof DocumentElement) {
120 element = e.canvas.createElement(params);
122 var target = (action === 'append' || action === 'prepend') ? e._container() : e.dom();
123 target[action](element.dom());
127 DocumentNodeElement.prototype = new DocumentElement();
130 $.extend(DocumentNodeElement.prototype, {
131 createDOM: function() {
133 .attr('document-node-element', ''),
134 widgetsContainer = $('<div>')
135 .addClass('canvas-widgets')
136 .attr('contenteditable', false),
137 container = $('<div>')
138 .attr('document-element-content', '');
140 dom.append(widgetsContainer, container);
141 // Make sure widgets aren't navigable with arrow keys
142 widgetsContainer.find('*').add(widgetsContainer).attr('tabindex', -1);
143 this.$element = dom; //@!!!
145 this.setWlxmlTag(this.wlxmlNode.getTagName());
146 this.setWlxmlClass(this.wlxmlNode.getClass());
148 this.wlxmlNode.contents().forEach(function(node) {
149 container.append(this.canvas.createElement(node).dom());
153 _container: function() {
154 return this.dom().children('[document-element-content]');
161 append: function(params) {
162 return manipulate(this, params, 'append');
164 prepend: function(params) {
165 return manipulate(this, params, 'prepend');
167 before: function(params) {
168 return manipulate(this, params, 'before');
171 after: function(params) {
172 return manipulate(this, params, 'after');
174 children: function() {
176 if(this instanceof DocumentTextElement) {
181 var elementContent = this._container().contents();
183 elementContent.each(function() {
184 var childElement = element.canvas.getDocumentElement(this);
185 if(childElement === undefined) {
188 toret.push(childElement);
192 childIndex: function(child) {
193 var children = this.children(),
195 children.forEach(function(c, idx) {
196 if(c.sameNode(child)) {
203 getWlxmlTag: function() {
204 return this._container().attr('wlxml-tag');
206 setWlxmlTag: function(tag) {
207 this._container().attr('wlxml-tag', tag);
209 getWlxmlClass: function() {
210 var klass = this._container().attr('wlxml-class');
212 return klass.replace(/-/g, '.');
216 setWlxmlClass: function(klass) {
217 if(klass === this.getWlxmlClass()) {
221 this._container().attr('wlxml-class', klass.replace(/\./g, '-'));
224 this._container().removeAttr('wlxml-class');
226 this.manager = wlxmlManagers.getFor(this);
227 this.manager.setup();
230 if(what === 'list' && _.contains(['list.items', 'list.items.enum'], this.getWlxmlClass())) {
235 toggleLabel: function(toggle) {
236 var displayCss = toggle ? 'inline-block' : 'none';
237 var label = this.dom().children('.canvas-widgets').find('.canvas-widget-label');
238 label.css('display', displayCss);
239 this.toggleHighlight(toggle);
242 toggleHighlight: function(toggle) {
243 this._container().toggleClass('highlighted-element', toggle);
246 toggle: function(toggle) {
248 this.manager.toggle(toggle);
254 // DocumentNodeElement represents a text node from WLXML document rendered inside Canvas
255 var DocumentTextElement = function(wlxmlTextNode, canvas) {
256 DocumentElement.call(this, wlxmlTextNode, canvas);
259 $.extend(DocumentTextElement, {
260 isContentContainer: function(htmlElement) {
261 return htmlElement.nodeType === Node.TEXT_NODE && $(htmlElement).parent().is('[document-text-element]');
265 DocumentTextElement.prototype = new DocumentElement();
267 $.extend(DocumentTextElement.prototype, {
268 createDOM: function() {
270 .attr('document-text-element', '')
271 .text(this.wlxmlNode.getText() || utils.unicode.ZWS);
278 setText: function(text) {
279 this.dom().contents()[0].data = text;
281 getText: function(options) {
282 options = _.extend({raw: false}, options || {});
283 var toret = this.dom().text();
285 toret = toret.replace(utils.unicode.ZWS, '');
289 isEmpty: function() {
290 // Having at least Zero Width Space is guaranteed be Content Observer
291 return this.dom().contents()[0].data === utils.unicode.ZWS;
293 after: function(params) {
294 if(params instanceof DocumentTextElement || params.text) {
298 if(params instanceof DocumentNodeElement) {
301 element = this.canvas.createElement(params);
303 this.dom().wrap('<div>');
304 this.dom().parent().after(element.dom());
308 before: function(params) {
309 if(params instanceof DocumentTextElement || params.text) {
313 if(params instanceof DocumentNodeElement) {
316 element = this.canvas.createElement(params);
318 this.dom().wrap('<div>');
319 this.dom().parent().before(element.dom());
324 toggleHighlight: function() {
325 // do nothing for now
330 DocumentElement: DocumentElement,
331 DocumentNodeElement: DocumentNodeElement,
332 DocumentTextElement: DocumentTextElement