All files / src index.js

53.06% Statements 26/49
43.33% Branches 13/30
50% Functions 5/10
59.52% Lines 25/42
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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141          5x     14x 8x             8x           8x                                           14x       14x     7x                       4x   4x           4x                                                             5x   21x 5x 5x   4x 4x   4x     4x     4x               4x 20x 5x       4x   4x              
import setupTools from "./tools/setup";
import themeTools from "./tools/theme";
 
export default {
    install: (Vue, setup) => {
        Vue.component('lyra-form', {
            name: "lyra-form",
            render: function (createElement) {
                if(this.active) {
                    let confEmbedded = {
                        class: { 'kr-embedded': true },
                        props: {
                            'krFormToken': this.krFormToken,
                        },
                    };
 
                    let confWrapper = {
                        style: {
                            opacity: (!this.isVisible) ? '0':'1',
                        },
                    };
 
                    return createElement(
                        'div', confWrapper, [
                            createElement('div', confEmbedded, this.$slots.default),
                        ]
                    );
                }
            },
            props: {
                krClientSrc: String,
                krPublicKey: String,
                krPostUrlSuccess: String,
                krLanguage: String,
                krFormToken: String,
                krTheme: String,
                isVisible: {
                    type: Boolean,
                    default: true,
                    required: false,
                },
            },
            computed: {
                active: function() {
                    return (this.krFormToken);
                }
            },
            created() {
                Iif (typeof(this.isVisible) != "boolean") this.isVisible = true;
            },
            mounted() {
                if(this.active) this.setupForm();
            },
            beforeDestroy() {
                if(this.krTheme) this.cleanDeps();
            },
            watch: {
                active: function(newVal) {
                    if(newVal) this.setupForm();
                }
            },
            methods: {
                setupForm() {
                    const _this = this;
 
                    Iif(window.hasOwnProperty('KR')) {
                        window.KR.onFormReady(() => {
                            _this.setConfig();
                        });
                    } else {
                        // Wait until the library is loaded
                        const checkInterval = setInterval(() => {
                            if(window.hasOwnProperty('KR')) {
                                _this.setupForm();
                                clearInterval(checkInterval);
                            }
                        }, 50);
                    }
                },
                setConfig() {
                    let formConfig = {
                        'formToken': this.krFormToken
                    };
                    if(this.krTheme) this.setupTheme();
                    if(this.krPublicKey) formConfig['publicKey'] = this.krPublicKey;
                    if(this.krLanguage) formConfig['language'] = this.krLanguage;
                    //if(this.krPostUrlSuccess) formConfig['postUrlSuccess'] = this.krPostUrlSuccess;
 
                    // Wait until everything is loaded
                    let loadCheckInterval = setInterval(() => {
                        if(document.readyState === 'complete') {
                            clearInterval(loadCheckInterval);
                            KR.setFormConfig(formConfig);
                        }
                    }, 25);
                },
                setupTheme() {
                    themeLoader(setup.clientDomain, this.krTheme, function() {});
                }
            }
        })
 
        Vue.mixin({
            created() {
                if(typeof(window.KR_CLIENT_LOADED)=="undefined") {
                    window.KR_CLIENT_LOADED = true;
                    themeTools.loader(setup.clientDomain, setup.theme, () => {
                        // Load the script
                        let script = document.createElement('script');
                        script.type = 'text/javascript';
 
                        Iif (/^http.+kr-payment-min\.js.*$/.test()) {
                            script.src = setup["kr-client-domain"];
                        } else {
                            script.src = `${setup['kr-client-domain']}/static/js/krypton-client/V4.0/stable/kr-payment-form.min.js`;
                        }
 
                        let propagationKeys = [
                            "kr-clear-on-error",
                            "kr-hide-debug-toolbar",
                            "kr-form-token",
                            "kr-public-key",
                            "kr-post-url-success",
                        ];
 
                        propagationKeys.forEach(propKey => {
                            if (setup.hasOwnProperty(propKey)) {
                                script.setAttribute(propKey, setup[propKey]);
                            }
                        });
 
                        window.__kr__script = script;
 
                        document.getElementsByTagName('body')[0].appendChild(script);
                    });
                }
            }
        });
    }
};