1 define(function(require) {
5 var $ = require('libs/jquery'),
6 _ = require('libs/underscore'),
7 documentElement = require('./documentElement'),
8 utils = require('./utils'),
9 wlxmlUtils = require('utils/wlxml'),
10 CommentsView = require('./comments/comments');
12 var labelWidget = function(tag, klass) {
14 .addClass('canvas-widget canvas-widget-label')
15 .text(wlxmlUtils.getTagLabel(tag) + (klass ? ' / ' + wlxmlUtils.getClassLabel(klass) : ''));
17 void(labelWidget); // for linters; labelWidget is unused on purpose for now
19 var DocumentNodeElement = documentElement.DocumentNodeElement;
21 var generic = Object.create(DocumentNodeElement.prototype);
25 DocumentNodeElement.prototype.init.call(this);
27 .attr('wlxml-tag', this.wlxmlNode.getTagName())
28 .attr('wlxml-class', this.wlxmlNode.getClass().replace(/\./g, '-'));
30 this.wlxmlNode.contents().forEach(function(node) {
31 var el = this.canvas.createElement(node);
33 this._container().append(el.dom);
37 this.commentsView = new CommentsView(this.wlxmlNode, this.canvas.metadata.user);
38 this.addToGutter(this.commentsView);
39 this.commentTip = $('<div class="comment-tip"><i class="icon-comment"></i></div>');
40 this.addWidget(this.commentTip);
42 if(!this.wlxmlNode.hasChild({klass: 'comment'})) {
43 this.commentTip.hide();
50 if(this.wlxmlNode.getTagName() === 'span') {
51 if(this.containsBlock()) {
52 this.displayAsBlock();
57 this.displayAsBlock();
61 getFirst: function(e1, e2) {
62 var idx1 = this.childIndex(e1),
63 idx2 = this.childIndex(e2);
64 if(e1 === null || e2 === null) {
67 return idx1 <= idx2 ? e1: e2;
70 children: function() {
73 this._container().contents().each(function() {
74 var childElement = element.canvas.getDocumentElement(this);
75 if(childElement === undefined) {
79 toret.push(childElement);
84 getVerticallyFirstTextElement: function(params) {
88 considerChildren: true
91 this.children().some(function(child) {
92 if(child instanceof documentElement.DocumentTextElement) {
95 } else if(params.considerChildren) {
96 toret = child.getVerticallyFirstTextElement();
105 onNodeAdded: function(event) {
106 if(event.meta.node.isRoot()) {
107 this.canvas.reloadRoot();
111 var ptr = event.meta.node.prev(),
112 referenceElement, referenceAction, actionArg;
114 while(ptr && !(referenceElement = utils.getElementForElementRootNode(ptr))) {
118 if(referenceElement) {
119 referenceAction = 'after';
121 referenceElement = this;
122 referenceAction = 'prepend';
125 if(event.meta.move) {
126 /* Let's check if this node had its own canvas element and it's accessible. */
127 actionArg = utils.getElementForElementRootNode(event.meta.node);
130 actionArg = event.meta.node;
133 referenceElement[referenceAction](actionArg);
135 if(event.meta.node.is('comment')) {
136 this.commentTip.show();
137 this.commentsView.render();
140 onNodeDetached: function(event) {
141 var isComment = event.meta.node.is('comment');
142 if(event.meta.node.sameNode(this)) {
145 this.children().some(function(child) {
146 if(child.wlxmlNode.sameNode(event.meta.node)) {
151 if(isComment && !this.wlxmlNode.hasChild({klass: 'comment'})) {
152 this.commentTip.hide();
154 this.commentsView.render();
157 onNodeTextChange: function(event) {
158 var node = event.meta.node,
159 toSet = node.getText(),
162 handled = this.children().some(function(child) {
163 if(child.wlxmlNode.sameNode(node)) {
165 toSet = utils.unicode.ZWS;
167 if(toSet !== child.getText()) {
168 child.setText(toSet);
174 if(!handled && node.parent() && node.parent().is('comment') && this.wlxmlNode.sameNode(node.parent().parent())) {
175 this.commentsView.render();
179 onStateChange: function(changes) {
180 if(_.isBoolean(changes.exposed) && !this.isSpan()) {
181 this._container().toggleClass('highlighted-element', changes.exposed);
183 if(_.isBoolean(changes.active) && !this.isSpan()) {
184 this._container().toggleClass('current-node-element', changes.active);
191 return this.wlxmlNode.getTagName() === 'span';
194 containsBlock: function() {
195 return this.children()
196 .filter(function(child) {
197 return child instanceof documentElement.DocumentNodeElement;
199 .some(function(child) {
200 if(child.isBlock()) {
203 return child.containsBlock();
208 prepend: function(param) {
210 if(param instanceof documentElement.DocumentElement) {
213 element = this.canvas.createElement(param);
216 this._container().prepend(element.dom);
222 childIndex: function(child) {
223 var children = this.children(),
225 children.forEach(function(c, idx) {
226 if(c.sameNode(child)) {