-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathyii2textareaautosize.php
98 lines (84 loc) · 2.33 KB
/
yii2textareaautosize.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
<?php
/**
* This class is used to embed textarea autosize JQuery Plugin to my Yii2 Projects
* @copyright Frenzel GmbH - www.frenzel.net
* @link http://www.frenzel.net
* @author Philipp Frenzel <[email protected]>
* @license MIT
*/
namespace net\frenzel\textareaautosize;
use Yii;
use yii\web\View;
use yii\helpers\Html;
use yii\helpers\Json;
use yii\web\JsExpression;
use yii\widgets\InputWidget;
class yii2textareaautosize extends InputWidget
{
/**
* @var array clientOptions the HTML attributes for the widget container tag.
*/
public $clientOptions = [];
/**
* @var array HTML attributes for the displayed input
*/
private $_displayOptions = [];
/**
* @inerhit doc
*/
private $_pluginName = 'textarea-autosize';
/**
* Initializes the widget.
* If you override this method, make sure you call the parent implementation first.
*/
public function init()
{
ob_start();
ob_implicit_flush(false);
//checks for the element id
if (!isset($this->options['id'])) {
$this->options['id'] = $this->getId();
}
parent::init();
}
/**
* Registers the needed assets
*/
public function registerAssets()
{
$id = $this->options['id'];
$view = $this->getView();
CoreAsset::register($view);
$cleanOptions = $this->getClientOptions();
$js[] = "$('textarea.element-$id').textareaAutoSize();";
$view->registerJs(implode("\n", $js),View::POS_READY);
}
/**
* Renders the widget.
*/
public function run()
{
$assets = ob_get_clean();
echo $assets;
$this->renderInput();
$this->registerAssets();
}
/**
* Renders the widget.
*/
public function renderInput()
{
Html::addCssClass($this->_displayOptions, 'element-' . $this->options['id']);
Html::addCssClass($this->_displayOptions, 'form-control');
$this->_displayOptions['rows'] = 1;
$input = Html::activeTextarea($this->model, $this->attribute, $this->_displayOptions);
echo $input;
}
/**
* @return array the options for the text field
*/
protected function getClientOptions()
{
return Json::encode($this->clientOptions);
}
}