protected function render()
 {
     $prop = $this->props;
     $this->beginContent();
     $xi = $prop->expandIcon;
     if ($prop->menu instanceof NavigationLinkInterface) {
         $links = $prop->excludeRoot ? $prop->menu : [$prop->menu];
     } else {
         $links = $prop->menu;
     }
     if (!$links) {
         return;
     }
     echo html(map($links, function ($link) use($xi) {
         /** @var NavigationLinkInterface $link */
         if (!$link) {
             return '';
         }
         if (is_array($link)) {
             $link = $link[0];
         }
         // Exclude hidden links and menu separators.
         if (!$link->isActuallyVisible()) {
             return null;
         }
         return [h('.' . $this->props->sectionClass, [h('h3', $link->title()), h('ul.' . $this->props->listClass, Flow::from($link->getMenu())->map(function (NavigationLinkInterface $link) use($xi) {
             // Exclude hidden links and menu separators.
             if (!$link->isActuallyVisible() || $link->isGroup() && $link->title() == '-') {
                 return null;
             }
             $children = $link->getMenu();
             $sub = '';
             /** @var NavigationLinkInterface $child */
             foreach ($children as $child) {
                 if ($child->isActuallyVisible()) {
                     $sub = '.sub';
                     break;
                 }
             }
             $children->rewind();
             $active = $link->isActive() ? '.active' : '';
             $current = $link->isSelected() ? '.current' : '';
             $url = $link->isGroup() && !isset($link->defaultURI) ? null : $link->url();
             $header = $link->isGroup() && $link->title() != '-' ? '.header' : '';
             return [h("li{$active}{$sub}{$current}{$header}", [h("a{$active}", ['href' => $url], [when($link->icon(), h('i', ['class' => $link->icon()])), $link->title(), when(isset($xi) && $sub, h("span.{$xi}"))]), when($sub, $this->renderMenuItem($children, $xi, false))])];
         })->all())])];
     }));
 }
 function set($handlers)
 {
     // Convert the list to an interable and prunte it of NULL values.
     $this->handlers = Flow::from($handlers)->where(identity());
     return $this;
 }
示例#3
0
文件: globals.php 项目: php-kit/flow
/**
 * Creates a Flow iteration from any value.
 *
 * If the value is not iterable, an iterator for a single value is returned.
 *
 * @param mixed $stuff Anything.
 * @return Flow
 */
function flow($stuff)
{
    return Flow::from($stuff);
}
 protected function render()
 {
     $prop = $this->props;
     $isMultiple = $prop->multiple;
     $assets = $this->context->getAssetsService();
     $assets->addInlineScript("selenia.ext.select.props['{$prop->id}']=" . JavascriptCodeGen::makeOptions(['autoOpenLinked' => $prop->autoOpenLinked, 'dataUrl' => $prop->dataUrl, 'emptyLabel' => $prop->emptyLabel, 'emptySelection' => $prop->emptySelection, 'id' => $prop->id, 'labelField' => $prop->labelField, 'linkedSelector' => $prop->linkedSelector, 'linkedUrl' => $prop->linkedUrl, 'multiple' => $prop->multiple, 'noResultsText' => $prop->noResultsText, 'valueField' => $prop->valueField, 'value' => $prop->value]));
     // If required, add auto-add tag behavior to this Chosen.
     if ($prop->autoTag && $prop->multiple) {
         $assets->addInlineScript("\n\$(function () {\n  \$ ('#{$prop->id}+.chosen-container .chosen-choices input').on ('keyup', function (ev) { console.log(ev);\n    var v = \$ (this).val ();\n    if (ev.keyCode == 13 && v) {\n      var tags  = \$ ('#{$prop->id} option').map (function (i, e) { return \$ (e).val () });\n      var found = false, l = v.length;\n      tags.each (function (i, x) {\n        if (x.substr (0, l) == v) {\n          found = true;\n          return false\n        }\n      });\n      if (found) return;\n      \$ ('#{$prop->id}').append (\"<option>\" + v + \"</option>\");\n      \$ ('#{$prop->id}').trigger ('chosen:updated');\n      ev.preventDefault ();\n      var e     = jQuery.Event (\"keyup\");\n      e.which   = 13;\n      \$ ('#{$prop->id}+.chosen-container .chosen-choices input').val (v).trigger ('keyup').trigger (e);\n    }\n  })\n});\n");
     }
     $this->attr('name', $prop->multiple ? "{$prop->name}[]" : $prop->name);
     $this->attrIf($isMultiple, 'multiple', '');
     $this->attrIf($prop->onChange, 'onchange', $prop->onChange);
     $this->beginContent();
     if ($prop->emptySelection && !$prop->multiple) {
         $sel = exists($prop->value) ? '' : ' selected';
         echo '<option value=""' . $sel . '>' . $prop->emptyLabel . '</option>';
     }
     $viewModel = $prop->get('data');
     if (isset($viewModel)) {
         /** @var \Iterator $dataIter */
         $dataIter = iteratorOf($viewModel);
         $dataIter->rewind();
         if ($dataIter->valid()) {
             $values = $selValue = null;
             // SETUP MULTI-SELECT
             if ($isMultiple) {
                 if (isset($prop->value) && !is_iterable($prop->value)) {
                     throw new ComponentException($this, sprintf("Value of multiple selection component must be iterable or null; %s was given.", typeOf($prop->value)));
                 }
                 $it = Flow::from($prop->value);
                 $it->rewind();
                 $values = $it->valid() && is_scalar($it->current()) ? $it->all() : $it->map(pluck($prop->valueField))->all();
             } else {
                 $selValue = strval($prop->get('value'));
             }
             // NOW RENDER IT
             $template = $this->getChildren('listItemTemplate');
             $first = true;
             do {
                 $v = $dataIter->current();
                 $value = getField($v, $prop->valueField);
                 $label = getField($v, $prop->labelField);
                 if (!strlen($label)) {
                     $label = $prop->emptyLabel;
                 }
                 if ($isMultiple) {
                     $sel = array_search($value, $values) !== false ? ' selected' : '';
                 } else {
                     if ($first && !$prop->emptySelection && !$prop->multiple && !exists($selValue)) {
                         $prop->value = $selValue = $value;
                     }
                     $eq = $prop->strict ? $value === $selValue : $value == $selValue;
                     if ($eq) {
                         $this->selectedLabel = $label;
                     }
                     $sel = $eq ? ' selected' : '';
                 }
                 if ($template) {
                     // Render templated list
                     $viewModel['value'] = $value;
                     $viewModel['label'] = $label;
                     Component::renderSet($template);
                 } else {
                     // Render standard list
                     echo "<option value=\"{$value}\"{$sel}>{$label}</option>";
                 }
                 $dataIter->next();
                 $first = false;
             } while ($dataIter->valid());
         }
     }
 }
 function getMenu()
 {
     return Flow::from($this->links)->where(function (NavigationLinkInterface $link) {
         return $link->isActuallyVisible();
     })->reindex()->getIterator();
 }