Completed
Push — master ( a32828...9d663d )
by Dmitry
02:55
created

AsyncLoader::run()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 0
1
<?php
2
3
namespace hipanel\widgets;
4
5
use yii\base\InvalidConfigException;
6
use yii\base\Widget;
7
use yii\helpers\Json;
8
use yii\helpers\Url;
9
10
class AsyncLoader extends Widget
11
{
12
    public $route;
13
14
    public $containerSelector;
15
16
    public function init()
17
    {
18
        parent::init();
19
20
        if (!isset($this->route)) {
21
            throw new InvalidConfigException('The "url" property is not set');
22
        }
23
24
        if (!isset($this->containerSelector)) {
25
            throw new InvalidConfigException('The "containerSelector" property is not set');
26
        }
27
    }
28
29
    public function run()
30
    {
31
        $url = Json::htmlEncode(Url::to($this->route));
32
        $selector = Json::htmlEncode($this->containerSelector);
33
34
        $this->view->registerJs("$.get($url, function (data) {
35
            $($selector).replaceWith(data);
36
        });");
37
38
        return $this->renderOverlay();
39
    }
40
41
    private function renderOverlay()
42
    {
43
        return '<div class="overlay"><i class="fa fa-refresh fa-spin"></i></div>';
44
    }
45
}
46