<commit_before>// Spawn a dialog for choosing a node from the db
var chooser = carena.build({
  x: 10,
  y: 10,
  height: 100,
  width: 420,
  style : {
    backgroundColor: "#424242"
  }
},[
  "carena.Node",
  "carena.Renderable",
  "carena.Draggable"
]),
frag   = carena.build({
  x: chooser.x+3+(chooser.width/2),
  y: chooser.y+3,
  width : (chooser.width/2)-6,
  height: chooser.height-34,
  style : {
    backgroundColor : "black"
  }
},[
  'carena.Node',
  'carena.RelativeToParent'
]),
action = carena.build({
  x: chooser.x+3,
  y: chooser.y+3,
  width : (chooser.width/2)-3,
  height: chooser.height-34,
  style : {
    backgroundColor : "black"
  }
},[
  'carena.Node',
  'carena.RelativeToParent'
]),
input = carena.build({
  x : chooser.x+3,
  y : chooser.y+chooser.height-28,
  width : chooser.width-6,
  height : 25,
  style : {
    backgroundColor: "#00000",
    color: "white",
    paddingLeft : 3,
    paddingRight : 3,
    paddingTop : 4,
    paddingBottom : 3
  }
},[
  "cider.Textual",
  "cider.Editable",
  "carena.RelativeToParent",
  "cider.FocusTarget"
]),
available = carena.build({},[
  "carena.Node"
]);

window.chooser = chooser;

input.font.set(composer.defaultFont);
input.setFocus(false);
chooser.add(input).add(available).add(action).add(frag);

composer.chisel = {
  filters      : [],
  actions      : {},
  selection    : 0,
  addAction    : function(type, cb) {
    if (!composer.chisel.actions[type]) {
      composer.chisel.actions[type] = [];
    }
    composer.chisel.actions[type].push(cb);
  },
  modalManager : null,
  hide         : function() {
    composer.chisel.modalManager.hide(chooser);
    input.setFocus(false);
    input.fromString("");
  },
  show         : function() {
    input.setFocus(true);
    composer.chisel.modalManager.show(chooser);
  },
  perform      : function(action, obj, cb) {

  },

  addFilterResult : function(err, data) {
    if (err) {
      console.log("ERROR:", err);
    };

    if (data.str === input.toString()) {
      node = carena.build({
        x : chooser.x,
        y : chooser.y + chooser.height + available.children.length*20,
        width: chooser.width,
        height: 20,
        text : data.name,
        representation: data,
        background : '#103069',
        style : {
          backgroundColor : "black",
          color: "white"
        }
      }, [
        "carena.Box",
        "cider.Textual",
        "carena.RelativeToParent"
      ]);
      if (available.children.length === 0) {
        node.background = "#5E99FF";
        window.node = node;
      }
      available.add(node);
    }
  }
};

available.event.bind("mouse.in", function(name, data) {
  available.child(composer.chisel.selection).background = '#103069';
  composer.chisel.selection = data.target.parent.childIndex(data.target);
  available.child(composer.chisel.selection).background = '#5E99FF';
});

// NOTE: this only works because input hasn't bound to this event yet.
input.event.bind("keyboard.down", function(name, data) {
  var ret = false, children = !!available.children.length;

  switch (data.key) {
    case 9:
      if (children) {
        available.child(composer.chisel.selection).background = '#BF6600';
      }
    break;

    case 38: // up
      if (children) {
        available.child(composer.chisel.selection).background = '#103069';
        if (composer.chisel.selection -1 < 0) {
          composer.chisel.selection = available.children.length-1;
        } else {
          composer.chisel.selection -= 1;
        }
        available.child(composer.chisel.selection).background = '#5E99FF';
      }
    break;

    case 40: // down
      if (children) {
        available.child(composer.chisel.selection).background = '#103069';
        if (composer.chisel.selection >= available.children.length-1) {
          composer.chisel.selection = 0;
        } else {
          composer.chisel.selection += 1;
        }
        available.child(composer.chisel.selection).background = '#5E99FF';
      }
    break;

    case 13:
      console.log("return");
    break;

    default:
      console.log(data.key);
      ret = true;
    break;
   }

   return ret;
});

var timeout = null;
input.event.bind("text.*", function(name, data) {
  if (timeout) {
    clearTimeout(timeout);
  }

  timeout = setTimeout(function() {
    var str = data.node.toString(), node;

    // clean the display
    while(available.children.length > 0) {
      available.remove(available.child(0));
    }
    composer.chisel.selection = 0;

    if (str.length > 0) {
      for (var i=0; i<composer.chisel.filters.length; i++) {
        composer.chisel.filters[i](str, composer.chisel.addFilterResult);
      }
    }
  }, 100);
});

