UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

206 lines 6.4 kB
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty"; import "core-js/modules/web.dom-collections.iterator.js"; import { globalSyncCounter, globalHeadingCounter } from './HeadingHelpers'; export const initCounter = function () { let props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 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() { let props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 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() { if (!(typeof process !== 'undefined' && process.env.NODE_ENV === 'production')) { for (var _len = arguments.length, str = new Array(_len), _key = 0; _key < _len; _key++) { str[_key] = arguments[_key]; } this.reports.push(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() { let level = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 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() { let { level } = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; 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(_ref) { let { action, level, current, report } = _ref; if (!this.bypassChecks && level - current > 1) { report && report.push(`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) { var _globalSyncCounter$cu; let action = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'set'; 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) { report.push(`Cannot [${action}] heading level higher than 6! Got:`, level, 'and had before', this.level); level = 6; } else if (level < 1 && this.level !== -1) { report.push(`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) { report.push(`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) { report.push('Cannot decrease to heading level 1! Had before', this.level); level = this.level; } if (report.length > 0) { report.push('- 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() { let level = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 1; this.bypassChecks = true; this.setLevel(level); this.bypassChecks = false; } reset() { let toLevel = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 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