GridEditorMagicSource.vue 7.73 KB
Newer Older
Anton's avatar
Intial  
Anton committed
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
<template lang="pug">

  q-list.default-source-container

    q-list-header
      q-input(
        v-model="term", type="textarea", :min-rows="1",
        :max-height="200", :style="{'line-height': '1.5em', width: '100%'}")

    q-item-separator

    template(v-if="results.length === 0")
      q-item This is the magic box, just enter:
      q-item ● A tag starting with a hash-symbol: #[span #my tag]
      q-item ● A URL pointing to a video, image or internal or external page
      q-item ● Short or long text

    // loading, nothing here
    template(v-if="loading")
      q-item
        q-item-side
          q-spinner(style="margin-right: 1em")
        q-item-main Loading videos for »{{term}}«
      q-item-separator
    template(v-else-if="nothingFound")
      q-item No Videos Found!

    // videos, annotations
    template(v-if="results")
      template(v-for="(result, i) in results")
        template(v-if="i > 0")
          q-item-separator
        q-item(draggable="true", @dragstart.native="event => {handleItemDragStart(event, result)}")
          q-item-side
            q-icon(:name="typeToIconName(result.body.type)", style="font-size: 1.8rem")
          q-item-main
            a(@click.prevent="event => {handleItemClick(event, result)}")
              template(v-if="result.body.source") {{result.body.source.substring(0, 100)}}
                template(v-if="result.body.source.length > 100") ...
              template(v-else-if="result.body.value") {{result.body.value.substring(0, 100)}}
                template(v-if="result.body.value.length > 100") ...
              template(v-else) Hm, no title?

</template>

