-
Notifications
You must be signed in to change notification settings - Fork 22
/
Timeline.php
158 lines (143 loc) · 4.59 KB
/
Timeline.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
<?php
namespace insolita\wgadminlte;
use yii\base\InvalidConfigException;
use yii\base\Widget;
use yii\helpers\Html;
use function array_merge;
/**
* This is just an example.
*/
class Timeline extends Widget
{
const TYPE_NAVY = 'navy';
const TYPE_LBLUE = 'light-blue';
const TYPE_BLUE = 'blue';
const TYPE_AQUA = 'aqua';
const TYPE_RED = 'red';
const TYPE_GREEN = 'green';
const TYPE_YEL = 'yellow';
const TYPE_PURPLE = 'purple';
const TYPE_MAR = 'maroon';
const TYPE_TEAL = 'teal';
const TYPE_OLIVE = 'olive';
const TYPE_LIME = 'lime';
const TYPE_ORANGE = 'orange';
const TYPE_FUS = 'fuchsia';
const TYPE_BLACK = 'black';
const TYPE_GRAY = 'gray';
/**@var [] $items array of events
*
* @example
* 'items'=>[
* '07.10.2014'=>[array of TimelineItems ],
* 'some object'=>[array of TimelineItems ],
* '15.11.2014'=>[array of TimelineItems ],
* 'some object'=>[array of TimelineItems ],
* ]
*
**/
public $items = [];
/**
* string|\Closure that return string
*
* @example
* 'defaultDateBg'=>function($data){
* if(is_string($data)){
* return insolita\wgadminlte\Timeline::TYPE_BLUE;
* }elseif($data->type==1){
* return insolita\wgadminlte\Timeline::TYPE_LBLUE;
* }else{
* return insolita\wgadminlte\Timeline::TYPE_TEAL;
* }
* }
**/
public $defaultDateBg = self::TYPE_GREEN;
/** callable function(obj) for prepare data
*
* @example
* 'dateFunc'=>function($data){
* return date('d/m/Y', $data)
* }
*
* @example
* 'dateFunc'=>function($data){
* return is_object($data)?date('d/m/Y', $data->created):$data;
* }
*
**/
public $dateFunc = null;
/**
* If true, only list items without wrapper will be rendered
* @var bool
*/
public $itemsOnly = false;
/**
* Common options for timeline-item elements
* @var array
*/
public $itemOptions = [];
public function run()
{
if ($this->itemsOnly) {
echo $this->renderItems();
} else {
echo Html::tag('ul', $this->renderItems(), ['class' => 'timeline', 'id' => $this->getId()]);
}
}
public function renderItems()
{
$res = '';
if (!empty($this->items)) {
foreach ($this->items as $data => $events) {
if (!empty($events)) {
$res .= $this->renderGroup($data);
foreach ($events as $event) {
$res .= $this->renderEvent($event);
}
}
}
}
return $res;
}
public function renderGroup($data)
{
$res = '';
$realdata = is_null($this->dateFunc) ? $data : call_user_func($this->dateFunc, $data);
if (is_string($this->defaultDateBg)) {
$res = Html::tag('span', $realdata, ['class' => 'bg-' . $this->defaultDateBg]);
} elseif (is_callable($this->defaultDateBg)) {
$class = call_user_func($this->defaultDateBg, $data);
$res = Html::tag('span', $realdata, ['class' => 'bg-' . $class]);
}
return Html::tag('li', $res, ['class' => 'time-label']);
}
public function renderEvent($ev)
{
$res = '';
if ($ev instanceof TimelineItem) {
$res .= '<i class="' . $ev->iconClass . ' bg-' . $ev->iconBg . '"></i>';
$item = '';
$clockIcon = $ev->showClockIcon ? Html::tag('i', '', ['class' => 'fa fa-clock-o']) . ' ' : '';
if ($ev->time) {
$item .= Html::tag('span', $clockIcon . $ev->time, ['class' => 'time']);
}
if ($ev->header) {
$item .= Html::tag(
'h3',
$ev->header,
['class' => 'timeline-header ' . (!$ev->body && !$ev->footer ? 'no-border' : '')]
);
}
$bodyOptions = array_merge(['class' => 'timeline-body'], $ev->bodyOptions);
$item .= Html::tag('div', $ev->body, $bodyOptions);
if ($ev->footer) {
$item .= Html::tag('div', $ev->footer, ['class' => 'timeline-footer']);
}
$itemOptions = array_merge(['class' => 'timeline-item'], $this->itemOptions);
$res .= Html::tag('div', $item, $itemOptions);
} else {
throw new InvalidConfigException('event must be instanceof TimelineItem');
}
return Html::tag('li', $res);
}
}