Use checkbox tree rather than a simple array for element tags in dirtag.xul
[dirtag.git] / static / dirtag.js
1 /* Useful helper functions */
2
3 function tree_selection(id) {
4     var tree = document.getElementById(id);
5     var name = tree.columns.getPrimaryColumn();
6     var i = tree.currentIndex;
7     var path = new Array();
8     while (i >= 0) {
9         path.splice(0, 0, tree.view.getCellText(i, name));
10         i = tree.view.getParentIndex(i);
11     }
12     return path;
13 }
14
15 function tree_selection_and_check(id) {
16     var tree = document.getElementById(id);
17     var name = tree.columns.getPrimaryColumn();
18     var check = tree.columns.getFirstColumn();
19     var i = tree.currentIndex;
20     var checked = tree.view.getCellValue(i, check);
21     var path = new Array();
22     while (i >= 0) {
23         path.splice(0, 0, tree.view.getCellText(i, name));
24         i = tree.view.getParentIndex(i);
25     }
26     return [path, checked];
27 }
28
29 /* Functions driving the CherryPy backend */
30
31 function new_tag(path) {
32     alert('Adding tag: '+path.toString());
33     /* https://developer.mozilla.org/en/RDF_in_Mozilla_FAQ */
34 }
35
36 function add_tag(path, tag) {
37     alert('Add tag '+tag+' to '+path.toString());
38 }
39
40 function remove_tag(path, tag) {
41     alert('Remove tag '+tag+' from '+path.toString());
42 }
43
44 /* The bindings themselves */
45
46 function initialize() {
47     element = document.getElementById('element').file_path = 'UNDEFINED';
48 }
49
50 function new_tag_button() {
51     var text = document.getElementById('new-tag-text').value;
52     new_tag(text.split('/'));
53 }
54
55 function set_selected_element(path) {
56     element = document.getElementById('element');
57     element.file_path = path;
58     alert('Set selected element: '+path.toString());
59 }
60
61 function raw_tree_select() {
62     set_selected_element(tree_selection('raw-tree'));
63 }
64
65 function tag_tree_select() {
66     set_selected_element(tree_selection('tag-tree'));
67 }
68
69 function tag_check(event) {
70     var path = document.getElementById('element').file_path;
71     var a = tree_selection_and_check('checkbox-tree');
72     id = a[0];
73     checked = a[1];
74     alert('path: '+path.toString());
75     alert('id: '+id);
76     alert('check: '+checked);
77     if (checked) {
78         add_tag(path, id);
79     } else {
80         remove_tag(path, id);
81     }
82 }