This repository has been archived by the owner on May 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
simple-credential-displayer-directive.js
143 lines (131 loc) · 3.98 KB
/
simple-credential-displayer-directive.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*!
* Simple credential displayer directive.
*
* Can take advantage of the following options sent through brOptions:
*
* limit: an integer limiting the amount of properties displayed, defaults to
* MAX
*
* collapsable: a flag denoting if the properties underneath the credential can
* collapse and expand, if true, the properties will collapse down to
* whatever number is specified by 'limit'
*
* hideTitle: a flag that hides the credential's title, used if you only want
* to display credential properties
*
* Copyright (c) 2015-2017 Digital Bazaar, Inc. All rights reserved.
*
* @author Alex Lamar
*/
import jsonld from 'jsonld';
/* @ngInject */
export default function factory() {
return {
restrict: 'E',
scope: {
model: '=brModel',
groups: '=brGroups',
options: '=brOptions'
},
templateUrl: 'bedrock-angular-credential/simple-credential-displayer.html',
link: Link
};
function Link(scope) {
var LIMIT_DEFAULT = Infinity;
var model = scope.model;
model.properties = getReadableProperties(scope.model, scope.groups);
model.hideTitle = false;
model.limit = Infinity;
if(scope.options) {
if(scope.options.limit) {
model.limit = scope.options.limit;
LIMIT_DEFAULT = model.limit;
}
if(scope.options.hideTitle) {
model.hideTitle = scope.options.hideTitle;
}
}
model.expandProperties = function() {
if(scope.options && !scope.options.collapsable) {
// Don't allow collapsable behavior if not specified through options
return;
}
if(model.limit === Infinity) {
// Collapse
model.limit = LIMIT_DEFAULT;
} else {
// Expand
model.limit = Infinity;
}
};
function getReadableProperties(model, groups) {
var properties = [];
if(groups.length === 0) {
// TODO: We can probably do a better job of printing claims that do not
// have a readable layout specified, but for now we just indicate that
// no such layout exists
properties.push('Failed to read credential properties');
return properties;
}
if(groups[0].label) {
model.title = groups[0].label;
} else if(groups[0].id) {
model.title = groups[0].id;
} else {
model.title = 'No Title';
}
parseGroups(model, groups, false);
return properties;
function parseGroups(model, groups) {
for(var key in groups) {
var group = groups[key];
parseGroup(model, group, false);
}
}
function parseGroup(model, group) {
for(var key in group.layout) {
var property = group.layout[key];
var propertyId;
var schema;
if(property.property === 'id') {
propertyId = 'id';
schema = {
label: 'ID',
range: 'URL'
};
} else {
propertyId = property.property.id;
schema = property.property;
}
var range = schema.range;
var propertyGroups = jsonld.getValues(property, 'propertyGroup');
var value = model[propertyId];
if(propertyGroups.length > 0) {
parseGroups(value, propertyGroups, false);
} else {
// We're at a leaf value in the claim tree
if(range === 'Date' && value) {
value = value['@value'];
}
if(!value) {
console.log('Failed to match value for group id: ' + propertyId);
continue;
}
var label = schema.label;
if(label === 'Image') {
continue;
}
if(label === 'Issuer') {
// A bit of a hack to extract issuer strings
if('id' in value) {
value = value.id;
}
}
var line = label + ': ' + value;
properties.push(line);
}
}
}
}
}
}