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);
22 onNodeAttrChange: function(event) {
23 if(event.meta.attr === 'class') {
24 this.setWlxmlClass(event.meta.newVal); //
27 onNodeAdded: function(event) {
28 if(event.meta.node.isRoot()) {
29 this.canvas.reloadRoot();
33 var nodeIndex = event.meta.node.getIndex(),
34 referenceElement, referenceAction, actionArg;
37 referenceElement = this;
38 referenceAction = 'prepend';
40 referenceElement = this.children()[nodeIndex-1];
41 referenceAction = 'after';
44 actionArg = (event.type === 'nodeMoved' && utils.findCanvasElement(event.meta.node, event.meta.parent)) || event.meta.node;
45 referenceElement[referenceAction](actionArg);
47 onNodeMoved: function(event) {
48 return this.onNodeAdded.call(this, event, true);
50 onNodeDetached: function(event) {
51 if(event.meta.node.sameNode(this)) {
54 this.children().some(function(child) {
55 if(child.wlxmlNode.sameNode(event.meta.node)) {
62 onNodeTextChange: function(event) {
63 var toSet = event.meta.node.getText();
64 this.children().some(function(child) {
65 if(child.wlxmlNode.sameNode(event.meta.node)) {
67 toSet = utils.unicode.ZWS;
69 if(toSet !== child.getText()) {
77 prepend: function(param) {
79 if(param instanceof documentElement.DocumentElement) {
82 element = this.canvas.createElement(param);
84 this._container().prepend(element.dom);
89 children: function() {
92 this._container().contents().each(function() {
93 var childElement = element.canvas.getDocumentElement(this);
94 if(childElement === undefined) {
98 toret.push(childElement);
103 getFirst: function(e1, e2) {
104 var idx1 = this.childIndex(e1),
105 idx2 = this.childIndex(e2);
106 if(e1 === null || e2 === null) {
109 return idx1 <= idx2 ? e1: e2;
112 childIndex: function(child) {
113 var children = this.children(),
115 children.forEach(function(c, idx) {
116 if(c.sameNode(child)) {
124 getWlxmlClass: function() {
125 var klass = this._container().attr('wlxml-class');
127 return klass.replace(/-/g, '.');
131 setWlxmlClass: function(klass) {
132 if(klass === this.getWlxmlClass()) {
136 this._container().attr('wlxml-class', klass.replace(/\./g, '-'));
139 this._container().removeAttr('wlxml-class');
144 DocumentNodeElement.prototype.init.call(this);
146 .attr('wlxml-tag', this.wlxmlNode.getTagName());
147 this.setWlxmlClass(this.wlxmlNode.getClass());
148 this.wlxmlNode.contents().forEach(function(node) {
149 this._container().append(this.canvas.createElement(node).dom);
154 refresh: function() {
155 if(this.wlxmlNode.getTagName() === 'span') {
156 if(this.containsBlock()) {
157 this.displayAsBlock();
159 this.displayInline();
162 this.displayAsBlock();
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();
178 getVerticallyFirstTextElement: function() {
180 this.children().some(function(child) {
181 if(child instanceof documentElement.DocumentTextElement) {
183 return true; // break
185 toret = child.getVerticallyFirstTextElement();
187 return true; // break