/** * Initializes the default button rendering callbacks. * This method uses [[Icon|Icon]] to display iconic buttons. */ protected function initDefaultButtons() { if (!isset($this->buttons['view'])) { $this->buttons['view'] = function ($url, $model, $key) { $options = array_merge(['title' => Yii::t('yii', 'View'), 'aria-label' => Yii::t('yii', 'View'), 'data-pjax' => '0'], $this->buttonOptions); return Html::a(Icon::widget(['name' => 'visibility']), $url, $options); }; } if (!isset($this->buttons['update'])) { $this->buttons['update'] = function ($url, $model, $key) { $options = array_merge(['title' => Yii::t('yii', 'Update'), 'aria-label' => Yii::t('yii', 'Update'), 'data-pjax' => '0'], $this->buttonOptions); return Html::a(Icon::widget(['name' => 'edit']), $url, $options); }; } if (!isset($this->buttons['delete'])) { $this->buttons['delete'] = function ($url, $model, $key) { $options = array_merge(['title' => Yii::t('yii', 'Delete'), 'aria-label' => Yii::t('yii', 'Delete'), 'data-confirm' => Yii::t('yii', 'Are you sure you want to delete this item?'), 'data-method' => 'post', 'data-pjax' => '0'], $this->buttonOptions); return Html::a(Icon::widget(['name' => 'delete']), $url, $options); }; } }
/** * Renders a widget's item. * @param string|array $item the item to render. * @return string the rendering result. * @throws InvalidConfigException */ public function renderItem($item) { if (is_string($item)) { return $item; } if (!isset($item['label'])) { throw new InvalidConfigException("The 'label' option is required."); } $encodeLabel = isset($item['encode']) ? $item['encode'] : $this->encodeLabels; $label = $encodeLabel ? Html::encode($item['label']) : $item['label']; $options = ArrayHelper::getValue($item, 'options', []); $items = ArrayHelper::getValue($item, 'items'); $url = ArrayHelper::getValue($item, 'url', '#'); $linkOptions = ArrayHelper::getValue($item, 'linkOptions', []); if (isset($item['active'])) { $active = ArrayHelper::remove($item, 'active', false); } else { $active = $this->isItemActive($item); } if (empty($items)) { $items = ''; } else { $toggleTarget = 'dropdown_' . md5(uniqid()); $linkOptions['data-activates'] = $toggleTarget; Html::addCssClass($options, ['widget' => 'dropdown']); Html::addCssClass($linkOptions, ['widget' => 'dropdown-button']); if ($this->dropDownCaret !== '') { $label .= ' ' . $this->dropDownCaret; } if (is_array($items)) { if ($this->activateItems) { $items = $this->isChildActive($items, $active); } $items = $this->renderDropdown($items, $item, $toggleTarget); } } if ($this->activateItems && $active) { Html::addCssClass($options, 'active'); } return Html::tag('li', Html::a($label, $url, $linkOptions) . $items, $options); }
/** * Renders a single breadcrumb item. * @param array $link the link to be rendered. It must contain the "label" element. The "url" element is optional. * @param string $template the template to be used to rendered the link. The token "{link}" will be replaced by the link. * @return string the rendering result * @throws InvalidConfigException if `$link` does not have "label" element. */ protected function renderItem($link, $template) { $encodeLabel = ArrayHelper::remove($link, 'encode', $this->encodeLabels); if (array_key_exists('label', $link)) { $label = $encodeLabel ? Html::encode($link['label']) : $link['label']; } else { throw new InvalidConfigException('The "label" element is required for each link.'); } if (isset($link['template'])) { $template = $link['template']; } if (isset($link['url'])) { $options = $link; Html::addCssClass($options, ['link' => 'breadcrumb']); unset($options['template'], $options['label'], $options['url']); $link = Html::a($label, $link['url'], $options); } else { $link = $label; } return strtr($template, ['{link}' => $link]); }
/** * Renders a list representing the single button items. * @return string * @throws InvalidConfigException */ protected function renderItems() { $elements = []; $items = $this->items; foreach ($items as $item) { if (isset($item['visible']) && !$item['visible']) { continue; } if (is_string($item)) { $elements[] = $item; continue; } if (!array_key_exists('label', $item)) { throw new InvalidConfigException("The 'label' option is required."); } $encodeLabel = isset($item['encode']) ? $item['encode'] : $this->encodeLabels; $label = $encodeLabel ? Html::encode($item['label']) : $item['label']; $itemOptions = ArrayHelper::getValue($item, 'options', []); $linkOptions = ArrayHelper::getValue($item, 'linkOptions', []); $url = array_key_exists('url', $item) ? $item['url'] : null; if ($url === null) { $content = $label; } else { $content = Html::a($label, $url, $linkOptions); } $elements[] = Html::tag('li', $content, $itemOptions); } return Html::tag('ul', implode("\n", $elements), $this->itemsContainerOptions); }
/** * Renders menu items. * * @param array $items the menu items to be rendered * @param array $options the container HTML attributes * @return string the rendering result. * @throws InvalidConfigException if the label option is not specified in one of the items. * @used-by [[run()]] */ protected function renderItems($items, $options = []) { $lines = []; foreach ($items as $item) { if (isset($item['visible']) && !$item['visible']) { continue; } if (is_string($item)) { $lines[] = $item; continue; } if (!array_key_exists('label', $item)) { throw new InvalidConfigException("The 'label' option is required."); } $encodeLabel = isset($item['encode']) ? $item['encode'] : $this->encodeLabels; $label = $encodeLabel ? Html::encode($item['label']) : $item['label']; $itemOptions = ArrayHelper::getValue($item, 'options', []); $linkOptions = ArrayHelper::getValue($item, 'linkOptions', []); $linkOptions['tabindex'] = '-1'; $url = array_key_exists('url', $item) ? $item['url'] : null; if (empty($item['items'])) { if ($url === null) { $content = $label; Html::addCssClass($itemOptions, ['widget' => 'dropdown-header']); } else { $content = Html::a($label, $url, $linkOptions); } } else { $submenuOptions = $this->submenuOptions; if (isset($item['submenuOptions'])) { $submenuOptions = array_merge($submenuOptions, $item['submenuOptions']); } $content = Html::a($label, $url === null ? '#' : $url, $linkOptions) . $this->renderItems($item['items'], $submenuOptions); Html::addCssClass($itemOptions, ['widget' => 'dropdown-submenu']); } $lines[] = Html::tag('li', $content, $itemOptions); } return Html::tag('ul', implode("\n", $lines), $options); }
/** * Renders collapsible toggle button. * @return string the rendered toggle button. */ protected function renderToggleButton() { $toggleTarget = 'sidenav_' . md5(uniqid()); $this->registerToggleButtonScript($toggleTarget); return Html::a('<i class="material-icons">menu</i>', '#', ['data-activates' => $toggleTarget, 'class' => 'button-collapse']); }