Converting end of lines from crlf to lf
[fnpeditor.git] / modules / documentCanvas / canvasNode.js
1 define(['libs/jquery-1.9.1.min'], function($) {
2
3 'use strict';
4
5
6 var tagSelector = '[wlxml-tag]';
7
8 var CanvasNode = function(desc) {
9     if(desc instanceof $) {
10         this.dom = desc;
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);}));
13         }
14     } else {
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);
19         if(desc.klass)
20             this.dom.attr('wlxml-class', desc.klass);
21         if(desc.content)
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);}));
24     }
25 };
26
27 CanvasNode.prototype.getTag = function() {
28     return this.dom.attr('wlxml-tag');
29 };
30
31 CanvasNode.prototype.getClass = function() {
32     return this.dom.attr('wlxml-class');
33 };
34
35 CanvasNode.prototype.getId = function() {
36     return this.dom.attr('id');
37 };
38
39 CanvasNode.prototype.getContent = function() {
40     return this.dom.text();
41 };
42
43 CanvasNode.prototype.setContent = function(content) {
44     this.dom.text(content);
45 };
46
47 CanvasNode.prototype.isSame = function(other) {
48     return (other instanceof CanvasNode) && this.dom.get(0) === other.dom.get(0);
49 };
50
51 CanvasNode.prototype.children = function() {
52     var list = [];
53     this.dom.children(tagSelector).each(function() {
54         list.push(new CanvasNode($(this)));
55     });
56     return $(list);
57 };
58
59
60 CanvasNode.prototype.parent = function() {
61     var node = this.dom.parent(tagSelector);
62     if(node.length)
63         return new CanvasNode(node);
64     return null;
65 };
66
67 CanvasNode.prototype.parents = function() {
68     var list = [];
69     this.dom.parents(tagSelector).each(function() {
70         list.push(new CanvasNode($(this)));
71     });
72     return $(list);
73 };
74
75
76 CanvasNode.prototype.isOfClass = function(klass) {
77     return this.getClass() && this.getClass().substr(0, klass.length) === klass;
78 };
79
80 return {
81     create: function(desc) {
82         return new CanvasNode(desc);
83     }
84
85 };
86     
87
88 });