/
Triggers.php
181 lines (154 loc) · 6.11 KB
/
Triggers.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<?php
namespace verbb\buttonbox\fields;
use verbb\buttonbox\assetbundles\ButtonBoxAsset;
use Craft;
use craft\base\ElementInterface;
use craft\fields\BaseOptionsField;
use craft\helpers\Cp;
use craft\helpers\UrlHelper;
class Triggers extends BaseOptionsField
{
// Static Methods
// =========================================================================
public static function displayName(): string
{
return Craft::t('buttonbox', 'Button Box - Triggers');
}
public static function hasContentColumn(): bool
{
return false;
}
// Properties
// =========================================================================
public array $options = [];
public ?bool $displayAsGraphic = null;
public ?bool $displayFullwidth = null;
// Public Methods
// =========================================================================
public function getSettingsHtml(): ?string
{
$options = $this->translatedOptions();
if (!$options) {
$options = [
[
'label' => '',
'showLabel' => false,
'imageUrl' => '',
'type' => '',
'link' => '',
'newWindow' => false,
],
];
}
$table = Cp::editableTableFieldHtml([
'label' => $this->optionsSettingLabel(),
'instructions' => Craft::t('buttonbox', 'Image URLs are relative to your `@webroot` e.g. `/images/align-left.png` is `{url}`.', [
'url' => UrlHelper::siteUrl('/images/align-left.png'),
]),
'id' => 'options',
'name' => 'options',
'addRowLabel' => Craft::t('buttonbox', 'Add a trigger'),
'cols' => [
'label' => [
'heading' => Craft::t('buttonbox', 'Option Label'),
'type' => 'singleline',
],
'showLabel' => [
'heading' => Craft::t('buttonbox', 'Show Label?'),
'type' => 'checkbox',
'class' => 'thin',
],
'imageUrl' => [
'heading' => Craft::t('buttonbox', 'Image URL'),
'type' => 'singleline',
],
'type' => [
'heading' => Craft::t('buttonbox', 'Trigger Type'),
'class' => 'thin triggerType',
'type' => 'select',
'options' => [
'link' => 'Link',
'js' => 'JavaScript',
],
],
'value' => [
'heading' => Craft::t('buttonbox', 'HREF or Custom JS'),
'type' => 'singleline',
'class' => 'code triggerValue',
],
'newWindow' => [
'heading' => Craft::t('buttonbox', 'New window?'),
'type' => 'checkbox',
'class' => 'thin newWindow',
],
],
'rows' => $options,
'static' => false,
'allowAdd' => true,
'allowDelete' => true,
'allowReorder' => true,
]);
$displayAsGraphic = Cp::checkboxFieldHtml([
'checkboxLabel' => Craft::t('buttonbox', 'Display as graphic'),
'instructions' => Craft::t('buttonbox', 'This will take the height restrictions off the buttons to allow for larger images.'),
'id' => 'displayAsGraphic',
'name' => 'displayAsGraphic',
'class' => 'displayAsGraphic',
'value' => 1,
'checked' => $this->displayAsGraphic,
]);
$displayFullwidth = Cp::checkboxFieldHtml([
'checkboxLabel' => Craft::t('buttonbox', 'Display full width'),
'instructions' => Craft::t('buttonbox', 'Allow the button group to be fullwidth, useful for allowing larger graphics to be more responsive.'),
'id' => 'displayFullwidth',
'name' => 'displayFullwidth',
'class' => 'displayFullwidth',
'value' => 1,
'checked' => $this->displayFullwidth,
]);
return $displayAsGraphic . $displayFullwidth . $table;
}
public function getInputHtml(mixed $value, ElementInterface $element = null): string
{
$name = $this->handle;
$options = $this->translatedOptions();
// If this is a new entry, look for a default option
if ($this->isFresh($element)) {
$value = $this->defaultValue();
}
Craft::$app->getView()->registerAssetBundle(ButtonBoxAsset::class);
Craft::$app->getView()->registerJs('new Craft.ButtonBoxButtons("' . Craft::$app->getView()->namespaceInputId($name) . '");');
// Parse element tags in links
foreach ($options as $i => $opt) {
$options[$i]['value'] = Craft::$app->getView()->renderObjectTemplate($opt['value'], $element);
}
return Craft::$app->getView()->renderTemplate('buttonbox/_field/triggers/input', [
'name' => $name,
'value' => $value,
'options' => $options,
'displayAsGraphic' => $this->displayAsGraphic,
'displayFullwidth' => $this->displayFullwidth,
]);
}
// Protected Methods
// =========================================================================
protected function optionsSettingLabel(): string
{
return Craft::t('buttonbox', 'Triggers Options');
}
protected function translatedOptions(bool $encode = false): array
{
$translatedOptions = [];
foreach ($this->options as $option) {
$translatedOptions[] = [
'label' => Craft::t('site', $option['label']),
'value' => $option['value'],
'showLabel' => $option['showLabel'],
'imageUrl' => $option['imageUrl'],
'type' => $option['type'],
'newWindow' => $option['newWindow'],
];
}
return $translatedOptions;
}
}