EdText.vue 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <script>
  2. import Vue from 'vue'
  3. export default {
  4. name: 'EdText',
  5. props: {
  6. tei: String
  7. },
  8. data: () => ({
  9. template: null,
  10. html: null
  11. }),
  12. watch: {
  13. tei: function (newtei, oldtei) {
  14. // console.log('tei watcher', oldtei, newtei)
  15. // we need this watcher to update display as route is changing
  16. this.buildTemplate()
  17. }
  18. },
  19. beforeMount () {
  20. console.log('EdText beforeMount')
  21. if (this.tei) {
  22. this.buildTemplate()
  23. }
  24. },
  25. methods: {
  26. buildTemplate () {
  27. console.log('EdText buildTemplate: tei', this.tei)
  28. // to get Vue.compile working we have to use the full build of vuejs
  29. // https://vuejs.org/v2/guide/installation.html#Explanation-of-Different-Builds
  30. // in /build/webpack.config.base.js alias -> 'vue': 'vue/dist/vue.js',
  31. this.buildHtml()
  32. this.template = Vue.compile(this.html)
  33. this.$options.staticRenderFns = []
  34. this._staticTrees = []
  35. this.template.staticRenderFns.map(fn => (this.$options.staticRenderFns.push(fn)))
  36. },
  37. buildHtml () {
  38. this.html = `<div class="tei">${this.tei}</div>`
  39. this.parseLinks()
  40. // console.log('EdText: builded html', this.html)
  41. },
  42. parseLinks () {
  43. const regx = /<a[^<]+<\/a>/g
  44. let links = this.html.match(regx)
  45. console.log('links', links)
  46. if (links) {
  47. let domlink, newlink, uuid
  48. let index = null
  49. for (var i = 0; i < links.length; i++) {
  50. domlink = new DOMParser().parseFromString(links[i], 'text/xml').firstChild
  51. console.log('domlink', domlink.classList)
  52. if (domlink.classList.contains('placeName')) { index = 'locorum' }
  53. if (domlink.classList.contains('persName')) { index = 'nominum' }
  54. if (domlink.classList.contains('objectName')) { index = 'operum' }
  55. console.log('index:', index)
  56. if (index) {
  57. uuid = domlink.getAttribute('href').replace('#', '')
  58. newlink = `<a` +
  59. ` class="${domlink.getAttribute('class')} active-link"` +
  60. ` data-index="${index}"` +
  61. ` uuid="${uuid}"` +
  62. ` href="/${index}/${uuid}"` +
  63. ` @click.prevent="onClickRef"` +
  64. ` @keyup.enter="onClickRef"` +
  65. `>${domlink.innerHTML}</a>`
  66. console.log('newlink', newlink)
  67. this.html = this.html.replace(links[i], newlink)
  68. }
  69. }
  70. // console.log('this.html', this.html)
  71. }
  72. },
  73. onClickRef (e) {
  74. console.log('onClickRef()', e)
  75. this.$router.push({
  76. name: e.target.getAttribute('data-index'),
  77. params: { id: e.target.getAttribute('uuid') }
  78. })
  79. }
  80. },
  81. render (h) {
  82. console.log('EdText render()')
  83. if (!this.template) {
  84. return h('span', 'Loading ...')
  85. } else {
  86. return this.template.render.call(this)
  87. }
  88. }
  89. }
  90. </script>
  91. <style lang="scss" scoped>
  92. </style>