1 define(['libs/jquery-1.9.1.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);
23 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);}));
27 CanvasNode.prototype.getTag = function() {
28 return this.dom.attr('wlxml-tag');
31 CanvasNode.prototype.getClass = function() {
32 return this.dom.attr('wlxml-class');
35 CanvasNode.prototype.getId = function() {
36 return this.dom.attr('id');
39 CanvasNode.prototype.getContent = function() {
40 return this.dom.text();
43 CanvasNode.prototype.setContent = function(content) {
44 this.dom.text(content);
47 CanvasNode.prototype.isSame = function(other) {
48 return (other instanceof CanvasNode) && this.dom.get(0) === other.dom.get(0);
51 CanvasNode.prototype.children = function() {
53 this.dom.children(tagSelector).each(function() {
54 list.push(new CanvasNode($(this)));
60 CanvasNode.prototype.parent = function() {
61 var node = this.dom.parent(tagSelector);
63 return new CanvasNode(node);
67 CanvasNode.prototype.parents = function() {
69 this.dom.parents(tagSelector).each(function() {
70 list.push(new CanvasNode($(this)));
76 CanvasNode.prototype.isOfClass = function(klass) {
77 return this.getClass() && this.getClass().substr(0, klass.length) === klass;
81 create: function(desc) {
82 return new CanvasNode(desc);