Passed
Branch master (32b4c5)
by Askupa
01:33
created

src/js/components/live-preview.js   A

Complexity

Total Complexity 14
Complexity/F 2.33

Size

Lines of Code 52
Function Count 6

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 0
c 2
b 0
f 0
nc 4
dl 0
loc 52
rs 10
wmc 14
mnd 3
bc 7
fnc 6
bpm 1.1666
cpm 2.3333
noi 6
1
Mivhak.component('live-preview', {
2
    template: '<iframe class="mivhak-live-preview" allowtransparency="true" sandbox="allow-scripts allow-pointer-lock allow-same-origin allow-popups allow-modals allow-forms" frameborder="0"></iframe>',
3
    props: {
4
        resources: []
5
    },
6
    methods: {
7
        renderHTML: function() {
8
            var html = '<html>',
9
                head = '<head>',
10
                body = '<body>';
11
            
12
            head += '<meta http-equiv="content-type" content="text/html; charset=UTF-8">';
13
            head += '<meta name="robots" content="noindex, nofollow">';
14
            head += '<meta name="googlebot" content="noindex, nofollow">';
15
            
16
            for(var i = 0; i < this.resources.count(); i++)
17
            {
18
                var source = this.resources.get(i);
19
                if('markup' === source.runAs) body += source.content;
20
                if('style' === source.runAs) head += this.createStyle(source.content, source.visible ? false : source.source);
21
                if('script' === source.runAs) head += this.createScript(source.content, source.visible ? false : source.source);
22
            }
23
            
24
            html += head+'</head>'+body+'</body></html>';
25
            
26
            return html;
27
        },
28
        createScript: function(content,src) {
29
            if(src) return '<script src="'+src+'" type="text/javascript"></script>';
30
            return '<script>\n//<![CDATA[\nwindow.onload = function(){'+content+'};//]]>\n</script>'; // @see http://stackoverflow.com/questions/66837/when-is-a-cdata-section-necessary-within-a-script-tag
31
        },
32
        createStyle: function(content,href) {
33
            if(href) return '<link href="'+href+'" rel="stylesheet">';
34
            return '<style>'+content+'</style>';
35
        },
36
        show: function() {
37
            this.$el.addClass('mivhak-active');
38
            this.run();
39
        },
40
        hide: function() {
41
            this.$el.removeClass('mivhak-active');
42
        },
43
        run: function() {
44
            var contents = this.$el.contents(),
45
                doc = contents[0];
46
        
47
            doc.open();
48
            doc.writeln(this.renderHTML());
49
            doc.close();
50
        }
51
    }
52
});