-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
VuetifyWebPage.php
141 lines (134 loc) · 5 KB
/
VuetifyWebPage.php
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
<?php
namespace themes\vuetifyCore;
use webfiori\framework\ui\WebPage;
use webfiori\framework\App;
use webfiori\json\Json;
use webfiori\ui\HTMLNode;
/**
* The base page which is used to created pages which is based on Vuetify.
*
* This class serves two objectives. First is to help in setting the script
* which is used to initialize Vue and Vuetify. The script is appended to the
* body of the page with <script> element with 'id='vue-script'.
*
* Secondly, it provides developer with the global 'data' JavaScript object at
* which the developer can use to pass values from backend to the rendered
* frontend web page. The object is created at the end of the <head>
* tag inside a <script> element before the page is rendered.
*
* @author Ibrahim
*/
class VuetifyWebPage extends WebPage {
/**
*
* @var Json
*/
private $jsonData;
/**
* Creates new instance of the class.
*/
public function __construct() {
parent::__construct();
$this->addBeforeRender(function (VuetifyWebPage $p)
{
$p->addToJson([
'dir' => $p->getTranslation()->getWritingDir()
]);
$p->addBeforeRender(function (VuetifyWebPage $p2) {
$jsCode = new HTMLNode('script', [
'id' => 'server-json-data',
'nonce' => str_replace('=','',base64_encode(hash('sha256', microtime().'-'. random_bytes(10))))
]);
$jsCode->text('data = '.$p2->getJson(), false);
$p2->getDocument()->getHeadNode()->addChild($jsCode);
},100);
}, 100);
$this->jsonData = new Json();
}
/**
* Adds a set of attributes to the global 'data' JavaScript object.
*
* This method will add attributes to the global 'data' JsvaScript object.
*
* @param array $arrOfAttrs An associative array. The indices of the array
* are attributes names and the value of each index is the value that will
* be passed.
*/
public function addToJson(array $arrOfAttrs) {
foreach ($arrOfAttrs as $attrKey => $attrVal) {
$this->getJson()->add($attrKey, $attrVal);
}
}
/**
* Returns an object of type Json that contains all JSON attributes which
* will be added to the global 'data' JavaScript object.
*
* Initially, the object will contain all common attributes for all pages.
*
* @return Json
*/
public function getJson() {
return $this->jsonData;
}
/**
* Sets the JavaScript file which will be used to initialize vue.
*
* @param string $jsFilePath A string that represents the path of the
* file such as 'assets/js/init-vue.js'.
*/
public function setVueScript($jsFilePath) {
$this->addBeforeRender(function (WebPage $page, string $jsPath)
{
$base = $page->getBase();
if (!strpos($jsPath, $base) === false) {
$jsPath = trim($base,"/").'/'.trim($jsPath, "/");
}
$page->removeChild('vue-init');
$page->getDocument()->addChild('script', [
'type' => 'text/javascript',
'src' => $jsPath.'?jv='.App::getConfig()->getAppVersion(),
'id' => 'vue-init'
]);
}, 0, [$jsFilePath]);
}
/**
* Converts an array of labels to JSON objects which could be used as items
* for selects or auto-complete components of Vuetify.
*
* @param string|array $labelOrArrOfLabels A label location which exist in the language class.
* Also, this can be an array that holds the labels.
*
* @param array $extraAttrs An associative array that holds extra
* attributes to set for an item. The indices of the array should
* represent the key and the value is a sub-associative array of
* values.
*
* @return array The method will return an array that holds objects of type
* Json. Each object will have at least two attributes, 'value'
* and 'text'. The object may have extra attributes based on what values
* passed in the array <b>$extraAttrs</b>.
*/
public function toVItems($labelOrArrOfLabels, array $extraAttrs = []) {
if (gettype($labelOrArrOfLabels) == 'array') {
$data = $labelOrArrOfLabels;
} else {
$data = $this->get($labelOrArrOfLabels);
}
$retVal = [];
if (gettype($data) == 'array') {
foreach ($data as $key => $lbl) {
$jsonItem = new Json([
'text' => $lbl,
'value' => $key
]);
if (isset($extraAttrs[$key]) && gettype($extraAttrs[$key]) == 'array') {
foreach ($extraAttrs[$key] as $itemKey => $val) {
$jsonItem->add($itemKey, $val);
}
}
$retVal[] = $jsonItem;
}
}
return $retVal;
}
}