Skip to content

Commit 5aed01d

Browse files
author
igor-chepurnoi
committed
added registerAssets function to LinkPreview widget, change run and init methods in the LinkPreview widget
1 parent 636c6aa commit 5aed01d

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

LinkPreview.php

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@
1414
* public function actions()
1515
* {
1616
* return [
17-
* 'link-preview' => LinkPreviewAction::className()
17+
* 'link-preview' => \yii2mod\linkpreview\actions\LinkPreview::className()
1818
* ];
1919
* }
20+
*
2021
* 2. Add widget to your page as follows:
21-
* echo \app\components\preview\LinkPreview::widget([
22+
* echo \yii2mod\linkpreview\LinkPreview::widget([
2223
* 'selector' => '#your-input-id or .someclass',
2324
* 'clientOptions' => [
2425
* 'previewActionUrl' => \yii\helpers\Url::to(['link-preview'])
@@ -55,16 +56,17 @@ class LinkPreview extends Widget
5556
*/
5657
public function init()
5758
{
59+
parent::init();
60+
5861
if (empty($this->id)) {
5962
throw new InvalidConfigException("The 'id' property is required.");
6063
}
64+
6165
if (empty($this->pjaxContainerId)) {
6266
throw new InvalidConfigException("The 'pjaxContainerId' property is required.");
6367
}
64-
echo $this->render($this->view, [
65-
'pjaxContainerId' => $this->pjaxContainerId,
66-
]);
67-
parent::init();
68+
69+
$this->registerAssets();
6870
}
6971

7072
/**
@@ -73,12 +75,21 @@ public function init()
7375
* @return string the result of widget execution to be outputted
7476
*/
7577
public function run()
78+
{
79+
return $this->render($this->view, [
80+
'pjaxContainerId' => $this->pjaxContainerId,
81+
]);
82+
}
83+
84+
/**
85+
* Register assets
86+
*/
87+
protected function registerAssets()
7688
{
7789
$view = $this->getView();
7890
LinkPreviewAsset::register($view);
7991
$options = $this->getClientOptions();
8092
$view->registerJs("$('{$this->selector}').linkPreview({$options});", $view::POS_END);
81-
parent::run();
8293
}
8394

8495
/**

views/template.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@
1010

1111
?>
1212
<?php Pjax::begin(['timeout' => 5000, 'enablePushState' => false, 'id' => $pjaxContainerId]); ?>
13-
<?php if (!empty($pageInfo)): ?>
13+
<?php if (!empty($pageInfo)) : ?>
1414
<div class="preview-container">
1515
<div class="media">
1616
<span title="Close" id="close-preview" class="close-preview-btn"></span>
1717
<div class="pull-left">
1818
<a href="<?php echo $pageInfo->getUrl(); ?>" class="preview-link-container" target="_blank">
1919
<?php echo Html::img($pageInfo->getImage(), ['id' => 'preview-image']); ?>
20-
<?php if ($pageInfo->getType() === 'video'): ?>
20+
<?php if ($pageInfo->getType() === 'video') : ?>
2121
<i class="video-play-inline"></i>
2222
<?php endif; ?>
2323
</a>

0 commit comments

Comments
 (0)