/* ************************************************************************ */
/* */
/* This file is part of Frama-C. */
/* */
/* Copyright (C) 2007-2022 */
/* CEA (Commissariat à l'énergie atomique et aux énergies */
/* alternatives) */
/* */
/* you can redistribute it and/or modify it under the terms of the GNU */
/* Lesser General Public License as published by the Free Software */
/* Foundation, version 2.1. */
/* */
/* It is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU Lesser General Public License for more details. */
/* */
/* See the GNU Lesser General Public License version 2.1 */
/* for more details (enclosed in the file licenses/LGPLv2.1). */
/* */
/* ************************************************************************ */
// --------------------------------------------------------------------------
// --- Data Collector
// --------------------------------------------------------------------------
/**
@packageDocumentation
@module dome/data/library
@description
This module allows to integrate data definitions within React elements.
Typically, you can use it to define your own structure of logical elements
and display them at different places in the GUI. For instance, you may
want to define a custom list of elements, where each element
will be rendered twice: locally with the currenly selected item, and in a side-bar
or in the menu-bar, or for any other purpose than rendering. You want to declare
your list like this:
* ```jsx
*
* ...
* ...
* ...
*
* ```
Dome data libraries, as provided by this module, allows you to define such
collection of data mixed with rendered elements.
Data are collected through libraries.
You create libraries with `createLibrary()` or `useLocalLibrary()` and
then provide them to ``, ``, `` or
`` elements.
At any (other) point of the tree, you can use the collected items
with the `useLibrary()` custom React hook.
Libraries are pushed down the React virtual tree _via_ React context,
so you don't need to re-specify it for children of ``
and ``.
Items must be identified with a unique `id` ; they are sorted according
to their `order` property, although `` and ``
preserve the order of data collected from each of their children. Items are
collected when they are actually _mounted_ by React,
like any other React element.
Data elements are normal React element, that might be rendered by
visible elements in the DOM. You may think of data elements as having a
double rendering: mounted data items are collected into libraries, and normal visible
elements are collected into the React virtual DOM. Each kind of data element is
rendered differenly with this respect:
- `` renders its children within a React fragment;
- `` is like an item, with its data children stored in the registered item;
- `` always renders 'null' and capture its React children elements
in the registered item;
- `` renders its children within a React fragment, with optional context
library and specified order.
See each component for more details.
As an example of use, the introductory example can be implemented as follows:
```jsx
const lib = createLibrary();
// To be used at any (possibly repeated) place in the hierarchy of react elements
// Children items are only added to the library when the list is mounted
const MyList = ({ children }) => {
// Actually renders nothing if children only contains items.
// The fragment makes items keep their declaration ordering.
return (
{children}
);
};
// To be used also at any (possibly repeated) place in the hierarchy
const MyItem = (props) => ;
// To be used for instance at top-level inside a side-bar
const MyListIndex = () => {
let items = Data.useLibrary(lib);
return (
{ items.map(({id,label}) => (
{label}
)) }
);
};
```
*/
import _ from 'lodash';
import React from 'react';
import * as Dome from 'dome';
import EventEmitter from 'events';
// --------------------------------------------------------------------------
// --- Libraries
// --------------------------------------------------------------------------
const reOrder = (items) =>
_.sortBy(items, ['order', 'id'])
.map((item, order) => Object.assign(item, { order }));
/**
@summary Data Collector.
@description
Libraries are used to collect data through the React virtual DOM.
*/
class Library extends EventEmitter {
constructor() {
super();
this.items = {};
this.lastItems = {};
this._trigger = _.debounce(this._trigger, 10);
}
_trigger() {
if (!_.isEqual(this.items, this.lastItems)) {
this.lastItems = this.items;
this.sorted = undefined;
this.emit('trigger');
}
};
/**
@summary Register Item.
@param {object} item - must have an `'id'` property
*/
add(item) {
const id = item.id;
if (Dome.DEVEL && this.items[id])
console.warn(`[dome/data] duplicate item identifier (${id})`);
this.items[id] = item;
this._trigger();
}
/**
@summary Remove Item.
@param {string} id - the item identifier to remove
*/
remove(id) {
delete this.items[id];
this._trigger();
}
/**
@summary Bugger Contents.
@return {Array