UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

196 lines 5.96 kB
import _defineProperty from "@babel/runtime-corejs3/helpers/esm/defineProperty"; import _pushInstanceProperty from "core-js-pure/stable/instance/push.js"; import { globalSyncCounter, globalHeadingCounter } from "./HeadingHelpers.js"; export const initCounter = (props = null) => { if (!globalHeadingCounter.current) { globalHeadingCounter.current = new Counter({ group: 'global', isGlobal: true }); } if (props !== null && props !== void 0 && props.counter) { return props.counter; } return new Counter(props); }; export class Counter { constructor(props = null) { _defineProperty(this, "level", 0); _defineProperty(this, "entry", 0); _defineProperty(this, "lastResetLevel", null); _defineProperty(this, "_isReady", false); _defineProperty(this, "countHeadings", 0); _defineProperty(this, "_initCount", 0); _defineProperty(this, "isGlobal", false); _defineProperty(this, "isHeading", false); _defineProperty(this, "bypassChecks", false); _defineProperty(this, "contextCounter", null); _defineProperty(this, "reports", []); _defineProperty(this, "group", null); _defineProperty(this, "children", null); props = props || {}; if (props.group) { this.group = props.group; } if (props.isGlobal) { this.isGlobal = true; } this.children = props.children; } report(...str) { if (!(typeof process !== 'undefined' && process.env.NODE_ENV === 'production')) { var _context; _pushInstanceProperty(_context = this.reports).call(_context, str); } } useLastReport() { return this.reports.shift(); } enableBypassChecks() { this.bypassChecks = true; } disableBypassChecks() { this.bypassChecks = false; } getLevel() { return this.level; } hasEntryLevel() { return this.entry > 0; } hasCorrection() { return this._isReady; } setEntryLevel(level = null) { this.entry = parseFloat(String(level)) || 1; } isInContext() { return Boolean(this.contextCounter); } setContextCounter(contextCounter) { this.contextCounter = contextCounter; } windup() { this.contextCounter.countHeadings++; } teardown() { if (this.contextCounter.countHeadings > 0) { this.contextCounter.level = this.contextCounter.entry; } this.contextCounter.countHeadings--; this.contextCounter._initCount--; } makeMeReady({ level } = {}) { if (!this.hasCorrection()) { if (this.contextCounter.level > 1) { this.level = this.contextCounter.level; } else if (this.contextCounter._isReady) { if (!this.bypassChecks) { this.level = 2; } } else if (!this.contextCounter._isReady) { this.contextCounter._isReady = true; if (!this.bypassChecks && !level) { this.level = 1; } } if (this.entry === 0) { this.entry = this.level; if (this.isHeading) { this.contextCounter._initCount++; } } } } factorCheck({ action, level, current, report }) { if (!this.bypassChecks && level - current > 1) { report && _pushInstanceProperty(report).call(report, `Heading levels can only be changed by factor one! Got:`, level, 'and had before', current); if (level > current) { action = 'increment'; } switch (action) { case 'increment': level = current + 1; break; case 'decrement': level = current - 1; break; default: level = current === 0 ? 1 : current; break; } } return level; } setLevel(level, action = 'set') { var _globalSyncCounter$cu; level = parseFloat(String(level)); const report = []; if (((_globalSyncCounter$cu = globalSyncCounter.current) === null || _globalSyncCounter$cu === void 0 ? void 0 : _globalSyncCounter$cu.level) > 0) { level = this.factorCheck({ action, level, current: globalSyncCounter.current.level, report }); } level = this.factorCheck({ action, level, current: this.level, report }); if (level > 6) { _pushInstanceProperty(report).call(report, `Cannot [${action}] heading level higher than 6! Got:`, level, 'and had before', this.level); level = 6; } else if (level < 1 && this.level !== -1) { _pushInstanceProperty(report).call(report, `Cannot [${action}] heading level lower than 1! Got:`, level, 'and had before', this.level); level = 1; } else if (!this.bypassChecks && level === 1 && this.level === 1) { _pushInstanceProperty(report).call(report, `Cannot set ([${action}]) heading level 1 several times! Got:`, level, 'and had before', this.level); level = 2; } else if (!this.bypassChecks && level < 2 && this.level === 2) { _pushInstanceProperty(report).call(report, 'Cannot decrease to heading level 1! Had before', this.level); level = this.level; } if (report.length > 0) { _pushInstanceProperty(report).call(report, '- The new level is', level); this.report(...report); } this.level = level; if (this.isHeading) { this.contextCounter.level = level; } else { this.entry = level; } } increment() { this.setLevel(this.level + 1, 'increment'); } decrement() { this.setLevel(this.level - 1, 'decrement'); } force(level = 1) { this.bypassChecks = true; this.setLevel(level); this.bypassChecks = false; } reset(toLevel = null) { toLevel = parseFloat(toLevel) || 2; this.level = this.entry = this.lastResetLevel = toLevel; if (this.contextCounter) { this.contextCounter.level = this.contextCounter.entry; } if (toLevel === 1 && globalHeadingCounter.current) { globalHeadingCounter.current.level = 2; } } } //# sourceMappingURL=HeadingCounter.js.map