TrackManiaControl/libs/FML/Script/Features/ActionTrigger.php

168 lines
3.6 KiB
PHP
Raw Permalink Normal View History

2014-04-27 14:44:40 +02:00
<?php
namespace FML\Script\Features;
use FML\Controls\Control;
2014-05-14 23:24:00 +02:00
use FML\Script\Builder;
2014-04-27 14:44:40 +02:00
use FML\Script\Script;
use FML\Script\ScriptLabel;
2014-05-14 23:24:00 +02:00
use FML\Types\Scriptable;
2014-04-27 14:44:40 +02:00
/**
2017-03-25 18:40:15 +01:00
* Script Feature for triggering a ManiaLink page action
2014-04-27 14:44:40 +02:00
*
2014-05-14 23:24:00 +02:00
* @author steeffeen
2017-03-25 18:40:15 +01:00
* @copyright FancyManiaLinks Copyright © 2017 Steffen Schröder
2014-05-14 23:24:00 +02:00
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
2014-04-27 14:44:40 +02:00
*/
2017-03-25 18:40:15 +01:00
class ActionTrigger extends ScriptFeature
{
/**
* @var string $actionName Triggered action
*/
protected $actionName = null;
/**
* @var Control $control Action Control
*/
protected $control = null;
/**
* @var string $labelName Script label name
*/
protected $labelName = null;
/**
* Construct a new Action Trigger
*
* @api
* @param string $actionName (optional) Triggered action
* @param Control $control (optional) Action Control
* @param string $labelName (optional) Script label name
*/
public function __construct($actionName = null, Control $control = null, $labelName = ScriptLabel::MOUSECLICK)
{
if ($actionName) {
$this->setActionName($actionName);
}
if ($control) {
$this->setControl($control);
}
if ($labelName) {
$this->setLabelName($labelName);
}
}
/**
* Get the action to trigger
*
* @api
* @return string
*/
public function getActionName()
{
return $this->actionName;
}
/**
* Set the action to trigger
*
* @api
* @param string $actionName Action name
* @return static
*/
public function setActionName($actionName)
{
$this->actionName = (string)$actionName;
return $this;
}
/**
* Get the Control that should trigger the action
*
* @api
* @return Control
*/
public function getControl()
{
return $this->control;
}
/**
* Set the Control that should trigger the action
*
* @api
* @param Control $control Action Control
* @return static
*/
public function setControl(Control $control = null)
{
if ($control) {
$control->checkId();
if ($control instanceof Scriptable) {
$control->setScriptEvents(true);
}
}
$this->control = $control;
return $this;
}
/**
* Get the script label name
*
* @api
* @return string
*/
public function getLabelName()
{
return $this->labelName;
}
/**
* Set the script label name
*
* @api
* @param string $labelName Script Label name
* @return static
*/
public function setLabelName($labelName)
{
$this->labelName = (string)$labelName;
return $this;
}
/**
* @see ScriptFeature::prepare()
*/
public function prepare(Script $script)
{
$script->appendGenericScriptLabel($this->labelName, $this->getScriptText());
return $this;
}
/**
* Get the script text
*
* @return string
*/
protected function getScriptText()
{
$actionName = Builder::escapeText($this->actionName);
if ($this->control) {
// Control event
$controlId = Builder::escapeText($this->control->getId());
$scriptText = "
2014-06-21 03:18:21 +02:00
if (Event.Control.ControlId == {$controlId}) {
TriggerPageAction({$actionName});
2014-04-27 14:44:40 +02:00
}";
2017-03-25 18:40:15 +01:00
} else {
// Other
$scriptText = "
2014-06-21 03:18:21 +02:00
TriggerPageAction({$actionName});";
2017-03-25 18:40:15 +01:00
}
return $scriptText;
}
2014-04-27 14:44:40 +02:00
}