Add basic trigger implementation to Form Field

Implement trigger on Mail Settings page
This commit is contained in:
Samuel Georges 2015-02-18 18:12:20 +11:00
parent 5a5db71e5a
commit 536a391754
2 changed files with 143 additions and 53 deletions

View File

@ -149,6 +149,11 @@ class FormField
*/
public $dependsOn;
/**
* @var array Other field names this field can be triggered by, see the Trigger API documentation.
*/
public $trigger;
/**
* Constructor.
* @param string $fieldName
@ -278,6 +283,9 @@ class FormField
if (isset($config['dependsOn'])) {
$this->dependsOn = $config['dependsOn'];
}
if (isset($config['trigger'])) {
$this->trigger = $config['trigger'];
}
if (isset($config['path'])) {
$this->path = $config['path'];
}
@ -352,9 +360,51 @@ class FormField
public function getAttributes($position = 'field', $htmlBuild = true)
{
$result = array_get($this->attributes, $position, []);
$result = $this->filterAttributes($result, $position);
return $htmlBuild ? Html::attributes($result) : $result;
}
/**
* Adds any circumstantial attributes to the field based on other
* settings, such as the 'trigger' option.
* @param array $attributes
* @param string $position
* @return array
*/
protected function filterAttributes($attributes, $position = 'field')
{
if (!$this->trigger || !is_array($this->trigger))
return $attributes;
$triggerAction = array_get($this->trigger, 'action');
$triggerField = array_get($this->trigger, 'field');
$triggerCondition = array_get($this->trigger, 'condition');
// Apply these to container
if (in_array($triggerAction, ['hide', 'show']) && $position != 'container')
return $attributes;
// Apply these to field/input
if (in_array($triggerAction, ['enable', 'disable', 'empty']) && $position != 'field')
return $attributes;
if ($this->arrayName) {
$fullTriggerField = $this->arrayName.'['.implode('][', Str::evalHtmlArray($triggerField)).']';
}
else {
$fullTriggerField = $triggerField;
}
$newAttributes = [
'data-trigger' => '[name="'.$fullTriggerField.'"]',
'data-trigger-action' => $triggerAction,
'data-trigger-condition' => $triggerCondition
];
$attributes = $attributes + $newAttributes;
return $attributes;
}
/**
* Returns a value suitable for the field name property.
* @param string $arrayName Specify a custom array name

View File

@ -3,68 +3,108 @@
# ===================================
tabs:
fields:
fields:
send_mode:
label: system::lang.mail.method
type: dropdown
tab: system::lang.mail.general
sender_name:
label: system::lang.mail.sender_name
span: auto
tab: system::lang.mail.general
sender_name:
label: system::lang.mail.sender_name
span: auto
tab: system::lang.mail.general
sender_email:
label: system::lang.mail.sender_email
span: auto
tab: system::lang.mail.general
sender_email:
label: system::lang.mail.sender_email
span: auto
tab: system::lang.mail.general
send_mode:
label: system::lang.mail.method
type: dropdown
tab: system::lang.mail.general
smtp_address:
label: system::lang.mail.smtp_address
tab: system::lang.mail.smtp
smtp_address:
label: system::lang.mail.smtp_address
tab: system::lang.mail.general
trigger:
action: show
field: send_mode
condition: value[smtp]
smtp_authorization:
type: checkbox
label: system::lang.mail.smtp_authorization
tab: system::lang.mail.smtp
comment: system::lang.mail.smtp_authorization_comment
smtp_authorization:
type: checkbox
label: system::lang.mail.smtp_authorization
tab: system::lang.mail.general
comment: system::lang.mail.smtp_authorization_comment
trigger:
action: show
field: send_mode
condition: value[smtp]
smtp_user:
label: system::lang.mail.smtp_username
tab: system::lang.mail.smtp
span: left
smtp_user:
label: system::lang.mail.smtp_username
tab: system::lang.mail.general
span: left
trigger:
action: show
field: send_mode
condition: value[smtp]
smtp_password:
label: system::lang.mail.smtp_password
tab: system::lang.mail.smtp
span: right
smtp_password:
label: system::lang.mail.smtp_password
tab: system::lang.mail.general
span: right
trigger:
action: show
field: send_mode
condition: value[smtp]
smtp_port:
label: system::lang.mail.smtp_port
tab: system::lang.mail.smtp
smtp_port:
label: system::lang.mail.smtp_port
tab: system::lang.mail.general
trigger:
action: show
field: send_mode
condition: value[smtp]
smtp_ssl:
type: checkbox
label: system::lang.mail.smtp_ssl
tab: system::lang.mail.smtp
smtp_ssl:
type: checkbox
label: system::lang.mail.smtp_ssl
tab: system::lang.mail.general
trigger:
action: show
field: send_mode
condition: value[smtp]
sendmail_path:
label: system::lang.mail.sendmail_path
commentAbove: system::lang.mail.sendmail_path_comment
tab: system::lang.mail.sendmail
sendmail_path:
label: system::lang.mail.sendmail_path
commentAbove: system::lang.mail.sendmail_path_comment
tab: system::lang.mail.general
trigger:
action: show
field: send_mode
condition: value[sendmail]
mailgun_domain:
label: system::lang.mail.mailgun_domain
commentAbove: system::lang.mail.mailgun_domain_comment
tab: system::lang.mail.mailgun
mailgun_domain:
label: system::lang.mail.mailgun_domain
commentAbove: system::lang.mail.mailgun_domain_comment
tab: system::lang.mail.general
trigger:
action: show
field: send_mode
condition: value[mailgun]
mailgun_secret:
label: system::lang.mail.mailgun_secret
commentAbove: system::lang.mail.mailgun_secret_comment
tab: system::lang.mail.mailgun
mandrill_secret:
label: system::lang.mail.mandrill_secret
commentAbove: system::lang.mail.mandrill_secret_comment
tab: system::lang.mail.mandrill
mailgun_secret:
label: system::lang.mail.mailgun_secret
commentAbove: system::lang.mail.mailgun_secret_comment
tab: system::lang.mail.general
trigger:
action: show
field: send_mode
condition: value[mailgun]
mandrill_secret:
label: system::lang.mail.mandrill_secret
commentAbove: system::lang.mail.mandrill_secret_comment
tab: system::lang.mail.general
trigger:
action: show
field: send_mode
condition: value[mandrill]