TreeViewInput.php 8.45 KB
Newer Older
Marcello Pivanti's avatar
Marcello Pivanti committed
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
<?php

/**
 * Art-ER Attrattività, ricerca e territorio dell’Emilia-Romagna
 * OPEN 2.0
 *
 * @package    Open20Package
 * @category   CategoryName
 * @author     Elite Division S.r.l.
 */

/**
 * @copyright Copyright &copy; Kartik Visweswaran, Krajee.com, 2015 - 2017
 * @package   yii2-tree-manager
 * @version   1.0.8
 */

namespace kartik\tree;

use Yii;
use yii\helpers\Html;
use yii\helpers\ArrayHelper;
use yii\helpers\Json;
use yii\base\Model;
use yii\base\InvalidConfigException;
use yii\web\View;

/**
 * An input widget that extends [[TreeView]], and allows one to select records from the tree.
 *
 * @author Kartik Visweswaran <kartikv2@gmail.com>
 * @since  1.0
 */
class TreeViewInput extends TreeView
{
    /**
     * Caret rendered for the dropdown toggle
     */
    const CARET = '<div class="kv-carets"><span class="caret kv-dn"></span><span class="caret kv-up"></span></div>';

    /**
     * @var Model the data model that this widget is associated with.
     */
    public $model;

    /**
     * @var string the model attribute that this widget is associated with.
     */
    public $attribute;

    /**
     * @var string the input name. This must be set if [[model]] and [[attribute]] are not set.
     */
    public $name;

    /**
     * @var string the input value.
     */
    public $value;

    /**
     * @var boolean whether to show the input as a dropdown select. If set to `false`, it will display directly the tree
     * view selector widget. Defaults to `true`. The `BootstrapPluginAsset` will automatically be loaded if this is
     * set to `true`. Defaults to `true`.
     */
    public $asDropdown = true;

    /**
     * @var boolean whether to autoclose the dropdown on input selection when `asDropdown` is true. Defaults to `true`.
     */
    public $autoCloseOnSelect = true;

    /**
     * @var boolean whether to show the toolbar in the footer. Defaults to `false`.
     */
    public $showToolbar = false;

    /**
     * @var array the configuration of the tree view dropdown. The following configuration options are available:
     * - `input`: _array_, the HTML attributes for the dropdown input container which displays the selected tree items.
     *   The following special options are available:
     *   - `placeholder`: string, defaults to `Select...`
     * - `dropdown`: _array_, the HTML attributes for the dropdown tree view menu.
     * - `options`: _array_, the HTML attributes for the wrapper container
     * - `caret`: _string_, the markup for rendering the dropdown indicator for up and down. Defaults to [[CARET]].
     */
    public $dropdownConfig = [];

    /**
     * @var array the HTML attributes for the input that will store the selected nodes for the widget
     */
    public $options = ['class' => 'form-control hide'];

    /**
     * @var string the placeholder for the dropdown input
     */
    private $_placeholder;

    /**
     * @var boolean whether the input is disabled
     */
    private $_disabled;

    /**
     * @inheritdoc
     */
    protected function initTreeView()
    {
        if (!$this->hasModel() && $this->name === null) {
            throw new InvalidConfigException("Either 'name', or 'model' and 'attribute' properties must be specified.");
        }
        $this->showCheckbox = true;
        $css = 'kv-tree-input-widget';
        if (!$this->showToolbar) {
            $css .= ' kv-tree-nofooter';
        }
        Html::addCssClass($this->treeOptions, $css);
        parent::initTreeView();
        $this->_hasBootstrap = $this->showTooltips || $this->asDropdown;
    }

    /**
     * @inheritdoc
     */
    public function run()
    {
        if ($this->hasModel()) {
            $this->value = Html::getAttributeValue($this->model, $this->attribute);
        }
        $this->_disabled = ArrayHelper::getValue($this->options, 'disabled', false);
        if ($this->asDropdown) {
            $this->initDropdown();
        }
        $this->registerInputAssets();
        parent::run();
    }

