Added working RDF refresh.
[dirtag.git] / static / dirtag.js
1 /* Useful helper functions */
2
3 function tree_tags(tree, row) {
4     var ctags = tree.columns.getLastColumn();
5     var tags = tree.view.getCellText(row, ctags).split(',');
6     for (var i=0; i<tags.length; i++) {
7         tags[i] = tags[i].split('/');
8     }
9     return tags;
10 }
11
12 function tree_path(tree, row) {
13     var name = tree.columns.getPrimaryColumn();
14     var path = new Array();
15     while (row >= 0) {
16         path.splice(0, 0, tree.view.getCellText(row, name));
17         row = tree.view.getParentIndex(row);
18     }
19     return path;
20 }
21
22 function tree_selection(id) {
23     var tree = document.getElementById(id);
24     return [tree_path(tree, tree.currentIndex),
25             tree_tags(tree, tree.currentIndex),];
26 }
27
28 function tag_in_tags(tag, tags) {
29     /* Convert a regular array into an associative array for 'in' comparisons
30          http://snook.ca/archives/javascript/testing_for_a_v
31     */
32     var o = {};
33     for(var i=0; i<tags.length; i++) {
34         o[tags[i].join('/')] = '';  // .join('/') b/c indexes must be strings
35     }
36     return tag.join('/') in o;
37 }
38
39 function setup_checkboxes(tags) {
40     var tree = document.getElementById('checkbox-tree');
41     var ctags = tree.columns.getLastColumn();
42     for (var row=0; row<tree.view.rowCount; row++) {
43         p = tree_path(tree, row);
44         tree.view.origSetCellValue(row, ctags, tag_in_tags(p, tags));
45     }
46     tree.treeBoxObject.invalidateColumn(ctags);
47 }
48
49 /* Functions driving the CherryPy backend */
50
51 function reload_rdf(e) {
52     /* https://developer.mozilla.org/en/RDF_in_Mozilla_FAQ
53        https://developer.mozilla.org/en/XUL/Template_Guide/Template_Builder_Interface
54      */
55     if (e.readyState == 4) { /* 4: request finished and response is ready */
56         if(e.status != 200) {
57             alert("Error loading page\n\n"+e.responseText);
58             return;
59         }
60         document.getElementById('raw-tree').builder.refresh();
61         document.getElementById('tag-tree').builder.refresh();
62         document.getElementById('checkbox-tree').builder.refresh();
63
64         var id = document.getElementById('element').tree_id;
65         var path_tags = tree_selection(id);
66         setup_checkboxes(path_tags[1]);
67     }
68 }
69
70 function push_data(url) {
71     /* https://developer.mozilla.org/en/using_xmlhttprequest */
72     xmlDoc = new XMLHttpRequest();
73     xmlDoc.open('GET', url, true);
74     xmlDoc.onreadystatechange = function() { reload_rdf(xmlDoc); };
75     xmlDoc.send(null);
76 }
77
78 function new_tag(tag) {
79     push_data('/new_tag?tag='+escape(tag.join('/')));
80 }
81
82 function add_tag(path, tag) {
83     push_data('/add_tag?path='+escape(path.join('/'))
84               +'&tag='+escape(tag.join('/')));
85 }
86
87 function remove_tag(path, tag) {
88     push_data('/remove_tag?path='+escape(path.join('/'))
89               +'&tag='+escape(tag.join('/')));
90 }
91
92 /* The bindings themselves */
93
94 function initialize() {
95     document.getElementById('element').file_path = 'UNDEFINED';
96     var tree = document.getElementById('checkbox-tree');
97     tree.view.origSetCellValue = tree.view.setCellValue;
98     tree.view.setCellValue = function (row, col, value) {
99         tree.view.origSetCellValue(row, col, value);
100         checkbox_activity(tree, row, col, value);
101     };
102 }
103
104 function new_tag_button() {
105     var text = document.getElementById('new-tag-text').value;
106     new_tag(text.split('/'));
107 }
108
109 function set_selected_element(path, tags, id) {
110     var caption = document.getElementById('selected-caption');
111     var title = caption.label.split(':', 1)[0];
112     caption.label = title+': '+path.join('/');
113
114     setup_checkboxes(tags);
115
116     var element = document.getElementById('element');
117     element.file_path = path;
118     element.file_tags = tags;
119     element.tree_id = id;
120     // element.source = ...
121 }
122
123 function raw_tree_select() {
124     var path_tags = tree_selection('raw-tree');
125     set_selected_element(path_tags[0], path_tags[1], 'raw-tree');
126 }
127
128 function tag_tree_select() {
129     var path_tags = tree_selection('tag-tree');
130     set_selected_element(path_tags[0], path_tags[1], 'tag-tree');
131 }
132
133 function checkbox_activity(tree, row, col, value) {
134     var path = document.getElementById('element').file_path;
135     var name = tree.columns.getPrimaryColumn();
136     id = tree_path(tree, row);
137     if (path == 'UNDEFINED') {
138         return;
139     } else if (value == true || value == 'true') {
140         add_tag(path, id);
141     } else {
142         remove_tag(path, id);
143     }
144 }