1 define(['libs/jquery-1.9.1.min', 'libs/underscore-min'], function($, _) {
6 var tagSelector = '[wlxml-tag]';
8 var CanvasNode = function(desc) {
9 if(desc instanceof $) {
11 if(!this.dom.attr('id')) {
12 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);}));
15 var toBlock = ['div', 'document', 'section', 'header'];
16 var htmlTag = _.contains(toBlock, desc.tag) ? 'div' : 'span';
17 this.dom = $('<' + htmlTag + '>');
18 this.dom.attr('wlxml-tag', desc.tag);
20 this.dom.attr('wlxml-class', desc.klass);
22 this.dom.text(desc.content);
25 _.keys(desc.meta).forEach(function(key) {
26 c.dom.attr('wlxml-meta-'+key, desc.meta[key]);
29 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);}));
33 CanvasNode.prototype.getTag = function() {
34 return this.dom.attr('wlxml-tag');
37 CanvasNode.prototype.getClass = function() {
38 return this.dom.attr('wlxml-class');
41 CanvasNode.prototype.setClass = function(klass) {
42 if(klass != this.getClass()) {
43 this.dom.attr('wlxml-class', klass);
45 this.getMetaAttrs().forEach(function(attr) {
46 c.dom.removeAttr('wlxml-meta-' + attr.name);
51 CanvasNode.prototype.getId = function() {
52 return this.dom.attr('id');
55 CanvasNode.prototype.getContent = function() {
56 return this.dom.text();
59 CanvasNode.prototype.setContent = function(content) {
60 this.dom.text(content);
63 CanvasNode.prototype.isSame = function(other) {
64 return (other instanceof CanvasNode) && this.dom.get(0) === other.dom.get(0);
67 CanvasNode.prototype.children = function() {
69 this.dom.children(tagSelector).each(function() {
70 list.push(new CanvasNode($(this)));
76 CanvasNode.prototype.parent = function() {
77 var node = this.dom.parent(tagSelector);
79 return new CanvasNode(node);
83 CanvasNode.prototype.parents = function() {
85 this.dom.parents(tagSelector).each(function() {
86 list.push(new CanvasNode($(this)));
92 CanvasNode.prototype.isOfClass = function(klass) {
93 return this.getClass() && this.getClass().substr(0, klass.length) === klass;
96 CanvasNode.prototype.getMetaAttr = function(attr) {
97 return this.dom.attr('wlxml-meta-'+attr);
100 CanvasNode.prototype.getMetaAttrs = function() {
102 var metaAttrPrefix = 'wlxml-meta-';
104 var attrs = this.dom.get(0).attributes;
105 for(var i = 0; i < attrs.length; i++) {
107 if(attr.name.substr(0, metaAttrPrefix.length) === metaAttrPrefix) {
108 toret.push({name: attr.name.substr(metaAttrPrefix.length), value: attr.value});
114 CanvasNode.prototype.setMetaAttr = function(attr, value) {
115 this.dom.attr('wlxml-meta-'+attr, value);
119 create: function(desc) {
120 return new CanvasNode(desc);