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 instanceof DocumentTextElement) {
64 toret = child.getVerticallyFirstTextElement();
73 getPreviousTextElement: function(includeInvisible) {
74 return this.getNearestTextElement('above', includeInvisible);
77 getNextTextElement: function(includeInvisible) {
78 return this.getNearestTextElement('below', includeInvisible);
81 getNearestTextElement: function(direction, includeInvisible) {
82 includeInvisible = includeInvisible !== undefined ? includeInvisible : false;
83 var selector = '[document-text-element]' + (includeInvisible ? '' : ':visible');
84 return this.canvas.getDocumentElement(utils.nearestInDocumentOrder(selector, direction, this.dom()[0]));
87 exec: function(method) {
88 if(this.manager && this.manager[method]) {
89 return this.manager[method].apply(this.manager, Array.prototype.slice.call(arguments, 1));
95 // DocumentNodeElement represents an element node from WLXML document rendered inside Canvas
96 var DocumentNodeElement = function(wlxmlNode, canvas) {
97 DocumentElement.call(this, wlxmlNode, canvas);
98 wlxmlNode.setData('canvasElement', this);
102 var manipulate = function(e, params, action) {
104 if(params instanceof DocumentElement) {
107 element = e.canvas.createElement(params);
109 var target = (action === 'append' || action === 'prepend') ? e._container() : e.dom();
110 target[action](element.dom());
114 DocumentNodeElement.prototype = new DocumentElement();
117 $.extend(DocumentNodeElement.prototype, {
118 createDOM: function() {
120 .attr('document-node-element', ''),
121 widgetsContainer = $('<div>')
122 .addClass('canvas-widgets')
123 .attr('contenteditable', false),
124 container = $('<div>')
125 .attr('document-element-content', '');
127 dom.append(widgetsContainer, container);
128 // Make sure widgets aren't navigable with arrow keys
129 widgetsContainer.find('*').add(widgetsContainer).attr('tabindex', -1);
130 this.$element = dom; //@!!!
132 this.setWlxmlTag(this.wlxmlNode.getTagName());
133 this.setWlxmlClass(this.wlxmlNode.getClass());
135 this.wlxmlNode.contents().forEach(function(node) {
136 container.append(this.canvas.createElement(node).dom());
140 _container: function() {
141 return this.dom().children('[document-element-content]');
148 append: function(params) {
149 return manipulate(this, params, 'append');
151 prepend: function(params) {
152 return manipulate(this, params, 'prepend');
154 before: function(params) {
155 return manipulate(this, params, 'before');
158 after: function(params) {
159 return manipulate(this, params, 'after');
161 children: function() {
163 if(this instanceof DocumentTextElement) {
168 var elementContent = this._container().contents();
170 elementContent.each(function() {
171 var childElement = element.canvas.getDocumentElement(this);
172 if(childElement === undefined) {
175 toret.push(childElement);
179 childIndex: function(child) {
180 var children = this.children(),
182 children.forEach(function(c, idx) {
183 if(c.sameNode(child)) {
190 getWlxmlTag: function() {
191 return this._container().attr('wlxml-tag');
193 setWlxmlTag: function(tag) {
194 this._container().attr('wlxml-tag', tag);
196 getWlxmlClass: function() {
197 var klass = this._container().attr('wlxml-class');
199 return klass.replace(/-/g, '.');
203 setWlxmlClass: function(klass) {
204 if(klass === this.getWlxmlClass()) {
208 this._container().attr('wlxml-class', klass.replace(/\./g, '-'));
211 this._container().removeAttr('wlxml-class');
213 this.manager = wlxmlManagers.getFor(this);
214 this.manager.setup();
216 toggleLabel: function(toggle) {
217 var displayCss = toggle ? 'inline-block' : 'none';
218 var label = this.dom().children('.canvas-widgets').find('.canvas-widget-label');
219 label.css('display', displayCss);
220 this.toggleHighlight(toggle);
223 toggleHighlight: function(toggle) {
224 this._container().toggleClass('highlighted-element', toggle);
227 toggle: function(toggle) {
229 this.manager.toggle(toggle);
235 // DocumentNodeElement represents a text node from WLXML document rendered inside Canvas
236 var DocumentTextElement = function(wlxmlTextNode, canvas) {
237 DocumentElement.call(this, wlxmlTextNode, canvas);
240 $.extend(DocumentTextElement, {
241 isContentContainer: function(htmlElement) {
242 return htmlElement.nodeType === Node.TEXT_NODE && $(htmlElement).parent().is('[document-text-element]');
246 DocumentTextElement.prototype = new DocumentElement();
248 $.extend(DocumentTextElement.prototype, {
249 createDOM: function() {
251 .attr('document-text-element', '')
252 .text(this.wlxmlNode.getText() || utils.unicode.ZWS);
259 setText: function(text) {
260 this.dom().contents()[0].data = text;
262 getText: function(options) {
263 options = _.extend({raw: false}, options || {});
264 var toret = this.dom().text();
266 toret = toret.replace(utils.unicode.ZWS, '');
270 isEmpty: function() {
271 // Having at least Zero Width Space is guaranteed be Content Observer
272 return this.dom().contents()[0].data === utils.unicode.ZWS;
274 after: function(params) {
275 if(params instanceof DocumentTextElement || params.text) {
279 if(params instanceof DocumentNodeElement) {
282 element = this.canvas.createElement(params);
284 this.dom().wrap('<div>');
285 this.dom().parent().after(element.dom());
289 before: function(params) {
290 if(params instanceof DocumentTextElement || params.text) {
294 if(params instanceof DocumentNodeElement) {
297 element = this.canvas.createElement(params);
299 this.dom().wrap('<div>');
300 this.dom().parent().before(element.dom());
305 toggleHighlight: function() {
306 // do nothing for now
311 DocumentElement: DocumentElement,
312 DocumentNodeElement: DocumentNodeElement,
313 DocumentTextElement: DocumentTextElement