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