@customviews-js/customviews 1.1.9 → 1.1.10

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.
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * @customviews-js/customviews v1.1.8
2
+ * @customviews-js/customviews v1.1.10
3
3
  * (c) 2025 Chan Ger Teck
4
4
  * Released under the MIT License.
5
5
  */
@@ -350,13 +350,10 @@ class TabManager {
350
350
  if (tabs[groupId]) {
351
351
  return tabs[groupId];
352
352
  }
353
- // 2. Check config default
353
+ // 2. Check config for first tab
354
354
  if (cfgGroups) {
355
355
  const groupCfg = cfgGroups.find(g => g.id === groupId);
356
356
  if (groupCfg) {
357
- if (groupCfg.default) {
358
- return groupCfg.default;
359
- }
360
357
  // Fallback to first tab in config
361
358
  const firstConfigTab = groupCfg.tabs[0];
362
359
  if (firstConfigTab) {
@@ -1065,7 +1062,7 @@ class CustomViewsCore {
1065
1062
  this.persistenceManager = new PersistenceManager();
1066
1063
  this.visibilityManager = new VisibilityManager();
1067
1064
  this.showUrlEnabled = opt.showUrl ?? false;
1068
- this.lastAppliedState = this.cloneState(this.config?.defaultState);
1065
+ this.lastAppliedState = this.cloneState(this.getComputedDefaultState());
1069
1066
  }
1070
1067
  getConfig() {
1071
1068
  return this.config;
@@ -1076,6 +1073,36 @@ class CustomViewsCore {
1076
1073
  getTabGroups() {
1077
1074
  return this.config.tabGroups;
1078
1075
  }
1076
+ /**
1077
+ * Generate a computed default state:
1078
+ * - If config.defaultState is defined, use it (even if empty)
1079
+ * - Otherwise, compute a default: enable all toggles and set all tab groups to their first tab
1080
+ */
1081
+ getComputedDefaultState() {
1082
+ const configDefaultState = this.config?.defaultState;
1083
+ // If defaultState is explicitly defined in config, use it as-is
1084
+ if (configDefaultState !== undefined) {
1085
+ return configDefaultState;
1086
+ }
1087
+ // Otherwise, compute a default state: all toggles on, all tabs to first
1088
+ const tabs = {};
1089
+ // Set all tab groups to their first tab
1090
+ if (this.config.tabGroups?.length) {
1091
+ this.config.tabGroups.forEach(group => {
1092
+ if (group.tabs && group.tabs.length > 0) {
1093
+ const firstTab = group.tabs[0];
1094
+ if (firstTab?.id) {
1095
+ tabs[group.id] = firstTab.id;
1096
+ }
1097
+ }
1098
+ });
1099
+ }
1100
+ const computedState = {
1101
+ toggles: [...(this.config.allToggles || [])],
1102
+ tabs
1103
+ };
1104
+ return computedState;
1105
+ }
1079
1106
  /**
1080
1107
  * Get currently active tabs (from URL > persisted (localStorage) > defaults)
1081
1108
  */
@@ -1087,7 +1114,7 @@ class CustomViewsCore {
1087
1114
  if (persistedState?.tabs) {
1088
1115
  return { ...persistedState.tabs };
1089
1116
  }
1090
- return this.config?.defaultState?.tabs ? { ...this.config.defaultState.tabs } : {};
1117
+ return this.getComputedDefaultState().tabs || {};
1091
1118
  }
1092
1119
  /**
1093
1120
  * Set active tab for a group and apply state
@@ -1139,7 +1166,7 @@ class CustomViewsCore {
1139
1166
  });
1140
1167
  this.loadAndCallApplyState();
1141
1168
  }
1142
- // Priority: URL state > persisted state > default
1169
+ // Priority: URL state > persisted state > config default > computed default
1143
1170
  // Also filters using the visibility manager to persist selection
1144
1171
  // across back/forward button clicks
1145
1172
  async loadAndCallApplyState() {
@@ -1155,8 +1182,8 @@ class CustomViewsCore {
1155
1182
  this.applyState(persistedState);
1156
1183
  return;
1157
1184
  }
1158
- // 3. Local Config Fallback
1159
- this.renderState(this.config.defaultState);
1185
+ // 3. Computed Default Fallback
1186
+ this.renderState(this.getComputedDefaultState());
1160
1187
  }
1161
1188
  /**
1162
1189
  * Apply a custom state, saves to localStorage and updates the URL
@@ -1175,7 +1202,7 @@ class CustomViewsCore {
1175
1202
  /** Render all toggles for the current state */
1176
1203
  renderState(state) {
1177
1204
  this.lastAppliedState = this.cloneState(state);
1178
- const toggles = state.toggles || [];
1205
+ const toggles = state?.toggles || [];
1179
1206
  const finalToggles = this.visibilityManager.filterVisibleToggles(toggles);
1180
1207
  // Apply toggle visibility
1181
1208
  ToggleManager.applyToggles(this.rootEl, finalToggles);
@@ -1194,7 +1221,7 @@ class CustomViewsCore {
1194
1221
  resetToDefault() {
1195
1222
  this.persistenceManager.clearAll();
1196
1223
  if (this.config) {
1197
- this.renderState(this.config.defaultState);
1224
+ this.renderState(this.getComputedDefaultState());
1198
1225
  }
1199
1226
  else {
1200
1227
  console.warn("No configuration loaded, cannot reset to default state");
@@ -1212,7 +1239,7 @@ class CustomViewsCore {
1212
1239
  return this.lastAppliedState.toggles || [];
1213
1240
  }
1214
1241
  if (this.config) {
1215
- return this.config.defaultState.toggles || [];
1242
+ return this.getComputedDefaultState().toggles || [];
1216
1243
  }
1217
1244
  return [];
1218
1245
  }
@@ -1222,7 +1249,7 @@ class CustomViewsCore {
1222
1249
  clearPersistence() {
1223
1250
  this.persistenceManager.clearAll();
1224
1251
  if (this.config) {
1225
- this.renderState(this.config.defaultState);
1252
+ this.renderState(this.getComputedDefaultState());
1226
1253
  }
1227
1254
  else {
1228
1255
  console.warn("No configuration loaded, cannot reset to default state");
@@ -1301,7 +1328,7 @@ class CustomViewsCore {
1301
1328
  return this.cloneState(this.lastAppliedState);
1302
1329
  }
1303
1330
  if (this.config) {
1304
- return this.cloneState(this.config.defaultState);
1331
+ return this.cloneState(this.getComputedDefaultState());
1305
1332
  }
1306
1333
  return {};
1307
1334
  }