codecombat / treema

jQuery plugin that generates HTML interfaces to edit JSON data defined by json-schema.
http://codecombat.github.io/treema/
MIT License
151 stars 36 forks source link

feature - filtering treema node. #10

Closed gintau closed 10 years ago

gintau commented 10 years ago

I add a function which accepts a string as argument and it hides the nodes with title not matching to the string. At first I intend to add a filter to CodeCombat's Thang editor, but I find the component list is built by treema, so maybe it's better to add filter here.

This function is used in this way

var treema = buildTreemaExample($('#demo'), product_schema, products);  
$('#filter').on('keyup', function(){
      treema.filterTreemaNodes($(this).val())
});

Please have a look, any feedback would be appreciated.

sderickson commented 10 years ago

Thanks gintau!

Here are some existing techniques for filtering things:

  1. Create a subclass with 'getChildren'.
  2. Use css rules to hide rows with certain classes (example).
  3. Create a new treema with the filtered data.

If one of these would work, I'd recommend that.

If none of these would work, then I think this needs to be more generic. Filtering based on the value of the text element is simple and works in your case but it's very rigid. Something better would be for it to receive a function which, given a node or data from a node, would return true or false.

One last thing: before I can accept any pull requests from you, you need to sign the CLA. Thanks!

gintau commented 10 years ago

Thanks for your advise, @sderickson

What I want to do is to add filters for components list of thang editor (under "components" tab), so I can quickly find certain components.

Both method 1 and 2 are only useful for the thang treema in level editor. They depend on the button group and pre-defined css classes. I'm thinking about change my function so it receives a function which accepts the TreemaNode as argument and returns a boolean value to decide whether the node is visible. The code may looks like this:

filterTreemaNodes: (filterer)->
    $.each: (childrenTreemas) ->
      childrentTreema.setVisible( filterer(childrenTreemas) )

I'm not sure if it's good to do filter on raw data. I have no complete picture of data manipulation, I'm afraid changing data in unexpected way would break the editor.

sderickson commented 10 years ago

Taking the TreemaNode as an argument sounds good to me. Yes, something structured like that would work. You might want to add some tests to make sure other behaviors act as expected, like for example:

There are existing tests for various keyboard behaviors you can base these tests on.

There should probably also be a function for clearing the filter.

gintau commented 10 years ago

Basically everything is new except a change in navigationContext function. All tests pass so it should be okay.

sderickson commented 10 years ago

Looks good, thanks!