This repository has been 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
/
haxcms-site-listing.html
589 lines (584 loc) · 20.7 KB
/
haxcms-site-listing.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
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../map-menu/map-menu.html">
<link rel="import" href="../jwt-login/jwt-login.html">
<link rel="import" href="../simple-colors/simple-colors.html">
<link rel="import" href="../simple-colors/simple-colors-picker.html">
<link rel="import" href="../paper-icon-button/paper-icon-button.html">
<link rel="import" href="../paper-input/paper-input.html">
<link rel="import" href="../magazine-cover/magazine-cover.html">
<link rel="import" href="../paper-toast/paper-toast.html">
<link rel="import" href="../dropdown-select/dropdown-select.html">
<link rel="import" href="../paper-item/paper-item.html">
<link rel="import" href="../paper-dialog/paper-dialog.html">
<link rel="import" href="../paper-icon-picker/paper-icon-picker.html">
<link rel="import" href="../paper-dialog-scrollable/paper-dialog-scrollable.html">
<link rel="import" href="../paper-tooltip/paper-tooltip.html">
<link rel="import" href="../app-layout/app-header/app-header.html">
<link rel="import" href="../app-layout/app-toolbar/app-toolbar.html">
<link rel="import" href="../app-layout/app-drawer/app-drawer.html">
<link rel="import" href="../iron-icons/iron-icons.html">
<link rel="import" href="../iron-icons/editor-icons.html">
<link rel="import" href="../iron-icons/notification-icons.html">
<link rel="import" href="../iron-icons/av-icons.html">
<link rel="import" href="../iron-icons/device-icons.html">
<link rel="import" href="../iron-icons/image-icons.html">
<link rel="import" href="../sites-listing/sites-listing.html">
<!--
`haxcms-site-listing`
A LRN element
@demo demo/index.html
@microcopy - the mental model for this element
-
-
-->
<dom-module id="haxcms-site-listing">
<template>
<style>
app-toolbar {
background-color: #3a0063;
color: #ffffff;
}
paper-icon-button {
--paper-icon-button-ink-color: #ffffff;
}
paper-icon-button+[main-title] {
margin-left: 24px;
}
app-header {
@apply --layout-fixed-top;
color: #ffffff;
--app-header-background-rear-layer: {
background-color: #ef6c00;
}
}
app-drawer {
--app-drawer-scrim-background: rgba(0, 0, 0, 0.6);
--app-drawer-content-container: {
background-color: rgba(93, 0, 159, .8);
overflow: scroll;
color: #ffffff;
padding-left: 8px;
padding-right: 8px;
}
}
sites-listing {
display: block;
margin: 100px 16px 0px 16px;
}
paper-dialog {
width: 60%;
min-height: 50%;
top: 10%;
border-radius: 16px;
}
paper-input {
--paper-input-container-focus-color: #3a0063;
}
#closedrawer {
float: left;
margin: 12px -20px 0 8px;
}
#add {
float: right;
margin: 12px 8px 0 -20px;
}
#newsitecolor {
padding: 4px;
margin: 0;
display: inline-flex;
vertical-align: middle;
}
#newsitecolor > * {
display: inline-flex;
align-self: center;
margin-right: 0.5em;
--simple-colors-picker-preview-size: 1.25em;
}
</style>
<jwt-login id="jwt" url="[[basePath]]system/login.php" logout-url="[[basePath]]system/logout.php" jwt="{{jwt}}"></jwt-login>
<iron-ajax
id="createrequest"
method="POST"
body="[[createParams]]"
headers='{"Authorization": "Bearer [[jwt]]"}'
content-type="application/json"
url="[[basePath]]system/createNewSite.php"
handle-as="json"
on-response="handleCreateResponse"></iron-ajax>
<iron-ajax
id="downloadrequest"
method="POST"
body="[[downloadParams]]"
headers='{"Authorization": "Bearer [[jwt]]"}'
content-type="application/json"
url="[[basePath]]system/downloadSite.php"
handle-as="json"
on-response="handleDownloadResponse"></iron-ajax>
<sites-listing id="siteslisting" sites-response="{{__sitesResponse}}" sites="{{__sites}}" data-source="[[dataSource]]" edit-mode="[[editMode]]"></sites-listing>
<app-header reveals>
<app-toolbar>
<paper-icon-button icon="menu" onclick="drawer.toggle()"></paper-icon-button>
<div main-title>[[title]]</div>
<paper-icon-button id="edit" icon="[[__editIcon]]" hidden$="[[!loggedIn]]"></paper-icon-button>
<paper-icon-button id="login" icon="[[__loginIcon]]"></paper-icon-button>
<paper-tooltip for="login" position="bottom" offset="12" animation-delay="200">[[__loginText]]</paper-tooltip>
</app-toolbar>
</app-header>
<app-drawer id="drawer" swipe-open>
<paper-icon-button id="closedrawer" icon="icons:close" onclick="drawer.toggle()"></paper-icon-button>
<paper-tooltip for="closedrawer" position="bottom" offset="12" animation-delay="200">Close</paper-tooltip>
<paper-icon-button id="add" icon="icons:add"></paper-icon-button>
<paper-tooltip for="add" position="bottom" offset="12" animation-delay="200">Add site</paper-tooltip>
<h3 style="margin: 18px; padding: 6px; border-bottom: 1px solid #ffffff; text-align: center; font-weight: normal;">Site list</h3>
<map-menu id="mapmenu" items="{{outline}}" data="[[__sites]]" selected="{{activeItemID}}"></map-menu>
</app-drawer>
<paper-dialog id="newdialog" with-backdrop>
<h3>Create new site</h3>
<paper-dialog-scrollable>
<paper-input id="newsitetitle" label="Title" required autofocus value="{{siteTitle}}"></paper-input>
<paper-input id="domain" label="Domain" required value="[[domainName]]"></paper-input>
<paper-input id="newsitedescription" label="Description"></paper-input>
<paper-input id="newsiteimage" label="Image" value="[[activeItem.metadata.image]]"></paper-input>
<label for="newsitecolor">Select a color:</label>
<simple-colors-picker id="newsitecolor"></simple-colors-picker>
<dropdown-select id="newsitetheme" label="Theme" value="simple-blog">
<paper-item value="simple-blog">Simple blog</paper-item>
<paper-item value="outline-player">Basic outline</paper-item>
<paper-item value="haxcms-dev-theme">DEVELOPER THEME</paper-item>
<paper-item value="infinite-scroll">Infinite scroll</paper-item>
</dropdown-select>
<label for="newsiteicon">Select an icon:</label>
<paper-icon-picker id="newsiteicon" icon="[[activeItem.metadata.icon]]"></paper-icon-picker>
</paper-dialog-scrollable>
<div class="buttons">
<paper-button dialog-confirm id="create" raised>Let's go!</paper-button>
<paper-button dialog-dismiss>cancel</paper-button>
</div>
</paper-dialog>
<paper-dialog id="itemdialog" with-backdrop>
<paper-dialog-scrollable>
<magazine-cover
image="[[activeItem.metadata.image]]"
header="[[activeItem.title]]" subheader="[[activeItem.description]]"
action="Access site" icon="[[activeItem.metadata.icon]]" event-data="[[activeItem]]" event-name="haxcms-load-site">
</magazine-cover>
</paper-dialog-scrollable>
<div class="buttons">
<iron-icon icon="editor:format-color-fill" style$="color:[[activeItem.metadata.hexCode]]"></iron-icon>[[activeItem.metadata.hexCode]]
<iron-icon icon="av:web"></iron-icon>Theme: [[activeItem.metadata.theme]]
<paper-icon-button id="editsite" icon="icons:settings" style="color:black"></paper-icon-button>
<paper-tooltip for="edit" position="bottom" offset="12" animation-delay="200">change details</paper-tooltip>
<paper-icon-button id="archivesite" icon="icons:archive" dialog-dismiss style="color:grey"></paper-icon-button>
<paper-tooltip for="archivesite" position="top" offset="12" animation-delay="200">archive site</paper-tooltip>
<paper-icon-button id="download" icon="icons:file-download" style="color:black"></paper-icon-button>
<paper-tooltip for="download" position="top" offset="12" animation-delay="200">Download zip</paper-tooltip>
</div>
</paper-dialog>
<paper-toast id="toast"></paper-toast>
</template>
<script>
Polymer({
is: 'haxcms-site-listing',
listeners: {
'create.tap': '_createSite',
'download.tap': '_downloadSite',
'add.tap': '_addTap',
'edit.tap': '_editTap',
'mapmenu.selected': '_mapMenuSelection',
'newsitetheme.change': '_themeChanged',
'newsitecolor.change': '_colorChanged',
'login.tap': '_loginUserRoutine',
'jwt.jwt-logged-in': '_loginStateChange',
'siteslisting.item-overlay-option-selected': '_itemOptionSelected',
'siteslisting.item-overlay-op-changed': '_itemOpChanged',
},
behaviors: [
window.simpleColorsBehaviors,
],
properties: {
/**
* Title
*/
title: {
type: String,
value: 'Site list'
},
/**
* Site title
*/
siteTitle: {
type: String,
},
/**
* Domain name
*/
domainName: {
type: String,
computed: '_computeDomainName(siteTitle)',
},
/**
* Sites response change
*/
__sitesResponse: {
type: String,
observer: '__sitesResponseChanged',
},
/**
* Base path of where this is located.
*/
basePath: {
type: String,
},
/**
* Data Source to power the loading of sites in JSON Outline Schema format.
*/
dataSource: {
type: String,
},
/**
* JSON Web token
*/
jwt: {
type: String,
observer: '_jwtChanged',
},
/**
* Request params for creating a new site
*/
createParams: {
type: Object,
value: {},
},
/**
* Request params for downloading a new site
*/
downloadParams: {
type: Object,
value: {},
},
/**
* Active item that's being reviewed / has bubbled up.
*/
activeItem: {
type: Object,
notify: true,
observer: '_activeItemChanged',
},
/**
* Logged in state
*/
loggedIn: {
type: Boolean,
notify: true,
reflectToAttribute: true,
observer: '_loginStateChanged',
},
/**
* Edit mode
*/
editMode: {
type: Boolean,
notify: true,
reflectToAttribute: true,
value: false,
observer: '_editModeChanged',
},
},
/**
* Generate domain from title
*/
_computeDomainName: function (title) {
return 'https://' + title.replace(/[^a-zA-Z0-9]/g, '').toLowerCase() + '.surge.sh';
},
/**
* Site response has changed.
*/
__sitesResponseChanged: function (newValue, oldValue) {
if (newValue) {
this.title = newValue.title;
setTimeout(() => {
if (this.jwt) {
this.$.siteslisting.$.list.querySelector('[data-site-id="item-new"]').hidden = false;
this.$.mapmenu.$.builder.$$('#item-new').hidden = false;
this.$.add.hidden = false;
}
else {
this.$.siteslisting.$.list.querySelector('[data-site-id="item-new"]').hidden = true;
this.$.mapmenu.$.builder.$$('#item-new').hidden = true;
this.$.add.hidden = true;
}
}, 100);
}
},
/**
* Request a user login if we need one or log out
*/
_loginStateChange: function(e) {
if (e.detail) {
this.loggedIn = true;
}
else {
this.loggedIn = false;
}
},
/**
* Request a user login if we need one or log out
*/
_editModeChanged: function(newValue, oldValue) {
if (newValue) {
this.__editIcon = 'icons:check';
}
else {
this.__editIcon = 'icons:create';
}
},
/**
* Login state changed
*/
_loginStateChanged: function (newValue, oldValue) {
if (newValue) {
this.__loginText = 'Log out';
this.__loginIcon = 'icons:account-circle';
this.$.toast.show('Welcome, log in successful!');
this.$.add.hidden = false;
this.$.siteslisting.$.list.querySelector('[data-site-id="item-new"]').hidden = false;
this.$.mapmenu.$.builder.$$('#item-new').hidden = false;
}
else {
this.__loginText = 'Log in';
this.__loginIcon = 'icons:power-settings-new';
this.$.toast.show('You logged out');
this.$.add.hidden = true;
this.$.siteslisting.$.list.querySelector('[data-site-id="item-new"]').hidden = true;
this.$.mapmenu.$.builder.$$('#item-new').hidden = true;
}
},
/**
* Request a user login if we need one or log out
*/
_jwtChanged: function (newValue, oldValue) {
if (newValue) {
this.__loginText = 'Log out';
this.__loginIcon = 'icons:account-circle';
}
else {
this.__loginText = 'Log in';
this.__loginIcon = 'icons:power-settings-new';
}
},
/**
* Request a user login if we need one or log out
*/
_loginUserRoutine: function (e) {
this.$.jwt.toggleLogin();
},
/**
* Use events for real value in theme.
*/
_themeChanged: function (e) {
this.set('activeItem.metadata.theme', e.detail.value);
},
/**
* Use events for real value in color area.
*/
_colorChanged: function (e) {
this.set('activeItem.metadata.cssVariable', e.detail.cssVariable);
this.set('activeItem.metadata.hexCode', e.detail.hexCode);
},
/**
* Add button clicked, trick DOM into clicking the add new site item.
*/
_addTap: function (e) {
this.$.siteslisting.$.list.querySelector('[data-site-id="item-new"]').click();
},
/**
* Toggle edit state
*/
_editTap: function (e) {
this.editMode = !this.editMode;
},
/**
* User clicked on the flyout menu, set that item to active.
*/
_mapMenuSelection: function (e) {
// run through available sites and find the matching ID
let findSite = this.__sites.filter(site => {
if (site.id !== e.detail) {
return false;
}
return true;
});
// if we found one, make it the top level item
if (findSite.length > 0) {
this.set('activeItem', {});
this.set('activeItem', findSite.pop());
}
},
/**
* Selected item has changed
*/
_activeItemChanged: function (newValue, oldvalue) {
if (typeof newValue !== typeof undefined && typeof newValue.id !== typeof undefined) {
// for new items we need the new item form
if (newValue.id === 'item-new') {
this.$.newdialog.opened = true;
this.$.itemdialog.opened = false;
}
else {
this.$.newdialog.opened = false;
this.$.itemdialog.opened = true;
}
}
// it's possible to have NO item in scope, ensure everything closes :)
else if (typeof newValue !== typeof undefined && typeof newValue.id !== typeof undefined) {
this.$.newdialog.opened = false;
this.$.itemdialog.opened = false;
this.$.drawer.opened = false;
}
},
/**
* Attached life cycle
*/
attached: function () {
// @todo support state routing for loadActiveSite
document.body.addEventListener('sites-listing-item-selected', this._itemSelected.bind(this));
document.body.addEventListener('haxcms-load-site', this.loadActiveSite.bind(this));
},
/**
* Ready life cycle
*/
ready: function () {
// set jwt from local storage bin
this.jwt = localStorage.getItem('jwt');
},
/**
* Simple method of loading whatever's been dictated as active.
*/
loadActiveSite: function (e) {
let findSite = this.__sites.filter(site => {
if (site.id !== e.detail.id) {
return false;
}
return true;
});
let item = findSite.pop();
// if location isn't there, push out to it
if (item.location) {
window.open(item.location);
}
else {
window.open('/_sites/' + item.metadata.siteName + '/index.html');
}
},
/**
* New button clicked, open modal
*/
_itemSelected: function (e) {
this.set('activeItem', {});
this.set('activeItem', e.detail);
},
/**
* Create a new site button was clicked
*/
_createSite: function (e) {
// ship off a new call
this.set('createParams.domain', this.$.domain.value);
this.set('createParams.siteName', this.$.newsitetitle.value);
this.set('createParams.description', this.$.newsitedescription.value);
// need to pull this from the active item bc of data binding silly
this.set('createParams.theme', this.activeItem.metadata.theme);
this.set('createParams.hexCode', this.activeItem.metadata.hexCode);
this.set('createParams.cssVariable', this.activeItem.metadata.cssVariable);
this.set('createParams.image', this.$.newsiteimage.value);
this.set('createParams.icon', this.$.newsiteicon.icon);
// pass along the jwt for user "session" purposes
this.set('createParams.jwt', this.jwt);
this.$.newsitetitle.value = '';
this.$.newsitedescription.value = null;
this.$.createrequest.generateRequest();
},
/**
* Download a new site button was clicked
*/
_downloadSite: function (e) {
// ship off a new call
this.set('downloadParams.siteName', this.activeItem.metadata.siteName);
// pass along the jwt for user "session" purposes
this.set('downloadParams.jwt', this.jwt);
this.$.downloadrequest.generateRequest();
},
/**
* Create a new site button was clicked
*/
handleCreateResponse: function (e) {
// update the listing data
this.fire('update-sites-listing-data', e.detail.response);
this.$.toast.show(e.detail.response.title + ' created successfully!');
},
/**
* Download a site
*/
handleDownloadResponse: function (e) {
// fire incase anyone cares
this.fire('download-site-listing', e.detail.response);
var element = document.createElement('a');
element.setAttribute('href', e.detail.response.link);
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
this.$.toast.show(this.activeItem.title + ' downloaded successfully!');
},
/**
* Option selected in an operation in context
*/
_itemOptionSelected: function (e) {
var element = e.detail.element;
switch (e.detail.operation) {
case 'remove':
if (e.detail.option === 'option1') {
this.$.toast.show('Deleting this');
// @todo send call out the door to delete callback
}
break;
case 'duplicate':
if (e.detail.option === 'option1') {
this.$.toast.show('Duplicating this');
// @todo send call out the door to duplicate callback
}
break;
case 'move':
if (e.detail.option === 'option1') {
this.$.toast.show('Move this item left');
}
else {
this.$.toast.show('Move this item right');
}
// @todo send call out the door to commit the move callback
break;
}
},
/**
* Selected operation changed; we use this for edit mode detection
*/
_itemOpChanged: function (e) {
var element = e.detail.element;
if (e.detail.operation === 'edit') {
let findSite = this.__sites.filter(site => {
if (site.id !== element.getAttribute('data-site-id')) {
return false;
}
return true;
});
// if we found one, make it the top level item
if (findSite.length > 0) {
this.set('activeItem', {});
this.set('activeItem', findSite.pop());
// simulate clicking on the edit button that's in this tab
this.$.edit.click();
}
}
},
});
</script>
</dom-module>