1 define(function(require) {
5 var $ = require('libs/jquery'),
6 documentElement = require('./documentElement'),
7 utils = require('./utils'),
8 wlxmlUtils = require('utils/wlxml');
10 var labelWidget = function(tag, klass) {
12 .addClass('canvas-widget canvas-widget-label')
13 .text(wlxmlUtils.getTagLabel(tag) + (klass ? ' / ' + wlxmlUtils.getClassLabel(klass) : ''));
15 void(labelWidget); // for linters; labelWidget is unused on purpose for now
17 var DocumentNodeElement = documentElement.DocumentNodeElement;
19 var generic = Object.create(DocumentNodeElement.prototype);
23 DocumentNodeElement.prototype.init.call(this);
25 .attr('wlxml-tag', this.wlxmlNode.getTagName());
26 this.setWlxmlClass(this.wlxmlNode.getClass());
27 this.wlxmlNode.contents().forEach(function(node) {
28 this._container().append(this.canvas.createElement(node).dom);
34 if(this.wlxmlNode.getTagName() === 'span') {
35 if(this.containsBlock()) {
36 this.displayAsBlock();
41 this.displayAsBlock();
45 getFirst: function(e1, e2) {
46 var idx1 = this.childIndex(e1),
47 idx2 = this.childIndex(e2);
48 if(e1 === null || e2 === null) {
51 return idx1 <= idx2 ? e1: e2;
54 children: function() {
57 this._container().contents().each(function() {
58 var childElement = element.canvas.getDocumentElement(this);
59 if(childElement === undefined) {
63 toret.push(childElement);
68 getVerticallyFirstTextElement: function() {
70 this.children().some(function(child) {
71 if(child instanceof documentElement.DocumentTextElement) {
75 toret = child.getVerticallyFirstTextElement();
84 onNodeAttrChange: function(event) {
85 if(event.meta.attr === 'class') {
86 this.setWlxmlClass(event.meta.newVal); //
89 onNodeAdded: function(event) {
90 if(event.meta.node.isRoot()) {
91 this.canvas.reloadRoot();
95 var nodeIndex = event.meta.node.getIndex(),
96 referenceElement, referenceAction, actionArg;
99 referenceElement = this;
100 referenceAction = 'prepend';
102 referenceElement = this.children()[nodeIndex-1];
103 referenceAction = 'after';
106 if(event.type === 'nodeMoved') {
108 if(event.meta.node.nodeType === Node.TEXT_NODE) {
109 actionArg = utils.getElementForTextNode(event.meta.node);
111 actionArg = utils.getElementForNode(event.meta.node);
114 actionArg = event.meta.node;
117 referenceElement[referenceAction](actionArg);
119 onNodeMoved: function(event) {
120 return this.onNodeAdded.call(this, event);
122 onNodeDetached: function(event) {
123 if(event.meta.node.sameNode(this)) {
126 this.children().some(function(child) {
127 if(child.wlxmlNode.sameNode(event.meta.node)) {
134 onNodeTextChange: function(event) {
135 var toSet = event.meta.node.getText();
136 this.children().some(function(child) {
137 if(child.wlxmlNode.sameNode(event.meta.node)) {
139 toSet = utils.unicode.ZWS;
141 if(toSet !== child.getText()) {
142 child.setText(toSet);
152 containsBlock: function() {
153 return this.children()
154 .filter(function(child) {
155 return child instanceof documentElement.DocumentNodeElement;
157 .some(function(child) {
158 if(child.isBlock()) {
161 return child.containsBlock();
166 prepend: function(param) {
168 if(param instanceof documentElement.DocumentElement) {
171 element = this.canvas.createElement(param);
173 this._container().prepend(element.dom);
178 childIndex: function(child) {
179 var children = this.children(),
181 children.forEach(function(c, idx) {
182 if(c.sameNode(child)) {
190 getWlxmlClass: function() {
191 var klass = this._container().attr('wlxml-class');
193 return klass.replace(/-/g, '.');
197 setWlxmlClass: function(klass) {
198 if(klass === this.getWlxmlClass()) {
202 this._container().attr('wlxml-class', klass.replace(/\./g, '-'));
205 this._container().removeAttr('wlxml-class');