forked from joni-jones/yii2-wschat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChatWidget.php
58 lines (53 loc) · 1.4 KB
/
ChatWidget.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php
namespace jones\wschat;
use Yii;
use yii\base\Widget;
use yii\web\View;
use yii\helpers\Json;
/**
* Class ChatWidget
* @package jones\wschat
*/
class ChatWidget extends Widget
{
/**
* @var boolean set to true if widget will be run for auth users
*/
public $auth = false;
public $user_id = null;
public $view = 'index';
/** @var integer $port web socket port */
public $port = 8080;
/** @var array $chatList list of preloaded chats */
public $chatList = [
'id' => 1,
'title' => 'All'
];
/** @var string path to avatars folder */
public $imgPath = '@vendor/joni-jones/yii2-wschat/assets/img';
/**
* @override
*/
public function run()
{
$this->registerJsOptions();
Yii::$app->assetManager->publish($this->imgPath);
return $this->render($this->view, ['auth' => $this->auth]);
}
/**
* Register js variables
*
* @access protected
* @return void
*/
protected function registerJsOptions()
{
$opts = [
'var currentUserId = '.($this->user_id ?: 0).';',
'var port = '.$this->port.';',
'var chatList = '.Json::encode($this->chatList).';',
'var imgPath = "'.Yii::$app->assetManager->getPublishedUrl($this->imgPath).'";',
];
$this->getView()->registerJs(implode(' ', $opts), View::POS_BEGIN);
}
}