/** * Generates a bar * @param integer $percent the percentage of the bar * @param string $label, optional, the label to display at the bar * @param array $options the HTML attributes of the bar * @return string the rendering result. */ protected function renderBar($percent, $label = '', $options = []) { $defaultOptions = ['role' => 'progressbar', 'aria-valuenow' => $percent, 'aria-valuemin' => 0, 'aria-valuemax' => 100, 'style' => "width:{$percent}%"]; $options = array_merge($defaultOptions, $options); Html::addCssClass($options, ['widget' => 'progress-bar']); $out = Html::beginTag('div', $options); $out .= $label; $out .= Html::tag('span', \Yii::t('yii', '{percent}% Complete', ['percent' => $percent]), ['class' => 'sr-only']); $out .= Html::endTag('div'); return $out; }
/** * Normalizes dropdown item options by removing tab specific keys `content` and `contentOptions`, and also * configure `panes` accordingly. * @param string $itemNumber number of the item * @param array $items the dropdown items configuration. * @param array $panes the panes reference array. * @return boolean whether any of the dropdown items is `active` or not. * @throws InvalidConfigException */ protected function renderDropdown($itemNumber, &$items, &$panes) { $itemActive = false; foreach ($items as $n => &$item) { if (is_string($item)) { continue; } if (isset($item['visible']) && !$item['visible']) { continue; } if (!array_key_exists('content', $item)) { throw new InvalidConfigException("The 'content' option is required."); } $content = ArrayHelper::remove($item, 'content'); $options = ArrayHelper::remove($item, 'contentOptions', []); Html::addCssClass($options, ['widget' => 'tab-pane']); if (ArrayHelper::remove($item, 'active')) { Html::addCssClass($options, 'active'); Html::addCssClass($item['options'], 'active'); $itemActive = true; } $options['id'] = ArrayHelper::getValue($options, 'id', $this->options['id'] . '-dd' . $itemNumber . '-tab' . $n); $item['url'] = '#' . $options['id']; $item['linkOptions']['data-toggle'] = 'tab'; $panes[] = Html::tag('div', $content, $options); unset($item); } return $itemActive; }
/** * Renders a single collapsible item group * @param string $header a label of the item group [[items]] * @param array $item a single item from [[items]] * @param integer $index the item index as each item group content must have an id * @return string the rendering result * @throws InvalidConfigException */ public function renderItem($header, $item, $index) { if (array_key_exists('content', $item)) { $id = $this->options['id'] . '-collapse' . $index; $options = ArrayHelper::getValue($item, 'contentOptions', []); $options['id'] = $id; Html::addCssClass($options, ['widget' => 'panel-collapse', 'collapse' => 'collapse']); $encodeLabel = isset($item['encode']) ? $item['encode'] : $this->encodeLabels; if ($encodeLabel) { $header = Html::encode($header); } $headerToggle = Html::a($header, '#' . $id, ['class' => 'collapse-toggle', 'data-toggle' => 'collapse', 'data-parent' => '#' . $this->options['id']]) . "\n"; $header = Html::tag('h4', $headerToggle, ['class' => 'panel-title']); if (is_string($item['content']) || is_object($item['content'])) { $content = Html::tag('div', $item['content'], ['class' => 'panel-body']) . "\n"; } elseif (is_array($item['content'])) { $content = Html::ul($item['content'], ['class' => 'list-group', 'itemOptions' => ['class' => 'list-group-item'], 'encode' => false]) . "\n"; if (isset($item['footer'])) { $content .= Html::tag('div', $item['footer'], ['class' => 'panel-footer']) . "\n"; } } else { throw new InvalidConfigException('The "content" option should be a string, array or object.'); } } else { throw new InvalidConfigException('The "content" option is required.'); } $group = []; $group[] = Html::tag('div', $header, ['class' => 'panel-heading']); $group[] = Html::tag('div', $content, $options); return implode("\n", $group); }
/** * 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. */ 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); }
/** * Parses and returns addon content * * @param string|array $addon the addon parameter * * @return string */ public static function getAppendAddonContent($addon) { if (!is_array($addon)) { return $addon; } $content = ArrayHelper::getValue($addon, 'content', ''); $options = ArrayHelper::getValue($addon, 'options', []); if (ArrayHelper::getValue($addon, 'asButton', false) == true) { Html::addCssClass($options, 'input-group-btn'); return Html::tag('span', $content, $options); } else { Html::addCssClass($options, 'icon-right'); return Html::tag('span', $content, $options); } }
/** * Renders the close button. * @return string the rendering result */ protected function renderCloseButton() { if (($closeButton = $this->closeButton) !== false) { $tag = ArrayHelper::remove($closeButton, 'tag', 'button'); $label = ArrayHelper::remove($closeButton, 'label', '×'); if ($tag === 'button' && !isset($closeButton['type'])) { $closeButton['type'] = 'button'; } return Html::tag($tag, $label, $closeButton); } else { return null; } }
/** * Renders a single carousel item * @param string|array $item a single item from [[items]] * @param integer $index the item index as the first item should be set to `active` * @return string the rendering result * @throws InvalidConfigException if the item is invalid */ public function renderItem($item, $index) { if (is_string($item)) { $content = $item; $caption = null; $options = []; } elseif (isset($item['content'])) { $content = $item['content']; $caption = ArrayHelper::getValue($item, 'caption'); if ($caption !== null) { $caption = Html::tag('div', $caption, ['class' => 'carousel-caption']); } $options = ArrayHelper::getValue($item, 'options', []); } else { throw new InvalidConfigException('The "content" option is required.'); } Html::addCssClass($options, ['widget' => 'item']); if ($index === 0) { Html::addCssClass($options, 'active'); } return Html::tag('div', $content . "\n" . $caption, $options); }
/** * Generates the addon markup * * @return string */ protected function generateAddon() { if (empty($this->addon)) { return '{input}'; } $addon = $this->addon; $prepend = static::getAddonContent(ArrayHelper::getValue($addon, 'prepend', '')); $append = static::getAddonContent(ArrayHelper::getValue($addon, 'append', '')); $content = $prepend . '{input}' . $append; $group = ArrayHelper::getValue($addon, 'groupOptions', []); Html::addCssClass($group, 'input-group'); $contentBefore = ArrayHelper::getValue($addon, 'contentBefore', ''); $contentAfter = ArrayHelper::getValue($addon, 'contentAfter', ''); $content = Html::tag('div', $contentBefore . $content . $contentAfter, $group); return $content; }
/** * Renders the widget. */ public function run() { BootstrapAsset::register($this->getView()); return Html::tag('div', $this->renderButtons(), $this->options); }
/** * Renders collapsible toggle button. * @return string the rendering toggle button. */ protected function renderToggleButton() { $bar = Html::tag('span', '', ['class' => 'icon-bar']); $screenReader = "<span class=\"sr-only\">{$this->screenReaderToggleText}</span>"; return Html::button("{$screenReader}\n{$bar}\n{$bar}\n{$bar}", ['class' => 'navbar-toggle', 'data-toggle' => 'collapse', 'data-target' => "#{$this->containerOptions['id']}"]); }
/** * 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 ($items !== null) { $linkOptions['data-toggle'] = 'dropdown'; Html::addCssClass($options, ['widget' => 'dropdown']); Html::addCssClass($linkOptions, ['widget' => 'dropdown-toggle']); if ($this->dropDownCaret !== '') { $label .= ' ' . $this->dropDownCaret; } if (is_array($items)) { if ($this->activateItems) { $items = $this->isChildActive($items, $active); } $items = $this->renderDropdown($items, $item); } } if ($this->activateItems && $active) { Html::addCssClass($options, 'active'); } return Html::tag('li', Html::a($label, $url, $linkOptions) . $items, $options); }