summaryrefslogtreecommitdiff
path: root/plugins/vue/demo/components/chat.js
blob: 800ac4568f48e6cce9849ce717e72993a68123a8 (plain)
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
import { Name } from './name.js'

export default {

  components: { Name },

  props: ['tags', 'objects'],

  data: ()=> ({
    message: ''
  }),

  computed: {
    messages() {
      return this.objects
                 .filter(o=> 
                   'message' in o &&
                   'timestamp' in o &&
                   typeof o.message == 'string' &&
                   typeof o.timestamp == 'number')
                 .sortBy('timestamp')
    }
  },

  methods: {
    sendMessage() {
      if (!this.message) return
      this.$graffitiUpdate({
        message: this.message,
        timestamp: Date.now(),
        _tags: this.tags
      })
      this.message = ''
    }
  },

  template: `
    <ul v-for="object in messages">
      <li>
        <graffiti-objects :tags="[object._by]" v-slot="{objects}">
          <name :of="object._by" :objects="objects"></name>
        </graffiti-objects>:

        {{ object.message }}

        <template v-if="object._by==$graffitiID.value">
          <button @click="object.message+='!!';object._update()">
            ‼️
          </button>

          <button @click="object._remove()">
            ❌
          </button>
        </template>
      </li>
    </ul>

    <form @submit.prevent="sendMessage">
      <input v-model="message">
      <input type="submit" value="Submit"/>
    </form>`
}