-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathelementRoleMap.js
90 lines (82 loc) · 2.56 KB
/
elementRoleMap.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/**
* @flow
*/
import deepEqual from 'deep-equal';
import iterationDecorator from "./util/iterationDecorator";
import rolesMap from './rolesMap';
type RoleSet = Array<ARIARoleDefinitionKey>;
type ElementARIARoleRelationTuple = [ARIARoleRelationConcept, RoleSet]
type ElementARIARoleRelations = Array<ElementARIARoleRelationTuple>;
const elementRoles: ElementARIARoleRelations = [];
const keys = rolesMap.keys();
for (let i = 0; i < keys.length; i++) {
const key: ARIARoleDefinitionKey = keys[i];
const role = rolesMap.get(key);
if (role) {
const concepts = [].concat(role.baseConcepts, role.relatedConcepts);
for (let k = 0; k < concepts.length; k++) {
const relation: ARIARoleRelation = concepts[k];
if (relation.module === 'HTML') {
const concept = relation.concept;
if (concept) {
const elementRoleRelation: ?ElementARIARoleRelationTuple = elementRoles.find(relation => deepEqual(relation, concept));
let roles: RoleSet;
if (elementRoleRelation) {
roles = elementRoleRelation[1];
} else {
roles = [];
}
let isUnique = true;
for (let i = 0; i < roles.length; i++) {
if (roles[i] === key) {
isUnique = false;
break;
}
}
if (isUnique) {
roles.push(key);
}
elementRoles.push([concept, roles]);
}
}
}
}
}
const elementRoleMap: TAriaQueryMap<
ElementARIARoleRelations,
ARIARoleRelationConcept,
RoleSet,
> = {
entries: function (): ElementARIARoleRelations {
return elementRoles;
},
forEach: function (
fn: (RoleSet, ARIARoleRelationConcept, ElementARIARoleRelations) => void,
thisArg: any = null,
): void {
for (let [key, values] of elementRoles) {
fn.call(thisArg, values, key, elementRoles);
}
},
get: function (key: ARIARoleRelationConcept): ?RoleSet {
const item = elementRoles.find(tuple => (
key.name === tuple[0].name && deepEqual(key.attributes, tuple[0].attributes)
));
return item && item[1];
},
has: function (key: ARIARoleRelationConcept): boolean {
return !!elementRoleMap.get(key);
},
keys: function (): Array<ARIARoleRelationConcept> {
return elementRoles.map(([key]) => key);
},
values: function (): Array<RoleSet> {
return elementRoles.map(([, values]) => values);
},
};
export default (
iterationDecorator(
elementRoleMap,
elementRoleMap.entries(),
): TAriaQueryMap<ElementARIARoleRelations, ARIARoleRelationConcept, RoleSet>
);