/*
      jQuery.ajax({
        url: "/nodes",
        dataType: "json",<commit_msg>more chisel improvements, you can now select a text fragment.. action next<commit_after>// Spawn a dialog for choosing a node from the db
var chooser = carena.build({
  x: 10,
  y: 10,
  height: 100,
  width: 420,
  style : {
    backgroundColor: "#424242"
  }
},[
  "carena.Node",
  "carena.Renderable",
  "carena.Draggable"
]),
action   = carena.build({
  x: chooser.x+3+(chooser.width/2),
  y: chooser.y+3,
  width : (chooser.width/2)-6,
  height: chooser.height-34,
  style : {
    backgroundColor : "black"
  }
},[
  'carena.Node',
  'carena.RelativeToParent'
]),
frag = carena.build({
  x: chooser.x+3,
  y: chooser.y+3,
  width : (chooser.width/2)-3,
  height: chooser.height-34,
  style : {
    backgroundColor : "black"
  }
},[
  'carena.Node',
  'carena.RelativeToParent'
]),
input = carena.build({
  x : chooser.x+3,
  y : chooser.y+chooser.height-28,
  width : chooser.width-6,
  height : 25,
  style : {
    backgroundColor: "#00000",
    color: "white",
    paddingLeft : 3,
    paddingRight : 3,
    paddingTop : 4,
    paddingBottom : 3
  }
},[
  "cider.Textual",
  "cider.Editable",
  "carena.RelativeToParent",
  "cider.FocusTarget"
]),
available = carena.build({},[
  "carena.Node",
  "carena.RelativeToParent"
]);

chooser._render = chooser.render;
chooser.render = function(renderer) {
  chooser.x = renderer.canvas.width/2-chooser.width/2;
  chooser.y = renderer.canvas.height/2-chooser.height;
  return chooser._render(renderer);
}
input.font.set(composer.defaultFont);
input.setFocus(false);
chooser.add(input).add(available).add(action).add(frag);

composer.chisel = {
  filters      : [],
  actions      : {},
  selection    : 0,
  addAction    : function(type, cb) {
    if (!composer.chisel.actions[type]) {
      composer.chisel.actions[type] = [];
    }
    composer.chisel.actions[type].push(cb);
  },
  modalManager : null,
  hide         : function() {
    composer.chisel.modalManager.hide(chooser);
    frag.removeAll();
    action.removeAll();
    input.setFocus(false);
    input.fromString("");
  },
  show         : function() {
    input.setFocus(true);
    composer.chisel.modalManager.show(chooser);
  },
  perform      : function(action, obj, cb) {

  },

  clearSuggestions : function() {
    available.removeAll();
  },

  addFilterResult : function(err, data) {
    if (err) {
      console.log("ERROR:", err);
    };

    if (data.str === input.toString()) {
      node = carena.build({
        x : chooser.x,
        y : chooser.y + chooser.height + available.children.length*20,
        width: chooser.width,
        height: 20,
        text : data.name,
        representation: data,
        background : '#103069',
        style : {
          backgroundColor : "black",
          color: "white"
        }
      }, [
        "carena.Box",
        "cider.Textual",
        "carena.RelativeToParent"
      ]);
      if (available.children.length === 0) {
        node.background = "#5E99FF";
        window.node = node;
      }
      available.add(node);
    }
  }
};

available.event.bind("mouse.in", function(name, data) {
  available.child(composer.chisel.selection).background = '#103069';
  composer.chisel.selection = data.target.parent.childIndex(data.target);
  available.child(composer.chisel.selection).background = '#5E99FF';
});

// NOTE: this only works because input hasn't bound to this event yet.
input.event.bind("keyboard.down", function(name, data) {
  var ret = false, children = !!available.children.length;

  switch (data.key) {
    case 9:
      if (children) {
        available.child(composer.chisel.selection).background = '#BF6600';
        var textNode = available.child(composer.chisel.selection);
        composer.chisel.clearSuggestions();
        textNode.text = input.text;
        input.fromString("");
        frag.removeAll();
        frag.add(textNode);
        textNode.x = frag.x+10;
        textNode.y = frag.y+10;
        textNode.width = frag.width-20;
        textNode.height = frag.height-20;
        textNode.style.paddingTop = textNode.height/3;
        textNode.style.paddingLeft = 5;

        // TODO: shift context

        // get a list of actions to display to the user

      }
    break;

    case 38: // up
      if (children) {
        available.child(composer.chisel.selection).background = '#103069';
        if (composer.chisel.selection -1 < 0) {
          composer.chisel.selection = available.children.length-1;
        } else {
          composer.chisel.selection -= 1;
        }
        available.child(composer.chisel.selection).background = '#5E99FF';
      }
    break;

    case 40: // down
      if (children) {
        available.child(composer.chisel.selection).background = '#103069';
        if (composer.chisel.selection >= available.children.length-1) {
          composer.chisel.selection = 0;
        } else {
          composer.chisel.selection += 1;
        }
        available.child(composer.chisel.selection).background = '#5E99FF';
      }
    break;

    case 13:
      console.log("return");
    break;

    default:
      console.log(data.key);
      ret = true;
    break;
   }

   return ret;
});

var timeout = null;
input.event.bind("text.*", function(name, data) {
  if (timeout) {
    clearTimeout(timeout);
  }

  timeout = setTimeout(function() {
    var str = data.node.toString(), node;

    // clean the display
    composer.chisel.clearSuggestions();
    composer.chisel.selection = 0;

    if (str.length > 0) {
      for (var i=0; i<composer.chisel.filters.length; i++) {
        composer.chisel.filters[i](str, composer.chisel.addFilterResult);
      }
    }
  }, 100);
});

/*
      jQuery.ajax({
        url: "/nodes",
        dataType: "json",
{'fraction_non_alphanumeric': 0.1542791718347486, 'fraction_numerical': 0.02242152466367713, 'mean_word_length': 2.503342245989305, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7188803', 'n_tokens_mistral': 3720, 'n_tokens_neox': 3539, 'n_words': 634}