Streamline data-input-preset in to FormField API

This commit is contained in:
Samuel Georges 2015-03-07 10:45:22 +11:00
parent 7e3cf98a75
commit f8c3eac9c6
2 changed files with 66 additions and 32 deletions

View File

@ -1,7 +1,8 @@
/*
* An input preset converter.
*
* The API allows to convert text entered into an element to an URL or file name value in another input element.
* The API allows to convert text entered into an element to a URL, slug or file name
* value in another input element.
*
* Supported data attributes:
* - data-input-preset: specifies a CSS selector for a source input element

View File

@ -154,6 +154,11 @@ class FormField
*/
public $trigger;
/**
* @var array Other field names text is converted in to a URL, slug or file name value in this field.
*/
public $preset;
/**
* Constructor.
* @param string $fieldName
@ -244,15 +249,38 @@ class FormField
*/
protected function evalConfig($config)
{
/*
* Standard config:property values
*/
$applyConfigValues = [
'context',
'placeholder',
'cssClass',
'dependsOn',
'trigger',
'preset',
'path',
'required',
'disabled',
'hidden',
'stretch',
];
foreach ($applyConfigValues as $value) {
if (array_key_exists($value, $config)) {
$this->{$value} = $config[$value];
}
}
/*
* Custom applicators
*/
if (isset($config['options'])) {
$this->options($config['options']);
}
if (isset($config['span'])) {
$this->span($config['span']);
}
if (isset($config['context'])) {
$this->context = $config['context'];
}
if (isset($config['size'])) {
$this->size($config['size']);
}
@ -265,42 +293,15 @@ class FormField
if (isset($config['comment'])) {
$this->comment($config['comment']);
}
if (isset($config['placeholder'])) {
$this->placeholder = $config['placeholder'];
}
if (isset($config['default'])) {
$this->defaults = $config['default'];
}
if (isset($config['cssClass'])) {
$this->cssClass = $config['cssClass'];
}
if (isset($config['attributes'])) {
$this->attributes($config['attributes']);
}
if (isset($config['containerAttributes'])) {
$this->attributes($config['containerAttributes'], 'container');
}
if (isset($config['dependsOn'])) {
$this->dependsOn = $config['dependsOn'];
}
if (isset($config['trigger'])) {
$this->trigger = $config['trigger'];
}
if (isset($config['path'])) {
$this->path = $config['path'];
}
if (array_key_exists('required', $config)) {
$this->required = $config['required'];
}
if (array_key_exists('disabled', $config)) {
$this->disabled = $config['disabled'];
}
if (array_key_exists('hidden', $config)) {
$this->hidden = $config['hidden'];
}
if (array_key_exists('stretch', $config)) {
$this->stretch = $config['stretch'];
}
if (isset($config['valueFrom'])) {
$this->valueFrom = $config['valueFrom'];
@ -376,6 +377,7 @@ class FormField
$position = strtolower($position);
$attributes = $this->filterTriggerAttributes($attributes, $position);
$attributes = $this->filterPresetAttributes($attributes, $position);
if ($position == 'field' && $this->disabled) {
$attributes = $attributes + ['disabled' => 'disabled'];
@ -385,7 +387,7 @@ class FormField
}
/**
* Adds attributes used specifically by the TriggerAPI
* Adds attributes used specifically by the Trigger API
* @param array $attributes
* @param string $position
* @return array
@ -425,6 +427,37 @@ class FormField
return $attributes;
}
/**
* Adds attributes used specifically by the Input Preset API
* @param array $attributes
* @param string $position
* @return array
*/
protected function filterPresetAttributes($attributes, $position = 'field')
{
if (!$this->preset || !is_array($this->preset) || $position != 'field')
return $attributes;
$presetField = array_get($this->preset, 'field');
$presetType = array_get($this->preset, 'type');
if ($this->arrayName) {
$fullPresetField = $this->arrayName.'['.implode('][', Str::evalHtmlArray($presetField)).']';
}
else {
$fullPresetField = $presetField;
}
$newAttributes = [
'data-input-preset' => '[name="'.$fullPresetField.'"]',
'data-input-preset-type' => $presetType,
'data-input-preset-closest-parent' => 'form'
];
$attributes = $attributes + $newAttributes;
return $attributes;
}
/**
* Returns a value suitable for the field name property.
* @param string $arrayName Specify a custom array name