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,
152 toSet = node.getText(),
155 handled = this.children().some(function(child) {
156 if(child.wlxmlNode.sameNode(node)) {
158 toSet = utils.unicode.ZWS;
160 if(toSet !== child.getText()) {
161 child.setText(toSet);
167 if(!handled && node.parent() && node.parent().is('comment') && this.wlxmlNode.sameNode(node.parent().parent())) {
168 this.commentsView.render();
172 onStateChange: function(changes) {
173 if(_.isBoolean(changes.exposed) && !this.isSpan()) {
174 this._container().toggleClass('highlighted-element', changes.exposed);
176 if(_.isBoolean(changes.active) && !this.isSpan()) {
177 this._container().toggleClass('current-node-element', changes.active);
184 return this.wlxmlNode.getTagName() === 'span';
187 containsBlock: function() {
188 return this.children()
189 .filter(function(child) {
190 return child instanceof documentElement.DocumentNodeElement;
192 .some(function(child) {
193 if(child.isBlock()) {
196 return child.containsBlock();
201 prepend: function(param) {
203 if(param instanceof documentElement.DocumentElement) {
206 element = this.canvas.createElement(param);
209 this._container().prepend(element.dom);
215 childIndex: function(child) {
216 var children = this.children(),
218 children.forEach(function(c, idx) {
219 if(c.sameNode(child)) {