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.getId = function() {
42 return this.dom.attr('id');
45 CanvasNode.prototype.getContent = function() {
46 return this.dom.text();
49 CanvasNode.prototype.setContent = function(content) {
50 this.dom.text(content);
53 CanvasNode.prototype.isSame = function(other) {
54 return (other instanceof CanvasNode) && this.dom.get(0) === other.dom.get(0);
57 CanvasNode.prototype.children = function() {
59 this.dom.children(tagSelector).each(function() {
60 list.push(new CanvasNode($(this)));
66 CanvasNode.prototype.parent = function() {
67 var node = this.dom.parent(tagSelector);
69 return new CanvasNode(node);
73 CanvasNode.prototype.parents = function() {
75 this.dom.parents(tagSelector).each(function() {
76 list.push(new CanvasNode($(this)));
82 CanvasNode.prototype.isOfClass = function(klass) {
83 return this.getClass() && this.getClass().substr(0, klass.length) === klass;
86 CanvasNode.prototype.getMetaAttr = function(attr) {
87 return this.dom.attr('wlxml-meta-'+attr);
90 CanvasNode.prototype.getMetaAttrs = function() {
92 var metaAttrPrefix = 'wlxml-meta-';
94 var attrs = this.dom.get(0).attributes;
95 for(var i = 0; i < attrs.length; i++) {
97 if(attr.name.substr(0, metaAttrPrefix.length) === metaAttrPrefix) {
98 toret.push({name: attr.name.substr(metaAttrPrefix.length), value: attr.value});
104 CanvasNode.prototype.setMetaAttr = function(attr, value) {
105 this.dom.attr('wlxml-meta-'+attr, value);
109 create: function(desc) {
110 return new CanvasNode(desc);