Copyright adjusted to 2009.
[FireMan.git] / src / chrome / content / FireManSidebar.js
blob3945f16d073abd18e489f17169db35618048b810
1 /*
2  * Copyright (c) 2008,2009 Tobias Sarnowski <sarnowski@new-thoughts.org>
3  *
4  * Permission to use, copy, modify, and distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
17 function FM_fillTree(searchTerm)
19         try {
20                 var tree = document.getElementById("ManualPageTree");
21                 var treec = document.getElementById("ManualPageTreeContent");
23                 var data = FM_getList(searchTerm);
25                 if (data.length == 0) {
26                         FM_showError("nothing found!");
27                 } else {
28                         FM_clearManualTree();
30                         for (var n = 0; n < data.length; n++) {
31                                 FM_addManual(data[n]["title"], data[n]["category"], data[n]["description"], data[n]["machines"], data[n]["aliases"]);
32                         }
33                 }
34                 treec = document.getElementById("ManualPageTreeContent");
36                 // if there are only a few results, open alle categories
37                 if (data.length < 25) {
38                         for (var n = 0; n < treec.childNodes.length; n++) {
39                                 treec.childNodes[n].setAttribute("open", "true");
40                         }
41                 } else
43                 // if it is only 1 category, open it
44                 if (treec.childNodes.length == 1) {
45                         treec.childNodes[0].setAttribute("open", "true");
46                 }
48         } catch (ex) {
49                 dump("FireMan Exception:\n" + ex);
50         }
53 function FM_clearManualTree()
55         var tree = document.getElementById("ManualPageTreeContent");
56         while (tree.childNodes.length > 0) {
57                 tree.removeChild(tree.childNodes[0]);
58         }
61 function FM_showError(msg)
63         FM_clearManualTree();
65         var tc = document.createElement("treecell");
66         tc.setAttribute("id", "FM-treecell-error");
67         tc.setAttribute("label", msg);
68         tc.setAttribute("src", "chrome://fireman/skin/images/error.png");
70         var tr = document.createElement("treerow");
71         tr.setAttribute("id", "FM-treerow-error");
72         tr.appendChild(tc);
74         var ti = document.createElement("treeitem");
75         ti.setAttribute("id", "FM-treeitem-error");
76         ti.appendChild(tr);
78         var tree = window.document.getElementById("ManualPageTreeContent");
79         tree.appendChild(ti);
82 function FM_addCategory(category)
84         var tc;
85         var tr;
86         var ti;
87         var tch;
89         // prepare the category
90         categorydesc = FM_getOSCategoryDescription(category);
91         if (!categorydesc) {
92                 categorydesc = category;
93         } else {
94                 categorydesc = categorydesc; // + " [" + category + "]";
95         }
97         tc = document.createElement("treecell");
98         tc.setAttribute("id", "FM-category-" + category + "-treecell");
99         tc.setAttribute("label", categorydesc);
100         tc.setAttribute("src", "chrome://fireman/skin/images/book.png");
102         tr = document.createElement("treerow");
103         tr.setAttribute("id", "FM-category-" + category + "-treerow");
104         tr.appendChild(tc);
106         tch = document.createElement("treechildren");
107         tch.setAttribute("id", "FM-category-" + category + "-children");
109         ti = document.createElement("treeitem");
110         ti.setAttribute("id", "FM-category-" + category);
111         ti.setAttribute("container", "true");
112         ti.setAttribute("fm_type", "category");
113         ti.setAttribute("fm_category", category);
114         ti.appendChild(tr);
115         ti.appendChild(tch);
117         // search the right place to insert
118         var tree = document.getElementById("ManualPageTreeContent");
120         var categories = FM_getOSCategories();
121         var catsToReappend = new Array();
123         for (var ci = 0; ci < tree.childNodes.length; ci++) {
124                 var found = false;
126                 if (!found) {
127                         for (ca in categories) {
128                                 if (ca == category) {
129                                         found = true;
130                                         break;
131                                 }
132                                 if ("FM-category-" + ca == tree.childNodes[ci].getAttribute("id")) {
133                                         break;
134                                 }
135                         }
136                 }
137                 if (found) {
138                         catsToReappend[catsToReappend.length] = tree.childNodes[ci];
139                 }
140         }
142         for (ci = 0; ci < catsToReappend.length; ci++) {
143                 tree.removeChild(catsToReappend[ci]);
144         }
145         tree.appendChild(ti);
146         for (ci = 0; ci < catsToReappend.length; ci++) {
147                 tree.appendChild(catsToReappend[ci]);
148         }
150         return tch;
153 function FM_addManual(title, category, description, machines, aliases)
155         var tc;
156         var tr;
157         var ti;
159         // aliases resolution
160         var aliascategory = FM_getOSCategoryAlias(category);
162         var tch = document.getElementById("FM-category-" + aliascategory + "-children");
163         if (!tch) {
164                 tch = FM_addCategory(aliascategory);
165         }
167         // add manual to the category
168         tc = document.createElement("treecell");
169         tc.setAttribute("id", "FM-page-" + category + "-" + title + "-treecell");
170         tc.setAttribute("label", title + " - " + description);
171         tc.setAttribute("src", "chrome://fireman/skin/images/page_white_text.png");
173         tr = document.createElement("treerow");
174         tr.setAttribute("id", "FM-page-" + category + "-" + title + "-treerow");
175         tr.appendChild(tc);
177         ti = document.createElement("treeitem");
178         ti.setAttribute("id", "FM-page-" + category + "-" + title);
179         ti.setAttribute("fm_type", "page");
180         ti.setAttribute("fm_title", title);
181         ti.setAttribute("fm_category", category);
182         ti.setAttribute("fm_machine", '');
183         ti.appendChild(tr);
185         tch.appendChild(ti);
188         if (machines.length + aliases.length > 0) {
189                 tch = document.createElement("treechildren");
190                 tch.setAttribute("id", "FM-page-" + category + "-" + title + "-children");
192                 ti.setAttribute("container", "true");
193                 ti.appendChild(tch);
195                 // add machine specific pages if available
196                 for (var n = 0; n < machines.length; n++) {
197                         tc = document.createElement("treecell");
198                         tc.setAttribute("id", "FM-page-" + category + "-" + machines[n] + "-" + title + "-treecell");
199                         tc.setAttribute("label", machines[n]);
200                         tc.setAttribute("src", "chrome://fireman/skin/images/page_white_gear.png");
202                         tr = document.createElement("treerow");
203                         tr.setAttribute("id", "FM-page-" + category + "-" + machines[n] + "-" + title + "-treerow");
204                         tr.appendChild(tc);
206                         ti = document.createElement("treeitem");
207                         ti.setAttribute("id", "FM-page-" + category + "-" + machines[n] + "-" + title);
208                         ti.setAttribute("fm_type", "page");
209                         ti.setAttribute("fm_title", title);
210                         ti.setAttribute("fm_category", category);
211                         ti.setAttribute("fm_machine", machines[n]);
212                         ti.appendChild(tr);
214                         tch.appendChild(ti);
215                 }
217                 // add aliases if available
218                 for (var n = 0; n < aliases.length; n++) {
219                         tc = document.createElement("treecell");
220                         tc.setAttribute("id", "FM-page-" + category + "-" + aliases[n] + "-" + title + "-treecell");
221                         tc.setAttribute("label", aliases[n]);
222                         tc.setAttribute("src", "chrome://fireman/skin/images/page_white_get.png");
224                         tr = document.createElement("treerow");
225                         tr.setAttribute("id", "FM-page-" + category + "-" + aliases[n] + "-" + title + "-treerow");
226                         tr.appendChild(tc);
228                         ti = document.createElement("treeitem");
229                         ti.setAttribute("id", "FM-page-" + category + "-" + aliases[n] + "-" + title);
230                         ti.setAttribute("fm_type", "page");
231                         ti.setAttribute("fm_title", title);
232                         ti.setAttribute("fm_category", category);
233                         ti.setAttribute("fm_machine", '');
234                         ti.appendChild(tr);
236                         tch.appendChild(ti);
237                 }
238         }
242 function FM_showMan(tree)
244         var item = tree.view.getItemAtIndex(tree.currentIndex);
246         if (item.getAttribute("fm_type") != "page") {
247                 // categories aren't handled atm
248                 return;
249         }
251         var title = item.getAttribute("fm_title");
252         var category = item.getAttribute("fm_category");
253         var machine = item.getAttribute("fm_machine");
255         if (machine != '') {
256                 category = category + "/" + machine.toLowerCase();
257         }
258         var manurl = "man://" + category + "/" + title;
260         var browser = top.document.getElementById("content");
261         browser.webNavigation.loadURI(manurl, Components.interfaces.nsIWebNavigation,null,null,null);