    /**
     * Initialize tree dropdown menu settings and options
     */
    protected function initDropdown()
    {
        $config = $this->dropdownConfig;
        $input = ArrayHelper::getValue($config, 'input', []);
        $dropdown = ArrayHelper::getValue($config, 'dropdown', []);
        $options = ArrayHelper::getValue($config, 'options', []);
        $css = ['form-control', 'dropdown-toggle', 'kv-tree-input'];
        if ($this->_disabled) {
            $css[] = 'disabled';
        }
        Html::addCssClass($input, $css);
        Html::addCssClass($dropdown, ['dropdown-menu', 'kv-tree-dropdown']);
        Html::addCssClass($options, ['dropdown', 'kv-tree-dropdown-container']);
        $id = $this->options['id'] . '-tree-input';
        $this->_placeholder = ArrayHelper::remove($input, 'placeholder', Yii::t('kvtree', 'Select...'));
        $this->_placeholder = Html::tag('span', $this->_placeholder, ['class' => 'kv-placeholder']);
        $config['dropdown'] = array_replace_recursive([
            'id' => $id . '-menu',
            'role' => 'menu',
            'aria-labelledby' => $id,
        ], $dropdown);
        $config['input'] = array_replace_recursive([
            'id' => $id,
            'tabindex' => -1,
            'data-toggle' => 'dropdown',
            'aria-haspopup' => 'true',
            'aria-expanded' => 'false',
        ], $input);
        if (empty($config['caret'])) {
            $config['caret'] = self::CARET;
        }
        $config['options'] = $options;
        $this->dropdownConfig = $config;
    }

    /**
     * @inheritdoc
     */
    public function renderWidget()
    {
        if (!$this->showToolbar) {
            $this->wrapperTemplate = strtr($this->wrapperTemplate, ['{footer}' => '']);
        }
        $content = strtr($this->renderWrapper(), [
                '{heading}' => $this->renderHeading(),
                '{search}' => $this->renderSearch(),
                '{toolbar}' => $this->renderToolbar(),
            ]) . "\n" .
            $this->getInput();
        if ($this->asDropdown) {
            return $this->renderDropdown($content);
        }
        return $content;
    }

    /**
     * Generates the dropdown tree menu
     *
     * @param string $content the content to be embedded in the dropdown menu
     *
     * @return string
     */
    protected function renderDropdown($content)
    {
        $config = $this->dropdownConfig;
        $input = Html::tag('div', $config['caret'] . $this->_placeholder, $config['input']);
        $dropdown = Html::tag('div', $content, $config['dropdown']);
        return Html::tag('div', $input . $dropdown, $config['options']);
    }

    /**
     * Generates the hidden input for storage
     *
     * @return string
     */
    public function getInput()
    {
        if ($this->hasModel()) {
            return Html::activeHiddenInput($this->model, $this->attribute, $this->options);
        }
        return Html::hiddenInput($this->name, $this->value, $this->options);
    }

    /**
     * Renders the markup for the button actions toolbar
     *
     * @return string
     */
    public function renderToolbar()
    {
        if (!$this->showToolbar) {
            return '';
        }
        unset($this->toolbar[self::BTN_CREATE], $this->toolbar[self::BTN_CREATE_ROOT], $this->toolbar[self::BTN_REMOVE]);
        return parent::renderToolbar();
    }

    /**
     * @return boolean whether this widget is associated with a data model.
     */
    protected function hasModel()
    {
        return $this->model instanceof Model && $this->attribute !== null;
    }

    /**
     * Registers assets for TreeViewInput
     */
    public function registerInputAssets()
    {
        if (!$this->asDropdown) {
            return;
        }
        $view = $this->getView();
        TreeViewInputAsset::register($view);
        $id = $this->options['id'];
        $name = 'treeinput';
        $opts = Json::encode([
            'treeId' => $this->treeOptions['id'],
            'inputId' => $this->dropdownConfig['input']['id'],
            'dropdownId' => $this->dropdownConfig['dropdown']['id'],
            'placeholder' => $this->_placeholder,
            'value' => empty($this->value) ? '' : $this->value,
            'caret' => $this->dropdownConfig['caret'],
            'autoCloseOnSelect' => $this->autoCloseOnSelect
        ]);
        $var = $name . '_' . hash('crc32', $opts);
        $this->options['data-krajee-' . $name] = $var;
        $view->registerJs("var {$var}={$opts};", View::POS_HEAD);
        $view->registerJs("jQuery('#{$id}').{$name}({$var});");
    }
}