@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
  */
@@ -354,13 +354,10 @@
354
354
  if (tabs[groupId]) {
355
355
  return tabs[groupId];
356
356
  }
357
- // 2. Check config default
357
+ // 2. Check config for first tab
358
358
  if (cfgGroups) {
359
359
  const groupCfg = cfgGroups.find(g => g.id === groupId);
360
360
  if (groupCfg) {
361
- if (groupCfg.default) {
362
- return groupCfg.default;
363
- }
364
361
  // Fallback to first tab in config
365
362
  const firstConfigTab = groupCfg.tabs[0];
366
363
  if (firstConfigTab) {
@@ -1069,7 +1066,7 @@ ${TAB_STYLES}
1069
1066
  this.persistenceManager = new PersistenceManager();
1070
1067
  this.visibilityManager = new VisibilityManager();
1071
1068
  this.showUrlEnabled = opt.showUrl ?? false;
1072
- this.lastAppliedState = this.cloneState(this.config?.defaultState);
1069
+ this.lastAppliedState = this.cloneState(this.getComputedDefaultState());
1073
1070
  }
1074
1071
  getConfig() {
1075
1072
  return this.config;
@@ -1080,6 +1077,36 @@ ${TAB_STYLES}
1080
1077
  getTabGroups() {
1081
1078
  return this.config.tabGroups;
1082
1079
  }
1080
+ /**
1081
+ * Generate a computed default state:
1082
+ * - If config.defaultState is defined, use it (even if empty)
1083
+ * - Otherwise, compute a default: enable all toggles and set all tab groups to their first tab
1084
+ */
1085
+ getComputedDefaultState() {
1086
+ const configDefaultState = this.config?.defaultState;
1087
+ // If defaultState is explicitly defined in config, use it as-is
1088
+ if (configDefaultState !== undefined) {
1089
+ return configDefaultState;
1090
+ }
1091
+ // Otherwise, compute a default state: all toggles on, all tabs to first
1092
+ const tabs = {};
1093
+ // Set all tab groups to their first tab
1094
+ if (this.config.tabGroups?.length) {
1095
+ this.config.tabGroups.forEach(group => {
1096
+ if (group.tabs && group.tabs.length > 0) {
1097
+ const firstTab = group.tabs[0];
1098
+ if (firstTab?.id) {
1099
+ tabs[group.id] = firstTab.id;
1100
+ }
1101
+ }
1102
+ });
1103
+ }
1104
+ const computedState = {
1105
+ toggles: [...(this.config.allToggles || [])],
1106
+ tabs
1107
+ };
1108
+ return computedState;
1109
+ }
1083
1110
  /**
1084
1111
  * Get currently active tabs (from URL > persisted (localStorage) > defaults)
1085
1112
  */
@@ -1091,7 +1118,7 @@ ${TAB_STYLES}
1091
1118
  if (persistedState?.tabs) {
1092
1119
  return { ...persistedState.tabs };
1093
1120
  }
1094
- return this.config?.defaultState?.tabs ? { ...this.config.defaultState.tabs } : {};
1121
+ return this.getComputedDefaultState().tabs || {};
1095
1122
  }
1096
1123
  /**
1097
1124
  * Set active tab for a group and apply state
@@ -1143,7 +1170,7 @@ ${TAB_STYLES}
1143
1170
  });
1144
1171
  this.loadAndCallApplyState();
1145
1172
  }
1146
- // Priority: URL state > persisted state > default
1173
+ // Priority: URL state > persisted state > config default > computed default
1147
1174
  // Also filters using the visibility manager to persist selection
1148
1175
  // across back/forward button clicks
1149
1176
  async loadAndCallApplyState() {
@@ -1159,8 +1186,8 @@ ${TAB_STYLES}
1159
1186
  this.applyState(persistedState);
1160
1187
  return;
1161
1188
  }
1162
- // 3. Local Config Fallback
1163
- this.renderState(this.config.defaultState);
1189
+ // 3. Computed Default Fallback
1190
+ this.renderState(this.getComputedDefaultState());
1164
1191
  }
1165
1192
  /**
1166
1193
  * Apply a custom state, saves to localStorage and updates the URL
@@ -1179,7 +1206,7 @@ ${TAB_STYLES}
1179
1206
  /** Render all toggles for the current state */
1180
1207
  renderState(state) {
1181
1208
  this.lastAppliedState = this.cloneState(state);
1182
- const toggles = state.toggles || [];
1209
+ const toggles = state?.toggles || [];
1183
1210
  const finalToggles = this.visibilityManager.filterVisibleToggles(toggles);
1184
1211
  // Apply toggle visibility
1185
1212
  ToggleManager.applyToggles(this.rootEl, finalToggles);
@@ -1198,7 +1225,7 @@ ${TAB_STYLES}
1198
1225
  resetToDefault() {
1199
1226
  this.persistenceManager.clearAll();
1200
1227
  if (this.config) {
1201
- this.renderState(this.config.defaultState);
1228
+ this.renderState(this.getComputedDefaultState());
1202
1229
  }
1203
1230
  else {
1204
1231
  console.warn("No configuration loaded, cannot reset to default state");
@@ -1216,7 +1243,7 @@ ${TAB_STYLES}
1216
1243
  return this.lastAppliedState.toggles || [];
1217
1244
  }
1218
1245
  if (this.config) {
1219
- return this.config.defaultState.toggles || [];
1246
+ return this.getComputedDefaultState().toggles || [];
1220
1247
  }
1221
1248
  return [];
1222
1249
  }
@@ -1226,7 +1253,7 @@ ${TAB_STYLES}
1226
1253
  clearPersistence() {
1227
1254
  this.persistenceManager.clearAll();
1228
1255
  if (this.config) {
1229
- this.renderState(this.config.defaultState);
1256
+ this.renderState(this.getComputedDefaultState());
1230
1257
  }
1231
1258
  else {
1232
1259
  console.warn("No configuration loaded, cannot reset to default state");
@@ -1305,7 +1332,7 @@ ${TAB_STYLES}
1305
1332
  return this.cloneState(this.lastAppliedState);
1306
1333
  }
1307
1334
  if (this.config) {
1308
- return this.cloneState(this.config.defaultState);
1335
+ return this.cloneState(this.getComputedDefaultState());
1309
1336
  }
1310
1337
  return {};
1311
1338
  }