All files / src/cms cms.js

95.12% Statements 39/41
100% Branches 11/11
100% Functions 8/8
94.29% Lines 33/35

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 91 92 93 94 95                    5x 5x             5x                 92x 92x 92x 92x             7x 7x   6x   5x         5x 5x   5x 4x 4x 4x 4x 4x 4x   4x 4x 4x     5x       1x 1x         4x 4x 3x 1x 1x               4x 4x                
// Local modules
import { panelStatusUpdater } from '../utils/panel-status-updater';
import { setHtml } from '../utils/setHtml';
import nodeScriptReplace from './utils/node-script-replace';
import toQueryString from '../utils/toQueryString';
import waitForElementToExist from '../utils/waitForElementToExist';
 
// NPM modules
//import isDomNode from 'is-dom-node';
 
const VYBE_API_ROOT = "http://cms.api.vybe.com/api";
const API = Object.freeze({
    GET_KEYS:  `${VYBE_API_ROOT}/cms/getkeys`,
    GET_PAGE_BY_KEY:  `${VYBE_API_ROOT}/cms/get`,
    GET_PAGE_BY_URL:  `${VYBE_API_ROOT}/cms/get-by-url`,
    GET_PAGE_PREVIEW:  `${VYBE_API_ROOT}/cms/getpreview`,
});
 
const API_RESPONSE = Object.freeze({
    NO_PAGE_FOUND: "No page found"
});
 
 
 
// Public API
class Cms {
    constructor(options) {
        this.token = options.token;
        this._tracking = options._tracking;
        this.componentScriptsRan = false;
        this.sentry = options.sentry;
        // window.addEventListener('load', () => {
        //     this.runComponentScripts();
        // });
    }
 
    loadPage(options = {}) {
        try {
            if (!options.container) throw new Error("container must be set as part of options");
            
            return waitForElementToExist(options.container)
                .then(container => {
                    const qs = toQueryString({
                        token: this.token,
                        url: encodeURI(location.pathname)
                    });
 
                    const loadPagePromise = fetch(`${API.GET_PAGE_BY_URL}?${qs}`)
                        .then(response => response.json())
                        .then(data => {
                            if (data === API_RESPONSE.NO_PAGE_FOUND) return;                            
                            setHtml(container, data.content);
                            this.addScriptFromContent(data.content);
                            this.runComponentScripts();
                            document.title = data.metaTitle;
                            const pageDescriptionElm = document.querySelector('meta[name="description"]');
                            if (pageDescriptionElm) pageDescriptionElm.setAttribute("content", data.metaDescription);                            
                            // set internal cmsPageKey
                            this._tracking.set(data.trackingData);
                            this._tracking.track("event", "CMS", "Page Loaded");
                            return data;
                        });
 
                    return panelStatusUpdater( container, loadPagePromise );
                });
        }
        catch (err) {
            this.sentry.captureException(err);
            throw Error(err);
        }
    }
    
    runComponentScripts() {
        try {
            if (this.componentScriptsRan) return;
            if (!Array.isArray(window.VybeCmsScripts)) return;
            window.VybeCmsScripts.forEach(s => s());
            this.componentScriptsRan = true;
        }
        catch (err) {
            this.sentry.captureException(err);
        }
    }
 
    addScriptFromContent(content) {
        try {
            nodeScriptReplace(document.getElementsByTagName("body")[0]);
        }
        catch (err) {
            this.sentry.captureException(err);
        }
    }
}
 
export default Cms;