1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace hauntd\vote\widgets; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @author Alexander Kononenko <[email protected]> |
9
|
|
|
* @package hauntd\vote\widgets |
10
|
|
|
*/ |
11
|
|
|
class Vote extends BaseWidget |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
public $jsCodeKey = 'vote'; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @return array |
20
|
|
|
*/ |
21
|
|
|
public function getDefaultOptions() |
22
|
|
|
{ |
23
|
|
|
return [ |
24
|
|
|
'class' => 'vote', |
25
|
|
|
]; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @inherit |
30
|
|
|
*/ |
31
|
|
|
public function init() |
32
|
|
|
{ |
33
|
|
|
parent::init(); |
34
|
|
|
$this->options = array_merge($this->getDefaultOptions(), $this->options); |
35
|
|
|
$this->initJsEvents($this->getSelector()); |
36
|
|
|
$this->registerJs(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @return string |
41
|
|
|
* @throws \yii\base\InvalidConfigException |
42
|
|
|
*/ |
43
|
|
|
public function run() |
44
|
|
|
{ |
45
|
|
|
return $this->render($this->viewFile, $this->getViewParams([ |
46
|
|
|
'jsCodeKey' => $this->jsCodeKey, |
47
|
|
|
'entity' => $this->entity, |
48
|
|
|
'model' => $this->model, |
49
|
|
|
'targetId' => $this->targetId, |
50
|
|
|
'userValue' => $this->userValue, |
51
|
|
|
'positive' => isset($this->aggregateModel->positive) ? $this->aggregateModel->positive : 0, |
52
|
|
|
'negative' => isset($this->aggregateModel->negative) ? $this->aggregateModel->negative : 0, |
53
|
|
|
'rating' => isset($this->aggregateModel->rating) ? $this->aggregateModel->rating : 0.0, |
54
|
|
|
'options' => $this->options, |
55
|
|
|
])); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Initialize with default events. |
60
|
|
|
* |
61
|
|
|
* @param string $selector |
62
|
|
|
*/ |
63
|
|
|
public function initJsEvents($selector) |
64
|
|
|
{ |
65
|
|
|
if (!isset($this->jsBeforeVote)) { |
66
|
|
|
$this->jsBeforeVote = " |
67
|
|
|
$('$selector .vote-btn').prop('disabled', 'disabled').addClass('vote-loading'); |
68
|
|
|
$('$selector .vote-count') |
69
|
|
|
.addClass('vote-loading') |
70
|
|
|
.append('<div class=\"vote-loader\"><span></span><span></span><span></span></div>'); |
71
|
|
|
"; |
72
|
|
|
} |
73
|
|
|
if (!isset($this->jsAfterVote)) { |
74
|
|
|
$this->jsAfterVote = " |
75
|
|
|
$('$selector .vote-btn').prop('disabled', false).removeClass('vote-loading'); |
76
|
|
|
$('$selector .vote-count').removeClass('vote-loading').find('.vote-loader').remove(); |
77
|
|
|
"; |
78
|
|
|
} |
79
|
|
|
if (!isset($this->jsChangeCounters)) { |
80
|
|
|
$this->jsChangeCounters = " |
81
|
|
|
if (data.success) { |
82
|
|
|
$('$selector .vote-count span').text(data.aggregate.positive - data.aggregate.negative); |
83
|
|
|
vote.find('button').removeClass('vote-active'); |
84
|
|
|
button.addClass('vote-active'); |
85
|
|
|
} |
86
|
|
|
"; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|