From c5845b62b4c1eb3d23f623e6e51248c1dc6b32f2 Mon Sep 17 00:00:00 2001 From: Joshua Pease Date: Thu, 17 Aug 2023 15:54:00 -0700 Subject: [PATCH] [#65] Adds ability to use new version of parts kit --- src/Bundle.php | 2 +- src/Module.php | 10 +- src/controllers/PartsKitController.php | 48 ++++++ src/models/PartsKitNavItem.php | 12 ++ src/services/PartsKit.php | 22 +++ src/templates/_layouts/parts-kit.html | 206 +++++++++++++------------ src/templates/_parts-kit/v2-index.html | 15 ++ 7 files changed, 218 insertions(+), 97 deletions(-) create mode 100644 src/models/PartsKitNavItem.php create mode 100644 src/templates/_parts-kit/v2-index.html diff --git a/src/Bundle.php b/src/Bundle.php index a943f38..9996088 100644 --- a/src/Bundle.php +++ b/src/Bundle.php @@ -17,7 +17,7 @@ public function init() ]; // Is this a parts kit request - if (PartsKit::isRequest()) { + if (PartsKit::isRequest() && !PartsKit::isV2()) { $css[] = 'css/parts-kit.css'; $this->js = [ diff --git a/src/Module.php b/src/Module.php index f3d04b6..4ee7b98 100644 --- a/src/Module.php +++ b/src/Module.php @@ -145,6 +145,12 @@ class="edit-entry" // Define viget base templates directory and index url if (self::$instance->partsKit->isRequest()) { + + if(PartsKit::isRoot() && PartsKit::isV2()) { + // Hacks to hide the Yii Debug bar on the root of the v2 parts kit + Craft::$app->getRequest()->setIsLivePreview(true); + } + Event::on( View::class, View::EVENT_REGISTER_SITE_TEMPLATE_ROOTS, @@ -161,7 +167,7 @@ function (RegisterTemplateRootsEvent $e) { function (RegisterUrlRulesEvent $event) { $partsKitDir = self::$config['partsKit']['directory']; - $event->rules[$partsKitDir] = 'viget-base/parts-kit/redirect-index'; + $event->rules[$partsKitDir] = self::$config['partsKit']['controllerPath'] . '/redirect-index'; } ); } @@ -205,6 +211,8 @@ private function _loadConfig() 'layout' => '_layouts/app', 'volume' => 'partsKit', 'theme' => 'light', + 'version' => 1, // Can be 1 or 2 + 'controllerPath' => 'viget-base/parts-kit', ], 'tailwind' => [ 'configPath' => Craft::getAlias('@config/tailwind/tailwind.json'), diff --git a/src/controllers/PartsKitController.php b/src/controllers/PartsKitController.php index 29b9cd1..3d8ad1f 100644 --- a/src/controllers/PartsKitController.php +++ b/src/controllers/PartsKitController.php @@ -2,6 +2,9 @@ namespace viget\base\controllers; +use craft\helpers\UrlHelper; +use viget\base\models\PartsKitNavItem; +use viget\base\services\PartsKit; use yii\web\Response; use viget\base\Module; @@ -17,6 +20,10 @@ class PartsKitController extends \craft\web\Controller */ public function actionRedirectIndex(): Response { + if (PartsKit::isV2()) { + return $this->renderTemplate('viget-base/_parts-kit/v2-index'); + } + $redirectUrl = Module::$instance->partsKit->getFirstNavUrl(); if (!$redirectUrl) { @@ -25,4 +32,45 @@ public function actionRedirectIndex(): Response return $this->redirect($redirectUrl, 301); } + + /** + * Generates the config file for the parts kit + * @return Response + */ + public function actionConfig(): Response + { + /** @var PartsKit $partsKitService */ + $partsKitService = Module::getInstance()->partsKit; + + $nav = []; + + foreach ($partsKitService->getNav() as $key => $item) { + + $children = array_map( + function(array $child) { + return new PartsKitNavItem([ + 'title' => $child['title'], + 'url' => UrlHelper::url($child['url'], [ + 'version' => 2, + ]), + // Our current folder parsing doesn't support more than 2 levels. + // Eventually we'd like it to + 'children' => [], + ]); + }, + $item['items'] ?? [] + ); + + $nav[] = new PartsKitNavItem([ + 'title' => $key, + 'url' => '', + 'children' => $children, + ]); + } + + return $this->asJson([ + 'schemaVersion' => '0.0.1', + 'nav' => $nav, + ]); + } } diff --git a/src/models/PartsKitNavItem.php b/src/models/PartsKitNavItem.php new file mode 100644 index 0000000..6f3ad4c --- /dev/null +++ b/src/models/PartsKitNavItem.php @@ -0,0 +1,12 @@ +request->segments[0] ?? null) === self::getConfig('directory'); } + /** + * Determine if top level of the parts kit folder. i.e. /parts-kit/ + * @return bool + */ + public static function isRoot(): bool + { + return self::isRequest() && count(Craft::$app->request->segments) === 1; + } + + /** + * Should we show the v2 version of the parts kit + * @return bool + */ + public static function isV2(): bool + { + return self::isRequest() + && ( + (int)Craft::$app->request->getQueryParam('version') === 2 + || self::getConfig('version') === 2 + ); + } + /** * Get a config item either the default or from the config file * diff --git a/src/templates/_layouts/parts-kit.html b/src/templates/_layouts/parts-kit.html index 0a4f56b..f561253 100644 --- a/src/templates/_layouts/parts-kit.html +++ b/src/templates/_layouts/parts-kit.html @@ -1,111 +1,127 @@ {% extends craft.viget.partsKit.getConfig('layout') %} -{% block content %} - +{% set partsKitVersion = craft.app.request.getParam( + 'version', + craft.viget.partsKit.getConfig('version') +) %} + +{# Allows us to use the main block in multiple branches #} +{% set main %} + {% block main %}{% endblock %} +{% endset %} -
- + +
+
+
- -
    - {% for item in section.items %} -
  1. - -
    - {{ svg('@viget/base/resources/icons/pk-sidebar.svg') | attr({ - class: 'parts-kit__nav-story', - }) }} - {{ item.title }} -
    -
    -
  2. - {% endfor %} -
- - {% endfor %} - - {% endif %} - - -
-
-
- - -
+ Close + {{ svg('@viget/base/resources/icons/close-circle.svg') }} + +
-
- {% block main %}{% endblock %} +
+ {{ main }} +
+
-
-
+ {% case 2 %} + {# Just render the block, no chrome #} + {{ main }} + {% endswitch %} {% endblock %} diff --git a/src/templates/_parts-kit/v2-index.html b/src/templates/_parts-kit/v2-index.html new file mode 100644 index 0000000..a21f4b3 --- /dev/null +++ b/src/templates/_parts-kit/v2-index.html @@ -0,0 +1,15 @@ + + + + + Parts Kit + + + + + {# We want to load all of this in an isolated page, so no styles conflict from the parent site #} + + + + +