]> git.0d.be Git - empathy.git/blob - src/empathy-chat.js
GNOME Goal: Update icon names
[empathy.git] / src / empathy-chat.js
1 /*
2  * Copyright (C) 2012 Red Hat, Inc.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  *
18  * Author: Debarshi Ray <debarshir@src.gnome.org>
19  */
20
21
22 var chat = document.getElementById("Chat");
23
24
25 function createHTMLNode(html) {
26   var range = document.createRange();
27   range.selectNode(chat);
28   return range.createContextualFragment(html);
29 }
30
31
32 function getContentsNode(node) {
33   var post = node.getElementsByClassName("post")[0];
34   return post.getElementsByClassName("post-contents")[0];
35 }
36
37
38 // Remove all but the last #insert
39 function removeInsertNodes(node) {
40   var inserts = node.querySelectorAll("#insert");
41   if (inserts.length < 2)
42     return;
43
44   for (var i = 0; i < inserts.length - 1; i++) {
45     var insert = inserts[i];
46     insert.parentNode.removeChild(insert);
47   }
48 }
49
50
51 // Remove all but the first #prepend
52 function removePrependNodes(node) {
53   var pres = node.querySelectorAll("#prepend");
54   if (pres.length < 2)
55     return;
56
57   for (var i = 1; i < pres.length; i++) {
58     var pre = pres[i];
59     pre.parentNode.removeChild(pre);
60   }
61 }
62
63
64 function prepend(html) {
65   var node = createHTMLNode(html);
66   chat.insertBefore(node, chat.firstChild);
67
68   // The lastChild should retain the #insert
69   if (chat.children.length == 1)
70     return;
71
72   removeInsertNodes(chat);
73   removePrependNodes(chat);
74 }
75
76
77 function prependPrev(html) {
78   var pre = chat.firstChild.querySelector("#prepend");
79
80   // For themes that don't support #prepend
81   if (!pre) {
82     prepend(html);
83     return;
84   }
85
86   var contents = pre.parentNode;
87   var node = createHTMLNode(html);
88
89   // Skip both the #prepend and #insert
90   for (var i = node.childNodes.length - 2; i > 0; i--)
91     contents.insertBefore(node.childNodes[i], pre.nextSibling);
92 }