-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfs-conclusion-mixin.html
172 lines (156 loc) · 4.89 KB
/
fs-conclusion-mixin.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<link rel="import" href="../polymer/polymer-element.html">
<link rel="import" href="../fs-api-aware/fs-api-aware.html">
<link rel="import" href="../fs-attribution/fs-attribution.html">
<template id="fs-conclusion">
<style>
:host {
display: block;
}
.toolbar {
visibility: hidden;
text-align: right;
}
:host(:hover) .toolbar {
visibility: visible;
}
[hidden] {
display: none !important;
}
</style>
<template is="dom-if" if="[[editable]]">
<div class="toolbar">
<a id="edit" href hidden$="[[edit]]">Edit</a>
<a id="close" href hidden$="[[!edit]]">Close</a>
<a id="save" href hidden$="[[!_showSaveButton(edit, saveUrl)]]">Save</a>
</div>
</template>
<div id="content"></div>
<template is="dom-if" if="[[editable]]">
<fs-attribution
hidden$="[[!edit]]"
client-name="[[clientName]]"
attribution="{{conclusion.attribution}}"
edit="[[edit]]"></fs-attribution>
</template>
<fs-request
id="saveRequest"
method="POST"
client-name="[[clientName]]"
url="[[saveUrl]]"
loading="{{saving}}"></fs-request>
</template>
<script>
(function(){
const myDoc = document.currentScript.ownerDocument;
const memoizedTemplates = {};
/**
* `FsConclusionMixin`
*
* Shared code for `fs-fact`, `fs-person-name`, `fs-person-gender`.
*
* The template is designed for base classes to fill in the #content div with
* their own content which places the edit toolbar on top and the attribution
* on the bottom. The template is used by adding these lines to the base element:
*
* static get template() {
* return super.conclusionTemplate(this.is);
* }
*
* FsConclusionMixin expects the conclusion data to be at the `conclusion`
* property. If you want your property to be named something different (e.g.
* it makes more sense for a fact conclusion to be set on the fact property)
* then setup an observer and sync your property with `conclusion.
*
* _conclusionSync(fact) {
* this.conclusion = fact;
* }
*
* The base element must also provide the body of the save request by
* implementing the `_conclusionBody()` method which returns an Object that is
* serialized into JSON for the request.
*
* _conclusionBody() {
* return { persons: [{ facts: [this.fact] }] };
* };
*
* @polymer
* @mixinFunction
* @appliesMixin FSApiAwareMixin
*/
window.FsConclusionMixin = Polymer.dedupingMixin((superClass) => class extends FSApiAwareMixin(superClass) {
static get properties() {
return {
conclusion: {
type: Object,
notify: true
},
/**
* Whether the conclusion can be edited. This will show a toolbar when the
* user hovers over the conclusion.
*/
editable: {
type: Boolean,
value: false,
observer: '_setupEditable'
},
/**
* Whether the conclusion is currently in edit mode
*/
edit: {
type: Boolean,
value: false
},
/**
* The URL that the conclusion data will be saved to.
*/
saveUrl: String,
/**
* Whether the conclusion is being saved
*/
saving: {
type: Boolean,
value: false,
readOnly: true,
notify: true
}
};
}
/**
* Here we grap the fs-conclusion-template and fill it in with the base
* template of the element that uses this mixin.
*/
static conclusionTemplate(childClass) {
if(!memoizedTemplates[childClass]) {
memoizedTemplates[childClass] = document.importNode(myDoc.querySelector('#fs-conclusion'), true);
let conclusionTemplate = Polymer.DomModule.import(childClass, 'template');
memoizedTemplates[childClass].content.querySelector('#content').appendChild(conclusionTemplate.content);
}
return memoizedTemplates[childClass];
}
_showSaveButton(edit, saveUrl) {
return edit && saveUrl;
}
_setupEditable(editable) {
if(editable) {
setTimeout(() => {
this.shadowRoot.querySelector('#edit').addEventListener('click', (e) => {
this.edit = true;
e.preventDefault();
});
this.shadowRoot.querySelector('#close').addEventListener('click', (e) => {
this.edit = false;
e.preventDefault();
});
this.shadowRoot.querySelector('#save').addEventListener('click', (e) => {
this.edit = false;
e.preventDefault();
const request = this.$.saveRequest;
request.body = this._conclusionBody();
request.generateRequest();
});
});
}
}
});
})();
</script>