Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement the zip.select function and assorted predicates #53

Closed
5 tasks done
ognen opened this issue Jul 14, 2017 · 1 comment
Closed
5 tasks done

Implement the zip.select function and assorted predicates #53

ognen opened this issue Jul 14, 2017 · 1 comment
Assignees
Labels
Milestone

Comments

@ognen
Copy link
Member

ognen commented Jul 14, 2017

We need a way to select one or more elements in the application state given a starting location; also we need a way to specify such a selector to be used in ways we use CSS selectors or xpath queries.

The trick clojure.data.xml is using is well suited here.

  • Implement the function
const select: (...predicates) => location => List<locations>

and place in src/zip/index.js

The function is in curried form. It expects a variable arg-set of predicates that is used to select/filter starting from the current location.

Each predicate can be:

  • a 'string' which is interpreted as descending into a property. The property should have an element (or child elements)
  • an array of strings which is interpreted as the predicate ofKind
  • a function returning a list of locations
  • a predicate function used to filter out nodes

The system should be identical to the one in clojure.

In addition to the select function, a set of built-in predicates need to be provided:

  • ancestors a list of the location's ancestors; nearest parent first
  • descendants, a list of all descendants, depth-first, right-to-left
  • children(collection) returns a list of the location's children. if a collection is provided it will list only the children in that collection
  • ofKind(kind) a predicate function which will return true if the location points to an element the given kind
@ognen
Copy link
Member Author

ognen commented Jul 24, 2017

A sample use case for the function:

// A tab navigation element containing a list of scenes, of which only one can be active at a time 
{
  'kind': 'tab-navigation',
  '@@girders-elements/children': 'scenes',
  'scenes': [
    {
      kind: 'scene',
      id: 1,
      active: true
    },
    {
      kind: 'scene',
      id: 2
    }
  ]
}


// essentially, dispatching 'select' with sceneId = someId on 'tab-navigation' will deactivate all scenes except the selected one
updates.register(['tab-navigation'], nav => {
  // thread  the 'select' action to all children of kind scene where the id is not the sceneId from the action
  nav.thread('select', action => select(children, kindOf('scene'), not(propEq('id', action.sceneId)), 'deactivate');
  // thread the same action to all children of kind scene where action.sceneId === id 
  nav.thread('select', action => select(children, kindOf('scene'), propEq('id', action.sceneId)))), 'activate');
}

// finally the receiving actions
update.register('scene', scene => {
  scene.register('deactivate', s => s.set('active', false));
  scene.register('activate', s => s.set('active', true));
});

@andon andon self-assigned this Aug 25, 2018
@andon andon added this to the 1.0 milestone Dec 22, 2018
andon added a commit that referenced this issue Jan 12, 2019
Implements the function

const select: (...arguments) => location => Iterable<locations>

The function is in curried form. It expects a variable arg-set that is used to select/filter starting from the current location.

Each argument can be:
- a 'string' which is interpreted as descending into a property. The property should have an element (or child elements)
- an array of strings which is interpreted as the predicate `ofKind` 
- a function returning a list of locations (selector or motion function)
- a predicate function used to filter out nodes

Also implements the functions that can me used in the `select` function, in some of the following categories:
- selectors
  - `ancestors` an iterable of the location's ancestors; nearest parent first
  - `descendants`, an iterable of all descendants, depth-first, right-to-left
  - `children` an iterable of the location's children
  - `childrenAt(key)` an iterable of the location's children for the specified key
- predicates
  - `ofKind(kind)` a predicate function which will return true if the location points to an element the given kind

Closes #53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants