Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UI Input: getUpdateOnLoadCode for all Inputs (and some more associated things) #1933

Merged
merged 19 commits into from
Sep 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
5f570ac
getUpdateOnLoadCode for all Inputs
tfamula Jun 18, 2019
c3b2767
Make more Inputs to Filter Inputs
tfamula Jun 18, 2019
989feba
Removed Tag Input for now because of ugly rendering in Filters
tfamula Jun 18, 2019
379f911
Corrected PHPDoc for some functions
tfamula Jun 18, 2019
45980f3
Template for withonUpdate in Forms
tfamula Jun 18, 2019
dd9e651
Remove withonUpdate in Forms due to missing use case
tfamula Jun 18, 2019
8e743d1
Merge branch 'trunk' of https://github.com/ILIAS-eLearning/ILIAS into…
tfamula Jun 18, 2019
d80ffe0
Removed unnecessary resource
tfamula Jun 18, 2019
bde5047
Template for withonUpdate in Forms
tfamula Jun 18, 2019
0ca1470
Merge branch 'trunk' of https://github.com/ILIAS-eLearning/ILIAS into…
tfamula Jul 1, 2019
4cd591f
Merge branch 'trunk' of https://github.com/ILIAS-eLearning/ILIAS into…
tfamula Jul 2, 2019
3ed55a7
Merge branch 'trunk' of https://github.com/ILIAS-eLearning/ILIAS into…
tfamula Jul 9, 2019
07b6449
Made OnUpdate work in Forms without a special use case + getUpdateOnL…
tfamula Jul 17, 2019
2e524d6
Merge branch 'trunk' of https://github.com/ILIAS-eLearning/ILIAS into…
tfamula Jul 17, 2019
6f94004
Removed Signal stuff from Form
tfamula Jul 18, 2019
f694ce9
Improvements in FilterContextRenderer
tfamula Jul 18, 2019
6e3dd24
Merge branch 'trunk' of https://github.com/ILIAS-eLearning/ILIAS into…
tfamula Sep 4, 2019
6d44cd2
Resolving conflicts with trunk
tfamula Sep 4, 2019
733d2c7
Test fixes
tfamula Sep 4, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/UI/Component/Input/Field/MultiSelect.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
/**
* This describes a multi-select input.
*/
interface MultiSelect extends Input {
interface MultiSelect extends FilterInput {

/**
* Get options as value=>label.
Expand Down
2 changes: 1 addition & 1 deletion src/UI/Component/Input/Field/Numeric.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
/**
* This describes numeric inputs.
*/
interface Numeric extends Input {
interface Numeric extends FilterInput {

}
2 changes: 1 addition & 1 deletion src/UI/Component/Input/Field/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*
* @package ILIAS\UI\Component\Input\Field
*/
interface Tag extends Input, InputInternal {
interface Tag extends FilterInput, InputInternal {

/**
* @return string[] of tags such as [ 'Interesting', 'Boring', 'Animating', 'Repetitious' ]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace ILIAS\UI\Implementation\Component\Input\Container\Form;

use ILIAS\UI\Implementation\Render\AbstractComponentRenderer;
use ILIAS\UI\Component\Input\Container\Form;
use ILIAS\UI\Renderer as RendererInterface;
use ILIAS\UI\Component;

Expand All @@ -16,15 +17,15 @@ class Renderer extends AbstractComponentRenderer {
public function render(Component\Component $component, RendererInterface $default_renderer) {
$this->checkComponent($component);

if ($component instanceof Component\Input\Container\Form\Standard) {
if ($component instanceof Form\Standard) {
return $this->renderStandard($component, $default_renderer);
}

throw new \LogicException("Cannot render: " . get_class($component));
}


protected function renderStandard(Component\Input\Container\Form\Standard $component, RendererInterface $default_renderer) {
protected function renderStandard(Form\Standard $component, RendererInterface $default_renderer) {
$tpl = $this->getTemplate("tpl.standard.html", true, true);

if($component->getPostURL()!= ""){
Expand Down
15 changes: 15 additions & 0 deletions src/UI/Implementation/Component/Input/Field/Checkbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

use ILIAS\Data\Factory as DataFactory;
use ILIAS\UI\Component as C;
use ILIAS\UI\Component\Signal;
use ILIAS\UI\Implementation\Component\JavaScriptBindable;
use ILIAS\UI\Implementation\Component\Triggerer;
use ILIAS\UI\Implementation\Component\Input\InputData;
Expand Down Expand Up @@ -105,4 +106,18 @@ public function appendOnChange(C\Signal $signal) {
public function withOnLoad(C\Signal $signal) {
return $this->withTriggeredSignal($signal, 'load');
}

/**
* @inheritdoc
*/
public function getUpdateOnLoadCode(): \Closure
{
return function ($id) {
$code = "$('#$id').on('input', function(event) {
il.UI.input.onFieldUpdate(event, '$id', $('#$id').prop('checked').toString());
});
il.UI.input.onFieldUpdate(event, '$id', $('#$id').prop('checked').toString());";
return $code;
};
}
}
16 changes: 15 additions & 1 deletion src/UI/Implementation/Component/Input/Field/DateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,4 +246,18 @@ public function withAdditionalPickerconfig(array $config): DateTime
return $clone;
}

}
/**
* @inheritdoc
*/
public function getUpdateOnLoadCode(): \Closure
{
return function ($id) {
$code = "$('#$id').on('input dp.change', function(event) {
il.UI.input.onFieldUpdate(event, '$id', $('#$id').find('input').val());
});
il.UI.input.onFieldUpdate(event, '$id', $('#$id').find('input').val());";
return $code;
};
}

}
21 changes: 21 additions & 0 deletions src/UI/Implementation/Component/Input/Field/Duration.php
Original file line number Diff line number Diff line change
Expand Up @@ -317,4 +317,25 @@ protected function getConstraintForRequirement() {
return null;
}

