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 this.setWlxmlClass(this.wlxmlNode.getClass());
29 this.wlxmlNode.contents().forEach(function(node) {
30 var el = this.canvas.createElement(node);
32 this._container().append(el.dom);
36 this.commentsView = new CommentsView(this.wlxmlNode, this.canvas.metadata.user);
37 this.addToGutter(this.commentsView);
38 this.commentTip = $('<div class="comment-tip"><i class="icon-comment"></i></div>');
39 this.addWidget(this.commentTip);
41 if(!this.wlxmlNode.hasChild({klass: 'comment'})) {
42 this.commentTip.hide();
49 if(this.wlxmlNode.getTagName() === 'span') {
50 if(this.containsBlock()) {
51 this.displayAsBlock();
56 this.displayAsBlock();
60 getFirst: function(e1, e2) {
61 var idx1 = this.childIndex(e1),
62 idx2 = this.childIndex(e2);
63 if(e1 === null || e2 === null) {
66 return idx1 <= idx2 ? e1: e2;
69 children: function() {
72 this._container().contents().each(function() {
73 var childElement = element.canvas.getDocumentElement(this);
74 if(childElement === undefined) {
78 toret.push(childElement);
83 getVerticallyFirstTextElement: function(params) {
87 considerChildren: true
90 this.children().some(function(child) {
91 if(child instanceof documentElement.DocumentTextElement) {
94 } else if(params.considerChildren) {
95 toret = child.getVerticallyFirstTextElement();
104 onNodeAttrChange: function(event) {
105 if(event.meta.attr === 'class') {
106 this.setWlxmlClass(event.meta.newVal); //
109 onNodeAdded: function(event) {
110 if(event.meta.node.isRoot()) {
111 this.canvas.reloadRoot();
115 var ptr = event.meta.node.prev(),
116 referenceElement, referenceAction, actionArg;
118 while(ptr && !(referenceElement = utils.getElementForElementRootNode(ptr))) {
122 if(referenceElement) {
123 referenceAction = 'after';
125 referenceElement = this;
126 referenceAction = 'prepend';
129 if(event.meta.move) {
130 /* Let's check if this node had its own canvas element and it's accessible. */
131 actionArg = utils.getElementForElementRootNode(event.meta.node);
134 actionArg = event.meta.node;
137 referenceElement[referenceAction](actionArg);
139 if(event.meta.node.is('comment')) {
140 this.commentTip.show();
141 this.commentsView.render();
144 onNodeDetached: function(event) {
145 var isComment = event.meta.node.is('comment');
146 if(event.meta.node.sameNode(this)) {
149 this.children().some(function(child) {
150 if(child.wlxmlNode.sameNode(event.meta.node)) {
155 if(isComment && !this.wlxmlNode.hasChild({klass: 'comment'})) {
156 this.commentTip.hide();
158 this.commentsView.render();
161 onNodeTextChange: function(event) {
162 var node = event.meta.node,
163 toSet = node.getText(),
166 handled = this.children().some(function(child) {
167 if(child.wlxmlNode.sameNode(node)) {
169 toSet = utils.unicode.ZWS;
171 if(toSet !== child.getText()) {
172 child.setText(toSet);
178 if(!handled && node.parent() && node.parent().is('comment') && this.wlxmlNode.sameNode(node.parent().parent())) {
179 this.commentsView.render();
183 onStateChange: function(changes) {
184 if(_.isBoolean(changes.exposed) && !this.isSpan()) {
185 this._container().toggleClass('highlighted-element', changes.exposed);
187 if(_.isBoolean(changes.active) && !this.isSpan()) {
188 this._container().toggleClass('current-node-element', changes.active);
195 return this.wlxmlNode.getTagName() === 'span';
198 containsBlock: function() {
199 return this.children()
200 .filter(function(child) {
201 return child instanceof documentElement.DocumentNodeElement;
203 .some(function(child) {
204 if(child.isBlock()) {
207 return child.containsBlock();
212 prepend: function(param) {
214 if(param instanceof documentElement.DocumentElement) {
217 element = this.canvas.createElement(param);
220 this._container().prepend(element.dom);
226 childIndex: function(child) {
227 var children = this.children(),
229 children.forEach(function(c, idx) {
230 if(c.sameNode(child)) {
238 getWlxmlClass: function() {
239 var klass = this._container().attr('wlxml-class');
241 return klass.replace(/-/g, '.');
245 setWlxmlClass: function(klass) {
246 if(klass === this.getWlxmlClass()) {
250 this._container().attr('wlxml-class', klass.replace(/\./g, '-'));
253 this._container().removeAttr('wlxml-class');