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, {});
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() {
85 this.children().some(function(child) {
86 if(child instanceof documentElement.DocumentTextElement) {
90 toret = child.getVerticallyFirstTextElement();
99 onNodeAttrChange: function(event) {
100 if(event.meta.attr === 'class') {
101 this.setWlxmlClass(event.meta.newVal); //
104 onNodeAdded: function(event) {
105 if(event.meta.node.isRoot()) {
106 this.canvas.reloadRoot();
110 var ptr = event.meta.node.prev(),
111 referenceElement, referenceAction, actionArg;
113 while(ptr && !(referenceElement = utils.getElementForElementRootNode(ptr))) {
117 if(referenceElement) {
118 referenceAction = 'after';
120 referenceElement = this;
121 referenceAction = 'prepend';
124 if(event.meta.move) {
125 /* Let's check if this node had its own canvas element and it's accessible. */
126 actionArg = utils.getElementForElementRootNode(event.meta.node);
129 actionArg = event.meta.node;
132 referenceElement[referenceAction](actionArg);
134 if(event.meta.node.is('comment')) {
135 this.commentTip.show();
136 this.commentsView.render();
139 onNodeDetached: function(event) {
140 var isComment = event.meta.node.is('comment');
141 if(event.meta.node.sameNode(this)) {
144 this.children().some(function(child) {
145 if(child.wlxmlNode.sameNode(event.meta.node)) {
150 if(isComment && !this.wlxmlNode.hasChild({klass: 'comment'})) {
151 this.commentTip.hide();
153 this.commentsView.render();
156 onNodeTextChange: function(event) {
157 var node = event.meta.node,
158 toSet = node.getText(),
161 handled = this.children().some(function(child) {
162 if(child.wlxmlNode.sameNode(node)) {
164 toSet = utils.unicode.ZWS;
166 if(toSet !== child.getText()) {
167 child.setText(toSet);
173 if(!handled && node.parent() && node.parent().is('comment') && this.wlxmlNode.sameNode(node.parent().parent())) {
174 this.commentsView.render();
178 onStateChange: function(changes) {
179 if(_.isBoolean(changes.exposed) && !this.isSpan()) {
180 this._container().toggleClass('highlighted-element', changes.exposed);
182 if(_.isBoolean(changes.active) && !this.isSpan()) {
183 this._container().toggleClass('current-node-element', changes.active);
190 return this.wlxmlNode.getTagName() === 'span';
193 containsBlock: function() {
194 return this.children()
195 .filter(function(child) {
196 return child instanceof documentElement.DocumentNodeElement;
198 .some(function(child) {
199 if(child.isBlock()) {
202 return child.containsBlock();
207 prepend: function(param) {
209 if(param instanceof documentElement.DocumentElement) {
212 element = this.canvas.createElement(param);
215 this._container().prepend(element.dom);
221 childIndex: function(child) {
222 var children = this.children(),
224 children.forEach(function(c, idx) {
225 if(c.sameNode(child)) {
233 getWlxmlClass: function() {
234 var klass = this._container().attr('wlxml-class');
236 return klass.replace(/-/g, '.');
240 setWlxmlClass: function(klass) {
241 if(klass === this.getWlxmlClass()) {
245 this._container().attr('wlxml-class', klass.replace(/\./g, '-'));
248 this._container().removeAttr('wlxml-class');