2 'libs/jquery-1.9.1.min',
4 'modules/documentCanvas/classAttributes'
5 ], function($, _, classAttributes) {
12 var tagSelector = '[wlxml-tag]';
14 var CanvasNode = function(desc) {
15 if(desc instanceof $) {
17 if(!this.dom.attr('id')) {
18 this.dom.attr('id', 'xxxxxxxx-xxxx-xxxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {var r = Math.random()*16|0,v=c=='x'?r:r&0x3|0x8;return v.toString(16);}));
21 var toBlock = ['div', 'document', 'section', 'header'];
22 var htmlTag = _.contains(toBlock, desc.tag) ? 'div' : 'span';
23 this.dom = $('<' + htmlTag + '>');
24 this.dom.attr('wlxml-tag', desc.tag);
26 this.dom.attr('wlxml-class', desc.klass);
28 this.dom.text(desc.content);
32 .filter(function(key) {return classAttributes.hasMetaAttr(c.getClass(), key);})
33 .forEach(function(key) {
34 c.dom.attr('wlxml-meta-'+key, desc.meta[key]);
37 this.dom.attr('id', 'xxxxxxxx-xxxx-xxxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {var r = Math.random()*16|0,v=c=='x'?r:r&0x3|0x8;return v.toString(16);}));
41 CanvasNode.prototype.getTag = function() {
42 return this.dom.attr('wlxml-tag');
45 CanvasNode.prototype.getClass = function() {
46 return this.dom.attr('wlxml-class');
49 CanvasNode.prototype.setClass = function(klass) {
50 if(klass != this.getClass()) {
52 this.getMetaAttrs().forEach(function(attr) {
53 c.dom.removeAttr('wlxml-meta-' + attr.name);
55 this.dom.attr('wlxml-class', klass);
59 CanvasNode.prototype.getId = function() {
60 return this.dom.attr('id');
63 CanvasNode.prototype.getContent = function() {
64 return this.dom.text();
67 CanvasNode.prototype.setContent = function(content) {
68 this.dom.text(content);
71 CanvasNode.prototype.isSame = function(other) {
72 return (other instanceof CanvasNode) && this.dom.get(0) === other.dom.get(0);
75 CanvasNode.prototype.children = function() {
77 this.dom.children(tagSelector).each(function() {
78 list.push(new CanvasNode($(this)));
84 CanvasNode.prototype.parent = function() {
85 var node = this.dom.parent(tagSelector);
87 return new CanvasNode(node);
91 CanvasNode.prototype.parents = function() {
93 this.dom.parents(tagSelector).each(function() {
94 list.push(new CanvasNode($(this)));
100 CanvasNode.prototype.isOfClass = function(klass) {
101 return this.getClass() && this.getClass().substr(0, klass.length) === klass;
104 CanvasNode.prototype.getMetaAttr = function(attr) {
105 return this.dom.attr('wlxml-meta-'+attr);
108 CanvasNode.prototype.getMetaAttrs = function() {
110 var metaAttrPrefix = 'wlxml-meta-';
112 var attrList = classAttributes.getMetaAttrsList(this.getClass());
114 attrList.all.forEach(function(attr) {
115 toret.push({name: attr.name, value: c.getMetaAttr(attr.name) || ''});
122 CanvasNode.prototype.setMetaAttr = function(attr, value) {
123 this.dom.attr('wlxml-meta-'+attr, value);
127 create: function(desc) {
128 return new CanvasNode(desc);