From 68150887f6e5b17dc9cdfae94e15bc4148db6584 Mon Sep 17 00:00:00 2001 From: Ryan Cramer Date: Mon, 11 Mar 2019 12:45:00 -0400 Subject: [PATCH] Update JqueryWireTabs to add a render() method that provides an all-in-one tab rendering solution for the PW admin without any other setup/initialization needed. --- .../JqueryWireTabs/JqueryWireTabs.module | 62 ++++++++++++++++++- 1 file changed, 61 insertions(+), 1 deletion(-) diff --git a/wire/modules/Jquery/JqueryWireTabs/JqueryWireTabs.module b/wire/modules/Jquery/JqueryWireTabs/JqueryWireTabs.module index 15c6a651..d1255bc4 100644 --- a/wire/modules/Jquery/JqueryWireTabs/JqueryWireTabs.module +++ b/wire/modules/Jquery/JqueryWireTabs/JqueryWireTabs.module @@ -3,11 +3,13 @@ /** * jQuery Tabs for ProcessWire * - * ProcessWire 3.x, Copyright 2016 by Ryan Cramer + * ProcessWire 3.x, Copyright 2019 by Ryan Cramer * https://processwire.com * * @property int $rememberTabs * + * @method string render(array $items, array $options = array()) + * */ class JqueryWireTabs extends ModuleJS implements ConfigurableModule { @@ -104,4 +106,62 @@ class JqueryWireTabs extends ModuleJS implements ConfigurableModule { $out .= ""; return $out; } + + /** + * All-in-one tabs rendering in ProcessWire admin + * + * Use this method to render tabs, tab containers, and initialization script in the return value. This method + * requires no other initialization or setup and returns ready-to-output tabs. + * + * ~~~~~ + * echo $modules->get('JqueryWireTabs')->render([ + * 'Foo' => 'Foo tab content', + * 'Bar' => 'Bar tab content', + * 'Baz' => 'Baz tab content', + * ]); + * ~~~~~ + * + * @param array $items Array of one or more [ 'Tab Title' => 'Tab Content' ] + * @param array $options Options to modify default behavior (I recommend omitting this argument unless you really need it) + * @return string Markup ready for output + * @since 3.0.127 + * + */ + public function ___render(array $items, array $options = array()) { + + static $n = 0; + $n++; + + $defaults = array( + 'id' => "pw-wiretabs-$n", + 'linksID' => "pw-wiretabs-$n-links", + 'itemClass' => "pw-wiretabs-item", + ); + + $sanitizer = $this->wire('sanitizer'); + $options = array_merge($defaults, $options); + $links = array(); + $out = ''; + $cnt = 0; + + foreach($items as $itemTitle => $itemContent) { + $cnt++; + $itemID = "pw-wiretabs-$n-$cnt"; + $links[$itemID] = $sanitizer->entities1($itemTitle); + $out .= "
$itemContent
"; + } + + $out = + "
" . + $this->renderTabList($links, array('id' => $options['linksID'])) . + $out . + "
" . + ""; + + return $out; + } }