2014-05-18 19:45:50 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace FML\Script\Features;
|
|
|
|
|
|
|
|
use FML\Controls\Entry;
|
|
|
|
use FML\Controls\Label;
|
|
|
|
use FML\Script\Builder;
|
|
|
|
use FML\Script\Script;
|
|
|
|
use FML\Script\ScriptInclude;
|
|
|
|
use FML\Script\ScriptLabel;
|
|
|
|
|
|
|
|
/**
|
2014-06-21 03:18:21 +02:00
|
|
|
* Script Feature for creating a ValuePicker behavior
|
2014-05-18 19:45:50 +02:00
|
|
|
*
|
2014-05-20 15:44:45 +02:00
|
|
|
* @author steeffeen <mail@steeffeen.com>
|
2014-05-18 19:45:50 +02:00
|
|
|
* @copyright FancyManiaLinks Copyright © 2014 Steffen Schröder
|
|
|
|
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
|
|
|
|
*/
|
|
|
|
class ValuePickerFeature extends ScriptFeature {
|
|
|
|
/*
|
|
|
|
* Constants
|
|
|
|
*/
|
|
|
|
const FUNCTION_UPDATE_PICKER_VALUE = 'FML_UpdatePickerValue';
|
|
|
|
const VAR_PICKER_VALUES = 'FML_Picker_Values';
|
|
|
|
const VAR_PICKER_DEFAULT_VALUE = 'FML_Picker_Default_Value';
|
|
|
|
const VAR_PICKER_ENTRY_ID = 'FML_Picker_EntryId';
|
|
|
|
|
|
|
|
/*
|
2014-06-21 03:18:21 +02:00
|
|
|
* Protected properties
|
2014-05-18 19:45:50 +02:00
|
|
|
*/
|
|
|
|
/** @var Label $label */
|
|
|
|
protected $label = null;
|
|
|
|
/** @var Entry $entry */
|
|
|
|
protected $entry = null;
|
2014-06-21 03:18:21 +02:00
|
|
|
protected $values = array();
|
2014-05-18 19:45:50 +02:00
|
|
|
protected $default = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Construct a new ValuePicker Feature
|
|
|
|
*
|
|
|
|
* @param Label $label (optional) ValuePicker Label
|
|
|
|
* @param Entry $entry (optional) Hidden Entry
|
2014-06-21 03:18:21 +02:00
|
|
|
* @param array $values (optional) Possible values
|
|
|
|
* @param string $default (optional) Default value
|
2014-05-18 19:45:50 +02:00
|
|
|
*/
|
|
|
|
public function __construct(Label $label = null, Entry $entry = null, array $values = array(), $default = null) {
|
2014-06-21 03:18:21 +02:00
|
|
|
if (!is_null($label)) {
|
|
|
|
$this->setLabel($label);
|
|
|
|
}
|
|
|
|
if (!is_null($entry)) {
|
|
|
|
$this->setEntry($entry);
|
|
|
|
}
|
|
|
|
if (!empty($values)) {
|
|
|
|
$this->setValues($values);
|
|
|
|
}
|
|
|
|
if (!is_null($default)) {
|
|
|
|
$this->setDefault($default);
|
|
|
|
}
|
2014-05-18 19:45:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-20 15:44:45 +02:00
|
|
|
* Set the ValuePicker Label
|
2014-05-18 19:45:50 +02:00
|
|
|
*
|
2014-05-20 15:44:45 +02:00
|
|
|
* @param Label $label ValuePicker Label
|
2014-07-03 22:34:47 +02:00
|
|
|
* @return static
|
2014-05-18 19:45:50 +02:00
|
|
|
*/
|
2014-06-21 03:18:21 +02:00
|
|
|
public function setLabel(Label $label) {
|
|
|
|
$this->label = $label->checkId()->setScriptEvents(true);
|
2014-05-18 19:45:50 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the ValuePicker Label
|
|
|
|
*
|
|
|
|
* @return \FML\Controls\Label
|
|
|
|
*/
|
|
|
|
public function getLabel() {
|
|
|
|
return $this->label;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-20 15:44:45 +02:00
|
|
|
* Set the hidden Entry
|
2014-05-18 19:45:50 +02:00
|
|
|
*
|
2014-05-20 15:44:45 +02:00
|
|
|
* @param Entry $entry Hidden Entry
|
2014-07-03 22:34:47 +02:00
|
|
|
* @return static
|
2014-05-18 19:45:50 +02:00
|
|
|
*/
|
2014-06-21 03:18:21 +02:00
|
|
|
public function setEntry(Entry $entry) {
|
|
|
|
$this->entry = $entry->checkId();
|
2014-05-18 19:45:50 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the hidden Entry
|
|
|
|
*
|
|
|
|
* @return \FML\Controls\Entry
|
|
|
|
*/
|
|
|
|
public function getEntry() {
|
|
|
|
return $this->entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-06-21 03:18:21 +02:00
|
|
|
* Set the possible values
|
2014-05-18 19:45:50 +02:00
|
|
|
*
|
2014-06-21 03:18:21 +02:00
|
|
|
* @param array $values Possible values
|
2014-07-03 22:34:47 +02:00
|
|
|
* @return static
|
2014-05-18 19:45:50 +02:00
|
|
|
*/
|
2014-05-20 15:44:45 +02:00
|
|
|
public function setValues(array $values) {
|
|
|
|
$this->values = array();
|
|
|
|
foreach ($values as $value) {
|
|
|
|
array_push($this->values, (string)$value);
|
2014-05-18 19:45:50 +02:00
|
|
|
}
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2014-05-20 15:44:45 +02:00
|
|
|
/**
|
2014-06-21 03:18:21 +02:00
|
|
|
* Set the default value
|
2014-05-20 15:44:45 +02:00
|
|
|
*
|
2014-06-21 03:18:21 +02:00
|
|
|
* @param string $default Default value
|
2014-07-03 22:34:47 +02:00
|
|
|
* @return static
|
2014-05-20 15:44:45 +02:00
|
|
|
*/
|
|
|
|
public function setDefault($default) {
|
|
|
|
$this->default = (string)$default;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-06-21 03:18:21 +02:00
|
|
|
* Get the default value
|
2014-05-20 15:44:45 +02:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getDefault() {
|
|
|
|
if ($this->default) {
|
|
|
|
return $this->default;
|
|
|
|
}
|
2014-06-21 03:18:21 +02:00
|
|
|
if (!empty($this->values)) {
|
2014-05-20 15:44:45 +02:00
|
|
|
return reset($this->values);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2014-05-18 19:45:50 +02:00
|
|
|
/**
|
|
|
|
* @see \FML\Script\Features\ScriptFeature::prepare()
|
|
|
|
*/
|
|
|
|
public function prepare(Script $script) {
|
|
|
|
if ($this->label) {
|
|
|
|
$script->setScriptInclude(ScriptInclude::TEXTLIB);
|
|
|
|
$script->addScriptFunction(self::FUNCTION_UPDATE_PICKER_VALUE, $this->buildUpdatePickerValueFunction());
|
|
|
|
$script->appendGenericScriptLabel(ScriptLabel::ONINIT, $this->buildInitScriptText(), true);
|
2014-05-18 23:35:39 +02:00
|
|
|
$script->appendGenericScriptLabel(ScriptLabel::MOUSECLICK, $this->buildClickScriptText());
|
2014-05-18 19:45:50 +02:00
|
|
|
}
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-06-21 03:18:21 +02:00
|
|
|
* Build the function text
|
2014-05-18 19:45:50 +02:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function buildUpdatePickerValueFunction() {
|
2014-06-21 03:18:21 +02:00
|
|
|
return "
|
2014-05-18 19:45:50 +02:00
|
|
|
Void " . self::FUNCTION_UPDATE_PICKER_VALUE . "(CMlLabel _Label) {
|
|
|
|
declare " . self::VAR_PICKER_VALUES . " as Values for _Label = Text[];
|
2014-05-18 23:35:39 +02:00
|
|
|
declare NewValueIndex = -1;
|
|
|
|
if (Values.exists(_Label.Value)) {
|
|
|
|
declare ValueIndex = Values.keyof(_Label.Value);
|
2014-05-18 19:45:50 +02:00
|
|
|
ValueIndex += 1;
|
|
|
|
if (Values.existskey(ValueIndex)) {
|
|
|
|
NewValueIndex = ValueIndex;
|
2014-05-18 23:35:39 +02:00
|
|
|
} else {
|
|
|
|
NewValueIndex = 0;
|
2014-05-18 19:45:50 +02:00
|
|
|
}
|
|
|
|
}
|
2014-06-21 03:18:21 +02:00
|
|
|
declare NewValue = " . Builder::EMPTY_STRING . ";
|
2014-05-18 19:45:50 +02:00
|
|
|
if (Values.existskey(NewValueIndex)) {
|
|
|
|
NewValue = Values[NewValueIndex];
|
|
|
|
} else {
|
2014-06-21 03:18:21 +02:00
|
|
|
declare " . self::VAR_PICKER_DEFAULT_VALUE . " as Default for _Label = " . Builder::EMPTY_STRING . ";
|
2014-05-18 19:45:50 +02:00
|
|
|
NewValue = Default;
|
|
|
|
}
|
|
|
|
_Label.Value = NewValue;
|
2014-06-21 03:18:21 +02:00
|
|
|
declare " . self::VAR_PICKER_ENTRY_ID . " as EntryId for _Label = " . Builder::EMPTY_STRING . ";
|
|
|
|
if (EntryId != " . Builder::EMPTY_STRING . ") {
|
2014-05-18 19:45:50 +02:00
|
|
|
declare Entry <=> (Page.GetFirstChild(EntryId) as CMlEntry);
|
|
|
|
Entry.Value = NewValue;
|
|
|
|
}
|
|
|
|
}";
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-06-21 03:18:21 +02:00
|
|
|
* Build the init script text
|
2014-05-18 19:45:50 +02:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function buildInitScriptText() {
|
2014-06-21 03:18:21 +02:00
|
|
|
$labelId = $this->label->getId(true, true);
|
|
|
|
$entryId = '""';
|
2014-05-18 19:45:50 +02:00
|
|
|
if ($this->entry) {
|
2014-06-21 03:18:21 +02:00
|
|
|
$entryId = $this->entry->getId(true, true);
|
2014-05-18 19:45:50 +02:00
|
|
|
}
|
2014-06-21 03:18:21 +02:00
|
|
|
$values = Builder::getArray($this->values);
|
|
|
|
$default = Builder::escapeText($this->getDefault(), true);
|
|
|
|
return "
|
|
|
|
declare Label_Picker <=> (Page.GetFirstChild({$labelId}) as CMlLabel);
|
2014-05-18 19:45:50 +02:00
|
|
|
declare Text[] " . self::VAR_PICKER_VALUES . " as Values for Label_Picker;
|
|
|
|
Values = {$values};
|
|
|
|
declare Text " . self::VAR_PICKER_DEFAULT_VALUE . " as Default for Label_Picker;
|
2014-06-21 03:18:21 +02:00
|
|
|
Default = {$default};
|
2014-05-18 19:45:50 +02:00
|
|
|
declare Text " . self::VAR_PICKER_ENTRY_ID . " as EntryId for Label_Picker;
|
2014-06-21 03:18:21 +02:00
|
|
|
EntryId = {$entryId};
|
2014-05-18 19:45:50 +02:00
|
|
|
" . self::FUNCTION_UPDATE_PICKER_VALUE . "(Label_Picker);
|
|
|
|
";
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-06-21 03:18:21 +02:00
|
|
|
* Build the script text for Label clicks
|
2014-05-18 19:45:50 +02:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function buildClickScriptText() {
|
2014-06-21 03:18:21 +02:00
|
|
|
$labelId = $this->label->getId(true, true);
|
|
|
|
return "
|
|
|
|
if (Event.ControlId == {$labelId}) {
|
2014-05-18 19:45:50 +02:00
|
|
|
declare Label_Picker <=> (Event.Control as CMlLabel);
|
|
|
|
" . self::FUNCTION_UPDATE_PICKER_VALUE . "(Label_Picker);
|
|
|
|
}";
|
|
|
|
}
|
|
|
|
}
|