-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlas2peer-userlist-widget.js
149 lines (133 loc) · 4.25 KB
/
las2peer-userlist-widget.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
144
145
146
147
148
149
import { PolymerElement, html } from "@polymer/polymer/polymer-element.js";
import "@polymer/iron-ajax/iron-ajax.js";
class Las2peerUserlistWidget extends PolymerElement {
static get template() {
return html`
<iron-ajax
id="ajaxGetContacts"
url='[[baseUrl]]/contactservice/'
params='{}'
handle-as="json"
on-response="_updateContactList"
on-error="_handleError"
headers='[[_requestHeaders]]'>
</iron-ajax>
<input list="contactlist" name="contactlist">
<datalist on-change="changeValue" id="contactlist">
<template is="dom-repeat" items="[[contacts]]">
<option value="{{item}}">{{item}}</option>
</template>
</datalist>`
}
static get properties() {
return {
data: Object,
item: Object,
name: String,
baseUrl: {
type: String,
value: 'https://las2peer.dbis.rwth-aachen.de:9098',
},
contacts: {
type: Object,
value: []
},
value: {
type: String
},
loggedIn: {
type: Boolean,
computed: '_computeLogin(loginName,loginOidcToken,loginOidcProvider)'
},
loginName: {
type: String,
value: null
},
loginPassword: {
type: String,
value: null
},
loginOidcToken: {
type: String,
value: null
},
loginOidcProvider: {
type: String,
value: null
},
_requestHeaders: {
type: Object,
computed: '_computeHeaders(loginName,loginPassword,loginOidcToken,loginOidcProvider)'
},
sendCookie: {
type: Boolean,
value: false
}
}
}
_computeHeaders(loginName, loginPassword, loginOidcToken, loginOidcProvider) {
var headers = {};
if (loginName != null && loginPassword != null) {
headers["Authorization"] = "Basic " + btoa(loginName + ":" + loginPassword);
} else if (loginOidcToken != null) {
headers["access-token"] = loginOidcToken;
if (loginOidcProvider != null) {
headers["oidc_provider"] = loginOidcProvider;
}
}
return headers;
}
addUser(list, user) {
this.push(list, user);
}
changeValue() {
this.value = this.$.contactlist.value;
}
handleResponse(e, detail) {
this.response.apply(this, arguments);
}
_computeLogin(loginName, loginOidcToken, loginOidcProvider) {
if (loginName != null) {
return true;
} else if (loginOidcToken != null && loginOidcProvider != null && loginOidcToken != "undefined") {
return true;
}
return false;
}
ready() {
super.ready();
if (this.sendCookie) {
let ajaxReqs = this.shadowRoot.querySelectorAll("iron-ajax");
for (var i = 0; i < ajaxReqs.length; i++)
if (this.sendCookie)
ajaxReqs[i].withCredentials = true;
}
if (this.loggedIn || this.sendCookie) {
this.$.ajaxGetContacts.generateRequest();
}
}
_updateContactList(event) {
var userliste = event.target.lastResponse.users;
this.contacts = [];
let keys = Object.keys(userliste);
for (var i = 0; i < keys.length; i++) {
this.addUser('contacts', userliste[keys[i]]);
}
if (this.contacts.length > 0) {
this.value = this.contacts[0];
}
this.contacts.sort(function(a, b) {
if (a.toLowerCase() < b.toLowerCase()) return -1;
if (a.toLowerCase() > b.toLowerCase()) return 1;
return 0;
});
}
_handleError(event) {
//alert(event.target.lastResponse);
console.error(event._);
}
_isNull(val) {
return val == null;
}
}
window.customElements.define('las2peer-userlist-widget', Las2peerUserlistWidget);