1 define(function(require) {
5 var $ = require('libs/jquery'),
6 _ = require('libs/underscore'),
7 documentElement = require('./documentElement'),
8 utils = require('./utils'),
9 CommentsView = require('./comments/comments');
12 var DocumentNodeElement = documentElement.DocumentNodeElement;
14 var generic = Object.create(DocumentNodeElement.prototype);
18 DocumentNodeElement.prototype.init.call(this);
20 .attr('wlxml-tag', this.wlxmlNode.getTagName())
21 .attr('wlxml-class', this.wlxmlNode.getClass().replace(/\./g, '-'));
23 this.wlxmlNode.contents().forEach(function(node) {
24 var el = this.canvas.createElement(node);
26 this._container().append(el.dom);
30 this.commentsView = new CommentsView(this.wlxmlNode, this.canvas.metadata.user);
31 this.addToGutter(this.commentsView);
32 this.commentTip = $('<div class="comment-tip"><i class="icon-comment"></i></div>');
33 this.addWidget(this.commentTip);
35 if(!this.wlxmlNode.hasChild({klass: 'comment'})) {
36 this.commentTip.hide();
43 if(this.wlxmlNode.getTagName() === 'span') {
44 if(this.containsBlock()) {
45 this.displayAsBlock();
50 this.displayAsBlock();
54 getFirst: function(e1, e2) {
55 var idx1 = this.childIndex(e1),
56 idx2 = this.childIndex(e2);
57 if(e1 === null || e2 === null) {
60 return idx1 <= idx2 ? e1: e2;
63 children: function() {
66 this._container().contents().each(function() {
67 var childElement = element.canvas.getDocumentElement(this);
68 if(childElement === undefined) {
72 toret.push(childElement);
77 getVerticallyFirstTextElement: function(params) {
81 considerChildren: true
84 this.children().some(function(child) {
85 if(child instanceof documentElement.DocumentTextElement) {
88 } else if(params.considerChildren) {
89 toret = child.getVerticallyFirstTextElement();
98 onNodeAdded: function(event) {
99 if(event.meta.node.isRoot()) {
100 this.canvas.reloadRoot();
104 var ptr = event.meta.node.prev(),
105 referenceElement, referenceAction, actionArg;
107 while(ptr && !(referenceElement = utils.getElementForElementRootNode(ptr))) {
111 if(referenceElement) {
112 referenceAction = 'after';
114 referenceElement = this;
115 referenceAction = 'prepend';
118 if(event.meta.move) {
119 /* Let's check if this node had its own canvas element and it's accessible. */
120 actionArg = utils.getElementForElementRootNode(event.meta.node);
123 actionArg = event.meta.node;
126 referenceElement[referenceAction](actionArg);
128 if(event.meta.node.is('comment')) {
129 this.commentTip.show();
130 this.commentsView.render();
133 onNodeDetached: function(event) {
134 var isComment = event.meta.node.is('comment');
135 if(event.meta.node.sameNode(this)) {
138 this.children().some(function(child) {
139 if(child.wlxmlNode.sameNode(event.meta.node)) {
144 if(isComment && !this.wlxmlNode.hasChild({klass: 'comment'})) {
145 this.commentTip.hide();
147 this.commentsView.render();
150 onNodeTextChange: function(event) {
151 var node = event.meta.node;
153 /* TODO: This handling of changes to the comments should probably be implemented via separate,
154 non-rendering comments element */
155 if(node.parent() && node.parent().is('comment') && this.wlxmlNode.sameNode(node.parent().parent())) {
156 this.commentsView.render();
160 onStateChange: function(changes) {
161 if(_.isBoolean(changes.exposed) && !this.isSpan()) {
162 this._container().toggleClass('highlighted-element', changes.exposed);
164 if(_.isBoolean(changes.active) && !this.isSpan()) {
165 this._container().toggleClass('current-node-element', changes.active);
172 return this.wlxmlNode.getTagName() === 'span';
175 containsBlock: function() {
176 return this.children()
177 .filter(function(child) {
178 return child instanceof documentElement.DocumentNodeElement;
180 .some(function(child) {
181 if(child.isBlock()) {
184 return child.containsBlock();
189 prepend: function(param) {
191 if(param instanceof documentElement.DocumentElement) {
194 element = this.canvas.createElement(param);
197 this._container().prepend(element.dom);
203 childIndex: function(child) {
204 var children = this.children(),
206 children.forEach(function(c, idx) {
207 if(c.sameNode(child)) {