From 75167fac837b12b1b8d00494a3dfcf727408b9f7 Mon Sep 17 00:00:00 2001 From: John Cowen Date: Mon, 1 Feb 2021 17:50:11 +0000 Subject: [PATCH] ui: Add 'Scenario' debug function for easy saving debug scenarios (#9675) --- ui/packages/consul-ui/README.md | 5 ++-- .../consul-ui/app/utils/get-environment.js | 27 ++++++++++++++++++- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/ui/packages/consul-ui/README.md b/ui/packages/consul-ui/README.md index 1fb5fbeae..31c851ccf 100644 --- a/ui/packages/consul-ui/README.md +++ b/ui/packages/consul-ui/README.md @@ -128,9 +128,7 @@ token/secret. | `CONSUL_EXPOSED_COUNT` | (random) | Configure the number of exposed paths that the API returns. | | `CONSUL_CHECK_COUNT` | (random) | Configure the number of health checks that the API returns. | | `CONSUL_OIDC_PROVIDER_COUNT` | (random) | Configure the number of OIDC providers that the API returns. | -| `DEBUG_ROUTES_ENDPOINT` | undefined | When using the window.Routes() debug -utility ([see utility functions](#browser-debug-utility-functions)), use a URL to pass the route DSL to. %s in the URL will be replaced -with the route DSL - http://url.com?routes=%s | +| `DEBUG_ROUTES_ENDPOINT` | undefined | When using the window.Routes() debug utility ([see utility functions](#browser-debug-utility-functions)), use a URL to pass the route DSL to. %s in the URL will be replaced with the route DSL - http://url.com?routes=%s | See `./mock-api` for more details. @@ -144,6 +142,7 @@ URLs i.e. `javascript:Routes()` | Variable | Arguments | Description | | -------- | --------- | ----------- | | `Routes(url)` | url: The url to pass the DSL to, if left `undefined` just use a blank tab | Provides a way to easily print out Embers Route DSL for the application or to pass it straight to any third party utility such as ember-diagonal | +| `Scenario(str)` | str: 'Cookie formatted' string, if left `undefined` open a new tab with a link/bookmarklet to the current Scenario | Provides a way to easily save and reload scenarios of configurations via URLs or bookmarklets | ### Code Generators diff --git a/ui/packages/consul-ui/app/utils/get-environment.js b/ui/packages/consul-ui/app/utils/get-environment.js index 468556150..04e252ac5 100644 --- a/ui/packages/consul-ui/app/utils/get-environment.js +++ b/ui/packages/consul-ui/app/utils/get-environment.js @@ -11,12 +11,37 @@ export default function(config = {}, win = window, doc = document) { // look at the hash in the URL and transfer anything after the hash into // cookies to enable linking of the UI with various settings enabled runInDebug(() => { + const cookies = function(str) { + return str + .split(';') + .map(item => item.trim()) + .filter(item => item !== '') + .filter(item => + item + .split('=') + .shift() + .startsWith('CONSUL_') + ); + }; + win.Scenario = function(str = '') { + if (str.length > 0) { + cookies(str).forEach(item => (doc.cookie = `${item};Path=/`)); + win.location.hash = ''; + location.reload(); + } else { + str = cookies(doc.cookie).join(';'); + const tab = win.open('', '_blank'); + tab.document.write( + `
${location.href}#${str}

Scenario` + ); + } + }; if ( typeof win.location !== 'undefined' && typeof win.location.hash === 'string' && win.location.hash.length > 0 ) { - doc.cookie = win.location.hash.substr(1); + win.Scenario(win.location.hash.substr(1)); } }); const dev = function(str = doc.cookie) {