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
19 onNodeAttrChange: function(event) {
20 if(event.meta.attr === 'class') {
21 this.setWlxmlClass(event.meta.newVal); //
24 onNodeAdded: function(event) {
25 if(event.meta.node.isRoot()) {
26 this.canvas.reloadRoot();
30 var nodeIndex = event.meta.node.getIndex(),
31 referenceElement, referenceAction, actionArg;
34 referenceElement = this;
35 referenceAction = 'prepend';
37 referenceElement = this.children()[nodeIndex-1];
38 referenceAction = 'after';
41 actionArg = (event.type === 'nodeMoved' && utils.findCanvasElement(event.meta.node, event.meta.parent)) || event.meta.node;
42 referenceElement[referenceAction](actionArg);
44 onNodeMoved: function(event) {
45 return this.onNodeAdded.call(this, event, true);
47 onNodeDetached: function(event) {
48 if(event.meta.node.sameNode(this)) {
51 this.children().some(function(child) {
52 if(child.wlxmlNode.sameNode(event.meta.node)) {
59 onNodeTextChange: function(event) {
60 var toSet = event.meta.node.getText();
61 this.children().some(function(child) {
62 if(child.wlxmlNode.sameNode(event.meta.node)) {
64 toSet = utils.unicode.ZWS;
66 if(toSet !== child.getText()) {
74 prepend: function(param) {
76 if(param instanceof documentElement.DocumentElement) {
79 element = this.canvas.createElement(param);
81 this._container().prepend(element.dom);
86 children: function() {
89 this._container().contents().each(function() {
90 var childElement = element.canvas.getDocumentElement(this);
91 if(childElement === undefined) {
95 toret.push(childElement);
100 getFirst: function(e1, e2) {
101 var idx1 = this.childIndex(e1),
102 idx2 = this.childIndex(e2);
103 if(e1 === null || e2 === null) {
106 return idx1 <= idx2 ? e1: e2;
109 childIndex: function(child) {
110 var children = this.children(),
112 children.forEach(function(c, idx) {
113 if(c.sameNode(child)) {
121 getWlxmlClass: function() {
122 var klass = this._container().attr('wlxml-class');
124 return klass.replace(/-/g, '.');
128 setWlxmlClass: function(klass) {
129 if(klass === this.getWlxmlClass()) {
133 this._container().attr('wlxml-class', klass.replace(/\./g, '-'));
136 this._container().removeAttr('wlxml-class');
142 .attr('wlxml-tag', this.wlxmlNode.getTagName());
143 this.setWlxmlClass(this.wlxmlNode.getClass());
144 this.wlxmlNode.contents().forEach(function(node) {
145 this._container().append(this.canvas.createElement(node).dom);
150 refresh: function() {
151 if(this.wlxmlNode.getTagName() === 'span') {
152 if(this.containsBlock()) {
153 this.displayAsBlock();
155 this.displayInline();
158 this.displayAsBlock();
161 containsBlock: function() {
162 return this.children()
163 .filter(function(child) {
164 return child instanceof documentElement.DocumentNodeElement;
166 .some(function(child) {
167 if(child.isBlock()) {
170 return child.containsBlock();
174 getVerticallyFirstTextElement: function() {
176 this.children().some(function(child) {
177 if(child instanceof documentElement.DocumentTextElement) {
179 return true; // break
181 toret = child.getVerticallyFirstTextElement();
183 return true; // break