@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
207 lines (206 loc) • 6.8 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.initCounter = exports.Counter = void 0;
var _push = _interopRequireDefault(require("core-js-pure/stable/instance/push.js"));
var _HeadingHelpers = require("./HeadingHelpers.js");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
const initCounter = (props = null) => {
if (!_HeadingHelpers.globalHeadingCounter.current) {
_HeadingHelpers.globalHeadingCounter.current = new Counter({
group: 'global',
isGlobal: true
});
}
if (props !== null && props !== void 0 && props.counter) {
return props.counter;
}
return new Counter(props);
};
exports.initCounter = initCounter;
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;
(0, _push.default)(_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 && (0, _push.default)(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 = _HeadingHelpers.globalSyncCounter.current) === null || _globalSyncCounter$cu === void 0 ? void 0 : _globalSyncCounter$cu.level) > 0) {
level = this.factorCheck({
action,
level,
current: _HeadingHelpers.globalSyncCounter.current.level,
report
});
}
level = this.factorCheck({
action,
level,
current: this.level,
report
});
if (level > 6) {
(0, _push.default)(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) {
(0, _push.default)(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) {
(0, _push.default)(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) {
(0, _push.default)(report).call(report, 'Cannot decrease to heading level 1! Had before', this.level);
level = this.level;
}
if (report.length > 0) {
(0, _push.default)(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 && _HeadingHelpers.globalHeadingCounter.current) {
_HeadingHelpers.globalHeadingCounter.current.level = 2;
}
}
}
exports.Counter = Counter;
//# sourceMappingURL=HeadingCounter.js.map