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);
38 if(this.wlxmlNode.getTagName() === 'span') {
39 if(this.containsBlock()) {
40 this.displayAsBlock();
45 this.displayAsBlock();
49 getFirst: function(e1, e2) {
50 var idx1 = this.childIndex(e1),
51 idx2 = this.childIndex(e2);
52 if(e1 === null || e2 === null) {
55 return idx1 <= idx2 ? e1: e2;
58 children: function() {
61 this._container().contents().each(function() {
62 var childElement = element.canvas.getDocumentElement(this);
63 if(childElement === undefined) {
67 toret.push(childElement);
72 getVerticallyFirstTextElement: function() {
74 this.children().some(function(child) {
75 if(child instanceof documentElement.DocumentTextElement) {
79 toret = child.getVerticallyFirstTextElement();
88 onNodeAttrChange: function(event) {
89 if(event.meta.attr === 'class') {
90 this.setWlxmlClass(event.meta.newVal); //
93 onNodeAdded: function(event) {
94 if(event.meta.node.isRoot()) {
95 this.canvas.reloadRoot();
99 var ptr = event.meta.node.prev(),
100 referenceElement, referenceAction, actionArg;
102 while(ptr && !(referenceElement = utils.getElementForElementRootNode(ptr))) {
106 if(referenceElement) {
107 referenceAction = 'after';
109 referenceElement = this;
110 referenceAction = 'prepend';
113 if(event.meta.move) {
114 /* Let's check if this node had its own canvas element and it's accessible. */
115 actionArg = utils.getElementForElementRootNode(event.meta.node);
118 actionArg = event.meta.node;
121 referenceElement[referenceAction](actionArg);
123 onNodeDetached: function(event) {
124 if(event.meta.node.sameNode(this)) {
127 this.children().some(function(child) {
128 if(child.wlxmlNode.sameNode(event.meta.node)) {
135 onNodeTextChange: function(event) {
136 var toSet = event.meta.node.getText();
137 this.children().some(function(child) {
138 if(child.wlxmlNode.sameNode(event.meta.node)) {
140 toSet = utils.unicode.ZWS;
142 if(toSet !== child.getText()) {
143 child.setText(toSet);
150 onStateChange: function(changes) {
151 if(_.isBoolean(changes.exposed) && !this.isSpan()) {
152 this._container().toggleClass('highlighted-element', changes.exposed);
154 if(_.isBoolean(changes.active) && !this.isSpan()) {
155 this._container().toggleClass('current-node-element', changes.active);
162 return this.wlxmlNode.getTagName() === 'span';
165 containsBlock: function() {
166 return this.children()
167 .filter(function(child) {
168 return child instanceof documentElement.DocumentNodeElement;
170 .some(function(child) {
171 if(child.isBlock()) {
174 return child.containsBlock();
179 prepend: function(param) {
181 if(param instanceof documentElement.DocumentElement) {
184 element = this.canvas.createElement(param);
187 this._container().prepend(element.dom);
193 childIndex: function(child) {
194 var children = this.children(),
196 children.forEach(function(c, idx) {
197 if(c.sameNode(child)) {
205 getWlxmlClass: function() {
206 var klass = this._container().attr('wlxml-class');
208 return klass.replace(/-/g, '.');
212 setWlxmlClass: function(klass) {
213 if(klass === this.getWlxmlClass()) {
217 this._container().attr('wlxml-class', klass.replace(/\./g, '-'));
220 this._container().removeAttr('wlxml-class');