@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
  */
@@ -348,13 +348,10 @@ class TabManager {
348
348
  if (tabs[groupId]) {
349
349
  return tabs[groupId];
350
350
  }
351
- // 2. Check config default
351
+ // 2. Check config for first tab
352
352
  if (cfgGroups) {
353
353
  const groupCfg = cfgGroups.find(g => g.id === groupId);
354
354
  if (groupCfg) {
355
- if (groupCfg.default) {
356
- return groupCfg.default;
357
- }
358
355
  // Fallback to first tab in config
359
356
  const firstConfigTab = groupCfg.tabs[0];
360
357
  if (firstConfigTab) {
@@ -1063,7 +1060,7 @@ class CustomViewsCore {
1063
1060
  this.persistenceManager = new PersistenceManager();
1064
1061
  this.visibilityManager = new VisibilityManager();
1065
1062
  this.showUrlEnabled = opt.showUrl ?? false;
1066
- this.lastAppliedState = this.cloneState(this.config?.defaultState);
1063
+ this.lastAppliedState = this.cloneState(this.getComputedDefaultState());
1067
1064
  }
1068
1065
  getConfig() {
1069
1066
  return this.config;
@@ -1074,6 +1071,36 @@ class CustomViewsCore {
1074
1071
  getTabGroups() {
1075
1072
  return this.config.tabGroups;
1076
1073
  }
1074
+ /**
1075
+ * Generate a computed default state:
1076
+ * - If config.defaultState is defined, use it (even if empty)
1077
+ * - Otherwise, compute a default: enable all toggles and set all tab groups to their first tab
1078
+ */
1079
+ getComputedDefaultState() {
1080
+ const configDefaultState = this.config?.defaultState;
1081
+ // If defaultState is explicitly defined in config, use it as-is
1082
+ if (configDefaultState !== undefined) {
1083
+ return configDefaultState;
1084
+ }
1085
+ // Otherwise, compute a default state: all toggles on, all tabs to first
1086
+ const tabs = {};
1087
+ // Set all tab groups to their first tab
1088
+ if (this.config.tabGroups?.length) {
1089
+ this.config.tabGroups.forEach(group => {
1090
+ if (group.tabs && group.tabs.length > 0) {
1091
+ const firstTab = group.tabs[0];
1092
+ if (firstTab?.id) {
1093
+ tabs[group.id] = firstTab.id;
1094
+ }
1095
+ }
1096
+ });
1097
+ }
1098
+ const computedState = {
1099
+ toggles: [...(this.config.allToggles || [])],
1100
+ tabs
1101
+ };
1102
+ return computedState;
1103
+ }
1077
1104
  /**
1078
1105
  * Get currently active tabs (from URL > persisted (localStorage) > defaults)
1079
1106
  */
@@ -1085,7 +1112,7 @@ class CustomViewsCore {
1085
1112
  if (persistedState?.tabs) {
1086
1113
  return { ...persistedState.tabs };
1087
1114
  }
1088
- return this.config?.defaultState?.tabs ? { ...this.config.defaultState.tabs } : {};
1115
+ return this.getComputedDefaultState().tabs || {};
1089
1116
  }
1090
1117
  /**
1091
1118
  * Set active tab for a group and apply state
@@ -1137,7 +1164,7 @@ class CustomViewsCore {
1137
1164
  });
1138
1165
  this.loadAndCallApplyState();
1139
1166
  }
1140
- // Priority: URL state > persisted state > default
1167
+ // Priority: URL state > persisted state > config default > computed default
1141
1168
  // Also filters using the visibility manager to persist selection
1142
1169
  // across back/forward button clicks
1143
1170
  async loadAndCallApplyState() {
@@ -1153,8 +1180,8 @@ class CustomViewsCore {
1153
1180
  this.applyState(persistedState);
1154
1181
  return;
1155
1182
  }
1156
- // 3. Local Config Fallback
1157
- this.renderState(this.config.defaultState);
1183
+ // 3. Computed Default Fallback
1184
+ this.renderState(this.getComputedDefaultState());
1158
1185
  }
1159
1186
  /**
1160
1187
  * Apply a custom state, saves to localStorage and updates the URL
@@ -1173,7 +1200,7 @@ class CustomViewsCore {
1173
1200
  /** Render all toggles for the current state */
1174
1201
  renderState(state) {
1175
1202
  this.lastAppliedState = this.cloneState(state);
1176
- const toggles = state.toggles || [];
1203
+ const toggles = state?.toggles || [];
1177
1204
  const finalToggles = this.visibilityManager.filterVisibleToggles(toggles);
1178
1205
  // Apply toggle visibility
1179
1206
  ToggleManager.applyToggles(this.rootEl, finalToggles);
@@ -1192,7 +1219,7 @@ class CustomViewsCore {
1192
1219
  resetToDefault() {
1193
1220
  this.persistenceManager.clearAll();
1194
1221
  if (this.config) {
1195
- this.renderState(this.config.defaultState);
1222
+ this.renderState(this.getComputedDefaultState());
1196
1223
  }
1197
1224
  else {
1198
1225
  console.warn("No configuration loaded, cannot reset to default state");
@@ -1210,7 +1237,7 @@ class CustomViewsCore {
1210
1237
  return this.lastAppliedState.toggles || [];
1211
1238
  }
1212
1239
  if (this.config) {
1213
- return this.config.defaultState.toggles || [];
1240
+ return this.getComputedDefaultState().toggles || [];
1214
1241
  }
1215
1242
  return [];
1216
1243
  }
@@ -1220,7 +1247,7 @@ class CustomViewsCore {
1220
1247
  clearPersistence() {
1221
1248
  this.persistenceManager.clearAll();
1222
1249
  if (this.config) {
1223
- this.renderState(this.config.defaultState);
1250
+ this.renderState(this.getComputedDefaultState());
1224
1251
  }
1225
1252
  else {
1226
1253
  console.warn("No configuration loaded, cannot reset to default state");
@@ -1299,7 +1326,7 @@ class CustomViewsCore {
1299
1326
  return this.cloneState(this.lastAppliedState);
1300
1327
  }
1301
1328
  if (this.config) {
1302
- return this.cloneState(this.config.defaultState);
1329
+ return this.cloneState(this.getComputedDefaultState());
1303
1330
  }
1304
1331
  return {};
1305
1332
  }