# All files / src Retriever.vue

0% Statements 0/31
0% Branches 0/16
0% Functions 0/6
0% Lines 0/31

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    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

     <template>
      <div>
        Loading...
      </div>
    </template>

    <script>
    import {getFragmentByHash, toBoolean} from './utils/utils.js'

    export default {
      props: {
        src: {
          type: String
        },
        fragment: {
          type: String // fragment identified (the '#' in URI)
        },
        delay: {
          type: Boolean,
          default: false
        },
        _hasFetched: {
          type: Boolean,
          default: false
        }
      },
      computed: {
        // Vue 2.0 coerce migration
        delayBool () {
          return toBoolean(this.delay);
        }
        // Vue 2.0 coerce migration end
      },
      methods: {
        fetch() {
          if (!this.src) {
            return;
          }
          if (this._hasFetched) {
            return;
          }
          jQuery.get(this.src)
            .done((response) => {
              var result = response;
              if (this.fragment) {
                var tempDom = jQuery('<temp>').append(jQuery.parseHTML(result));
                var appContainer = jQuery('#' + this.fragment, tempDom);
                result = appContainer.html();
              }
              this._hasFetched = true
              // result is empty / undefined
              if (result == void(0) && this.fragment) {
                this.$el.innerHTML = `<strong>Error</strong>: Failed to retrieve page fragment: ${this.src}#${this.fragment}`
                return
              }

              // Mount result in retriever
              let tempComponent = Vue.extend({
                template: `<div>\n${result}\n</div>`,
              })
              new tempComponent().$mount(this.$el);
              this.$emit('src-loaded');
            })
            .fail((error) => {
              console.error(error.responseText)
              this.$el.innerHTML = `<strong>Error</strong>: Failed to retrieve content from source: <em>${this.src}</em>`
              this.$emit('src-loaded');
            });
        }
      },
      mounted() {
        this.$nextTick( function () {
            if (!this.src) {
              this.$el.innerHTML = ''
            } else {
              var hash = getFragmentByHash(this.src)
              if (hash) {
                this.fragment = hash
                this.src = this.src.split('#')[0];
              }
            }

            if (!this.delayBool) {
              this.fetch();
            }
        })
      }
    }
    </script>