<script>
  import url from 'url'
  import { ObjectUtil } from 'mbjs-utils'

  const hostToTypeMap = {
    'vimeo.com': 'Video',
    'www.vimeo.com': 'Video',
    'youtube.com': 'Video',
    'www.youtube.com': 'Video'
  }
  const typeToIconName = {
    'image': 'photo',
    'iframe': 'picture in picture',
    'internal-link': 'link',
    'video': 'local movies',
    'text': 'subject',
    'title': 'title'
  }
  const mimeTypesToAnnotationTypeMap = {
    'image/jpeg': 'Image',
    'image/png': 'Image',
    'image/gif': 'Image',
    'image/svg+xml': 'Image',
    'image/tiff': 'Image',
    'image/webp': 'Image',
    'application/pdf': 'PDF-Document',
    'video/webm': 'Video',
    'video/3gpp': 'Video',
    'video/3gpp2': 'Video',
    'video/x-msvideo': 'Video',
    'video/mpeg': 'Video',
    'video/mp4': 'Video',
    'video/ogg': 'Video',
    'audio/x-wav': 'Audio',
    'audio/webm': 'Audio',
    'audio/3gpp': 'Audio',
    'audio/3gpp2': 'Audio',
    'audio/aac': 'Audio',
    'audio/midi': 'Audio',
    'audio/ogg': 'Audio',
    'text/csv': 'Unknown',
    'text/calendar': 'Unknown',
    'application/json': 'Unknown',
    'application/rtf': 'Unknown',
    'application/xml': 'Unknown',
    'text/plain': 'IFrame',
    'text/html': 'IFrame',
    'application/xhtml+xml': 'IFrame'
  }

  export default {
    data () {
      return {
        term: '',
        results: [],
        nothingFound: false,
        loading: false
      }
    },
    watch: {
      term () {
        this.term = this.term.trim()
        this.handleSearchTerm()
      }
    },
    methods: {
      typeToIconName (type) {
        let iconName = typeToIconName[type.toLowerCase()]
        if (!iconName) {
          iconName = 'broken image'
        }
        return iconName
      },
      handleItemClick () {},
      handleItemDragStart (event, result) {
        let resourceCell = {
          uuid: null,
          type: result.body.type,
          x: 1,
          y: 1,
          width: 1,
          height: 1,
          content: result.body.source,
          sourceUuid: result.body.sourceUuid
        }
        event.dataTransfer.setData('text/plain', JSON.stringify(resourceCell))
      },
      handleSearchTerm () {
        const _this = this
        if (this.term.length > 0) {
          let res = {
            body: {
              source: this.term,
              purpose: 'linking',
              type: 'Undefined'
            }
          }
          let results = []
          // A URL
          if (/^[\s]*http[s]?:\/\/.+/.test(this.term)) {
            let sourceUrl = url.parse(this.term)
            let currentUrl = url.parse(window.location.href)
            let type = 'Unknown'
            if (sourceUrl.host === currentUrl.host) {
              type = 'Internal-Link'
              let internalPath = sourceUrl.path
              if (internalPath) {
                // TODO: fix internal links to only point to public displays?
              }
              res.body.source = internalPath
            }
            else {
              // TODO: how to deal with redirects here? Short URLs? …
              type = this.getTypeFromSourceURL(sourceUrl)
            }
            if (!type) {
              type = 'IFrame'

              // TODO: check if change from superagent to axios plugin is breaking
              _this.$axios.get(`${process.env.API_HOST}/proxy?url=${encodeURIComponent(this.term)}`)
                .then(resp => {
                  if (resp.status === 301) { // prem redirect
                    if (resp.headers['location']) _this.term = resp.headers['location']
                  }
                  else {
                    res.body.type = _this.mimeTypeToType(resp.headers['content-type']) // TODO: global mime-type to annotation type lookup
                    results.push(res)
                    this.results = results
                  }
                })
                .catch(() => {
                  results.push(res)
                })
            }
            else {
              res.body.type = type
              results.push(res)
            }
          }
          // TAGGING
          else if (/^[\s]*#.+/.test(this.term)) {
            const tag = this.term.replace(/^[\s]*#/, '').toLowerCase()
Anton's avatar
Anton committed
188
            _this.$store.dispatch('annotations/find', { 'body.purpose': 'tagging' })
Anton's avatar
Intial  
Anton committed
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
              .then(tagAnnotations => {
                console.log(tagAnnotations)
                let targetsMatched = {}
                tagAnnotations.filter(ta => {
                  return ta.body.value.toLowerCase().indexOf(tag) >= 0
                }).map(ta => {
                  if (!targetsMatched[ta.target.id]) {
                    targetsMatched[ta.target.id] = ta
                    ta.body.type = ta.target.type || ta.body.type
                    ta.body.source = ta.target.id
                    ta.body.sourceUuid = ta.target.id
                    results.push(ta)
                  }
                })
                _this.results = results
              })
          }
          // MULTI LINE
          else if (/[\n\r]/.test(this.term) || this.term.length > 200) {
            res.body.type = 'text'
            results.push(res)
          }
          // ASSUME ITs A TITLE
          else {
            res.body.type = 'title'
            results.push(res)
            let body = ObjectUtil.merge({}, res.body)
            body.type = 'text'
Anton's avatar
Anton committed
217
            results.push({ body })
Anton's avatar
Intial  
Anton committed
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
          }
          this.results = results
        }
        else {
          this.results = []
        }
      },
      getTypeFromSourceURL (sourceUrl) {
        let type = hostToTypeMap[sourceUrl.hostname]
        if (!type) {

        }
        if (!type) {
          console.log('Unknown URL resource', sourceUrl.toString())
          return null
        }
        return type
      },
      mimeTypeToType (mimeType) {
        let type = mimeTypesToAnnotationTypeMap[mimeType]
        if (!type) {
          type = 'IFrame'
        }
        return type
      }
    }
  }
</script>

<style scoped lang="stylus">

  .q-list-header
    padding 0

    .q-input
      padding-left 1em
      padding-right 1em

    .q-input:before
    .q-input:after
      display none

  .q-input
    padding-left 0

</style>