Debian Codegraph @

ivette/src/dome/misc/register.js

raw
1 /* ************************************************************************ */
2 /* */
3 /* This file is part of Frama-C. */
4 /* */
5 /* Copyright (C) 2007-2022 */
6 /* CEA (Commissariat à l'énergie atomique et aux énergies */
7 /* alternatives) */
8 /* */
9 /* you can redistribute it and/or modify it under the terms of the GNU */
10 /* Lesser General Public License as published by the Free Software */
11 /* Foundation, version 2.1. */
12 /* */
13 /* It is distributed in the hope that it will be useful, */
14 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
15 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
16 /* GNU Lesser General Public License for more details. */
17 /* */
18 /* See the GNU Lesser General Public License version 2.1 */
19 /* for more details (enclosed in the file licenses/LGPLv2.1). */
20 /* */
21 /* ************************************************************************ */
22
23 // --------------------------------------------------------------------------
24 // --- Dome Classes
25 // --------------------------------------------------------------------------
26
27 import _ from 'lodash' ;
28 import React from 'react' ;
29
30 // --------------------------------------------------------------------------
31 // --- Register a Dome Class
32 // --------------------------------------------------------------------------
33
34 export function register(Component,NAME) {
35 Component._DOME = NAME ;
36 Component.istanceOf = (elt) => (elt.type ? elt.type._DOME === NAME : false) ;
37 }
38
39 // --------------------------------------------------------------------------
40 // --- Get the Dome Class of a Component
41 // --------------------------------------------------------------------------
42
43 export function classOf(elt) {
44 return elt && elt.type && elt.type._DOME ;
45 }
46
47 // --------------------------------------------------------------------------
48 // --- Dispatch Children components among different Dome Classes
49 // --------------------------------------------------------------------------
50
51 /*
52 Split a React Children structure according to filter.
53 For each field { key: spec } in the filter, returns
54 a field { key: selection } with all children satisfying
55 the given spec (undefined if none).
56 Filter specification can be: a Dome class name,
57 an array of Dome class names, or 'undefined' for all non-matched
58 children in the filter.
59
60 Relies on React.Children.map instead ot React.Children.toArray
61 in order to avoid putting keys in original children when not necessary.
62 */
63 export function dispatch(children,filter)
64 {
65
66 var others = undefined;
67
68 const acceptor = _.mapValues( filter , (spec,field) => {
69 switch(typeof(spec)) {
70 case 'string':
71 return (elt) => classOf(elt) === spec ;
72 case 'undefined':
73 others = field ;
74 return undefined ;
75 default:
76 console.error('[Dome.common.register] non-supported filter spec',spec);
77 return undefined ;
78 }
79 } );
80
81 var count = 0 , first = true , accepted = [] ;
82 const collected = _.mapValues( acceptor , (accept,_field) => {
83 if (accept) {
84 var empty = true ;
85 var filtered =
86 React.Children.map( children , (elt) => {
87 var ok = elt && accept(elt);
88 if (ok) empty = false ;
89 if (others) {
90 if (first) accepted.push(ok);
91 else {
92 if (ok) accepted[count] = ok ;
93 count++;
94 }
95 }
96 return ok && elt;
97 });
98 first = false ; count = 0 ;
99 return empty ? undefined : filtered ;
100 } else
101 return undefined ;
102 });
103
104 if (others) {
105 var empty = true ;
106 var remaining = React.Children.map( children , (elt) => {
107 var ok = elt && !accepted[count] ;
108 if (ok) empty = false ;
109 count++;
110 return ok ? elt : null ;
111 });
112 if (!empty) collected[others] = remaining ;
113 }
114
115 return collected;
116 }
117
118 // --------------------------------------------------------------------------