-
Notifications
You must be signed in to change notification settings - Fork 1
/
fs-date.html
154 lines (137 loc) · 4.29 KB
/
fs-date.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
152
153
154
<link rel="import" href="../polymer/polymer-element.html">
<link rel="import" href="../polymer/lib/elements/dom-if.html">
<link rel="import" href="../polymer/lib/utils/debounce.html">
<link rel="import" href="../paper-autocomplete/paper-autocomplete.html">
<link rel="import" href="../fs-api-aware/fs-api-aware.html">
<link rel="import" href="../fs-request/fs-request.html">
<dom-module id="fs-date">
<template>
<style>
:host {
display: block;
}
</style>
<div id="date" hidden$="[[edit]]">{{_displayDate(date)}}</div>
<template is="dom-if" if="[[edit]]">
<fs-request
id="request"
client-name="[[clientName]]"
url="{{_computeUrl(date.original)}}"
headers='{"Accept":"text/plain"}'
on-response="_handleResponse"></fs-request>
<paper-autocomplete
id="autocomplete"
label="[[label]]"
no-label-float="[[no-label-float]]"
remote-source
disabled="{{disabled}}"
on-keyup="_autocompleteTextChanged"
on-autocomplete-selected="_autocompleteSelected"
on-autocomplete-reset-blur="_autocompleteCleared"></paper-autocomplete>
</template>
</template>
<script>
/**
* `fs-date`
*
* @customElement
* @polymer
* @demo demo/index.html
*/
class FsDate extends FSApiAwareMixin(Polymer.Element) {
static get is() { return 'fs-date'; }
static get properties() {
return {
date: {
type: Object,
notify: true
},
label: {
type: String,
value: 'Date'
},
noLabelFloat: {
type: Boolean,
value: false
},
disabled: {
type: Boolean,
value: false
},
/* Toggle edit mode */
edit: {
type: Boolean,
value: false,
observer: '_setupEdit'
},
_debouncer: Object
};
}
_displayDate(date) {
if(!date) {
return '';
}
return date.original || (date.normalized && date.normalized[0] ? date.normalized[0].value : false) || date.formal;
}
_setupEdit(edit) {
// Have to defer this execution to give time for Polymer to stamp the
// DOM since it's in a dom-if helper template
if(edit && this.date) {
setTimeout(() => {
this.shadowRoot.querySelector('#autocomplete').setOption({
text: this._displayDate(this.date),
value: JSON.stringify(this.date)
});
});
}
}
_computeUrl(original) {
if(original) {
return `/platform/dates?date=${original}`;
}
}
_autocompleteSelected(e) {
this.date = JSON.parse(e.detail.value);
}
_autocompleteTextChanged() {
const text = this.shadowRoot.querySelector('#autocomplete').text;
if(text) {
if(!this.date) {
this.date = {};
}
this.set('date.original', text);
this._debouncer = Polymer.Debouncer.debounce(this._debouncer, Polymer.Async.timeOut.after(300), () => {
this.shadowRoot.querySelector('#request').generateRequest();
});
} else {
this.date = undefined;
}
}
_autocompleteCleared() {
this.date = undefined;
}
_handleResponse(e) {
const response = e.detail.response;
if(response.statusCode === 200) {
const value = {
original: response.body,
normalized: [{
lang: response.headers['content-language'],
value: response.body
}],
formal: response.headers.location ? response.headers.location.replace('gedcomx-date:', '') : undefined
};
this.shadowRoot.querySelector('#autocomplete').suggestions([
{
text: response.body,
value: JSON.stringify(value)
}
]);
} else {
this.shadowRoot.querySelector('#autocomplete').suggestions([]);
}
}
}
window.customElements.define(FsDate.is, FsDate);
</script>
</dom-module>