|
| 1 | +import { html, css } from 'https://cdn.jsdelivr.net/gh/lit/dist@3/all/lit-all.min.js'; |
| 2 | +import 'https://cdn.jsdelivr.net/npm/[email protected]/dist/axios.min.js'; |
| 3 | +import { StyledLitElement } from '/ux/StyledLitElement.mjs'; |
| 4 | +import './list-tile.mjs'; |
| 5 | + |
| 6 | +class ConfigList extends StyledLitElement { |
| 7 | + constructor() { |
| 8 | + super(); |
| 9 | + this.layers = []; |
| 10 | + this.topo = []; |
| 11 | + this.loadData(); |
| 12 | + this.message = this.readCookie('message'); |
| 13 | + this.eraseCookie('message'); |
| 14 | + this.addName = ''; |
| 15 | + } |
| 16 | + |
| 17 | + async loadData() { |
| 18 | + Promise.all([ |
| 19 | + axios.get('/api/config/layers'), |
| 20 | + axios.get('/api/config/topo'), |
| 21 | + axios.get('/api/config/default') |
| 22 | + ]) |
| 23 | + .then(responses => { |
| 24 | + this.layers = responses[0]?.data; |
| 25 | + this.layers.unshift('default'); |
| 26 | + this.topo = responses[1]?.data; |
| 27 | + this.requestUpdate(); |
| 28 | + }) |
| 29 | + .catch(error => { |
| 30 | + console.error('Error fetching data:', error); |
| 31 | + }); |
| 32 | + } |
| 33 | + |
| 34 | + readCookie(name) { |
| 35 | + const cookies = document.cookie.split(';'); |
| 36 | + for (let i = 0; i < cookies.length; i++) { |
| 37 | + const cookie = cookies[i].trim(); |
| 38 | + if (cookie.startsWith(name + '=')) { |
| 39 | + return cookie.substring(name.length + 1); |
| 40 | + } |
| 41 | + } |
| 42 | + return ''; |
| 43 | + } |
| 44 | + |
| 45 | + eraseCookie(name) { |
| 46 | + document.cookie = name + '=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;'; |
| 47 | + } |
| 48 | + |
| 49 | + updateName(e) { |
| 50 | + this.addName = e.target.value; |
| 51 | + } |
| 52 | + |
| 53 | + addLayer() { |
| 54 | + // get a name |
| 55 | + const v = this.addName; |
| 56 | + if (v === '') { |
| 57 | + alert('Error: Layer name cannot be empty'); |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + axios.post('/api/config/addlayer', { name: v }) |
| 62 | + .then(response => { |
| 63 | + window.location.href = '/config/edit.html?layer=' + v; |
| 64 | + }) |
| 65 | + .catch(error => { |
| 66 | + alert('Error adding layer:', error); |
| 67 | + }); |
| 68 | + } |
| 69 | + |
| 70 | + render() { |
| 71 | + return html` |
| 72 | + <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous"> |
| 73 | +
|
| 74 | + ${this.message ? html`<div class="alert">${this.message}</div>` : ''} |
| 75 | +
|
| 76 | + <ul class="pt-3"> |
| 77 | + ${this.layers.map((layer, index) => html` |
| 78 | + <list-tile id=${index} |
| 79 | + .layerName=${layer} |
| 80 | + .layerNodes=${this.topo.filter(topo => topo.LayersCSV.split(",").includes(layer))}></list-tile> |
| 81 | + `)} |
| 82 | + </ul> |
| 83 | + <div class="add-layer"> |
| 84 | + <input autofocus type="text" id="layername" placeholder="Layer Name" @change=${this.updateName}> |
| 85 | + <button class="btn" @click=${this.addLayer}>Add Layer</button> |
| 86 | + </div> |
| 87 | +
|
| 88 | + <hr> |
| 89 | + <span> |
| 90 | + To delete a layer, use ysqlsh to issue the following command:<br> |
| 91 | + <code lang=sql>DELETE FROM curio.harmony_config WHERE title = 'my_layer_name';</code> |
| 92 | + </span> |
| 93 | + ` |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +ConfigList.styles = css` |
| 98 | + .alert { |
| 99 | + font-size: 24px; |
| 100 | + display: inline-block; |
| 101 | + color: green; |
| 102 | + } |
| 103 | +
|
| 104 | + .add-layer { |
| 105 | + display: grid; |
| 106 | + grid-template-columns: 1fr max-content; |
| 107 | + grid-column-gap: 0.75rem; |
| 108 | + margin-bottom: 1rem; |
| 109 | + } |
| 110 | +
|
| 111 | + .btn { |
| 112 | + padding: 0.4rem 1rem; |
| 113 | + border: none; |
| 114 | + border-radius: 0; |
| 115 | + background-color: var(--color-form-default); |
| 116 | + color: var(--color-text-primary); |
| 117 | +
|
| 118 | + &:hover, &:focus, &:focus-visible { |
| 119 | + background-color: var(--color-form-default-pressed); |
| 120 | + color: var(--color-text-secondary); |
| 121 | + } |
| 122 | + } |
| 123 | +`; |
| 124 | + |
| 125 | +customElements.define('config-list', ConfigList) |
0 commit comments