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

AsyncLoader   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 5
dl 0
loc 36
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 12 3
A run() 0 11 1
A renderOverlay() 0 4 1
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