Skip to content

Commit

Permalink
Always observe Modal changes (#1881)
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek authored Oct 4, 2022
1 parent 9c19eff commit 9256fbd
Show file tree
Hide file tree
Showing 7 changed files with 12 additions and 114 deletions.
3 changes: 1 addition & 2 deletions demos/interactive/modal.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@

$transitionModal = Modal::addTo($app, ['title' => 'Animated modal']);
Message::addTo($transitionModal)->set('A lot of animated transition available');
$transitionModal->duration(1000);
$transitionModal->setOption('duration', 1000);

$menuBar = View::addTo($app, ['ui' => 'buttons']);
$main = Menu::addTo($menuBar);
Expand Down Expand Up @@ -156,7 +156,6 @@

// Add modal to layout.
$stepModal = Modal::addTo($app, ['title' => 'Multi step actions']);
$stepModal->setOption('observeChanges', true);

// Add buttons to modal for next and previous actions.
$action = new View(['ui' => 'buttons']);
Expand Down
1 change: 0 additions & 1 deletion docs/js.rst
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,6 @@ Modal
.. php:method:: addContentCss()
.. php:method:: addScrolling()
.. php:method:: setOption()
.. php:method:: setOptions()
This class allows you to open modal dialogs and close them easily. It's based around Fomantic-UI
`.modal(), <https://fomantic-ui.com/modules/modal.html>`_ but integrates PHP callback for dynamically
Expand Down
106 changes: 11 additions & 95 deletions src/Modal.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
* $modal->addApproveAction('Yes', new JsExpression('function() { window.alert("You\'re good to go!"); }'));
*
* You may also prevent modal from closing via the esc or dimmed area click using $modal->notClosable().
*
* Some helper methods are also available to set: transition time, transition type or modal settings from Fomantic-UI.
*/
class Modal extends View
{
Expand All @@ -47,7 +45,11 @@ class Modal extends View
/** @var array */
public $args = [];
/** @var array */
public $options = [];
public $options = [
// any change in modal DOM should automatically refresh cached positions
// allow modal window to add scrolling when content is added after modal is created
'observeChanges' => true,
];

/** @var string Currently only "json" response type is supported. */
public $type = 'json';
Expand Down Expand Up @@ -160,38 +162,7 @@ public function hide()
*/
public function setOption($option, $value)
{
$this->options['modal_option'][$option] = $value;

return $this;
}

/**
* Set modal options passing an array.
*
* @param array<string, mixed> $options
*
* @return $this
*/
public function setOptions($options)
{
if (isset($this->options['modal_option'])) {
$this->options['modal_option'] = array_merge($this->options['modal_option'], $options);
} else {
$this->options['modal_option'] = $options;
}

return $this;
}

/**
* Whether any change in modal DOM should automatically refresh cached positions.
* Allow modal window to add scrolling when adding content dynamically after modal creation.
*
* @return $this
*/
public function observeChanges()
{
$this->setOptions(['observeChanges' => true]);
$this->options[$option] = $value;

return $this;
}
Expand All @@ -208,49 +179,6 @@ public function addScrolling()
return $this;
}

/**
* Set modal transition.
*
* @param string $transitionType
*
* @return $this
*/
public function transition($transitionType)
{
$this->settings('transition', $transitionType);

return $this;
}

/**
* Set modal transition duration.
*
* @param float|int $time
*
* @return $this
*/
public function duration($time)
{
$this->settings('duration', $time);

return $this;
}

/**
* Add modal settings.
*
* @param string $settingOption
* @param mixed $value
*
* @return $this
*/
public function settings($settingOption, $value)
{
$this->options['setting'][$settingOption] = $value;

return $this;
}

/**
* Add a deny action to modal.
*
Expand All @@ -264,7 +192,7 @@ public function addDenyAction($label, $jsAction)
$button = new Button();
$button->set($label)->addClass('red cancel');
$this->addButtonAction($button);
$this->options['modal_option']['onDeny'] = $jsAction;
$this->options['onDeny'] = $jsAction;

return $this;
}
Expand All @@ -282,7 +210,7 @@ public function addApproveAction($label, $jsAction)
$b = new Button();
$b->set($label)->addClass('green ok');
$this->addButtonAction($b);
$this->options['modal_option']['onApprove'] = $jsAction;
$this->options['onApprove'] = $jsAction;

return $this;
}
Expand All @@ -309,7 +237,7 @@ public function addButtonAction($button)
*/
public function notClosable()
{
$this->options['modal_option']['closable'] = false;
$this->options['closable'] = false;

return $this;
}
Expand Down Expand Up @@ -337,21 +265,9 @@ protected function renderView(): void
$this->template->del('ActionContainer');
}

// call modal creation first
if (isset($this->options['modal_option'])) {
$this->js(true)->modal($this->options['modal_option']);
} else {
$this->js(true)->modal();
}

// add setting if available.
if (isset($this->options['setting'])) {
foreach ($this->options['setting'] as $key => $value) {
$this->js(true)->modal('setting', $key, $value);
}
}
$this->js(true)->modal($this->options);

if (!isset($this->options['modal_option']['closable']) || $this->options['modal_option']['closable']) {
if (!isset($this->options['closable']) || $this->options['closable']) {
$this->template->trySet('closeIcon', 'close');
}

Expand Down
12 changes: 0 additions & 12 deletions src/Popup.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,18 +212,6 @@ public function setOption(string $name, $option)
return $this;
}

/**
* Setting options using using an array.
*
* @return $this
*/
public function setOptions(array $options)
{
$this->popOptions = array_merge($this->popOptions, $options);

return $this;
}

/**
* Return js action need to display popup.
* When a grid is reloading, this method can be call
Expand Down
2 changes: 0 additions & 2 deletions src/Table/Column/ActionButtons.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,6 @@ public function addModal($button, $defaults, \Closure $callback, $owner = null,

$modal = Modal::addTo($owner, $defaults);

$modal->observeChanges(); // adds scrollbar if needed

$modal->set(function (View $t) use ($callback) {
$callback($t, $t->stickyGet($this->name));
});
Expand Down
1 change: 0 additions & 1 deletion src/UserAction/ConfirmationExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ protected function init(): void
{
parent::init();

$this->observeChanges();
$this->addClass($this->size);
}

Expand Down
1 change: 0 additions & 1 deletion src/UserAction/ModalExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ protected function init(): void

protected function initExecutor(): void
{
$this->observeChanges();
}

public function getAction(): Model\UserAction
Expand Down

0 comments on commit 9256fbd

Please sign in to comment.