diff --git a/LinkPreview.php b/LinkPreview.php index f40e0c3..7acbdc7 100644 --- a/LinkPreview.php +++ b/LinkPreview.php @@ -20,7 +20,7 @@ * } * 2. Add widget to your page as follows: * echo \app\components\preview\LinkPreview::widget([ - * 'id' => 'your-input-id', + * 'selector' => '#your-input-id or .someclass', * 'clientOptions' => [ * 'previewActionUrl' => \yii\helpers\Url::to(['link-preview']) * ], @@ -30,9 +30,9 @@ class LinkPreview extends Widget { /** - * @var string your input or textArea id + * @var string input selector */ - public $id; + public $selector; /** * Template view name @@ -45,15 +45,25 @@ class LinkPreview extends Widget */ public $clientOptions = []; + /** + * @var string pjax container id + */ + public $pjaxContainerId = 'link-preview-container'; + /** * Init function */ public function init() { - if ($this->id === null) { + if (empty($this->id)) { throw new InvalidConfigException("The 'id' property is required."); } - echo $this->render($this->view); + if (empty($this->pjaxContainerId)) { + throw new InvalidConfigException("The 'pjaxContainerId' property is required."); + } + echo $this->render($this->view, [ + 'pjaxContainerId' => $this->pjaxContainerId + ]); parent::init(); } @@ -63,12 +73,21 @@ public function init() */ public function run() { - $id = $this->id; $view = $this->getView(); LinkPreviewAsset::register($view); - $options = Json::encode($this->clientOptions); - $view->registerJs("$('#{$id}').linkPreview({$options});", $view::POS_END); + $options = $this->getClientOptions(); + $view->registerJs("$('{$this->selector}').linkPreview({$options});", $view::POS_END); parent::run(); } + /** + * Get client options + * @return string + */ + protected function getClientOptions() + { + $this->clientOptions['pjaxContainer'] = '#' . $this->pjaxContainerId; + return Json::encode($this->clientOptions); + } + }