-
-
Notifications
You must be signed in to change notification settings - Fork 632
Expand file tree
/
Copy pathAlpine.php
More file actions
127 lines (107 loc) · 3.08 KB
/
Copy pathAlpine.php
File metadata and controls
127 lines (107 loc) · 3.08 KB
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
<?php
namespace Statamic\Forms\JsDrivers;
use Statamic\Fields\Field;
use Statamic\Statamic;
class Alpine extends AbstractJsDriver
{
protected $scope;
protected $component;
/**
* Parse driver options.
*
* @param array $options
*/
protected function parseOptions($options)
{
$this->scope = $options[0] ?? null;
$this->component = $options[1] ?? null;
}
/**
* Add to form html tag attributes.
*
* @return array
*/
public function addToFormAttributes()
{
$extraData = $this->params->pull('x-data', []);
if (is_string($extraData)) {
$extraData = json_decode($extraData);
}
return [
'x-data' => $this->renderAlpineXData(collect($this->getInitialFormData())->replaceRecursive($extraData)->all(), $this->scope),
];
}
/**
* Add to renderable field view data.
*
* @param \Statamic\Fields\Field $field
* @param array $data
* @return array
*/
public function addToRenderableFieldData($field, $data)
{
return [
'show_field' => $this->renderAlpineShowFieldJs($field, $this->scope),
];
}
/**
* Add to renderable field html tag attributes.
*
* @param \Statamic\Fields\Field $field
* @return array
*/
public function addToRenderableFieldAttributes($field)
{
$attributes = [];
if ($field->fieldtype()->hasJsDriverDataBinding()) {
$attributes['x-model.fill'] = $this->getAlpineXDataKey($field->handle(), $this->scope);
}
return $attributes;
}
/**
* Render alpine x-data string for fields, with scope if necessary.
*
* @param array $xData
* @param bool|string $alpineScope
* @return string
*/
protected function renderAlpineXData($xData, $alpineScope)
{
if (is_string($alpineScope)) {
$xData = [
$alpineScope => $xData,
];
}
$xData = Statamic::modify($xData)->toJson()->entities();
if ($this->component) {
$xData = "{$this->component}({$xData})";
}
return $xData;
}
/**
* Get alpine x-data key, with scope if necessary.
*
* @param string $fieldHandle
* @param bool|string $alpineScope
* @return string
*/
protected function getAlpineXDataKey($fieldHandle, $alpineScope)
{
return is_string($alpineScope)
? "{$alpineScope}.{$fieldHandle}"
: $fieldHandle;
}
/**
* Render alpine `x-if` show field JS logic.
*/
protected function renderAlpineShowFieldJs(Field $field, ?string $alpineScope): string
{
$conditionsObject = Statamic::modify($field->conditions())->toJson()->entities();
$dataObject = '$data';
if (is_string($alpineScope)) {
$dataObject .= ".{$alpineScope}";
}
$currentFieldPath = $field->handle();
return "Statamic.\$conditions.showField({$conditionsObject}, {$dataObject}, '{$currentFieldPath}')";
}
}