Broken implementation (multiple hits on some clicks) setCellText
authorW. Trevor King <wking@drexel.edu>
Sun, 18 Jul 2010 07:20:30 +0000 (03:20 -0400)
committerW. Trevor King <wking@drexel.edu>
Sun, 18 Jul 2010 13:22:09 +0000 (09:22 -0400)
static/dirtag.js
template/dirtag.xul

index b6f5373e93884ec745e3260cb64ecfc0628edb1b..30626a329433adf90fcbdb85dd9bcca363409262 100644 (file)
@@ -13,7 +13,7 @@ function tree_path(tree, row) {
     var name = tree.columns.getPrimaryColumn();
     var path = new Array();
     while (row >= 0) {
-       path.splice(0, 0, tree.view.getCellText(row, name));
+       path.splice(0, 0, split_checkbox_value(tree.view.getCellText(row, name))[1]);
        row = tree.view.getParentIndex(row);
     }
     return path;
@@ -36,14 +36,41 @@ function tag_in_tags(tag, tags) {
     return tag.join('/') in o;
 }
 
+function join_checkbox_value(name, value) {
+    var v = ' ';
+    if (value == true)
+       v = '+';
+    return '('+v+') ' + name;
+}
+
+function split_checkbox_value(text) {
+    if (text.length > 3 && text[0] == '(' && text[2] == ')') {
+       return [text[1] == '+', text.slice(4)];
+    }
+    return [false, text];  // No checkbox set yet.
+}
+
+function get_checkbox_value(tree, row) {
+    var name = tree.columns.getPrimaryColumn();
+    var text = tree.view.getCellText(row, name);
+    return split_checkbox_value(text)[0];
+}
+
+function set_checkbox_value(tree, row, value) {
+    var name = tree.columns.getPrimaryColumn();
+    var text = tree.view.getCellText(row, name);
+    text = split_checkbox_value(text)[1];
+    tree.view.setCellText(row, name, join_checkbox_value(text, value));
+}
+
 function setup_checkboxes(tags) {
+    //alert(tags);
     var tree = document.getElementById('checkbox-tree');
-    var ctags = tree.columns.getLastColumn();
     for (var row=0; row<tree.view.rowCount; row++) {
        p = tree_path(tree, row);
-       tree.view.origSetCellValue(row, ctags, tag_in_tags(p, tags));
+       set_checkbox_value(tree, row, tag_in_tags(p, tags));
     }
-    tree.treeBoxObject.invalidateColumn(ctags);
+    tree.treeBoxObject.invalidate();
 }
 
 /* Functions driving the CherryPy backend */
@@ -57,13 +84,20 @@ function reload_rdf(e) {
            alert("Error loading page\n\n"+e.responseText);
            return;
        }
-       document.getElementById('raw-tree').builder.refresh();
-       document.getElementById('tag-tree').builder.refresh();
-       document.getElementById('checkbox-tree').builder.refresh();
-
-       var id = document.getElementById('element').tree_id;
-       var path_tags = tree_selection(id);
-       setup_checkboxes(path_tags[1]);
+       document.getElementById('raw-tree').builder.refresh("blocking");
+       document.getElementById('tag-tree').builder.refresh("blocking");
+       document.getElementById('checkbox-tree').builder.refresh("blocking");
+
+       /* The refreshed data sources will overwrite some of the
+          checkbox text.  Wait until the data is refreshed, and
+          reload the checkboxes.
+       */
+       window.setTimeout(function () {
+               var id = document.getElementById('element').tree_id;
+               var path_tags = tree_selection(id);
+               setup_checkboxes(path_tags[1]);
+               alert('reset');
+           }, 3000);
     }
 }
 
@@ -111,6 +145,7 @@ function set_selected_element(path, tags, id) {
     var title = caption.label.split(':', 1)[0];
     caption.label = title+': '+path.join('/');
 
+    //alert('setup checkboxes for set_selecte_elt');
     setup_checkboxes(tags);
 
     var element = document.getElementById('element');
@@ -130,15 +165,30 @@ function tag_tree_select() {
     set_selected_element(path_tags[0], path_tags[1], 'tag-tree');
 }
 
-function checkbox_activity(tree, row, col, value) {
+
+function checkbox_select(e) {
+    /* https://developer.mozilla.org/en/XUL_Tutorial/Tree_Selection
+       https://developer.mozilla.org/en/XPCOM_Interface_Reference/nsITreeSelection
+       */
+    var tree = document.getElementById('checkbox-tree');
+    var s = tree.view.selection.isSelected(tree.currentIndex);
+    var row = tree.currentIndex;
+    if (tree.view.selection.isSelected(row) == false)
+       return;  // clearing the selection raises another onselect event
+    tree.view.selection.clearSelection();
+    tree.blur()
     var path = document.getElementById('element').file_path;
-    var name = tree.columns.getPrimaryColumn();
-    id = tree_path(tree, row);
     if (path == 'UNDEFINED') {
        return;
-    } else if (value == true || value == 'true') {
-       add_tag(path, id);
+    }
+    alert(e.target.id);
+    var tag = tree_path(tree, row);
+    var value = !get_checkbox_value(tree, row);
+    //alert('set checkbox by hand');
+    set_checkbox_value(tree, row, value);
+    if (value == true || value == 'true') {
+       add_tag(path, tag);
     } else {
-       remove_tag(path, id);
+       remove_tag(path, tag);
     }
 }
index 26b145766b55fd736e9ff15c3a82cbb8797e1977..c803801b91cb2b73515f6cfc7fab0af442ff75c7 100644 (file)
       <caption id="selected-caption" label="&selected.caption;"/>
       <groupbox id="tags" flex="1">
        <caption label="&tags.caption;"/>
-       <tree id="checkbox-tree" rows="2" seltype="single" selstype="primary"
-             editable="true"
-             datasources="tag.rdf raw.rdf"
-             ref="http://dirtag.com/tag/files"
-             flex="1">
-         <!-- checkbox callback attached in dirtag.js's initialize() -->
+       <tree id="checkbox-tree" rows="1" seltype="single" selstype="primary"
+             editable="true" flex="1"
+             datasources="tag.rdf raw.rdf" ref="http://dirtag.com/tag/files"
+             onselect="checkbox_select(event);">
          <treecols>
-           <treecol id="checkbox-tree-check-column" label="CK" type="checkbox"
-                    editable="true"/>
            <treecol id="checkbox-tree-path-column" label="&path_column.label;"
                     editable="true" primary="true"
                     sort="rdf:http://dirtag.com/rdf#name" flex="1"/>
              <treechildren>
                <treeitem uri="rdf:*" open="true">
                  <treerow>
-                   <treecell editable="true"/>
                    <treecell label="rdf:http://dirtag.com/rdf#name"/>
                  </treerow>
                </treeitem>