Skip to content
Snippets Groups Projects
DefElement.vue 3.49 KiB
Newer Older
Ole Voldsæter's avatar
Ole Voldsæter committed
<template>
  <li :is="tag" :class="body.type_"><!--
Ole Voldsæter's avatar
Ole Voldsæter committed
 --><span :is="item.tag || 'span'" v-for="(item, index) in assemble_text"
          :class="item.type"
          :key="index"
           v-bind="item.props"><!--
          -->{{item.html}}<!--
       --><router-link v-if="item.type == 'article_ref'" :to="item.ref" @click.native="article_link_click(item)"><!--
         -->{{item.link_text}} {{item.definition_order ? ` (${item.definition_order})` : ''}}<!--
       --></router-link><!--
 --></span></li>
Ole Voldsæter's avatar
Ole Voldsæter committed
</template>

<script>
Ole Voldsæter's avatar
Ole Voldsæter committed
import entities from '../utils/entities.js'

Ole Voldsæter's avatar
Ole Voldsæter committed
export default {
  name: 'DefElement',
  props: {
    body: Object,
    tag: {
      type: String,
      default: 'li'
    },
    dictionary: String
Ole Voldsæter's avatar
Ole Voldsæter committed
  },
  data: function() {
    return {
      path: this.$route.fullPath
    }
  },
Ole Voldsæter's avatar
Ole Voldsæter committed
  computed: {
    unparsed: function(){
      let lang = this.dictionary
      return this.body.items.map(
Ole Voldsæter's avatar
Ole Voldsæter committed
        function(item){
          if      (item.type_ == 'usage') return {type: item.type_, html: item.text, tag: 'mark'}
Ole Voldsæter's avatar
Ole Voldsæter committed
          else if (item.type_ == 'article_ref') return {
                                                          type: item.type_,
                                                          link_text: item.word_form ||  item.lemmas[0].lemma,
Ole Voldsæter's avatar
Ole Voldsæter committed
                                                          ref: '/' + lang + '/' + item.article_id + '/' + encodeURIComponent(item.word_form ||  item.lemmas[0].lemma) + (item.definition_id ? '#def' + item.definition_id : ''),
Ole Voldsæter's avatar
Ole Voldsæter committed
                                                          article_id: item.article_id,
                                                          definition_id: item.definition_id,
                                                          definition_order: item.definition_order,
                                                          source: path
          else if (item.type_ == 'pronunciation') return {type: item.type_, html: item.string}
Ole Voldsæter's avatar
Ole Voldsæter committed
          else if (item.type_ == 'superscript') return {type: item.type_, html: item.text, tag: 'sup'}
          else if (item.type_ == 'subscript') return {type: item.type_, html: item.text, 'tag': 'sub'}
          else if (item.type_ == 'quote_inset') return {type: item.type_, body: item, html: '', tag: 'DefElement', props: {body: item, tag: 'i'}}
          else if (item.id) return {type: item.type_, html: (entities[lang][item.id] || {})['expansion'] || item.id}
          else return {type: item.type_ || 'plain', html: item}
Ole Voldsæter's avatar
Ole Voldsæter committed
        }
      )
    },
    assemble_text: function(){
      var old_parts = this.body.content.split(/(\$)/)
Ole Voldsæter's avatar
Ole Voldsæter committed
      var text_items = this.unparsed.slice(0).reverse()
      var new_parts = []
      old_parts.forEach(function(item){
        if(item == '$') {
          new_parts.push(text_items.pop())
        } else if (item.length) {
          new_parts.push({type: 'plain', html: item})
        }
      })
      return new_parts
    }
Ole Voldsæter's avatar
Ole Voldsæter committed
  },
  methods: {
    article_link_click: function(item) {
      this.$emit('article-click', item)
    }
Ole Voldsæter's avatar
Ole Voldsæter committed
  }
}
</script>
Ole Voldsæter's avatar
Ole Voldsæter committed

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.usage, .pronunciation {
Ole Voldsæter's avatar
Ole Voldsæter committed
  font-style: italic;
}

mark {
  background: rgba(255, 255, 255, 0);
Ole Voldsæter's avatar
Ole Voldsæter committed
span.language {
  font-weight: bold;
}
Ole Voldsæter's avatar
Ole Voldsæter committed

i {
  font-style: normal;
  font-family: monospace;
}

Ole Voldsæter's avatar
Ole Voldsæter committed

Ole Voldsæter's avatar
Ole Voldsæter committed
</style>