-
Notifications
You must be signed in to change notification settings - Fork 1
/
Dateline.php
133 lines (110 loc) · 3.2 KB
/
Dateline.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
<?php
/**
* MIT licence
* Version 2.0.0
* Sjaak Priester, Amsterdam 05-07-2015 ... 15-08-2019.
*
* Widget for date related data in Yii 2.0 framework
*/
namespace sjaakp\dateline;
use yii\base\Widget;
use yii\base\InvalidConfigException;
use yii\helpers\Html;
use yii\helpers\Json;
class Dateline extends Widget {
// Values for band scale
const MILLISECOND = 0;
const SECOND = 1;
const MINUTE = 2;
const HOUR = 3;
const DAY = 4;
const WEEK = 5;
const MONTH = 6;
const YEAR = 7;
const DECADE = 8;
const CENTURY = 9;
const MILLENNIUM = 10;
/**
* @var \yii\data\DataProviderInterface the data provider for the dateline. This property is required.
*/
public $dataProvider;
/**
* @var array key => value pairs of {dateline property name} => {model property name}.
* Mapping from dateline properties to model properties
*/
public $attributes = [];
/**
* @var array
* Client options for the Dateline widget.
* @link https://github.com/sjaakp/dateline#cursor
*/
public $options = [];
/**
* @var array
* HTML options of the dateline container.
* Use this if you want to explicitly set the ID.
*/
public $htmlOptions = [];
protected $bands = [];
protected $props = [
'id',
'start',
'stop',
'post_start',
'pre_stop',
'class',
'text'
];
/**
* @throws InvalidConfigException
*/
public function init() {
if (! $this->dataProvider) {
throw new InvalidConfigException('The "dataProvider" property must be set.');
}
if (isset($this->htmlOptions['id'])) {
$this->setId($this->htmlOptions['id']);
}
else $this->htmlOptions['id'] = $this->getId();
}
/**
* @return string|void
*/
public function run() {
$view = $this->getView();
$asset = new DatelineAsset();
$asset->register($view);
$tData = array_map(function($model) {
/** @var $model \yii\base\Model */
$v = [];
foreach($this->props as $prop) {
if (isset($this->attributes[$prop])) {
$p = $this->attributes[$prop];
if (is_callable($p)) { $v[$prop] = $p($model); }
else $v[$prop] = $model->$p;
}
else if ($model->hasAttribute($prop))
$v[$prop] = $model->$prop;
}
return $v;
}, $this->dataProvider->getModels());
$options = array_merge ($this->options, [
'bands' => $this->bands,
'events' => $tData,
]);
$jOpts = Json::encode($options);
$id = $this->getId();
$var = 'q_' . str_replace('-', '_', $id);
$js = "var {$var}=dateline('$id',$jOpts);";
$view->registerJs($js);
echo Html::tag('div', '', $this->htmlOptions);
}
/**
* @param $options
* @return $this
*/
public function band($options) {
$this->bands[] = $options;
return $this;
}
}