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 this._container().append(this.canvas.createElement(node).dom);
35 if(this.wlxmlNode.getTagName() === 'span') {
36 if(this.containsBlock()) {
37 this.displayAsBlock();
42 this.displayAsBlock();
46 getFirst: function(e1, e2) {
47 var idx1 = this.childIndex(e1),
48 idx2 = this.childIndex(e2);
49 if(e1 === null || e2 === null) {
52 return idx1 <= idx2 ? e1: e2;
55 children: function() {
58 this._container().contents().each(function() {
59 var childElement = element.canvas.getDocumentElement(this);
60 if(childElement === undefined) {
64 toret.push(childElement);
69 getVerticallyFirstTextElement: function() {
71 this.children().some(function(child) {
72 if(child instanceof documentElement.DocumentTextElement) {
76 toret = child.getVerticallyFirstTextElement();
85 onNodeAttrChange: function(event) {
86 if(event.meta.attr === 'class') {
87 this.setWlxmlClass(event.meta.newVal); //
90 onNodeAdded: function(event) {
91 if(event.meta.node.isRoot()) {
92 this.canvas.reloadRoot();
96 var nodeIndex = event.meta.node.getIndex(),
97 referenceElement, referenceAction, actionArg;
100 referenceElement = this;
101 referenceAction = 'prepend';
103 referenceElement = this.children()[nodeIndex-1];
104 referenceAction = 'after';
107 if(event.meta.move) {
108 /* Let's check if this node had its own canvas element and it's accessible. */
109 actionArg = utils.getElementForElementRootNode(event.meta.node);
110 if(actionArg && actionArg.sameNode(referenceElement)) {
111 referenceElement = this.children()[nodeIndex];
115 actionArg = event.meta.node;
118 referenceElement[referenceAction](actionArg);
120 onNodeDetached: function(event) {
121 if(event.meta.node.sameNode(this)) {
124 this.children().some(function(child) {
125 if(child.wlxmlNode.sameNode(event.meta.node)) {
132 onNodeTextChange: function(event) {
133 var toSet = event.meta.node.getText();
134 this.children().some(function(child) {
135 if(child.wlxmlNode.sameNode(event.meta.node)) {
137 toSet = utils.unicode.ZWS;
139 if(toSet !== child.getText()) {
140 child.setText(toSet);
147 onStateChange: function(changes) {
148 if(_.isBoolean(changes.exposed) && !this.isSpan()) {
149 this._container().toggleClass('highlighted-element', changes.exposed);
151 if(_.isBoolean(changes.active) && !this.isSpan()) {
152 this._container().toggleClass('current-node-element', changes.active);
159 return this.wlxmlNode.getTagName() === 'span';
162 containsBlock: function() {
163 return this.children()
164 .filter(function(child) {
165 return child instanceof documentElement.DocumentNodeElement;
167 .some(function(child) {
168 if(child.isBlock()) {
171 return child.containsBlock();
176 prepend: function(param) {
178 if(param instanceof documentElement.DocumentElement) {
181 element = this.canvas.createElement(param);
183 this._container().prepend(element.dom);
188 childIndex: function(child) {
189 var children = this.children(),
191 children.forEach(function(c, idx) {
192 if(c.sameNode(child)) {
200 getWlxmlClass: function() {
201 var klass = this._container().attr('wlxml-class');
203 return klass.replace(/-/g, '.');
207 setWlxmlClass: function(klass) {
208 if(klass === this.getWlxmlClass()) {
212 this._container().attr('wlxml-class', klass.replace(/\./g, '-'));
215 this._container().removeAttr('wlxml-class');