mn-angular-lib 2.0.56 → 2.0.58

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1085,6 +1085,15 @@ var MnWizardBodyComponent = class MnWizardBodyComponent {
1085
1085
  isCurrentStepValid = true;
1086
1086
  isCompleting = false;
1087
1087
  wizardErrors = {};
1088
+ historyDepth = 0;
1089
+ ignorePopstate = false;
1090
+ resolveUnwind;
1091
+ static UNWIND_FALLBACK_MS = 300;
1092
+ static HISTORY_STATE_KEY = "mnWizard";
1093
+ static HISTORY_POS_KEY = "mnWizardPos";
1094
+ static HISTORY_ID_KEY = "mnWizardId";
1095
+ static nextHistoryId = 1;
1096
+ historyId = MnWizardBodyComponent.nextHistoryId++;
1088
1097
  measuredMinHeight = 0;
1089
1098
  languageService = inject(MnLanguageService);
1090
1099
  static DEFAULT_LABELS = {
@@ -1136,6 +1145,77 @@ var MnWizardBodyComponent = class MnWizardBodyComponent {
1136
1145
  }
1137
1146
  this.setCurrentStep(this.config.startStepId || this.config.steps[0]?.id);
1138
1147
  if (this.currentStepId) this.visitedStepIds.push(this.currentStepId);
1148
+ this.pushWizardHistory();
1149
+ }
1150
+ wizardHistoryState(pos) {
1151
+ return {
1152
+ [MnWizardBodyComponent.HISTORY_STATE_KEY]: this.currentStepId,
1153
+ [MnWizardBodyComponent.HISTORY_POS_KEY]: pos,
1154
+ [MnWizardBodyComponent.HISTORY_ID_KEY]: this.historyId
1155
+ };
1156
+ }
1157
+ isOwnHistoryEntry() {
1158
+ return typeof history !== "undefined" && history.state?.[MnWizardBodyComponent.HISTORY_ID_KEY] === this.historyId && history.state?.[MnWizardBodyComponent.HISTORY_STATE_KEY] != null && typeof history.state?.[MnWizardBodyComponent.HISTORY_POS_KEY] === "number";
1159
+ }
1160
+ pushWizardHistory() {
1161
+ if (typeof history === "undefined" || typeof history.pushState !== "function") return;
1162
+ history.pushState(this.wizardHistoryState(this.historyDepth), "");
1163
+ this.historyDepth += 1;
1164
+ }
1165
+ syncWizardHistoryEntry() {
1166
+ if (typeof history === "undefined" || typeof history.replaceState !== "function") return;
1167
+ if (!this.isOwnHistoryEntry()) return;
1168
+ if (history.state[MnWizardBodyComponent.HISTORY_STATE_KEY] === this.currentStepId) return;
1169
+ history.replaceState(this.wizardHistoryState(this.historyDepth - 1), "");
1170
+ }
1171
+ onBrowserBack() {
1172
+ if (this.ignorePopstate) {
1173
+ this.ignorePopstate = false;
1174
+ this.syncWizardHistoryEntry();
1175
+ this.resolveUnwind?.();
1176
+ return;
1177
+ }
1178
+ if (!this.isOwnHistoryEntry()) {
1179
+ if (this.historyDepth <= 0) return;
1180
+ this.historyDepth = 0;
1181
+ this.dismissFromBrowserBack();
1182
+ return;
1183
+ }
1184
+ const stateStep = history.state[MnWizardBodyComponent.HISTORY_STATE_KEY];
1185
+ const statePos = history.state[MnWizardBodyComponent.HISTORY_POS_KEY];
1186
+ const fromDepth = this.historyDepth;
1187
+ this.historyDepth = statePos + 1;
1188
+ if (stateStep === this.currentStepId) return;
1189
+ if (this.historyDepth > fromDepth) {
1190
+ this.applyHistoryForward(stateStep, fromDepth);
1191
+ return;
1192
+ }
1193
+ this.applyHistoryBack(stateStep);
1194
+ }
1195
+ async applyHistoryForward(stepId, fromDepth) {
1196
+ const target = this.visibleSteps.find((s) => s.id === stepId);
1197
+ if (!target || !await this.canLeaveCurrentStep() || !await this.canEnterStep(target)) {
1198
+ this.historyDepth = fromDepth;
1199
+ this.ignorePopstate = true;
1200
+ history.back();
1201
+ return;
1202
+ }
1203
+ const previousStepId = this.currentStepId;
1204
+ this.setCurrentStep(stepId);
1205
+ if (!this.visitedStepIds.includes(stepId)) this.visitedStepIds.push(stepId);
1206
+ await this.notifyStepChange(previousStepId, NavigationDirection.FORWARD);
1207
+ this.trackCurrentStepValidity();
1208
+ }
1209
+ async applyHistoryBack(stepId) {
1210
+ const previousStepId = this.currentStepId;
1211
+ this.visitedStepIds = this.visitedStepIds.filter((id) => id !== this.currentStepId);
1212
+ this.setCurrentStep(stepId);
1213
+ await this.notifyStepChange(previousStepId, NavigationDirection.BACKWARD);
1214
+ this.trackCurrentStepValidity();
1215
+ }
1216
+ async dismissFromBrowserBack() {
1217
+ if (this.config.onCancel) await this.config.onCancel(ModalCloseReason.CANCELLED);
1218
+ this.modalRef.dismiss(ModalCloseReason.CANCELLED);
1139
1219
  }
1140
1220
  ngAfterViewInit() {
1141
1221
  this.formBodiesSubscription = this.formBodies.changes.subscribe(() => {
@@ -1174,12 +1254,49 @@ var MnWizardBodyComponent = class MnWizardBodyComponent {
1174
1254
  const previousStepId = this.currentStepId;
1175
1255
  this.setCurrentStep(step.id);
1176
1256
  if (!this.visitedStepIds.includes(this.currentStepId)) this.visitedStepIds.push(this.currentStepId);
1257
+ this.pushWizardHistory();
1177
1258
  await this.notifyStepChange(previousStepId, NavigationDirection.DIRECT);
1178
1259
  this.trackCurrentStepValidity();
1179
1260
  }
1180
1261
  ngOnDestroy() {
1181
1262
  this.statusSubscription?.unsubscribe();
1182
1263
  this.formBodiesSubscription?.unsubscribe();
1264
+ this.dropWizardHistory();
1265
+ }
1266
+ async unwindWizardHistory() {
1267
+ if (this.historyDepth <= 0) return;
1268
+ if (typeof history === "undefined" || typeof history.go !== "function" || !this.isOwnHistoryEntry()) {
1269
+ this.historyDepth = 0;
1270
+ return;
1271
+ }
1272
+ const depth = this.historyDepth;
1273
+ this.historyDepth = 0;
1274
+ await new Promise((resolve) => {
1275
+ const done = () => {
1276
+ clearTimeout(fallback);
1277
+ this.resolveUnwind = void 0;
1278
+ this.ignorePopstate = false;
1279
+ resolve();
1280
+ };
1281
+ const fallback = setTimeout(done, MnWizardBodyComponent.UNWIND_FALLBACK_MS);
1282
+ this.resolveUnwind = done;
1283
+ this.ignorePopstate = true;
1284
+ history.go(-depth);
1285
+ });
1286
+ }
1287
+ restoreWizardHistoryEntry() {
1288
+ if (this.historyDepth === 0) this.pushWizardHistory();
1289
+ }
1290
+ dropWizardHistory() {
1291
+ if (this.historyDepth <= 0) return;
1292
+ if (typeof history === "undefined" || typeof history.go !== "function") {
1293
+ this.historyDepth = 0;
1294
+ return;
1295
+ }
1296
+ const depth = this.historyDepth;
1297
+ this.historyDepth = 0;
1298
+ if (!this.isOwnHistoryEntry()) return;
1299
+ history.go(-depth);
1183
1300
  }
1184
1301
  measureTallestStep() {
1185
1302
  if (this.config.sizeHeight || this.config.sizeWidth === ModalSize.FULL) return;
@@ -1246,28 +1363,35 @@ var MnWizardBodyComponent = class MnWizardBodyComponent {
1246
1363
  if (!this.isFreeFlow) return false;
1247
1364
  return this.isStepVisible(step);
1248
1365
  }
1249
- async next() {
1250
- if (!this.canGoNext) return;
1366
+ async canLeaveCurrentStep() {
1251
1367
  const currentStep = this.currentStep;
1252
1368
  const formBody = this.getCurrentFormBody();
1253
1369
  if (formBody && (formBody.form.invalid || formBody.hasFormErrors)) {
1254
1370
  formBody.form.markAllAsTouched();
1255
- return;
1371
+ return false;
1256
1372
  }
1257
1373
  if (currentStep?.guard?.canExit) {
1258
- if (!await currentStep.guard.canExit()) return;
1374
+ if (!await currentStep.guard.canExit()) return false;
1259
1375
  }
1260
1376
  if (currentStep?.validators) {
1261
- for (const validator of currentStep.validators) if ((await validator.validate()).status === "invalid") return;
1377
+ for (const validator of currentStep.validators) if ((await validator.validate()).status === "invalid") return false;
1262
1378
  }
1379
+ return true;
1380
+ }
1381
+ async canEnterStep(step) {
1382
+ if (!step.guard?.canEnter) return true;
1383
+ return step.guard.canEnter();
1384
+ }
1385
+ async next() {
1386
+ if (!this.canGoNext) return;
1387
+ if (!await this.canLeaveCurrentStep()) return;
1263
1388
  const nextStep = this.visibleSteps[this.currentVisibleIndex + 1];
1264
1389
  if (!nextStep) return;
1265
- if (nextStep.guard?.canEnter) {
1266
- if (!await nextStep.guard.canEnter()) return;
1267
- }
1390
+ if (!await this.canEnterStep(nextStep)) return;
1268
1391
  const previousStepId = this.currentStepId;
1269
1392
  this.setCurrentStep(nextStep.id);
1270
1393
  if (!this.visitedStepIds.includes(this.currentStepId)) this.visitedStepIds.push(this.currentStepId);
1394
+ this.pushWizardHistory();
1271
1395
  await this.notifyStepChange(previousStepId, NavigationDirection.FORWARD);
1272
1396
  this.trackCurrentStepValidity();
1273
1397
  }
@@ -1301,9 +1425,24 @@ var MnWizardBodyComponent = class MnWizardBodyComponent {
1301
1425
  const previousStepId = this.currentStepId;
1302
1426
  this.visitedStepIds = this.visitedStepIds.filter((id) => id !== this.currentStepId);
1303
1427
  this.setCurrentStep(prevStep.id);
1428
+ this.popWizardHistoryEntry();
1304
1429
  await this.notifyStepChange(previousStepId, NavigationDirection.BACKWARD);
1305
1430
  this.trackCurrentStepValidity();
1306
1431
  }
1432
+ popWizardHistoryEntry() {
1433
+ if (this.historyDepth <= 0) return;
1434
+ if (this.historyDepth === 1) {
1435
+ this.syncWizardHistoryEntry();
1436
+ return;
1437
+ }
1438
+ if (typeof history === "undefined" || typeof history.back !== "function") {
1439
+ this.historyDepth -= 1;
1440
+ return;
1441
+ }
1442
+ this.ignorePopstate = true;
1443
+ this.historyDepth -= 1;
1444
+ history.back();
1445
+ }
1307
1446
  setCurrentStep(stepId) {
1308
1447
  this.currentStepId = stepId;
1309
1448
  this.currentStepTitle.set(this.config.steps.find((s) => s.id === stepId)?.title);
@@ -1344,13 +1483,16 @@ var MnWizardBodyComponent = class MnWizardBodyComponent {
1344
1483
  visitedStepIds: this.visitedStepIds,
1345
1484
  payload: aggregatedData
1346
1485
  };
1486
+ await this.unwindWizardHistory();
1347
1487
  if (this.config.onComplete != null && await this.config.onComplete.handle(result) === false) {
1488
+ this.restoreWizardHistoryEntry();
1348
1489
  this.isCompleting = false;
1349
1490
  this.cdr.detectChanges();
1350
1491
  return;
1351
1492
  }
1352
1493
  this.modalRef.close(result);
1353
1494
  } catch (error) {
1495
+ this.restoreWizardHistoryEntry();
1354
1496
  this.isCompleting = false;
1355
1497
  this.cdr.detectChanges();
1356
1498
  console.error("Wizard completion error:", error);
@@ -1418,6 +1560,7 @@ var MnWizardBodyComponent = class MnWizardBodyComponent {
1418
1560
  config: "config",
1419
1561
  modalRef: "modalRef"
1420
1562
  },
1563
+ host: { listeners: { "window:popstate": "onBrowserBack()" } },
1421
1564
  viewQueries: [
1422
1565
  {
1423
1566
  propertyName: "stepScroller",
@@ -1535,6 +1678,10 @@ i0.ɵɵngDeclareClassMetadata({
1535
1678
  stepScroller: [{
1536
1679
  type: ViewChild,
1537
1680
  args: ["stepScroller"]
1681
+ }],
1682
+ onBrowserBack: [{
1683
+ type: HostListener,
1684
+ args: ["window:popstate"]
1538
1685
  }]
1539
1686
  }
1540
1687
  });