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

113 lines
2.5 KiB
PHP
Raw 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
/**
2014-06-21 03:18:21 +02: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
2014-04-27 14:44:40 +02:00
* @copyright FancyManiaLinks Copyright © 2014 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
*/
class ActionTrigger extends ScriptFeature {
/*
2014-06-21 03:18:21 +02:00
* Protected properties
2014-04-27 14:44:40 +02:00
*/
protected $actionName = null;
2014-05-14 23:24:00 +02:00
/** @var Control $control */
2014-04-27 14:44:40 +02:00
protected $control = null;
protected $labelName = null;
/**
* Construct a new Action Trigger Feature
*
2014-06-21 03:18:21 +02:00
* @param string $actionName (optional) Triggered action
2014-05-14 23:24:00 +02:00
* @param Control $control (optional) Action Control
2014-06-21 03:18:21 +02:00
* @param string $labelName (optional) Script Label name
2014-04-27 14:44:40 +02:00
*/
public function __construct($actionName = null, Control $control = null, $labelName = ScriptLabel::MOUSECLICK) {
2014-08-11 23:15:53 +02:00
if ($actionName !== null) {
2014-06-21 03:18:21 +02:00
$this->setActionName($actionName);
}
2014-08-11 23:15:53 +02:00
if ($control !== null) {
2014-06-21 03:18:21 +02:00
$this->setControl($control);
}
2014-08-11 23:15:53 +02:00
if ($labelName !== null) {
2014-06-21 03:18:21 +02:00
$this->setLabelName($labelName);
}
2014-04-27 14:44:40 +02:00
}
/**
2014-06-21 03:18:21 +02:00
* Set the action to trigger
2014-04-27 14:44:40 +02:00
*
* @param string $actionName
2014-07-03 22:34:47 +02:00
* @return static
2014-04-27 14:44:40 +02:00
*/
public function setActionName($actionName) {
2014-06-21 03:18:21 +02:00
$this->actionName = (string)$actionName;
2014-04-27 14:44:40 +02:00
return $this;
}
/**
* Set the Control
2014-05-14 23:24:00 +02:00
*
2014-04-27 14:44:40 +02:00
* @param Control $control Action Control
2014-07-03 22:34:47 +02:00
* @return static
2014-04-27 14:44:40 +02:00
*/
public function setControl(Control $control) {
$control->checkId();
2014-05-14 23:24:00 +02:00
if ($control instanceof Scriptable) {
$control->setScriptEvents(true);
}
2014-04-27 14:44:40 +02:00
$this->control = $control;
return $this;
}
/**
2014-06-21 03:18:21 +02:00
* Set the label name
2014-04-27 14:44:40 +02:00
*
2014-06-21 03:18:21 +02:00
* @param string $labelName Script Label name
2014-07-03 22:34:47 +02:00
* @return static
2014-04-27 14:44:40 +02:00
*/
public function setLabelName($labelName) {
2014-06-21 03:18:21 +02:00
$this->labelName = (string)$labelName;
2014-04-27 14:44:40 +02:00
return $this;
}
/**
* @see \FML\Script\Features\ScriptFeature::prepare()
*/
public function prepare(Script $script) {
$script->appendGenericScriptLabel($this->labelName, $this->getScriptText());
return $this;
}
/**
2014-06-21 03:18:21 +02:00
* Get the script text
2014-04-27 14:44:40 +02:00
*
* @return string
*/
protected function getScriptText() {
2014-06-21 03:18:21 +02:00
$actionName = Builder::escapeText($this->actionName, true);
2014-04-27 14:44:40 +02:00
if ($this->control) {
// Control event
2014-06-21 03:18:21 +02:00
$controlId = Builder::escapeText($this->control->getId(), true);
2014-04-27 14:44:40 +02:00
$scriptText = "
2014-06-21 03:18:21 +02:00
if (Event.Control.ControlId == {$controlId}) {
TriggerPageAction({$actionName});
2014-04-27 14:44:40 +02:00
}";
2014-05-14 23:24:00 +02:00
} else {
2014-04-27 14:44:40 +02:00
// Other
$scriptText = "
2014-06-21 03:18:21 +02:00
TriggerPageAction({$actionName});";
2014-04-27 14:44:40 +02:00
}
return $scriptText;
}
}