Skip to content

Commit d13c80c

Browse files
committed
feat: hide/show fields based on editor action
1 parent 25fe850 commit d13c80c

File tree

1 file changed

+73
-13
lines changed

1 file changed

+73
-13
lines changed

src/Html/Editor/Editor.php

Lines changed: 73 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ class Editor extends Fluent
2424
use HasEvents;
2525
use HasAuthorizations;
2626

27-
public array $events = [];
28-
2927
const DISPLAY_LIGHTBOX = 'lightbox';
3028
const DISPLAY_ENVELOPE = 'envelope';
3129
const DISPLAY_BOOTSTRAP = 'bootstrap';
3230
const DISPLAY_FOUNDATION = 'foundation';
3331
const DISPLAY_JQUERYUI = 'jqueryui';
3432

33+
public array $events = [];
34+
3535
/**
3636
* Editor constructor.
3737
*
@@ -156,30 +156,30 @@ public function fields(array $fields): static
156156
}
157157

158158
/**
159-
* Set Editor's formOptions.
159+
* Set Editor's bubble formOptions.
160160
*
161161
* @param array $formOptions
162162
* @return $this
163-
* @see https://editor.datatables.net/reference/option/formOptions
164-
* @see https://editor.datatables.net/reference/type/form-options
163+
* @see https://editor.datatables.net/reference/option/formOptions.bubble
165164
*/
166-
public function formOptions(array $formOptions): static
165+
public function formOptionsBubble(array $formOptions): static
167166
{
168-
$this->attributes['formOptions'] = $formOptions;
169-
170-
return $this;
167+
return $this->formOptions(['bubble' => Helper::castToArray($formOptions)]);
171168
}
172169

173170
/**
174-
* Set Editor's bubble formOptions.
171+
* Set Editor's formOptions.
175172
*
176173
* @param array $formOptions
177174
* @return $this
178-
* @see https://editor.datatables.net/reference/option/formOptions.bubble
175+
* @see https://editor.datatables.net/reference/option/formOptions
176+
* @see https://editor.datatables.net/reference/type/form-options
179177
*/
180-
public function formOptionsBubble(array $formOptions): static
178+
public function formOptions(array $formOptions): static
181179
{
182-
return $this->formOptions(['bubble' => Helper::castToArray($formOptions)]);
180+
$this->attributes['formOptions'] = $formOptions;
181+
182+
return $this;
183183
}
184184

185185
/**
@@ -272,4 +272,64 @@ public function toJson($options = 0): string
272272

273273
return Helper::toJsonScript($parameters, $options);
274274
}
275+
276+
/**
277+
* Hide fields on create action.
278+
*
279+
* @param array $fields
280+
* @return $this
281+
*/
282+
public function hiddenOnCreate(array $fields): static
283+
{
284+
return $this->hiddenOn('create', $fields);
285+
}
286+
287+
/**
288+
* Hide fields on specific action.
289+
*
290+
* @param string $action
291+
* @param array $fields
292+
* @return $this
293+
*/
294+
public function hiddenOn(string $action, array $fields): static
295+
{
296+
$script = 'function(e, mode, action) {'.PHP_EOL;
297+
$script .= "if (action === '{$action}') {".PHP_EOL;
298+
foreach ($fields as $field) {
299+
$script .= "this.hide('{$field}');".PHP_EOL;
300+
}
301+
$script .= '} else {'.PHP_EOL;
302+
foreach ($fields as $field) {
303+
$script .= "this.show('{$field}');".PHP_EOL;
304+
}
305+
$script .= '}'.PHP_EOL;
306+
$script .= 'return true;'.PHP_EOL;
307+
$script .= '}'.PHP_EOL;
308+
309+
$this->onPreOpen($script);
310+
311+
return $this;
312+
}
313+
314+
/**
315+
* Hide fields on edit action.
316+
*
317+
* @param array $fields
318+
* @return $this
319+
*/
320+
public function hiddenOnEdit(array $fields): static
321+
{
322+
return $this->hiddenOn('edit', $fields);
323+
}
324+
325+
/**
326+
* Hide fields on remove action.
327+
*
328+
* @param array $fields
329+
* @return $this
330+
*/
331+
public function hiddenOnRemove(array $fields): static
332+
{
333+
return $this->hiddenOn('remove', $fields);
334+
}
275335
}

0 commit comments

Comments
 (0)