Skip to content

Commit b89bd6b

Browse files
authored
feat: persist _debugLog in localStorage (#1570)
1 parent 9411f1f commit b89bd6b

File tree

4 files changed

+34
-14
lines changed

4 files changed

+34
-14
lines changed

CHANGELOG-1.x.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44

55
# 1.8.0 (2021-05-18)
66

7-
- [f1c1176](https://github.com/patternfly/patternfly-elements/commit/f1c1176d9278d6e5b8066b42fc040ea01d98ecb2) feat: pfe-icon now supports setting default icon sets
7+
- [](https://github.com/patternfly/patternfly-elements/commit/) feat: persist debugLog in localStorage
88
- [267ff8e](https://github.com/patternfly/patternfly-elements/commit/267ff8ee7df7cd0512f16c58fdb169f941bfa4cd) feat: Update fetch mixin to support region input (#1328)
9+
- [f1c1176](https://github.com/patternfly/patternfly-elements/commit/f1c1176d9278d6e5b8066b42fc040ea01d98ecb2) feat: pfe-icon now supports setting default icon sets
910
- [56eb55e](https://github.com/patternfly/patternfly-elements/commit/56eb55ec8b4b62aee7d36950d158229cbf50ddef) fix: pfe-accordion IE11 regression; background context should always be white with black text
1011
- [398003e](https://github.com/patternfly/patternfly-elements/commit/398003e3d805567af826d924bfd5e2e9655425a4) fix: [pfe-icon] Update icon color vs. background color custom property support
1112
- [63df897](https://github.com/patternfly/patternfly-elements/commit/63df897d5235a2af15733b52a1c0f4d8304dcb96) fix: Add in missing polyfills for pfelement

elements/pfelement/README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,11 @@ If a value is defined at any point of this flow, an `on` attribute will be attac
125125

126126
The `context_update` function will use much the same logic except it updates the context for the component calling the function and triggers an update for all of it's pfelement children.
127127

128-
### Log
128+
### Debug logging
129129

130-
Prints a message to the console log in a standardized format when debugging is turned on: `[pfe-band]: This is my console message.`. To invoke this inside an element, you can add `this.log("This is my console message");` to your JS file.
130+
Debug logging can be enabled by running `localStorage.pfeLog = true` in the console. This setting is sticky, so you can refresh the page and debug logging will stay enabled on that domain. To disable debug logging, run `localStorage.removeItem("pfeLog")`. You can also run `PFElement.debugLog(true)` to enable and `PFElement.debugLog(false)` to disable, however it's almost always easier to use localStorage directly because it can be tedious to get a reference to `PFElement` from the console.
131+
132+
Printing debug log messages can be done with `this.log("This is my debug log message")` within any element's method. This will produce a console log such as: `[pfe-band]: This is my console message.`.
131133

132134
### Emit event
133135

elements/pfelement/src/pfelement.js

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { autoReveal } from "./reveal.js";
2-
import { isAllowedType, isValidDefaultType } from "./attrDefValidators.js";
1+
import {autoReveal} from "./reveal.js";
2+
import {isAllowedType, isValidDefaultType} from "./attrDefValidators.js";
33
// Import polyfills: includes
44
import "./polyfills--pfelement.js";
55

@@ -19,14 +19,23 @@ class PFElement extends HTMLElement {
1919
/**
2020
* A boolean value that indicates if the logging should be printed to the console; used for debugging.
2121
* For use in a JS file or script tag; can also be added in the constructor of a component during development.
22-
* @example PFElement._debugLog = true;
22+
* @example PFElement.debugLog(true);
2323
* @tags debug
2424
*/
2525
static debugLog(preference = null) {
2626
if (preference !== null) {
27-
PFElement._debugLog = !!preference;
27+
// wrap localStorage references in a try/catch; merely referencing it can
28+
// throw errors in some locked down environments
29+
try {
30+
localStorage.pfeLog = !!preference;
31+
} catch (e) {
32+
// if localStorage fails, fall back to PFElement._debugLog
33+
PFElement._debugLog = !!preference;
34+
return PFElement._debugLog;
35+
}
2836
}
29-
return PFElement._debugLog;
37+
// @TODO the reference to _debugLog is for backwards compatibiilty and will be removed in 2.0
38+
return localStorage.pfeLog === "true" || PFElement._debugLog;
3039
}
3140

3241
/**
@@ -353,7 +362,7 @@ class PFElement extends HTMLElement {
353362
this.on = value;
354363
}
355364

356-
constructor(pfeClass, { type = null, delayRender = false } = {}) {
365+
constructor(pfeClass, {type = null, delayRender = false} = {}) {
357366
super();
358367

359368
this._pfeClass = pfeClass;
@@ -386,7 +395,7 @@ class PFElement extends HTMLElement {
386395
// Initalize the properties and attributes from the property getter
387396
this._initializeProperties();
388397

389-
this.attachShadow({ mode: "open" });
398+
this.attachShadow({mode: "open"});
390399

391400
// Tracks if the component has been initially rendered. Useful if for debouncing
392401
// template updates.
@@ -501,7 +510,7 @@ class PFElement extends HTMLElement {
501510

502511
// If the slot definition exists, set up an observer
503512
if (typeof this.slots === "object" && this._slotsObserver) {
504-
this._slotsObserver.observe(this, { childList: true });
513+
this._slotsObserver.observe(this, {childList: true});
505514
}
506515

507516
// If an observer was defined, set it to begin observing here
@@ -519,7 +528,7 @@ class PFElement extends HTMLElement {
519528
/**
520529
* A wrapper around an event dispatch to standardize formatting.
521530
*/
522-
emitEvent(name, { bubbles = true, cancelable = false, composed = true, detail = {} } = {}) {
531+
emitEvent(name, {bubbles = true, cancelable = false, composed = true, detail = {}} = {}) {
523532
if (detail) this.log(`Custom event: ${name}`, detail);
524533
else this.log(`Custom event: ${name}`);
525534

@@ -792,7 +801,7 @@ class PFElement extends HTMLElement {
792801

793802
this.log("Slots validated.");
794803

795-
if (this._slotsObserver) this._slotsObserver.observe(this, { childList: true });
804+
if (this._slotsObserver) this._slotsObserver.observe(this, {childList: true});
796805
}
797806

798807
/**
@@ -1036,7 +1045,7 @@ class PFElement extends HTMLElement {
10361045
*/
10371046
static _populateCache(pfe) {
10381047
// @TODO add a warning when a component property conflicts with a global property.
1039-
const mergedProperties = { ...pfe.properties, ...PFElement.properties };
1048+
const mergedProperties = {...pfe.properties, ...PFElement.properties};
10401049

10411050
pfe._setCache("componentProperties", pfe.properties);
10421051
pfe._setCache("globalProperties", PFElement.properties);

elements/pfelement/test/pfelement_logging_test.html

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,14 @@
8181
PFElement.debugLog(false);
8282
});
8383

84+
test("logging should begin when localStorage value is present", () => {
85+
const test = document.querySelector("pfe-test");
86+
localStorage.setItem("pfeLog", true);
87+
test.setAttribute(testAttrName, 3);
88+
assert.isTrue(logSpy.called);
89+
localStorage.removeItem("pfeLog");
90+
});
91+
8492
test("logging should end when opted out of", () => {
8593
const test = document.querySelector("pfe-test");
8694
PFElement.debugLog(true);

0 commit comments

Comments
 (0)