-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfoaas-element.html
151 lines (135 loc) · 4.59 KB
/
foaas-element.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
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
150
151
<link rel="import" href="bower_components/polymer/polymer-element.html">
<link rel="import" href="bower_components/iron-ajax/iron-ajax.html">
<link rel="import" href="bower_components/shadycss/apply-shim.html">
<dom-module id="foaas-element">
<template>
<style>
:host {
display: inline;
}
#foaas {
@apply --foaas-element-theme;
}
#foaas_content {
@apply --foaas-element-quote-theme;
}
#foaas_signature {
@apply --foaas-element-signature-theme;
}
</style>
<iron-ajax id="foaas_load" handle-as="json" headers='{"Accept": "application/json"}'>
</iron-ajax>
<div id="foaas">
<div id="foaas_content"></div>
<div id="foaas_signature"></div>
</div>
</template>
<script>
/**
* `foaas-element`
* This component will help you add the contents provided by www.foaas.com
*
* @customElement
* @polymer
* @demo demo/index.html
*/
class FoaasElement extends Polymer.Element {
static get is() { return 'foaas-element'; }
static get properties() {
return {
loading: {
type: Boolean,
value: false,
notify: true,
ReadOnly: true,
reflectToAttribute: true
},
type: {
type: String,
observer: '_typeChanged'
},
from: { type: String, value: 'Arsène Lupin' },
company: { type: String, value: 'Acme' },
name: { type: String, value: 'My friend' },
tool: { type: String, value: 'computer' },
something: { type: String, value: 'lava lamp' },
reference: { type: String, value: 'W3C' },
noun: { type: String, value: 'ben' },
reaction: { type: String, value: 'code' },
behavior: { type: String, value: 'behavior' },
thing: { type: String, value: 'the thing' },
do: { type: String, value: 'throw' },
language: { type: String, value: 'javascript' }
};
}
static get URL() { return "http://www.foaas.com/"; }
connectedCallback() {
super.connectedCallback();
this._loadFoaas();
}
_typeChanged(newValue, oldValue) {
if (oldValue !== undefined) {
this._loadFoaas();
}
}
async _loadFoaas() {
this.loading = true;
try {
if (this._type_descriptions === undefined) {
let description = await this._loadTypeDescriptions();
this._processDescriptions(description);
}
let quote = await this._loadQuote();
this._processResponseQuote(quote);
}
catch (e) {
this._processError(e);
}
}
async _loadQuote() {
let ajax = this.$.foaas_load;
ajax.url = this._generateUrl();
return ajax.generateRequest().completes;
}
_processResponseQuote(response) {
let foaas_response = response.__data.response;
this.$.foaas_content.textContent = foaas_response.message;
this.$.foaas_signature.textContent = foaas_response.subtitle;
this.loading = false;
}
_processError(error) {
this.$.foaas_content.textContent = "oups, there was a problem with you giving a fuck";
this.loading = false;
}
_generateUrl() {
let url = FoaasElement.URL;
// First we add the type
url += this.type + "/";
// Then the parameters
// We are using the fact that the properties of the component have
// the same name than the parameters in the description
url += this._type_descriptions[this.type].map(elem => this[elem]).join("/");
return url;
}
async _loadTypeDescriptions() {
let ajax = this.$.foaas_load;
ajax.url = FoaasElement.URL + "operations";
return ajax.generateRequest().completes;
}
_processDescriptions(response) {
this._type_descriptions = {};
let foaas_response = response.__data.response;
for (let elem of foaas_response) { // forEach doesn't work here. wtf ?
let tab_api = elem.url.split('/');
// The second item of the url is the type (because the url begins with "/")
// Then comes the list of parameters. Every parameter begins with a ':'
tab_api.shift();
let type = tab_api.shift();
this._type_descriptions[type] = tab_api.map(elem => elem.substring(1));
}
return Promise.resolve();
}
}
window.customElements.define(FoaasElement.is, FoaasElement);
</script>
</dom-module>