-
Notifications
You must be signed in to change notification settings - Fork 1
/
fs-source-attachments.html
118 lines (105 loc) · 3.5 KB
/
fs-source-attachments.html
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
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../fs-request/fs-request.html">
<link rel="import" href="../fs-api-aware/fs-api-aware.html">
<link rel="import" href="../fs-person-chip/fs-person-chip.html">
<dom-module id="fs-source-attachments">
<template>
<style>
:host {
display: block;
}
fs-person-chip {
margin: 8px 0;
}
</style>
<fs-request
id="request"
url="{{_computeRequestUrl(url)}}"
client-name="[[clientName]]"
require-authentication
on-response="_handleResponse"></fs-client>
<template is="dom-repeat" items="{{personIds}}">
<fs-person-chip person-id="{{item}}" clientName="[[clientName]]"></fs-person-chip>
</template>
</template>
<script>
/**
* List persons that a source (URL) is attached to.
*
* Basic example:
*
* <fs-source-attachments url="https://domain.com/source/url"></fs-source-attachments>
*
* @customElement
* @polymer
* @demo demo/index.html
*/
class FsSourceAttachments extends FSApiAwareMixin(Polymer.Element) {
static get is() { return 'fs-source-attachments'; }
static get properties() {
return {
url: {
type: String,
notify: true,
value: '',
observer: '_urlChanged'
},
personIds: {
type: Array,
notify: true,
readOnly: true,
value: function(){
return [];
}
}
};
}
_computeRequestUrl(url) {
if(url) {
return `/platform/tree/source-references?source=${encodeURIComponent(url)}`;
}
}
_urlChanged() {
if(this.url) {
this._debouncer = Polymer.Debouncer.debounce(
this._debouncer,
Polymer.Async.timeOut.after(400),
() => {
this.$.request.generateRequest();
}
);
} else {
this._setPersonIds([]);
}
}
_handleResponse(e) {
const response = e.detail.response;
this._setPersonIds([]);
if(response.data && response.data.sourceDescriptions && response.data.persons) {
// https://groups.google.com/a/ldsmail.net/d/msg/FSDN/Pg_VyWNYlzs/BECXG0nnBwAJ
// Urls are not matched exactly so we enforce exact matching here. First
// we gather a list of source Ids that actually match. If we found any
// then we compare the source Ids against source refs to get a list of
// persons that the matching sources are attached to.
const matchingSourceIds = response.data.sourceDescriptions.filter((sourceDescription) => {
return sourceDescription.about === this.url;
}).map((sourceDescription) => {
return sourceDescription.id;
});
const matchingPersonIds = response.data.persons.filter((person) => {
for(let i = 0; i < person.sources.length; i++) {
if(matchingSourceIds.indexOf(person.sources[i].descriptionId) !== -1) {
return true;
}
}
return false;
}).map((person) => {
return person.id;
});
this._setPersonIds(matchingPersonIds);
}
}
}
window.customElements.define(FsSourceAttachments.is, FsSourceAttachments);
</script>
</dom-module>