This repository was archived by the owner on Jul 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcms-token.html
More file actions
349 lines (339 loc) · 13.3 KB
/
Copy pathcms-token.html
File metadata and controls
349 lines (339 loc) · 13.3 KB
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../iron-ajax/iron-ajax.html">
<link rel="import" href="../hax-body-behaviors/hax-body-behaviors.html">
<link rel="import" href="../paper-spinner/paper-spinner.html">
<!--
`cms-token`
Render and process a shortcode / token from a content management system.
@demo demo/index.html
@microcopy - the mental model for this element
- cms - Content management system, while writen against Drupal should be
abstract enough to work with just about anything.
- token - a snippet / shortcode of logic to unpack and turn into something
more complex. Usually of the form [actual:thing:here] or
[[action|thing=stuff|here=place]] style. Either way, it's a
snippet which will get sent to a backend and dynamically replaced.
-->
<dom-module id="cms-token">
<template strip-whitespace>
<style>
:host {
display: inline;
min-width: 112px;
min-height: 112px;
transition: .6s all ease;
background-color: transparent;
}
paper-spinner {
transition: .6s all ease;
position: absolute;
visibility: hidden;
display: none;
opacity: 0;
height: 0;
width: 0;
}
#replacementcontent {
transition: .6s all ease;
visibility: visible;
opacity: 1;
height: auto;
width: auto;
}
:host[loading] {
text-align: center;
}
:host[loading] paper-spinner {
visibility: visible;
opacity: 1;
position: relative;
height: 80px;
width: 80px;
padding: 16px;
display: flex;
}
:host[loading] #replacementcontent {
opacity: 0;
visibility: hidden;
height: 0;
width: 0;
}
</style>
<iron-ajax id="tokenrequest" method="GET" params="[[bodyData]]" url="[[tokenEndPoint]]" handle-as="json"
last-response="{{tokenData}}"></iron-ajax>
<paper-spinner active="[[loading]]"></paper-spinner>
<span id="replacementcontent">
<slot></slot>
</span>
</template>
<script>
Polymer({
is: 'cms-token',
behaviors: [HAXBehaviors.PropertiesBehaviors],
properties: {
/**
* Loading state
*/
loading: {
type: Boolean,
reflectToAttribute: true,
value: false,
},
/**
* Token changed (somehow) do the token processing.
*/
token: {
type: String,
reflectToAttribute: true,
},
/**
* Token end point updated, change the way we do processing.
*/
tokenEndPoint: {
type: String,
},
/**
* Body data which is just token with some encapsulation.
*/
bodyData: {
type: Object,
computed: '_generateBodyData(token, _clickInvoked)',
observer: '_tokenChanged',
},
/**
* internal tracking for edit button being clicked in HAX presentation
*/
_clickInvoked: {
type: String,
value: false,
},
/**
* Token data from the end point.
*/
tokenData: {
type: String,
observer: '_handleTokenResponse',
},
/**
* Prefix for the token to be processed
*/
tokenPrefix: {
type: String,
value: '[',
},
/**
* Suffix for the token to be processed
*/
tokenSuffix: {
type: String,
value: ']',
},
/**
*
*/
_displayMode: {
type: String,
value: 'full',
observer: '_displayModeChanged',
}
},
/**
* Display mode value updated.
*/
_displayModeChanged: function (newValue, oldValue) {
if (typeof newValue !== typeof undefined && newValue != '' && typeof this.token !== typeof undefined) {
// @todo need more sanity checks then this to get default and replace better
this.token = this.token.replace(oldValue, newValue);
}
},
/**
* Generate body data.
*/
_generateBodyData: function (token, $editingState) {
if (token !== null && token !== '') {
let tokenPrefix = this.tokenPrefix;
let tokenSuffix = this.tokenSuffix;
return {
'token': `${tokenPrefix}${token}${tokenSuffix}`,
'cachedResponse': $editingState
};
}
},
/**
* Handle the response from the token processing endpoint
*/
_handleTokenResponse: function (newValue, oldValue) {
if (newValue !== null && typeof newValue.content !== typeof undefined) {
// store the text and url callbacks in the event we're in an editing mode
if (document.getElementById('cmstokenidtolockonto') != null) {
document.getElementById('cmstokenidtolockonto').setAttribute('href', newValue.editEndpoint);
document.getElementById('cmstokenidtolockonto').innerHTML = newValue.editText;
document.getElementById('cmstokenidtolockonto').addEventListener("click", this.__tokenClicked.bind(this));
}
// wipe our own slot here
this.wipeSlot(Polymer.dom(this));
// now inject the content we got
this.async( () => {
let template = document.createElement('template');
template.innerHTML = newValue.content;
Polymer.dom(this).appendChild(document.importNode(template.content, true));
this.loading = false;
});
}
},
/**
* wipe out the slot
*/
wipeSlot: function (element) {
while (element.firstChild !== null) {
element.removeChild(element.firstChild);
}
},
/**
* Token end point changed
*/
_tokenChanged: function (newValue, oldValue) {
// ensure we have something and are not loading currently
if (typeof newValue !== typeof undefined && newValue !== '' && !this.loading) {
// support going from a null element to a real one
if (typeof this.tokenEndPoint === typeof undefined && typeof window.cmstokenEndPoint !== typeof undefined) {
this.tokenEndPoint = window.cmstokenEndPoint;
}
if (this.tokenEndPoint) {
this.loading = true;
this.async(() => {
this.$.tokenrequest.generateRequest();
});
}
}
},
/**
* Window visibility callback to monitor when we are being seen
*/
_windowVisibilityChanged: function (e) {
// ensure we aren't already loading
if (!this.loading && this._clickInvoked) {
// generate request which will kick off "loading" state
this.$.tokenrequest.generateRequest();
// kill our clickInvoked handler so we aren't generating requests until the
// user clicks to edit the thing again
this._clickInvoked = false;
}
},
/**
* Notice a click on our edit button and set a flag.
*/
__tokenClicked: function (e) {
// set flag so we know to generate a new request when we come back into focus
this._clickInvoked = true;
},
/**
* Detatched life cycle.
*/
detached: function () {
// remove event listener for clean up
document.removeEventListener("visibilitychange", this._windowVisibilityChanged.bind(this));
},
/**
* Attached to the DOM, now fire.
*/
attached: function () {
// notice visibilty change
document.addEventListener("visibilitychange", this._windowVisibilityChanged.bind(this));
if (typeof this.token !== typeof undefined && this.token !== null && this.token !== '') {
let slot = Polymer.dom(this).getEffectiveChildNodes();
// only kick off request if there's nothing in it
// if it has something in it that means we did some
// remote rendering ahead of time
if (slot.length === 0 && !this.loading) {
// support for autoloading the token data needed for the request from globals
if (typeof this.tokenEndPoint === typeof undefined && typeof window.cmstokenEndPoint !== typeof undefined) {
this.tokenEndPoint = window.cmstokenEndPoint;
}
if (this.tokenEndPoint) {
this.loading = true;
this.async(() => {
this.$.tokenrequest.generateRequest();
});
}
}
}
// Establish hax property binding
let props = {
'canScale': true,
'canPosition': true,
'canEditSource': false,
'gizmo': {
'title': 'CMS Token',
'description': 'CMS token rendered on the backend',
'icon': 'icons:code',
'color': 'light-blue',
'groups': ['CMS'],
'handles': [
{
'type': 'cmstoken',
'token': 'token'
}
],
'meta': {
'author': 'LRNWebComponents'
}
},
'settings': {
'quick': [
],
'configure': [
{
'property': 'token',
'title': 'Token',
'description': 'Token from our CMS',
'inputMethod': 'textfield',
'icon': 'editor:title',
}
],
'advanced': [
]
},
'saveOptions': {
'wipeSlot': true,
'unsetAttributes': [
'loading',
'token-data',
'body-data',
'token-end-point'
]
},
};
this.setHaxProperties(props);
},
/**
* Implements getHaxJSONSchema post processing callback.
*/
postProcessgetHaxJSONSchema: function (schema) {
let href = '';
let slot = 'Edit';
// if we have values populate them
if (typeof this.tokenData !== typeof undefined) {
href = this.tokenData.editEndpoint;
slot = this.tokenData.editText;
for (var i in this.tokenData.schema) {
schema.properties[i] = this.tokenData.schema[i];
}
}
schema.properties['__editThis'] = {
'type': 'string',
'component': {
'name': 'a',
'properties': {
'id': 'cmstokenidtolockonto',
'href': href,
'target': '_blank'
},
'slot': slot
},
};
return schema;
},
});
</script>
</dom-module>