/**
* @inheritdoc
*/
public function getUpdateOnLoadCode(): \Closure
{
return function ($id) {
$code = "var combinedDuration = function() {
var options = [];
$('#$id').find('input').each(function() {
options.push($(this).val());
});
return options.join(' - ');
}
$('#$id').on('input dp.change', function(event) {
il.UI.input.onFieldUpdate(event, '$id', combinedDuration());
});
il.UI.input.onFieldUpdate(event, '$id', combinedDuration());";
return $code;
};
}

}
100 changes: 80 additions & 20 deletions src/UI/Implementation/Component/Input/Field/FilterContextRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,34 @@ public function render(Component\Component $component, RendererInterface $defaul
}


/**
* @param Input $input
* @return Input|\ILIAS\UI\Implementation\Component\JavaScriptBindable
*/
protected function setSignals(Input $input) {
foreach ($input->getTriggeredSignals() as $s)
{
$signals[] = [
"signal_id" => $s->getSignal()->getId(),
"event" => $s->getEvent(),
"options" => $s->getSignal()->getOptions()
];
}
if ($signals !== null) {
$signals = json_encode($signals);


$input = $input->withAdditionalOnLoadCode(function ($id) use ($signals) {
$code = "il.UI.input.setSignalsForId('$id', $signals);";
return $code;
});

$input = $input->withAdditionalOnLoadCode($input->getUpdateOnLoadCode());
}
return $input;
}


