~tychi/code-mirror-shield

code-mirror-shield/public/hello-universe.html -rw-r--r-- 7.5 KiB
176bdc62Tyler Childs feature: add todo list example 10 months ago
                                                                                
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
<!doctype html>
<html lang="">
<head>
  <meta charset="utf-8">
  <title>Hello Universe</title>
  <meta name="description" content="">
  <meta name="viewport" content="width=device-width, initial-scale=1">

  <!-- define critial styles inline the head of the document -->
  <style type="text/css">
    html {
      height: 100%;
      font-family: sans-serif;
      text-align: center;
    }

    body {
      color: cornsilk;
      background: radial-gradient(
        indigo,
        darkslateblue,
        darkslategrey,
        black
      ) no-repeat;
      overflow: hidden;
      height: 100%;
    }
  </style>
</head>

<body>
  <h1 style="margin: 0;">Hello Universe</h1>
  <!-- optionally set a zero state in between the tags -->
  <hello-universe>
    Application is loading...
  </hello-universe>

  <script type="module">
    import tag from 'https://thelanding.page/tag/tag.bundle.js'

    const {
      html,
      get,
      on,
      set,
      css
    } = tag('hello-universe', {
      // set an initial array of planets
      'planets': [
        {
          'id': 'mercury',
          'name': 'Mercury',
          'background': 'sienna'
        },
        {
          'id': 'venus',
          'name': 'Venus',
          'background': 'darkgoldenrod'
        },
        {
          'id': 'Earth',
          'name': 'Earth',
          'background': 'deepskyblue'
        },
        {
          'id': 'mars',
          'name': 'Mars',
          'background': 'orangered'
        },
        {
          'id': 'jupiter',
          'name': 'Jupiter',
          'background': 'lightsalmon'
        },
        {
          'id': 'saturn',
          'name': 'Saturn',
          'background': 'bisque'
        },
        {
          'id': 'neptune',
          'name': 'Neptune',
          'background': 'dodgerblue'
        },
        {
          'id': 'uranus',
          'name': 'Uranus',
          'background': 'darkturquoise'
        }
      ],
      // an incrementable integer for looping through planets
      'tick': 0,
      // a boolean value to control visibility of the admin view
      'showAdmin': false
    })

    // increase our tick twice a second (once every 500 milliseconds)
    setInterval(function tick() {
      const { tick } = get()
      set({ tick: tick + 1 })
    }, 500)

    html(() => {
      // pluck our current values from our current state
      const { planets, tick, showAdmin } = get()

      // get the current planet by modulo of current tick by # of planets
      const current = planets[tick % planets.length]

      // save html for displaying the current planet
      const view = `
        <button class="admin-toggle">
          Manage Planets
        </button>
        <planet style="--planet-background: ${current.background}">
          <greeting>Hello ${current.name}</greeting>
        </planet>
      `

      // save html for displaying the admin view
      const admin = `
        <button class="admin-toggle">
          View Planets
        </button>
        <admin>
          ${renderAdministrationTable(planets)}
          <button class="add-planet">Add Planet</button>
        </admin>
      `

      // determine which html is visible
      return showAdmin ? admin : view
    })

    // toggle the showAdmin variable on button click
    on('click', 'button.admin-toggle', () => {
      const showAdmin = ! get().showAdmin
      set({ showAdmin })
    })

    // create a random planet with the current tick as a seed
    on('click', 'button.add-planet', () => {
      const { tick } = get()
      set({
        id: `planet-${tick}`,
        name: `Planet ${tick}`,
        background: 'cornsilk'
      }, addPlanet)
    })

    // surgically add a new planet to our tag's state
    function addPlanet(state, payload) {
      return {
        ...state,
        planets: [
          ...state.planets,
          payload
        ]
      }
    }

    // update planet information when input values have been changed
    on('keyup', 'admin input', (event) => {
      const { value, dataset } = event.target
      const { id, attribute } = dataset

      set({
        id,
        attribute,
        value
      }, updatePlanet)
    })

    // return the index of the planet with a given id
    function getPlanetIndexById(id) {
      const index = get().planets
        .findIndex(x => x.id === id)

      return index
    }

    // surgically update planet information in our tag's state
    function updatePlanet(state, payload){
      const { id, attribute, value } = payload

      const index = getPlanetIndexById(id)
      const planet = state.planets[index]

      return {
        ...state,
        planets: [
          ...state.planets.slice(0, index),
          { ...planet, [attribute]: value },
          ...state.planets.slice(index + 1)
        ]
      }
    }

    // loop over the planets array to create a complex form to manage values
    function renderAdministrationTable(planets) {
      const rows = planets.map(x => `
        <input type="text" data-id="${x.id}"
          value="${x.id}" data-attribute="id" />
        <input type="text" data-id="${x.id}"
          value="${x.name}" data-attribute="name" />
        <input type="text" data-id="${x.id}"
          value="${x.background}" data-attribute="background" />
      `).join('')

      return `
        <admin-table>
          <cell-head>ID</cell-head>
          <cell-head>Name</cell-head>
          <cell-head>Background</cell-head>
        </admin-table>
        <admin-table>
          ${rows}
        </admin-table>
      `
    }

    // create scoped rulesets for all of our content
    css(`
      & {
        display: block;
        margin: 0 auto;
      }

      & button {
        background: rgba(255, 255, 255, .75);
        border: 1px solid;
        border-color:
          rgba(255, 255, 255, .5)
          rgba(0, 0, 0, .75)
          rgba(0, 0, 0, .75)
          rgba(0, 0, 0, .75);
        border-radius: 2px;
        font-size: 1rem;
        font-weight: bold;
        padding: .5rem;
      }

      & button:hover {
        background: rgba(255, 255, 255, .5);
        border-color:
          rgba(0, 0, 0, .75)
          rgba(255, 255, 255, .5)
          rgba(255, 255, 255, .5)
          rgba(255, 255, 255, .5);
        cursor: pointer;
      }

      & planet {
        background: var(--planet-background, cornsilk);
        border-radius: 100%;
        display: grid;
        grid-template-areas: 'greeting';
        height: 75vmin;
        margin: 0 auto;
        mix-blend-mode: lighten;
        position: relative;
        width: 75vmin;
      }

      & planet greeting {
        background: cornsilk;
        border-radius: 100%;
        color: darkslategray;
        grid-area: greeting;
        place-self: center;
        font-size: 5vmin;
        padding: 3rem;
      }

      & admin {
        background: rgba(0, 0, 0, .85);
        border-radius: 1rem;
        display: block;
        margin: 0 auto;
        max-width: 500px;
        padding: 2rem 1rem;
      }

      & admin-table {
        display: grid;
        grid-template-columns: repeat(3, minmax(80px, 1fr));
        gap: 10px;
        max-height: 40vh;
        overflow-y: auto;
        margin-bottom: 10px;
      }

      & cell-head {
        font-weight: bold;
      }

      & input {
        background: rgba(33, 33, 33, .85);
        border: 1px solid rgba(255, 255, 255, .2);
        border-radius: 2px;
        color: cornsilk;
        height: 1.5rem;
      }

      & .admin-toggle {
        margin: 1rem;
      }

      & .add-planet {
        grid-column: 1 / -1;
      }
    `)
  </script>
</body>

</html>