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 toSet = event.meta.node.getText();
158 this.children().some(function(child) {
159 if(child.wlxmlNode.sameNode(event.meta.node)) {
161 toSet = utils.unicode.ZWS;
163 if(toSet !== child.getText()) {
164 child.setText(toSet);
171 onStateChange: function(changes) {
172 if(_.isBoolean(changes.exposed) && !this.isSpan()) {
173 this._container().toggleClass('highlighted-element', changes.exposed);
175 if(_.isBoolean(changes.active) && !this.isSpan()) {
176 this._container().toggleClass('current-node-element', changes.active);
183 return this.wlxmlNode.getTagName() === 'span';
186 containsBlock: function() {
187 return this.children()
188 .filter(function(child) {
189 return child instanceof documentElement.DocumentNodeElement;
191 .some(function(child) {
192 if(child.isBlock()) {
195 return child.containsBlock();
200 prepend: function(param) {
202 if(param instanceof documentElement.DocumentElement) {
205 element = this.canvas.createElement(param);
208 this._container().prepend(element.dom);
214 childIndex: function(child) {
215 var children = this.children(),
217 children.forEach(function(c, idx) {
218 if(c.sameNode(child)) {
226 getWlxmlClass: function() {
227 var klass = this._container().attr('wlxml-class');
229 return klass.replace(/-/g, '.');
233 setWlxmlClass: function(klass) {
234 if(klass === this.getWlxmlClass()) {
238 this._container().attr('wlxml-class', klass.replace(/\./g, '-'));
241 this._container().removeAttr('wlxml-class');