Setting/getting text from DocumentTextElement
[fnpeditor.git] / modules / documentCanvas / canvas / documentElement.js
1 define([
2 'libs/jquery-1.9.1.min'
3 ], function($) {
4     
5 'use strict';
6
7 // DocumentElement represents a node from WLXML document rendered inside Canvas
8 var DocumentElement = function(htmlElement) {
9     if(arguments.length === 0)
10         return;
11     this.$element = $(htmlElement);
12     this.wlxmlTag = this.$element.prop('tagName');
13 };
14
15 $.extend(DocumentElement.prototype, {
16     children: function() {
17         var toret = [];
18         if(this instanceof DocumentTextElement)
19             return toret;
20
21
22         var elementContent = this.$element.contents();
23         elementContent.each(function(idx) {
24             var element = documentElementFromHTMLElement(this);
25             if(idx === 0 && elementContent.length > 1 && elementContent[1].nodeType === Node.ELEMENT_NODE && (element instanceof DocumentTextElement) && $.trim($(this).text()) === '')
26                 return true;
27             if(idx > 0 && element instanceof DocumentTextElement) {
28                 if(toret[toret.length-1] instanceof DocumentNodeElement && $.trim($(this).text()) === '')
29                     return true;
30             }
31             toret.push(element);
32         });
33         return toret;
34     },
35     parent: function() {
36         return documentElementFromHTMLElement(this.$element.parent()[0]);
37     },
38
39     sameNode: function(other) {
40         return other && (typeof other === typeof this) && other.$element[0] === this.$element[0];
41     },
42
43     wrapWithNodeElement: function(wlxmlNode) {
44         this.$element.wrap($('<' + wlxmlNode.tag + ' class="' + wlxmlNode.klass + '"">')[0]);
45         return documentElementFromHTMLElement(this.$element.parent().get(0));
46     },
47
48     childIndex: function(child) {
49         var children = this.children(),
50             toret = null;
51         children.forEach(function(c, idx) {
52             if(c.sameNode(child)) {
53                 toret = idx;
54                 return false;
55             }
56         });
57         return toret;
58     }
59 });
60
61 var DocumentNodeElement = function(htmlElement) {
62     DocumentElement.call(this, htmlElement);
63 };
64
65 var DocumentTextElement = function(htmlElement) {
66     DocumentElement.call(this, htmlElement);  
67 };
68
69 DocumentNodeElement.prototype = new DocumentElement();
70 DocumentTextElement.prototype = new DocumentElement();
71
72 $.extend(DocumentNodeElement.prototype, {
73     append: function(params) {
74         var to_append = DocumentNodeElement.createDOM(params.tag, params.klass);
75         this.$element.append(to_append);
76         return documentElementFromHTMLElement(to_append);
77     },
78     before: function(params) {
79         var to_append = DocumentNodeElement.createDOM(params.tag, params.klass);
80         this.$element.before(to_append);
81         return documentElementFromHTMLElement(to_append);
82     },
83     after: function(params) {
84         var to_append = DocumentNodeElement.createDOM(params.tag, params.klass);
85         this.$element.after(to_append);
86         return documentElementFromHTMLElement(to_append);   
87     }
88 });
89
90 DocumentNodeElement.createDOM = function(tag, klass) {
91     var dom = $('<' + tag + '>');
92     if(klass)
93         dom.attr('class', klass);
94     return dom;
95 };
96
97 $.extend(DocumentTextElement.prototype, {
98     setText: function(text) {
99         this.$element[0].data = text;
100     },
101     getText: function() {
102         return this.$element.text();
103     }
104 });
105
106 var documentElementFromHTMLElement = function(htmlElement) {
107     if(htmlElement.nodeType === Node.ELEMENT_NODE)
108         return new DocumentNodeElement(htmlElement);
109     if(htmlElement.nodeType === Node.TEXT_NODE)
110         return new DocumentTextElement(htmlElement);
111 };
112
113 return {
114     wrap: function(htmlElement) {
115         return documentElementFromHTMLElement(htmlElement);
116     },
117     DocumentElement: DocumentElement,
118     DocumentNodeElement: DocumentNodeElement,
119     DocumentTextElement: DocumentTextElement
120 };
121
122 });