-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeep-configuration.html
82 lines (80 loc) · 2.45 KB
/
deep-configuration.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
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../iron-ajax/iron-ajax.html">
<link rel="import" href="../deep-hybrid-behavior/deep-hybrid-behavior.html">
<!-- TODO: Handle additional config file types -->
<!--
TODO: How to handle filtering config data. I.e, polymer equivalent to nodejs
const { host } = require('configuration.json');
-->
<dom-module id="deep-configuration">
<template>
<iron-ajax
url="[[filePath]]"
handle-as="json"
></iron-ajax>
</template>
<script>
/**
* `deep-configuration`
* Polymer component providing access to configuration throughout an application.
*
* @customElement
* @polymer
* @demo demo/index.html
*/
Polymer({
is: 'deep-configuration',
behaviors: [
deep.HybridBehavior
],
properties: {
/* Path to the configuration file */
filePath: {
type: String,
value: './config.json'
},
/* Public hook into configurations */
configurations: {
type: Object,
value() {
return {};
},
notify: true
},
/* Indicates when configuration has finished. True means complete. False otherwise. */
done: {
type: Boolean,
value: false,
notify: true
}
},
attached() {
let configurations = {};
this.read().then(() => {
this.set('done', true);
});
},
/**
* Read in configuration file.
* @param {String} [filePath = this.filePath] - Path to the file to read.
* @return {Promise} - Resolves when file processing completes. Resolves with
* the configurations or a default.
*/
read(filePath = this.filePath) {
let configurations = {};
const ajax = this.querySelector('iron-ajax');
ajax.url = filePath;
return ajax.generateRequest().completes.then(({response}) => {
const config = response ? response : {};
this.set('configurations', config);
}, () => {
console.error(`${this.localName}: fetching configuration file at path
'${this.filePath}' failed. Configurations will be empty.`);
this.set('configurations', {});
}).then(() => {
return this.get('configurations');
});
},
});
</script>
</dom-module>