2 'libs/jquery-1.9.1.min'
8 // DocumentElement represents a node from WLXML document rendered inside Canvas
9 var DocumentElement = function(htmlElement, canvas) {
10 if(arguments.length === 0)
13 this.$element = $(htmlElement);
15 var tagNameProp = this.$element.prop('tagName');
16 this.wlxmlTag = tagNameProp ? tagNameProp.toLowerCase() : undefined;
19 $.extend(DocumentElement.prototype, {
23 children: function() {
25 if(this instanceof DocumentTextElement)
29 var elementContent = this.$element.contents();
31 elementContent.each(function(idx) {
32 var childElement = documentElementFromHTMLElement(this, element.canvas);
33 if(idx === 0 && elementContent.length > 1 && elementContent[1].nodeType === Node.ELEMENT_NODE && (childElement instanceof DocumentTextElement) && $.trim($(this).text()) === '')
35 if(idx > 0 && childElement instanceof DocumentTextElement) {
36 if(toret[toret.length-1] instanceof DocumentNodeElement && $.trim($(this).text()) === '')
39 toret.push(childElement);
44 return documentElementFromHTMLElement(this.$element.parent()[0], this.canvas);
47 sameNode: function(other) {
48 return other && (typeof other === typeof this) && other.$element[0] === this.$element[0];
51 wrapWithNodeElement: function(wlxmlNode) {
52 this.$element.wrap($('<' + wlxmlNode.tag + ' class="' + wlxmlNode.klass + '"">')[0]);
53 return documentElementFromHTMLElement(this.$element.parent().get(0), this.canvas);
56 childIndex: function(child) {
57 var children = this.children(),
59 children.forEach(function(c, idx) {
60 if(c.sameNode(child)) {
69 this.$element.detach();
75 var DocumentNodeElement = function(htmlElement, canvas) {
76 DocumentElement.call(this, htmlElement, canvas);
79 var DocumentTextElement = function(htmlElement, canvas) {
80 DocumentElement.call(this, htmlElement, canvas);
83 DocumentNodeElement.prototype = new DocumentElement();
84 DocumentTextElement.prototype = new DocumentElement();
86 var manipulate = function(e, params, action) {
88 if(params instanceof DocumentElement) {
91 dom = DocumentNodeElement.createDOM(params);
93 e.$element[action](dom);
94 return documentElementFromHTMLElement(dom);
97 $.extend(DocumentNodeElement.prototype, {
98 append: function(params) {
99 manipulate(this, params, 'append');
101 before: function(params) {
102 manipulate(this, params, 'before');
105 after: function(params) {
106 manipulate(this, params, 'after');
110 DocumentNodeElement.createDOM = function(params) {
113 dom = $(document.createTextNode(params.text));
115 dom = $('<' + params.tag + '>');
117 dom.attr('class', params.klass);
123 DocumentNodeElement.create = function(params, canvas) {
124 return documentElementFromHTMLElement(DocumentNodeElement.createDOM(params)[0]);
128 $.extend(DocumentTextElement.prototype, {
129 setText: function(text) {
130 this.$element[0].data = text;
132 getText: function() {
133 return this.$element.text();
135 after: function(params) {
136 if(params instanceof DocumentTextElement || params.text)
139 if(params instanceof DocumentNodeElement) {
142 dom = DocumentNodeElement.createDOM(params);
144 this.$element.wrap('<div>');
145 this.$element.parent().after(dom[0]);
146 this.$element.unwrap();
147 return documentElementFromHTMLElement(dom[0]);
149 wrapWithNodeElement: function(wlxmlNode) {
150 if(wlxmlNode.start && wlxmlNode.end) {
151 return this.canvas.wrapText({
152 inside: this.parent(),
153 textNodeIdx: this.parent().childIndex(this),
154 offsetStart: wlxmlNode.start,
155 offsetEnd: wlxmlNode.end,
156 _with: {tag: wlxmlNode.tag, klass: wlxmlNode.klass}
159 return DocumentElement.prototype.wrapWithNodeElement.call(this, wlxmlNode);
162 split: function(params) {
163 var parentElement = this.parent(),
164 myIdx = parentElement.childIndex(this),
165 myCanvas = this.canvas,
167 succeedingChildren = [],
169 prefix = this.getText().substr(0, params.offset),
170 suffix = this.getText().substr(params.offset);
172 parentElement.children().forEach(function(child) {
174 succeedingChildren.push(child);
175 if(child.sameNode(thisElement))
179 if(prefix.length > 0)
180 this.setText(prefix);
184 var newElement = DocumentNodeElement.create({tag: parentElement.wlxmlTag, klass: parentElement.wlxmlClass}, myCanvas);
185 parentElement.after(newElement);
187 if(suffix.length > 0)
188 newElement.append({text: suffix});
189 succeedingChildren.forEach(function(child) {
190 newElement.append(child);
195 var documentElementFromHTMLElement = function(htmlElement, canvas) {
197 // throw 'no canvas specified';
198 if(htmlElement.nodeType === Node.ELEMENT_NODE)
199 return new DocumentNodeElement(htmlElement, canvas);
200 if(htmlElement.nodeType === Node.TEXT_NODE)
201 return new DocumentTextElement(htmlElement, canvas);
205 wrap: function(htmlElement, canvas) {
206 return documentElementFromHTMLElement(htmlElement, canvas);
208 DocumentElement: DocumentElement,
209 DocumentNodeElement: DocumentNodeElement,
210 DocumentTextElement: DocumentTextElement