From 8211b98142adde0ca0c7161bc82e529c27c9aebc Mon Sep 17 00:00:00 2001 From: Richard Klees Date: Mon, 17 Jul 2017 15:28:04 +0200 Subject: [PATCH 01/10] UI: minor indentation --- src/UI/Implementation/Render/AbstractComponentRenderer.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/UI/Implementation/Render/AbstractComponentRenderer.php b/src/UI/Implementation/Render/AbstractComponentRenderer.php index b44a0b8964d5..82a636faf3cb 100644 --- a/src/UI/Implementation/Render/AbstractComponentRenderer.php +++ b/src/UI/Implementation/Render/AbstractComponentRenderer.php @@ -113,7 +113,7 @@ final protected function getTemplate($name, $purge_unfilled_vars, $purge_unused_ * @return string|null */ final protected function bindJavaScript(JavaScriptBindable $component) { - $binder = $component->getOnLoadCode(); + $binder = $component->getOnLoadCode(); if ($binder === null) { return null; } @@ -125,7 +125,7 @@ final protected function bindJavaScript(JavaScriptBindable $component) { " (used component: ".get_class($component).")"); } $this->js_binding->addOnLoadCode($on_load_code); - return $id; + return $id; } From 2221be0a94ee77f2ede748c23a7b318f7b06ab25 Mon Sep 17 00:00:00 2001 From: Richard Klees Date: Tue, 18 Jul 2017 07:45:37 +0200 Subject: [PATCH 02/10] UI: don't use caching in ilTemplate for testing. --- tests/UI/Renderer/ilIndependentTemplate.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/UI/Renderer/ilIndependentTemplate.php b/tests/UI/Renderer/ilIndependentTemplate.php index ee216b0255c6..39fa25e72470 100644 --- a/tests/UI/Renderer/ilIndependentTemplate.php +++ b/tests/UI/Renderer/ilIndependentTemplate.php @@ -10,6 +10,12 @@ require_once("./Services/UICore/classes/class.ilTemplate.php"); class ilIndependentTemplate extends ilTemplate implements \ILIAS\UI\Implementation\Render\Template { + function __construct($file,$flag1,$flag2,$in_module = false, $vars = "DEFAULT", + $plugin = false, $a_use_cache = true) + { + parent::__construct($file, $flag1, $flag2, $in_module, $vars, $plugin, false); + } + /** * Reads a file from disk and returns its content. * From 5f5a9d0b2eb8190431e5d469aa25b09c5a15abdc Mon Sep 17 00:00:00 2001 From: Richard Klees Date: Tue, 18 Jul 2017 07:59:20 +0200 Subject: [PATCH 03/10] UI: fixed some doc-string. --- src/UI/Component/JavaScriptBindable.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/UI/Component/JavaScriptBindable.php b/src/UI/Component/JavaScriptBindable.php index 635282ae949f..881282fad32f 100644 --- a/src/UI/Component/JavaScriptBindable.php +++ b/src/UI/Component/JavaScriptBindable.php @@ -37,7 +37,7 @@ interface JavaScriptBindable { * }); * * @param \Closure $binder - * @param self + * @return self */ public function withOnLoadCode(\Closure $binder); From dfc9485ee89c18d104ab6c8c39300f931d1ebef9 Mon Sep 17 00:00:00 2001 From: Richard Klees Date: Tue, 18 Jul 2017 08:05:19 +0200 Subject: [PATCH 04/10] UI: added JavaScriptBindable::withAdditionalOnLoadCode. --- src/UI/Component/JavaScriptBindable.php | 11 ++++++++ .../Component/JavaScriptBindable.php | 15 +++++++++++ tests/UI/Component/JavaScriptBindableTest.php | 25 +++++++++++++++++++ 3 files changed, 51 insertions(+) diff --git a/src/UI/Component/JavaScriptBindable.php b/src/UI/Component/JavaScriptBindable.php index 881282fad32f..ce6ae61f8d1f 100644 --- a/src/UI/Component/JavaScriptBindable.php +++ b/src/UI/Component/JavaScriptBindable.php @@ -41,6 +41,17 @@ interface JavaScriptBindable { */ public function withOnLoadCode(\Closure $binder); + /** + * Add some onload-code to the component instead of replacing the existing one. + * + * Must work like getOnLoadCode was called and the result of the existing binder + * (if any) and the result of the new binder are concatenated in a new binder. + * + * @param \Closure $binder + * @return self + */ + public function withAdditionalOnLoadCode(\Closure $binder); + /** * Get the currently bound on load code. * diff --git a/src/UI/Implementation/Component/JavaScriptBindable.php b/src/UI/Implementation/Component/JavaScriptBindable.php index dd27554f6073..bc38e7cdaa5c 100644 --- a/src/UI/Implementation/Component/JavaScriptBindable.php +++ b/src/UI/Implementation/Component/JavaScriptBindable.php @@ -24,6 +24,21 @@ public function withOnLoadCode(\Closure $binder) { return $clone; } + /** + * @see \ILIAS\UI\Component\JavaScriptBindable::withAdditionalOnLoadCode + */ + public function withAdditionalOnLoadCode(\Closure $binder) { + $current_binder = $this->getOnLoadCode(); + if ($current_binder === null) { + return $this->withOnLoadCode($binder); + } + + $this->checkBinder($binder); + return $this->withOnLoadCode(function($id) use ($current_binder, $binder) { + return $current_binder($id)."\n".$binder($id); + }); + } + /** * @see \ILIAS\UI\Component\JavaScriptBindable::getOnLoadCode */ diff --git a/tests/UI/Component/JavaScriptBindableTest.php b/tests/UI/Component/JavaScriptBindableTest.php index adaab3d6d9d4..583cadcea6d9 100644 --- a/tests/UI/Component/JavaScriptBindableTest.php +++ b/tests/UI/Component/JavaScriptBindableTest.php @@ -45,4 +45,29 @@ public function test_withOnLoadCode_false_closure_2() { $this->assertTrue(true); } } + + public function test_withAdditionalOnLoadCode() { + $m = $this->mock + ->withOnLoadCode(function($id) { + return "Its me, $id!"; + }) + ->withAdditionalOnLoadCode(function($id) { + return "And again, me: $id."; + }); + + $binder = $m->getOnLoadCode(); + $this->assertInstanceOf(\Closure::class, $binder); + $this->assertEquals("Its me, Mario!\nAnd again, me: Mario.", $binder("Mario")); + } + + public function test_withAdditionalOnLoadCode_no_previous() { + $m = $this->mock + ->withAdditionalOnLoadCode(function($id) { + return "And again, me: $id."; + }); + + $binder = $m->getOnLoadCode(); + $this->assertInstanceOf(\Closure::class, $binder); + $this->assertEquals("And again, me: Mario.", $binder("Mario")); + } } From 6c9ab38d61ab999c3635c105f9708659cb0c4604 Mon Sep 17 00:00:00 2001 From: Richard Klees Date: Tue, 18 Jul 2017 08:24:55 +0200 Subject: [PATCH 05/10] UI: some refactoring of Popover. --- src/UI/Component/Triggerable.php | 4 +- .../Component/Popover/Popover.php | 5 ++- .../Component/Popover/Renderer.php | 42 ++++++++++++------- .../Render/AbstractComponentRenderer.php | 1 - 4 files changed, 32 insertions(+), 20 deletions(-) diff --git a/src/UI/Component/Triggerable.php b/src/UI/Component/Triggerable.php index 8265a5c9f4d8..c428b4863495 100644 --- a/src/UI/Component/Triggerable.php +++ b/src/UI/Component/Triggerable.php @@ -12,7 +12,7 @@ * * @package ILIAS\UI\Component */ -interface Triggerable { +interface Triggerable extends JavaScriptBindable { /** * Get a component like this but reset (regenerate) its signals. @@ -21,4 +21,4 @@ interface Triggerable { */ public function withResetSignals(); -} \ No newline at end of file +} diff --git a/src/UI/Implementation/Component/Popover/Popover.php b/src/UI/Implementation/Component/Popover/Popover.php index 65afd8fbfebc..49a0d508baae 100644 --- a/src/UI/Implementation/Component/Popover/Popover.php +++ b/src/UI/Implementation/Component/Popover/Popover.php @@ -5,6 +5,7 @@ use \ILIAS\UI\Component; use ILIAS\UI\Component\Signal; use ILIAS\UI\Implementation\Component\ComponentHelper; +use ILIAS\UI\Implementation\Component\JavaScriptBindable; use ILIAS\UI\Implementation\Component\SignalGeneratorInterface; /** @@ -16,6 +17,8 @@ abstract class Popover implements Component\Popover\Popover { use ComponentHelper; + use JavaScriptBindable; + /** * @var string */ @@ -155,4 +158,4 @@ protected function initSignals() { $this->show_signal = $this->signal_generator->create(); $this->replace_content_signal = $this->signal_generator->create("ILIAS\\UI\\Implementation\\Component\\Popover\\ReplaceContentSignal"); } -} \ No newline at end of file +} diff --git a/src/UI/Implementation/Component/Popover/Renderer.php b/src/UI/Implementation/Component/Popover/Renderer.php index 6890168b77e6..45eba78b57fe 100755 --- a/src/UI/Implementation/Component/Popover/Renderer.php +++ b/src/UI/Implementation/Component/Popover/Renderer.php @@ -23,39 +23,49 @@ public function render(Component\Component $component, RendererInterface $defaul $tpl = $this->getTemplate('tpl.popover.html', true, true); $tpl->setVariable('FORCE_RENDERING', ''); /** @var Component\Popover\Popover $component */ + $options = array( 'title' => $this->escape($component->getTitle()), 'placement' => $component->getPosition(), 'multi' => true, 'template' => str_replace('"', '\"', $tpl->get()), ); - // Check if the content is rendered async or via DOM - $content_id = $this->createId(); - if ($component->getAsyncContentUrl()) { + + $is_async = $component->getAsyncContentUrl(); + if ($is_async) { $options['type'] = 'async'; $options['url'] = $component->getAsyncContentUrl(); - } else { - $options['url'] = "#{$content_id}"; } - $options = json_encode($options); + $show = $component->getShowSignal(); - $this->getJavascriptBinding()->addOnLoadCode(" - $(document).on('{$show}', function(event, signalData) { - il.UI.popover.showFromSignal(signalData, JSON.parse('{$options}')); - });"); $replace = $component->getReplaceContentSignal(); - $this->getJavascriptBinding()->addOnLoadCode(" - $(document).on('{$replace}', function(event, signalData) { - il.UI.popover.replaceContentFromSignal('{$show}', signalData); - });"); + + $component = $component->withAdditionalOnLoadCode(function($id) use ($options, $show, $replace, $is_async) { + if (!$is_async) { + $options["url"] = "#{$id}"; + } + $options = json_encode($options); + + return + "$(document).on('{$show}', function(event, signalData) { + il.UI.popover.showFromSignal(signalData, JSON.parse('{$options}')); + });". + "$(document).on('{$replace}', function(event, signalData) { + il.UI.popover.replaceContentFromSignal('{$show}', signalData); + });"; + }); + + $id = $this->bindJavaScript($component); + if ($component->getAsyncContentUrl()) { return ''; } + if ($component instanceof Component\Popover\Standard) { - return $this->renderStandardPopover($component, $default_renderer, $content_id); + return $this->renderStandardPopover($component, $default_renderer, $id); } else { if ($component instanceof Component\Popover\Listing) { - return $this->renderListingPopover($component, $default_renderer, $content_id); + return $this->renderListingPopover($component, $default_renderer, $id); } } diff --git a/src/UI/Implementation/Render/AbstractComponentRenderer.php b/src/UI/Implementation/Render/AbstractComponentRenderer.php index 82a636faf3cb..e3f872e39c1a 100644 --- a/src/UI/Implementation/Render/AbstractComponentRenderer.php +++ b/src/UI/Implementation/Render/AbstractComponentRenderer.php @@ -128,7 +128,6 @@ final protected function bindJavaScript(JavaScriptBindable $component) { return $id; } - /** * Create an ID * From 10fdb55eeee995b16c7e7ca8e3c0443f2776f1b8 Mon Sep 17 00:00:00 2001 From: Richard Klees Date: Tue, 18 Jul 2017 09:13:59 +0200 Subject: [PATCH 06/10] UI: some refactoring of modal. --- .../Component/Modal/Renderer.php | 30 +++++++++---------- tests/UI/Component/Modal/LightboxTest.php | 6 ++-- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/UI/Implementation/Component/Modal/Renderer.php b/src/UI/Implementation/Component/Modal/Renderer.php index 75177a0a8faa..6b3034ba6fc8 100644 --- a/src/UI/Implementation/Component/Modal/Renderer.php +++ b/src/UI/Implementation/Component/Modal/Renderer.php @@ -47,18 +47,18 @@ public function registerResources(ResourceRegistry $registry) { * @param Component\Modal\Modal $modal * @param string $id */ - protected function registerSignals(Component\Modal\Modal $modal, $id) { + protected function registerSignals(Component\Modal\Modal $modal) { $show = $modal->getShowSignal(); $close = $modal->getCloseSignal(); - $js = $this->getJavascriptBinding(); $options = json_encode(array( 'ajaxRenderUrl' => $modal->getAsyncRenderUrl(), 'keyboard' => $modal->getCloseWithKeyboard(), )); - $js->addOnLoadCode(" - $(document).on('{$show}', function() { il.UI.modal.showModal('{$id}', {$options}); return false; }); - $(document).on('{$close}', function() { il.UI.modal.closeModal('{$id}'); return false; });" - ); + return $modal->withAdditionalOnLoadCode(function($id) use ($show, $close, $options) { + return + "$(document).on('{$show}', function() { il.UI.modal.showModal('{$id}', {$options}); return false; });". + "$(document).on('{$close}', function() { il.UI.modal.closeModal('{$id}'); return false; });"; + }); } /** @@ -66,8 +66,8 @@ protected function registerSignals(Component\Modal\Modal $modal, $id) { * @return string */ protected function renderAsync(Component\Modal\Modal $modal) { - $id = $this->createId(); - $this->registerSignals($modal, $id); + $modal = $this->registerSignals($modal); + $id = $this->bindJavaScript($modal); return ""; } @@ -79,8 +79,8 @@ protected function renderAsync(Component\Modal\Modal $modal) { */ protected function renderInterruptive(Component\Modal\Interruptive $modal, RendererInterface $default_renderer) { $tpl = $this->getTemplate('tpl.interruptive.html', true, true); - $id = $this->createId(); - $this->registerSignals($modal, $id); + $modal = $this->registerSignals($modal); + $id = $this->bindJavaScript($modal); $this->triggerRegisteredSignals($modal, $id); $tpl->setVariable('ID', $id); $tpl->setVariable('FORM_ACTION', $modal->getFormAction()); @@ -113,8 +113,8 @@ protected function renderInterruptive(Component\Modal\Interruptive $modal, Rende */ protected function renderRoundTrip(Component\Modal\RoundTrip $modal, RendererInterface $default_renderer) { $tpl = $this->getTemplate('tpl.roundtrip.html', true, true); - $id = $this->createId(); - $this->registerSignals($modal, $id); + $modal = $this->registerSignals($modal); + $id = $this->bindJavaScript($modal); $this->triggerRegisteredSignals($modal, $id); $tpl->setVariable('ID', $id); $tpl->setVariable('TITLE', $modal->getTitle()); @@ -141,11 +141,11 @@ protected function renderRoundTrip(Component\Modal\RoundTrip $modal, RendererInt */ protected function renderLightbox(Component\Modal\Lightbox $modal, RendererInterface $default_renderer) { $tpl = $this->getTemplate('tpl.lightbox.html', true, true); - $id = $this->createId(); - $this->registerSignals($modal, $id); + $modal = $this->registerSignals($modal); + $id = $this->bindJavaScript($modal); $this->triggerRegisteredSignals($modal, $id); $tpl->setVariable('ID', $id); - $id_carousel = $this->createId(); + $id_carousel = "{$id}_carousel"; $pages = $modal->getPages(); $tpl->setVariable('TITLE', $pages[0]->getTitle()); $tpl->setVariable('ID_CAROUSEL', $id_carousel); diff --git a/tests/UI/Component/Modal/LightboxTest.php b/tests/UI/Component/Modal/LightboxTest.php index 88f7c15ff87d..398912ad02f6 100644 --- a/tests/UI/Component/Modal/LightboxTest.php +++ b/tests/UI/Component/Modal/LightboxTest.php @@ -45,7 +45,7 @@ protected function getExpectedHTML() {