Passed
Push — master ( 07c97c...8ea5ca )
by Bruno
05:33
created

Framework::setMode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Formularium\Frontend\Vue;
4
5
use Formularium\HTMLElement;
6
7
class Framework extends \Formularium\Framework
8
{
9
    const VUE_MODE_SINGLE_FILE = 'VUE_MODE_SINGLE_FILE';
10
    const VUE_MODE_EMBEDDED = 'VUE_MODE_EMBEDDED';
11
12
    /**
13
     * @var string
14
     */
15
    protected $mode;
16
17
    public function __construct(string $name = 'Vue')
18
    {
19
        parent::__construct($name);
20
        $this->mode = self::VUE_MODE_EMBEDDED;
21
    }
22
23
    public function setMode(string $mode): Framework
24
    {
25
        $this->mode = $mode;
26
        return $this;
27
    }
28
29
    public function htmlHead(): string
30
    {
31
        return '<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>';
32
        // return '<script src="https://cdn.jsdelivr.net/npm/[email protected]"></script>';
33
    }
34
35
    public function viewableCompose(\Formularium\Model $m, array $elements, string $previousCompose): string
36
    {
37
        return join('', $elements); // TODO
38
    }
39
40
    public function editableCompose(\Formularium\Model $m, array $elements, string $previousCompose): string
41
    {
42
        $data = [];
43
        foreach ($m->getFields() as $name => $field) {
44
            $data[$name] = $field->getDatatype()->getDefault();
45
        }
46
47
        $editableForm = join('', $elements);
48
        $jsonData = json_encode($data);
49
50
        if ($this->mode === self::VUE_MODE_SINGLE_FILE) {
51
            return <<<EOF
52
<template>
53
<div>
54
    $editableForm
55
</div>
56
</template>
57
<script>
58
module.exports = {
59
    data: function () {
60
        return $jsonData;
61
    }
62
};
63
</script>
64
<style>
65
</style>
66
EOF;
67
        } else {
68
            $id = 'vueapp';
69
            $t = new HTMLElement('div', ['id' => $id], $editableForm, true);
70
            $script = <<<EOF
71
            console.log("asdfasd");
72
var app = new Vue({
73
    el: '#$id',
74
    data: $jsonData
75
});
76
console.log(app);
77
EOF;
78
            $s = new HTMLElement('script', [], $script, true);
79
            return new HTMLElement('div', [], [$t, $s]);
80
        }
81
    }
82
}
83