Skip to content

armgit / dashy

Public Imported from GitHub
0

πŸš€ A self-hostable personal dashboard built for you. Includes status-checking, widgets, themes, icon packs, a UI editor and tons more!

2.8 KB
Raw Blame
<template>
<div class="html-widget">
  <div :id="elementId" />
</div>
</template>

<script>
import WidgetMixin from '@/mixins/WidgetMixin';

export default {
  mixins: [WidgetMixin],
  computed: {
    /* Optional HTML markup to be rendered */
    html() {
      return this.options.html || '';
    },
    /* Optional CSS styles to be applied */
    css() {
      return this.options.css || '';
    },
    /* Optional raw JavaScript to be executed */
    script() {
      return this.options.script || '';
    },
    /* Optional path to JS script to be fetched */
    scriptSrc() {
      return this.options.scriptSrc || '';
    },
    /* Optional URL to fetch HTML markup from */
    htmlSrc() {
      return this.options.htmlSrc || '';
    },
    /* Unique element ID */
    elementId() {
      return `elem-${Math.round(Math.random() * 10000)}`;
    },
  },
  mounted() {
    this.initiate();
  },
  beforeUnmount() {
    window.removeEventListener('load', this.injectHtml);
  },
  methods: {
    /* Fetches remote HTML (if htmlSrc set), then renders it */
    fetchData() {
      if (!this.htmlSrc) {
        this.finishLoading();
        return;
      }
      this.makeRequest(this.htmlSrc)
        .then(this.processData)
        .catch(() => { /* error already surfaced by the mixin */ });
    },
    /* Renders fetched HTML into the widget */
    processData(data) {
      if (typeof data !== 'string') {
        this.error('Fetched content is not HTML', data);
        return;
      }
      const element = document.getElementById(this.elementId);
      if (element) element.innerHTML = data;
    },
    /* Injects users content */
    injectHtml() {
      if (this.html) {
        const element = document.getElementById(this.elementId);
        if (element) element.innerHTML = this.html;
      }
      if (this.css) {
        const styleElem = document.createElement('style');
        styleElem.textContent = this.css;
        document.head.append(styleElem);
      }
      if (this.script) {
        const scriptElem = document.createElement('script');
        scriptElem.text = this.script;
        document.head.append(scriptElem);
      }
      if (this.scriptSrc) {
        const scriptElem = document.createElement('script');
        scriptElem.src = this.scriptSrc;
        document.head.append(scriptElem);
      }
    },
    /* What for the DOM to finish loading, before proceeding */
    initiate() {
      if (document.readyState === 'complete' || document.readyState === 'loaded') {
        this.injectHtml();
      } else {
        window.addEventListener('load', this.injectHtml);
      }
    },
    update() {
      if (this.htmlSrc) {
        this.startLoading();
        this.fetchData();
      } else {
        this.injectHtml();
      }
    },
  },
};
</script>

<style scoped lang="scss">
.html-widget {
  width: 100%;
  min-height: 120px;
}

</style>