Skip to content

Commit

Permalink
Update LinkPreview.php
Browse files Browse the repository at this point in the history
  • Loading branch information
Igor Chepurnoy committed Oct 1, 2015
1 parent 9596a98 commit 7fde012
Showing 1 changed file with 27 additions and 8 deletions.
35 changes: 27 additions & 8 deletions LinkPreview.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'])
* ],
Expand All @@ -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
Expand All @@ -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();
}

Expand All @@ -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);
}

}

0 comments on commit 7fde012

Please sign in to comment.