/**
* @param Component\Input\Field\Input $input
* @param RendererInterface $default_renderer
Expand All @@ -49,8 +77,12 @@ protected function renderNoneGroupInput(Component\Input\Field\Input $input, Rend

if ($input instanceof Component\Input\Field\Text) {
$input_tpl = $this->getTemplate("tpl.text.html", true, true);
} elseif ($input instanceof Component\Input\Field\Numeric) {
$input_tpl = $this->getTemplate("tpl.numeric.html", true, true);
} elseif ($input instanceof Component\Input\Field\Select) {
$input_tpl = $this->getTemplate("tpl.select.html", true, true);
} elseif ($input instanceof Component\Input\Field\MultiSelect) {
$input_tpl = $this->getTemplate("tpl.multiselect.html", true, true);
} else {
throw new \LogicException("Cannot render '" . get_class($input) . "'");
}
Expand Down Expand Up @@ -121,7 +153,7 @@ protected function renderProxyFieldWithContext(Template $input_tpl, Input $input


/**
* @param Template $tpl
* @param Template $input_tpl
* @param Input $input
* @param RendererInterface $default_renderer
*
Expand Down Expand Up @@ -149,15 +181,17 @@ protected function renderProxyField(Template $input_tpl, Input $input, RendererI

/**
* @param Template $tpl
* @param Input $input
* @param RendererInterface $default_renderer
*
* @param Input $input
* @return string
*/
protected function renderInputField(Template $tpl, Input $input) {

$id = null;
$input = $this->setSignals($input);

switch (true) {
case ($input instanceof Text):
case ($input instanceof Numeric):
$tpl->setVariable("NAME", $input->getName());

if ($input->getValue() !== null) {
Expand All @@ -177,28 +211,51 @@ protected function renderInputField(Template $tpl, Input $input) {
$tpl = $this->renderSelectInput($tpl, $input);
break;

}
case ($input instanceof MultiSelect):
$tpl = $this->renderMultiSelectInput($tpl, $input);
break;

foreach ($input->getTriggeredSignals() as $s)
{
$signals[] = [
"signal_id" => $s->getSignal()->getId(),
"event" => $s->getEvent(),
"options" => $s->getSignal()->getOptions()
];
}
$signals = json_encode($signals);

$input = $input->withAdditionalOnLoadCode(function ($id) use ($signals) {
$code = "il.UI.input.setSignalsForId('$id', $signals);";
return $code;
});
$input = $input->withAdditionalOnLoadCode($input->getUpdateOnLoadCode());
$this->maybeRenderId($input, $tpl);
if ($id === null) {
$this->maybeRenderId($input, $tpl);
}

return $tpl->get();
}

/**
* @param Template $tpl
* @param MultiSelect $input
* @return Template
*/
public function renderMultiSelectInput(Template $tpl, MultiSelect $input) : Template {
$value = $input->getValue();
$name = $input->getName();

foreach ($input->getOptions() as $opt_value => $opt_label) {
$tpl->setCurrentBlock("option");
$tpl->setVariable("NAME", $name);
$tpl->setVariable("VALUE", $opt_value);
$tpl->setVariable("LABEL", $opt_label);

if($value && in_array($opt_value, $value)) {
$tpl->setVariable("CHECKED", 'checked="checked"');
}
if ($input->isDisabled()) {
$tpl->setVariable("DISABLED", 'disabled="disabled"');
}

$tpl->parseCurrentBlock();
}
return $tpl;
}

/**
* @param Template $tpl
* @param Select $input
* @return Template
*/
public function renderSelectInput(Template $tpl, Select $input)
{
if ($input->isDisabled()) {
Expand Down Expand Up @@ -235,6 +292,7 @@ public function renderSelectInput(Template $tpl, Select $input)
}

/**
* @param array $input_labels
* @param RendererInterface $default_renderer
*
* @return string
Expand Down Expand Up @@ -298,8 +356,10 @@ public function registerResources(ResourceRegistry $registry) {
protected function getComponentInterfaceName() {
return [
Component\Input\Field\Text::class,
Component\Input\Field\Numeric::class,
Component\Input\Field\Group::class,
Component\Input\Field\Select::class,
Component\Input\Field\Group::class
Component\Input\Field\MultiSelect::class
];
}
}
17 changes: 14 additions & 3 deletions src/UI/Implementation/Component/Input/Field/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,7 @@ public function withRequired($is_required) {
}

public function withOnUpdate(Signal $signal) {
//TODO: use $clone = parent::withOnUpdate($signal); once the exception there
//is solved.
$clone = $this->withTriggeredSignal($signal, 'update');
$clone = parent::withOnUpdate($signal);
$clone->inputs = array_map(function($i) use ($signal) {
return $i->withOnUpdate($signal);
}, $this->inputs);
Expand Down Expand Up @@ -208,4 +206,17 @@ public function getInputs() {
protected function getConstraintForRequirement() {
return null;
}

/**
* @inheritdoc
*/
public function getUpdateOnLoadCode(): \Closure
{
return function () {
/*
* Currently, there is no use case for Group here. The single Inputs
* within the Group are responsible for handling getUpdateOnLoadCode().
*/
};
}
}
31 changes: 0 additions & 31 deletions src/UI/Implementation/Component/Input/Field/Input.php
Original file line number Diff line number Diff line change
Expand Up @@ -424,34 +424,11 @@ public function getContent() {
return $this->content;
}

/**
* @inheritdoc
*/
public function getUpdateOnLoadCode(): \Closure
{
throw new \Exception(
"This is not implemented yet. Every Input needs to implement ".
"this, but to be able to move on currently this is broken. ".
"If you see this message, please file a bug at mantis.ilias.de. ".
"Also have a look into the roadmap: \"Implement `Input::getUpdateOnLoadCode`, ".
"`Input::withOnUpdate` and `Input::appendOnUpdate` for every Input"
);
// TODO: This method will need to be removed.
}

/**
* @inheritdoc
*/
public function withOnUpdate(Signal $signal)
{
// TODO: This exception will need to be removed.
throw new \Exception(
"This is not implemented yet. Every Input needs to implement ".
"this, but to be able to move on currently this is broken. ".
"If you see this message, please file a bug at mantis.ilias.de. ".
"Also have a look into the roadmap: \"Implement `Input::getUpdateOnLoadCode`, ".
"`Input::withOnUpdate` and `Input::appendOnUpdate` for every Input"
);
return $this->withTriggeredSignal($signal, 'update');
}

Expand All @@ -460,14 +437,6 @@ public function withOnUpdate(Signal $signal)
*/
public function appendOnUpdate(Signal $signal)
{
// TODO: This exception will need to be removed.
throw new \Exception(
"This is not implemented yet. Every Input needs to implement ".
"this, but to be able to move on currently this is broken. ".
"If you see this message, please file a bug at mantis.ilias.de. ".
"Also have a look into the roadmap: \"Implement `Input::getUpdateOnLoadCode`, ".
"`Input::withOnUpdate` and `Input::appendOnUpdate` for every Input"
);
return $this->appendTriggeredSignal($signal, 'update');
}
}
Loading