Skip to content

Commit

Permalink
added registerAssets function to LinkPreview widget, change run and i…
Browse files Browse the repository at this point in the history
…nit methods in the LinkPreview widget
  • Loading branch information
igor-chepurnoi committed Nov 22, 2016
1 parent 636c6aa commit 5aed01d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
25 changes: 18 additions & 7 deletions LinkPreview.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@
* public function actions()
* {
* return [
* 'link-preview' => LinkPreviewAction::className()
* 'link-preview' => \yii2mod\linkpreview\actions\LinkPreview::className()
* ];
* }
*
* 2. Add widget to your page as follows:
* echo \app\components\preview\LinkPreview::widget([
* echo \yii2mod\linkpreview\LinkPreview::widget([
* 'selector' => '#your-input-id or .someclass',
* 'clientOptions' => [
* 'previewActionUrl' => \yii\helpers\Url::to(['link-preview'])
Expand Down Expand Up @@ -55,16 +56,17 @@ class LinkPreview extends Widget
*/
public function init()
{
parent::init();

if (empty($this->id)) {
throw new InvalidConfigException("The 'id' property is required.");
}

if (empty($this->pjaxContainerId)) {
throw new InvalidConfigException("The 'pjaxContainerId' property is required.");
}
echo $this->render($this->view, [
'pjaxContainerId' => $this->pjaxContainerId,
]);
parent::init();

$this->registerAssets();
}

/**
Expand All @@ -73,12 +75,21 @@ public function init()
* @return string the result of widget execution to be outputted
*/
public function run()
{
return $this->render($this->view, [
'pjaxContainerId' => $this->pjaxContainerId,
]);
}

/**
* Register assets
*/
protected function registerAssets()
{
$view = $this->getView();
LinkPreviewAsset::register($view);
$options = $this->getClientOptions();
$view->registerJs("$('{$this->selector}').linkPreview({$options});", $view::POS_END);
parent::run();
}

/**
Expand Down
4 changes: 2 additions & 2 deletions views/template.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@

?>
<?php Pjax::begin(['timeout' => 5000, 'enablePushState' => false, 'id' => $pjaxContainerId]); ?>
<?php if (!empty($pageInfo)): ?>
<?php if (!empty($pageInfo)) : ?>
<div class="preview-container">
<div class="media">
<span title="Close" id="close-preview" class="close-preview-btn"></span>
<div class="pull-left">
<a href="<?php echo $pageInfo->getUrl(); ?>" class="preview-link-container" target="_blank">
<?php echo Html::img($pageInfo->getImage(), ['id' => 'preview-image']); ?>
<?php if ($pageInfo->getType() === 'video'): ?>
<?php if ($pageInfo->getType() === 'video') : ?>
<i class="video-play-inline"></i>
<?php endif; ?>
</a>
Expand Down

0 comments on commit 5aed01d

Please sign in to comment.