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');
11 var labelWidget = function(tag, klass) {
13 .addClass('canvas-widget canvas-widget-label')
14 .text(wlxmlUtils.getTagLabel(tag) + (klass ? ' / ' + wlxmlUtils.getClassLabel(klass) : ''));
16 void(labelWidget); // for linters; labelWidget is unused on purpose for now
18 var DocumentNodeElement = documentElement.DocumentNodeElement;
20 var generic = Object.create(DocumentNodeElement.prototype);
24 DocumentNodeElement.prototype.init.call(this);
26 .attr('wlxml-tag', this.wlxmlNode.getTagName());
27 this.setWlxmlClass(this.wlxmlNode.getClass());
28 this.wlxmlNode.contents().forEach(function(node) {
29 var el = this.canvas.createElement(node);
31 this._container().append(el.dom);
35 this.commentTip = $('<div class="comment-tip"><i class="icon-comment"></i></div>');
36 this.addWidget(this.commentTip);
38 if(!this.wlxmlNode.hasChild({klass: 'comment'})) {
39 this.commentTip.hide();
46 if(this.wlxmlNode.getTagName() === 'span') {
47 if(this.containsBlock()) {
48 this.displayAsBlock();
53 this.displayAsBlock();
57 getFirst: function(e1, e2) {
58 var idx1 = this.childIndex(e1),
59 idx2 = this.childIndex(e2);
60 if(e1 === null || e2 === null) {
63 return idx1 <= idx2 ? e1: e2;
66 children: function() {
69 this._container().contents().each(function() {
70 var childElement = element.canvas.getDocumentElement(this);
71 if(childElement === undefined) {
75 toret.push(childElement);
80 getVerticallyFirstTextElement: function() {
82 this.children().some(function(child) {
83 if(child instanceof documentElement.DocumentTextElement) {
87 toret = child.getVerticallyFirstTextElement();
96 onNodeAttrChange: function(event) {
97 if(event.meta.attr === 'class') {
98 this.setWlxmlClass(event.meta.newVal); //
101 onNodeAdded: function(event) {
102 if(event.meta.node.isRoot()) {
103 this.canvas.reloadRoot();
107 var ptr = event.meta.node.prev(),
108 referenceElement, referenceAction, actionArg;
110 while(ptr && !(referenceElement = utils.getElementForElementRootNode(ptr))) {
114 if(referenceElement) {
115 referenceAction = 'after';
117 referenceElement = this;
118 referenceAction = 'prepend';
121 if(event.meta.move) {
122 /* Let's check if this node had its own canvas element and it's accessible. */
123 actionArg = utils.getElementForElementRootNode(event.meta.node);
126 actionArg = event.meta.node;
129 referenceElement[referenceAction](actionArg);
131 if(event.meta.node.is('comment')) {
132 this.commentTip.show();
135 onNodeDetached: function(event) {
136 var isComment = event.meta.node.is('comment');
137 if(event.meta.node.sameNode(this)) {
140 this.children().some(function(child) {
141 if(child.wlxmlNode.sameNode(event.meta.node)) {
146 if(isComment && !this.wlxmlNode.hasChild({klass: 'comment'})) {
147 this.commentTip.hide();
151 onNodeTextChange: function(event) {
152 var toSet = event.meta.node.getText();
153 this.children().some(function(child) {
154 if(child.wlxmlNode.sameNode(event.meta.node)) {
156 toSet = utils.unicode.ZWS;
158 if(toSet !== child.getText()) {
159 child.setText(toSet);
166 onStateChange: function(changes) {
167 if(_.isBoolean(changes.exposed) && !this.isSpan()) {
168 this._container().toggleClass('highlighted-element', changes.exposed);
170 if(_.isBoolean(changes.active) && !this.isSpan()) {
171 this._container().toggleClass('current-node-element', changes.active);
178 return this.wlxmlNode.getTagName() === 'span';
181 containsBlock: function() {
182 return this.children()
183 .filter(function(child) {
184 return child instanceof documentElement.DocumentNodeElement;
186 .some(function(child) {
187 if(child.isBlock()) {
190 return child.containsBlock();
195 prepend: function(param) {
197 if(param instanceof documentElement.DocumentElement) {
200 element = this.canvas.createElement(param);
203 this._container().prepend(element.dom);
209 childIndex: function(child) {
210 var children = this.children(),
212 children.forEach(function(c, idx) {
213 if(c.sameNode(child)) {
221 getWlxmlClass: function() {
222 var klass = this._container().attr('wlxml-class');
224 return klass.replace(/-/g, '.');
228 setWlxmlClass: function(klass) {
229 if(klass === this.getWlxmlClass()) {
233 this._container().attr('wlxml-class', klass.replace(/\./g, '-'));
236 this._container().removeAttr('wlxml-class');