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 if(!classAttributes.hasMetaAttr(klass, attr.name))
54 c.dom.removeAttr('wlxml-meta-' + attr.name);
56 this.dom.attr('wlxml-class', klass);
60 CanvasNode.prototype.getId = function() {
61 return this.dom.attr('id');
64 CanvasNode.prototype.getContent = function() {
65 return this.dom.text();
68 CanvasNode.prototype.setContent = function(content) {
69 this.dom.text(content);
72 CanvasNode.prototype.isSame = function(other) {
73 return (other instanceof CanvasNode) && this.dom.get(0) === other.dom.get(0);
76 CanvasNode.prototype.children = function() {
78 this.dom.children(tagSelector).each(function() {
79 list.push(new CanvasNode($(this)));
85 CanvasNode.prototype.parent = function() {
86 var node = this.dom.parent(tagSelector);
88 return new CanvasNode(node);
92 CanvasNode.prototype.parents = function() {
94 this.dom.parents(tagSelector).each(function() {
95 list.push(new CanvasNode($(this)));
101 CanvasNode.prototype.isOfClass = function(klass) {
102 return this.getClass() && this.getClass().substr(0, klass.length) === klass;
105 CanvasNode.prototype.getMetaAttr = function(attr) {
106 return this.dom.attr('wlxml-meta-'+attr);
109 CanvasNode.prototype.getMetaAttrs = function() {
111 var metaAttrPrefix = 'wlxml-meta-';
113 var attrList = classAttributes.getMetaAttrsList(this.getClass());
115 attrList.all.forEach(function(attr) {
116 toret.push({name: attr.name, value: c.getMetaAttr(attr.name) || ''});
123 CanvasNode.prototype.setMetaAttr = function(attr, value) {
124 this.dom.attr('wlxml-meta-'+attr, value);
128 create: function(desc) {
129 return new CanvasNode(desc);