/
Kanban.js
718 lines (589 loc) · 19.3 KB
/
Kanban.js
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
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
var observableAttributes = [
// The value of the cell that the plugin is being rendered in
"cellvalue",
// The value of the row that the plugin is being rendered in
"rowvalue",
// The value of the table that the plugin is being rendered in
"tablevalue",
// The schema of the table that the plugin is being rendered in
"tableschemavalue",
// The schema of the database that the plugin is being rendered in
"databaseschemavalue",
// The configuration object that the user specified when installing the plugin
"configuration",
// Additional information about the view such as count, page and offset.
"metadata",
];
var OuterbaseEvent = {
// The user has triggered an action to save updates
onSave: "onSave",
};
var OuterbaseColumnEvent = {
// The user has began editing the selected cell
onEdit: "onEdit",
// Stops editing a cells editor popup view and accept the changes
onStopEdit: "onStopEdit",
// Stops editing a cells editor popup view and prevent persisting the changes
onCancelEdit: "onCancelEdit",
// Updates the cells value with the provided value
updateCell: "updateCell",
};
var OuterbaseTableEvent = {
// Updates the value of a row with the provided JSON value
updateRow: "updateRow",
// Deletes an entire row with the provided JSON value
deleteRow: "deleteRow",
// Creates a new row with the provided JSON value
createRow: "createRow",
// Performs an action to get the next page of results, if they exist
getNextPage: "getNextPage",
// Performs an action to get the previous page of results, if they exist
getPreviousPage: "getPreviousPage",
};
/**
* ******************
* Custom Definitions
* ******************
*
* ░░░░░░░░░░░░░░░░░
* ░░░░▄▄████▄▄░░░░░
* ░░░██████████░░░░
* ░░░██▄▄██▄▄██░░░░
* ░░░░▄▀▄▀▀▄▀▄░░░░░
* ░░░▀░░░░░░░░▀░░░░
* ░░░░░░░░░░░░░░░░░
*
* Define your custom classes here. We do recommend the usage of our `OuterbasePluginConfig_$PLUGIN_ID`
* class for you to manage properties between the other classes below, however, it's strictly optional.
* However, this would be a good class to contain the properties you need to store when a user installs
* or configures your plugin.
*/
class OuterbasePluginConfig_$PLUGIN_ID {
// Inputs from Outerbase for us to retain
tableValue = undefined;
count = 0;
page = 1;
offset = 50;
theme = "light";
// Inputs from the configuration screen
firstValue = undefined;
secondValue = undefined;
thirdValue = undefined;
//
// Variables for us to hold state of user actions
deletedRows = [];
constructor(object) {
this.firstValue = object?.firstValue;
this.secondValue = object?.secondValue;
this.thirdValue = object?.thirdValue;
//
//this.data = object?.data;
}
toJSON() {
return {
firstValue: this.firstValue,
secondValue: this.secondValue,
thirdValue: this.thirdValue,
data: this.data,
};
}
}
var triggerEvent = (fromClass, data) => {
const event = new CustomEvent("custom-change", {
detail: data,
bubbles: true,
composed: true,
});
fromClass.dispatchEvent(event);
};
var decodeAttributeByName = (fromClass, name) => {
if (fromClass.getAttribute(name)) {
const encodedJSON = fromClass.getAttribute(name);
if (encodedJSON && encodedJSON !== "undefined") {
const decodedJSON = encodedJSON
?.replace(/"/g, '"')
?.replace(/'/g, "'");
return decodedJSON ? JSON.parse(decodedJSON) : {};
}
}
};
/**
* **********
* Table View
* **********
*
* ░░░░░░░░░░░░░░░░░░
*
* ░░░░░▄▄████▄▄░░░░░
* ░░░▄██████████▄░░░
* ░▄██▄██▄██▄██▄██▄░
* ░░░▀█▀░░▀▀░░▀█▀░░░
* ░░░░░░░░░░░░░░░░░░
* ░░░░░░░░░░░░░░░░░░
*/
var templateTable = document.createElement("template");
templateTable.innerHTML = `
<style>
.container {
width: 70%;
min-width: 50%;
margin: auto;
display: flex;
flex-direction: column;
overflow:scroll
overflow-y:scroll
}
#theme-container{
overflow:scroll
}
#kanban-config>div{
display:flex;
width:80%;
flex-wrap:wrap;
}
.task {
background-color: white;
margin: 0.2rem 0rem 0.3rem 0rem;
border: 0.1rem solid black;
border-radius: 0.2rem;
padding: 0.5rem 0.2rem 0.5rem 2rem;
}
#task-button {
margin: 0.2rem 0rem 0.1rem 0rem;
background-color: white;
border-radius: 0.2rem;
width: 100%;
border: 0.25rem solid black;
padding: 0.5rem 2.7rem;
border-radius: 0.3rem;
font-size: 1rem;
}
/* */
/* ---- RESET/BASIC STYLING ---- */
* {
padding: 0;
margin: 0;
box-sizing: border-box;
font-family: sans-serif;
-ms-overflow-style: none; /* IE and Edge */
scrollbar-width: none; /* Firefox */
}
.board {
width: 100%;
height: 100vh;
overflow: scroll;
background-image: url(https://images.unsplash.com/photo-1519681393784-d120267933ba?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1470&q=80);
background-position: center;
background-size: cover;
}
/* ---- BOARD ---- */
.kanban {
display: flex;
align-items: flex-start;
justify-content: start;
gap: 16px;
padding: 24px 32px;
overflow: scroll;
height: 500px;
}
.heading {
font-size: 22px;
font-weight: bold;
margin-bottom: 8px;
}
.board {
display: flex;
flex-direction: column;
gap: 12px;
background: #f4f4f4;
box-shadow: 0px 5px 15px rgba(0, 0, 0, 0.25);
padding: 12px;
border-radius: 4px;
width: 225px;
min-height: 120px;
height:100%
flex-wrap:wrap;
flex-shrink: 0;
}
.task {
background: white;
color: black;
box-shadow: 0px 5px 15px rgba(0, 0, 0, 0.15);
padding: 12px;
border-radius: 4px;
word-wrap: break-word;
font-size: 16px;
cursor: move;
}
.is-dragging {
scale: 1.05;
box-shadow: 0px 5px 15px rgba(0, 0, 0, 0.25);
background: rgb(50, 50, 50);
color: white;
}
</style>
<div id="theme-container">
<div id="container">
</div>
</div>
`;
// Can the above div just be a self closing container: <div />
class OuterbasePluginTable_$PLUGIN_ID extends HTMLElement {
static get observedAttributes() {
return observableAttributes;
}
config = new OuterbasePluginConfig_$PLUGIN_ID({});
constructor() {
super();
this.shadow = this.attachShadow({ mode: "open" });
this.shadow.appendChild(templateTable.content.cloneNode(true));
}
connectedCallback() {
this.render();
}
attributeChangedCallback(name, oldValue, newValue) {
this.config = new OuterbasePluginConfig_$PLUGIN_ID(
decodeAttributeByName(this, "configuration")
);
this.config.tableValue = decodeAttributeByName(this, "tableValue");
//console.log(this.config.tableValue);
// console.log(this.config.data);
this.config.theme = decodeAttributeByName(this, "metadata").theme;
var element = this.shadow.getElementById("theme-container");
element.classList.remove("dark");
element.classList.add(this.config.theme);
this.render();
}
render() {
this.shadow.querySelector("#container").innerHTML = `
<div class="kanban">
<div class="board" id="todo-lane">
<h3 class="heading" contentEditable="true">Todo</h3>
${
this.config?.tableValue?.length &&
this.config?.tableValue
?.map(
(row) =>
`<div class="task" draggable="true">
<div><span style="font-size:15px;font-weight:bold;">${
this.config.firstValue
}:</span>${row[this.config.firstValue]}</div>
<div><span style="font-size:15px;font-weight:bold;">${
this.config.secondValue
}:</span>${row[this.config.secondValue]}</div>
<div><span style="font-size:15px;font-weight:bold;">${
this.config.thirdValue
}:</span>${row[this.config.thirdValue]}</div>
</div>`
)
.join(" ")
}
</div>
<div class="board">
<h3 class="heading" contentEditable="true">Doing</h3>
</div>
<div class="board">
<h3 class="heading" contentEditable="true">Done</h3>
</div>
</div>
`;
const draggables = this.shadow.querySelectorAll(".task");
const droppables = this.shadow.querySelectorAll(".board");
draggables.forEach((task) => {
task.addEventListener("dragstart", () => {
task.classList.add("is-dragging");
});
task.addEventListener("dragend", () => {
task.classList.remove("is-dragging");
});
});
droppables.forEach((zone) => {
zone.addEventListener("dragover", (e) => {
e.preventDefault();
const bottomTask = insertAboveTask(zone, e.clientY);
const curTask = this.shadow.querySelector(".is-dragging");
if (!bottomTask) {
zone.appendChild(curTask);
} else {
zone.insertBefore(curTask, bottomTask);
}
});
});
const insertAboveTask = (zone, mouseY) => {
const els = zone.querySelectorAll(".task:not(.is-dragging)");
let closestTask = null;
let closestOffset = Number.NEGATIVE_INFINITY;
els.forEach((task) => {
const { top } = task.getBoundingClientRect();
const offset = mouseY - top;
if (offset < 0 && offset > closestOffset) {
closestOffset = offset;
closestTask = task;
}
});
return closestTask;
};
const deleteRowButtons = this.shadow.querySelectorAll(".deleteRowButton");
deleteRowButtons.forEach((btn, index) => {
btn.addEventListener("click", () => {
let row = this.config.tableValue[index];
triggerEvent(this, {
action: OuterbaseTableEvent.deleteRow,
value: row,
});
this.config.deletedRows.push(row);
this.config.tableValue.splice(index, 1);
this.render();
});
});
var previousPageButton = this.shadow.getElementById("previousPageButton");
previousPageButton.addEventListener("click", () => {
triggerEvent(this, {
action: OuterbaseTableEvent.getPreviousPage,
value: {},
});
});
var nextPageButton = this.shadow.getElementById("nextPageButton");
nextPageButton.addEventListener("click", () => {
triggerEvent(this, {
action: OuterbaseTableEvent.getNextPage,
value: {},
});
});
}
}
/**
* ******************
* Configuration View
* ******************
*
* ░░░░░░░░░░░░░░░░░
* ░░░░░▀▄░░░▄▀░░░░░
* ░░░░▄█▀███▀█▄░░░░
* ░░░█▀███████▀█░░░
* ░░░█░█▀▀▀▀▀█░█░░░
* ░░░░░░▀▀░▀▀░░░░░░
* ░░░░░░░░░░░░░░░░░
*
* When a user either installs a plugin onto a table resource for the first time
* or they configure an existing installation, this is the view that is presented
* to the user. For many plugin applications it's essential to capture information
* that is required to allow your plugin to work correctly and this is the best
* place to do it.
*
* It is a requirement that a save button that triggers the `OuterbaseEvent.onSave`
* event exists so Outerbase can complete the installation or preference update
* action.
*/
var templateConfiguration = document.createElement("template");
templateConfiguration.innerHTML = `
<style>
#configuration-container {
display: flex;
height: 100%;
overflow-y: scroll;
padding: 40px 50px 65px 40px;
}
.field-title {
font: "Inter", sans-serif;
font-size: 12px;
line-height: 18px;
font-weight: 500;
margin: 0 0 8px 0;
}
select {
width: 320px;
height: 40px;
margin-bottom: 16px;
background: transparent;
border: 1px solid #343438;
border-radius: 8px;
color: black;
font-size: 14px;
padding: 0 8px;
cursor: pointer;
background-image: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" height="28" viewBox="0 -960 960 960" width="32"><path fill="black" d="M480-380 276-584l16-16 188 188 188-188 16 16-204 204Z"/></svg>');
background-position: 100%;
background-repeat: no-repeat;
appearance: none;
-webkit-appearance: none !important;
-moz-appearance: none !important;
}
input {
width: 320px;
height: 40px;
margin-bottom: 16px;
background: transparent;
border: 1px solid #343438;
border-radius: 8px;
color: black;
font-size: 14px;
padding: 0 8px;
}
button {
border: none;
background-color: #834FF8;
color: white;
padding: 6px 18px;
font: "Inter", sans-serif;
font-size: 14px;
line-height: 18px;
border-radius: 8px;
cursor: pointer;
}
.preview-card {
margin-left: 80px;
width: 240px;
background-color: white;
border-radius: 16px;
overflow: hidden;
}
.preview-card > img {
width: 100%;
height: 165px;
}
.preview-card > div {
padding: 16px;
display: flex;
flex-direction: column;
color: black;
}
.preview-card > div > p {
margin: 0;
}
.dark {
#configuration-container {
background-color: black;
color: white;
}
}
.dark > div > div> input {
color: white !important;
}
.dark > div > div> select {
color: white !important;
background-image: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" height="28" viewBox="0 -960 960 960" width="32"><path fill="white" d="M480-380 276-584l16-16 188 188 188-188 16 16-204 204Z"/></svg>');
}
</style>
<div id="theme-container">
<div id="configuration-container">
</div>
</div>
`;
// Can the above div just be a self closing container: <div />
class OuterbasePluginTableConfiguration_$PLUGIN_ID extends HTMLElement {
static get observedAttributes() {
return observableAttributes;
}
config = new OuterbasePluginConfig_$PLUGIN_ID({});
constructor() {
super();
this.shadow = this.attachShadow({ mode: "open" });
this.shadow.appendChild(templateConfiguration.content.cloneNode(true));
}
connectedCallback() {
this.render();
}
attributeChangedCallback(name, oldValue, newValue) {
console.log(this);
this.config = new OuterbasePluginConfig_$PLUGIN_ID(
decodeAttributeByName(this, "configuration")
);
this.config.tableValue = decodeAttributeByName(this, "tableValue");
console.log(this.config.tableValue);
this.config.theme = decodeAttributeByName(this, "metadata").theme;
var element = this.shadow.getElementById("theme-container");
element.classList.remove("dark");
element.classList.add(this.config.theme);
this.render();
}
render() {
let sample = this.config.tableValue.length ? this.config.tableValue[0] : {};
let keys = Object.keys(sample);
console.log(keys);
// let newData = this.config.data;
if (
!keys ||
keys.length === 0 ||
!this.shadow.querySelector("#configuration-container")
)
return;
this.shadow.querySelector("#configuration-container").innerHTML =
`
<div style="flex: 1;">
<p class="field-title">First Value</p>
<select id="firstValue">
` +
keys
.map(
(key) =>
`<option value="${key}" ${
key === this.config.firstValue ? "selected" : ""
}>${key}</option>`
)
.join("") +
`
</select>
<p class="field-title">Second Value</p>
<select id="secondValue">
` +
keys
.map(
(key) =>
`<option value="${key}" ${
key === this.config.secondValue ? "selected" : ""
}>${key}</option>`
)
.join("") +
`
</select>
<p class="field-title">Third Value</p>
<select id="thirdValue">
` +
keys
.map(
(key) =>
`<option value="${key}" ${
key === this.config.thirdValue ? "selected" : ""
}>${key}</option>`
)
.join("") +
`
</select>
<div style="margin-top: 8px;">
<button id="saveButton">Save View</button>
</div>
</div>
`;
var saveButton = this.shadow.getElementById("saveButton");
saveButton.addEventListener("click", () => {
triggerEvent(this, {
action: OuterbaseEvent.onSave,
value: this.config.toJSON(),
});
});
var firstValue = this.shadow.getElementById("firstValue");
firstValue.addEventListener("change", () => {
this.config.firstValue = firstValue.value;
this.render();
});
var thirdValue = this.shadow.getElementById("thirdValue");
thirdValue.addEventListener("change", () => {
this.config.thirdValue = thirdValue.value;
this.render();
});
var secondValue = this.shadow.getElementById("secondValue");
secondValue.addEventListener("change", () => {
this.config.secondValue = secondValue.value;
this.render();
});
}
}
window.customElements.define(
"outerbase-plugin-table-$PLUGIN_ID",
OuterbasePluginTable_$PLUGIN_ID
);
window.customElements.define(
"outerbase-plugin-configuration-$PLUGIN_ID",
OuterbasePluginTableConfiguration_$PLUGIN_ID
);