This repository has been archived by the owner on Sep 22, 2022. It is now read-only.
/
jalaliDateRangePicker.php
99 lines (88 loc) · 2.2 KB
/
jalaliDateRangePicker.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
<?php
/**
* @link http://faravaghi.ir
* @copyright Copyright (c) 2017 faravaghi
* @license MIT http://opensource.org/licenses/MIT
*/
namespace faravaghi\jalaliDateRangePicker;
use faravaghi\jalaliDateRangePicker\JalaliRangePickerAsset;
use Yii;
use yii\base\Model;
use yii\web\View;
use yii\helpers\Html;
use yii\helpers\Json;
use yii\base\Widget as Widget;
/**
* @author Mohammad Ebrahim Amini <info@faravaghi.ir>
*/
class jalaliDateRangePicker extends Widget
{
/**
* @var string $selector
*/
public $selector;
/**
* @var string attribute associated with this widget.
*/
public $attribute;
/**
* @var \yii\db\ActiveRecord model associated with this widget.
*/
public $model;
/**
* @var string JS Callback for Daterange picker
*/
public $callback;
/**
* @var array Options to be passed to daterange picker
*/
public $options = [];
/**
* @var array the HTML attributes for the widget container.
*/
public $htmlOptions = [];
/**
* @var string[] the JavaScript event handlers.
*/
public $events = [];
/**
* Initializes the widget.
* If you override this method, make sure you call the parent implementation first.
*/
public function init()
{
//checks for the element id
if (!isset($this->htmlOptions['id'])) {
$this->htmlOptions['id'] = $this->getId();
}
parent::init();
}
/**
* Renders the widget.
*/
public function run()
{
$this->registerPlugin();
}
protected function registerPlugin()
{
if ($this->selector){
$this->registerJs($this->selector, $this->options, $this->callback);
}
else {
$id = $this->htmlOptions['id'];
echo Html::activeInput('text', $this->model, $this->attribute, $this->htmlOptions);
$this->registerJs("#{$id}", $this->options, $this->callback);
}
}
protected function registerJs($selector, $options, $callback)
{
$view = $this->getView();
JalaliRangePickerAsset::register($view);
$js = [];
$js[] = '$("' . $selector . '").daterangepicker(' . Json::encode($options) . ($callback ? ', ' . Json::encode($callback) : '') . ')';
foreach ($this->events as $event => $handler)
$js[] = ".on('{$event}', " . Json::encode($handler) . ")";
$view->registerJs(implode("\n", $js) . ';', View::POS_READY);
}
}