47f63d4f0fb8534bb8e84406d424d20f9e26ebe2
[dirtag.git] / static / dirtag.js
1 /* Useful helper functions */
2
3 function tree_path(tree, row) {
4     var name = tree.columns.getPrimaryColumn();
5     var path = new Array();
6     while (row >= 0) {
7         path.splice(0, 0, tree.view.getCellText(row, name));
8         row = tree.view.getParentIndex(row);
9     }
10     return path;
11 }
12
13 function tree_selection(id) {
14     var tree = document.getElementById(id);
15     return tree_path(tree, tree.currentIndex);
16 }
17
18 /* Functions driving the CherryPy backend */
19
20 function reload_rdf(e) {
21     /* https://developer.mozilla.org/en/RDF_in_Mozilla_FAQ */
22     if (e.readyState == 4) { /* 4: request finished and response is ready */
23         if(e.status != 200) {
24             alert("Error loading page\n\n"+e.responseText);
25             return;
26         }
27         // Get the RDF service
28         var RDF =
29             Components
30             .classes['@mozilla.org/rdf/rdf-service;1']
31             .getService(Components.interfaces.nsIRDFService);
32         // Get the datasources and refresh them
33         RDF.GetDataSource('raw.rdf').Refresh('non-blocking');
34         RDF.GetDataSource('tag.rdf').Refresh('non-blocking');
35     }
36 }
37
38 function push_data(url) {
39     /* https://developer.mozilla.org/en/using_xmlhttprequest */
40     xmlDoc = new XMLHttpRequest();
41     xmlDoc.open('GET', url, true);
42     xmlDoc.onreadystatechange = function() { reload_rdf(xmlDoc); };
43     xmlDoc.send(null);
44 }
45
46 function new_tag(tag) {
47     push_data('/new_tag?tag='+escape(tag.join('/')));
48 }
49
50 function add_tag(path, tag) {
51     push_data('/add_tag?path='+escape(path.join('/'))
52               +'&tag='+escape(tag.join('/')));
53 }
54
55 function remove_tag(path, tag) {
56     push_data('/remove_tag?path='+escape(path.join('/'))
57               +'&tag='+escape(tag.join('/')));
58 }
59
60 /* The bindings themselves */
61
62 function initialize() {
63     element = document.getElementById('element').file_path = 'UNDEFINED';
64     var tree = document.getElementById('checkbox-tree');
65     tree.view.origSetCellValue = tree.view.setCellValue;
66     tree.view.setCellValue = function (row, col, value) {
67         tree.view.origSetCellValue(row, col, value);
68         checkbox_activity(tree, row, col, value);
69     };
70 }
71
72 function new_tag_button() {
73     var text = document.getElementById('new-tag-text').value;
74     new_tag(text.split('/'));
75 }
76
77 function set_selected_element(path) {
78     element = document.getElementById('element');
79     element.file_path = path;
80 }
81
82 function raw_tree_select() {
83     set_selected_element(tree_selection('raw-tree'));
84 }
85
86 function tag_tree_select() {
87     set_selected_element(tree_selection('tag-tree'));
88 }
89
90 function checkbox_activity(tree, row, col, value) {
91     var path = document.getElementById('element').file_path;
92     var name = tree.columns.getPrimaryColumn();
93     id = tree_path(tree, row);
94     if (path == 'UNDEFINED') {
95         return;
96     } else if (value == true || value == 'true') {
97         add_tag(path, id);
98     } else {
99         remove_tag(path, id);
100     }
101 }