-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathYii2TimerCountDown.php
188 lines (161 loc) · 5.6 KB
/
Yii2TimerCountDown.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<?php
namespace aneeshikmat\yii2\Yii2TimerCountDown;
use yii\base\Widget;
use yii\db\Exception;
class Yii2TimerCountDown extends Widget
{
/**
* @var This option give you apilty to change default timer wrapper,
* its usfall if you have more than one timer in the same html page,
* default selctor value is 'time-down-counter', and this selector must be an ID.
*/
public $countDownIdSelector = 'time-down-counter';
/*
* @var milisecond time
* This option will accept count down date in millisecond, if you keep it empty the default value will be current time
*/
public $countDownDate;
/*
* @var bool
* This option give you ability to set current Time from server.
*/
public $addServerTime = false;
/*
* @var milisecond time
* This option will accept start of count down in millisecond, if you keep it empty the default value will be current time
*/
public $now;
/*
* @var string
* This option give you apilty to change time Sperator, default sperator is : nested in tag.
*/
public $countDownResSperator = '<span class="timeDownSperator">:</span>';
/*
* @var string
* This option give you apilty to display full timer result (days, hours, minutes, seconds),
* or (hours, minutes, seconds) or (minutes, seconds),
* and accpet options is ('from-days' is default, 'from-hours', 'from-minutes').
* Note: This option will keep timer work nomraly without removed any value, just hide / show option.
*/
public $countDownReturnData = 'from-days';
/*
* @var bool
* This option give you ability to set each number group in tag contain general class called item-counter-down.
*/
public $addSpanForResult = false;
/*
* @var bool
* This option give you apilty to set each number in timer in tag contain general class called inner-item-counter-down.
*/
public $addSpanForEachNum = false;
/*
* @var string
* This option give you apilty to update message for count down over (when timer is finshed).
*/
public $countDownOver = "EXPIRED";
/*
* @var string
* his option give you apilty to stop count down timer,
* the default value is 0 and thats mean timer work,
* 1 is mean stop timer and display timer result,
* 2 is mean stop timer and display html timer result.
*/
public $getTemplateResult = 0;
/*
* @var string contain js callback script
*/
public $callBack = '';
/**
* @var Integer to determind template Style
* 0: Default time down counter
* 1: Template - 1
* 2: Template - 2
*/
public $templateStyle = 0;
/**
* Initializes the view.
*/
public function init(){
parent::init();
if(empty($this->countDownDate)){
$this->countDownDate = time() * 1000;
}
if($this->addServerTime){
$this->now = time() * 1000;
}
// If you change default template or you need to use template, you most has an inner spans
if(!empty($this->templateStyle)){
$this->addSpanForEachNum = $this->addSpanForResult = true;
}
}
/**
* Runs the widget.
*/
public function run()
{
$this->registerClientScript();
}
public function registerClientScript()
{
$view = $this->getView();
Yii2TimerCountDownAsset::register($view);
$js = <<<JS
timeDownCounter({
'countDownIdSelector': '$this->countDownIdSelector',
'countDownDate': '$this->countDownDate',
'now': '$this->now',
'countDownResSperator': '$this->countDownResSperator',
'countDownReturnData': '$this->countDownReturnData',
'addSpanForResult': '$this->addSpanForResult',
'addSpanForEachNum': '$this->addSpanForEachNum',
'countDownOver': '$this->countDownOver',
'getTemplateResult': '$this->getTemplateResult'
}, function(){
$this->callBack
}).startCountDown();
JS;
$template1 = <<<temp1
#$this->countDownIdSelector .item-counter-down {
background: #c0c0c0;
display: inline-block;
padding: 15px;
font-size: 45px;
color: #fff;
border-radius: 20%;
margin: 0 5px;
}
#$this->countDownIdSelector .timeDownSperator {
font-size: 45px;
color: #c0c0c0;
font-weight: bold;
}
temp1;
$template2 = <<<temp2
#$this->countDownIdSelector .item-counter-down {
margin: 0 3px;
}
#$this->countDownIdSelector .inner-item-counter-down {
background: #c0c0c0;
display: inline-block;
padding: 15px;
font-size: 45px;
line-height: 30px;
color: #fff;
border-radius: 7px;
margin: 0 1px;
}
#$this->countDownIdSelector .timeDownSperator {
font-size: 45px;
color: #c0c0c0;
font-weight: bold;
}
temp2;
$view->registerJs($js);
// Set Template Style
if($this->templateStyle == 1){
$view->registerCss($template1);
}else if($this->templateStyle == 2){
$view->registerCss($template2);
}
}
}