forked from yii2mod/yii2-editable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Editable.php
183 lines (163 loc) · 4.92 KB
/
Editable.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
<?php
namespace yii2mod\editable;
use yii\base\InvalidConfigException;
use yii\db\ActiveRecordInterface;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use yii\helpers\Json;
use yii\helpers\Url;
use yii\web\JsExpression;
use yii\widgets\InputWidget;
use yii2mod\editable\bundles\EditableAddressAsset;
use yii2mod\editable\bundles\EditableBootstrapAsset;
use yii2mod\editable\bundles\EditableComboDateAsset;
use yii2mod\editable\bundles\EditableDatePickerAsset;
use yii2mod\editable\bundles\EditableDateTimePickerAsset;
/**
* Class Editable
* @package yii2mod\editable
*/
class Editable extends InputWidget
{
/**
* @var string the type of input. Type of input.
*/
public $type = 'text';
/**
* @var string the Mode of editable, can be popup or inline.
*/
public $mode = 'inline';
/**
* @var string|array Url for submit, e.g. '/post'.
*/
public $url;
/**
* @var array the options for the X-editable.js plugin.
*/
public $pluginOptions = [];
/**
* @var array the event handlers for the X-editable.js plugin.
*/
public $clientEvents = [];
/**
* Initializes the widget.
*/
public function init()
{
if ($this->url === null) {
throw new InvalidConfigException("You must setup the 'Url' property.");
}
if (!isset($this->options['id'])) {
$this->options['id'] = $this->hasModel() ? Html::getInputId($this->model, $this->attribute) : $this->getId();
}
parent::init();
}
/**
* Executes the widget.
*/
public function run()
{
$linkText = $this->getLinkText();
echo Html::a($linkText, null, $this->options);
$this->registerClientScript();
}
/**
* Register client script
*/
protected function registerClientScript()
{
$view = $this->getView();
switch ($this->type) {
case 'address':
EditableAddressAsset::register($view);
break;
case 'combodate':
EditableComboDateAsset::register($view);
break;
case 'date':
EditableDatePickerAsset::register($view);
break;
case 'datetime':
EditableDateTimePickerAsset::register($view);
break;
default:
EditableBootstrapAsset::register($view);
}
$id = ArrayHelper::remove($this->pluginOptions, 'selector', '#' . $this->options['id']);
$id = preg_replace('/([.])/', '\\\\\\\$1', $id);
if ($this->hasActiveRecord() && $this->model->isNewRecord) {
$this->pluginOptions['send'] = 'always'; // send to server without pk
}
$pluginOptions = $this->getPluginOptions();
$js = "jQuery('$id').editable($pluginOptions);";
$view->registerJs($js);
if (!empty($this->clientEvents)) {
$this->registerClientEvents($id);
}
}
/**
* Return plugin options in json format
*
* @return string
*/
public function getPluginOptions()
{
$pk = ArrayHelper::getValue($this->pluginOptions,
'pk',
$this->hasActiveRecord() ? $this->model->getPrimaryKey() : null
);
$this->pluginOptions['pk'] = $pk;
$this->pluginOptions['url'] = $this->url instanceof JsExpression ? $this->url : Url::toRoute($this->url);
$this->pluginOptions['type'] = $this->type;
$this->pluginOptions['mode'] = $this->mode;
$this->pluginOptions['name'] = $this->attribute ?: $this->name;
return Json::encode($this->pluginOptions);
}
/**
* Register client events
*
* @param $id
*/
public function registerClientEvents($id)
{
$view = $this->getView();
$js = [];
foreach ($this->clientEvents as $event => $handler) {
$js[] = "jQuery('$id').on('$event', $handler);";
}
$view->registerJs(implode("\n", $js));
}
/**
* Return link text
*
* @return mixed|string
*/
protected function getLinkText()
{
$value = $this->value;
if ($this->hasModel()) {
$model = $this->model;
if ($value !== null) {
if (is_string($value)) {
$linkText = ArrayHelper::getValue($model, $value);
} else {
$linkText = call_user_func($value, $model);
}
} else {
$linkText = ArrayHelper::getValue($model, $this->attribute);
}
} else {
$linkText = $value;
}
return $linkText;
}
/**
* To ensure that `getPrimaryKey()` and `getIsNewRecord()` methods are implemented in model.
*
* @return bool
*/
protected function hasActiveRecord()
{
return $this->hasModel() && $this->model instanceof ActiveRecordInterface;
}
}