Files
orion/app/modules/tenancy/static/admin/js/platform-modules.js
Samir Boulahtit 4e28d91a78 refactor: migrate templates and static files to self-contained modules
Templates Migration:
- Migrate admin templates to modules (tenancy, billing, monitoring, marketplace, etc.)
- Migrate vendor templates to modules (tenancy, billing, orders, messaging, etc.)
- Migrate storefront templates to modules (catalog, customers, orders, cart, checkout, cms)
- Migrate public templates to modules (billing, marketplace, cms)
- Keep shared templates in app/templates/ (base.html, errors/, partials/, macros/)
- Migrate letzshop partials to marketplace module

Static Files Migration:
- Migrate admin JS to modules: tenancy (23 files), core (5 files), monitoring (1 file)
- Migrate vendor JS to modules: tenancy (4 files), core (2 files)
- Migrate shared JS: vendor-selector.js to core, media-picker.js to cms
- Migrate storefront JS: storefront-layout.js to core
- Keep framework JS in static/ (api-client, utils, money, icons, log-config, lib/)
- Update all template references to use module_static paths

Naming Consistency:
- Rename static/platform/ to static/public/
- Rename app/templates/platform/ to app/templates/public/
- Update all extends and static references

Documentation:
- Update module-system.md with shared templates documentation
- Update frontend-structure.md with new module JS organization

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-01 14:34:16 +01:00

238 lines
8.3 KiB
JavaScript

// static/admin/js/platform-modules.js
// Platform module configuration management
const moduleConfigLog = window.LogConfig?.loggers?.moduleConfig || window.LogConfig?.createLogger?.('moduleConfig') || console;
function adminPlatformModules(platformCode) {
return {
// Inherit base layout functionality from init-alpine.js
...data(),
// Page-specific state
currentPage: 'platforms',
platformCode: platformCode,
loading: true,
error: null,
successMessage: null,
saving: false,
// Data
platform: null,
moduleConfig: null,
// Computed properties
get coreModules() {
if (!this.moduleConfig?.modules) return [];
return this.moduleConfig.modules.filter(m => m.is_core);
},
get optionalModules() {
if (!this.moduleConfig?.modules) return [];
return this.moduleConfig.modules.filter(m => !m.is_core);
},
get coreModulesCount() {
return this.coreModules.length;
},
get enabledOptionalCount() {
return this.optionalModules.filter(m => m.is_enabled).length;
},
// Module icons mapping (must match icons.js definitions)
getModuleIcon(moduleCode) {
const icons = {
'core': 'home',
'platform-admin': 'office-building',
'billing': 'credit-card',
'inventory': 'archive',
'orders': 'shopping-cart',
'marketplace': 'shopping-bag',
'customers': 'users',
'cms': 'document-text',
'analytics': 'chart-bar',
'messaging': 'chat',
'dev-tools': 'code',
'monitoring': 'chart-pie'
};
return icons[moduleCode] || 'puzzle-piece';
},
// Modules with configuration options
hasConfig(moduleCode) {
return ['billing', 'inventory', 'orders', 'marketplace',
'customers', 'cms', 'analytics', 'messaging', 'monitoring'].includes(moduleCode);
},
async init() {
// Guard against duplicate initialization
if (window._platformModulesInitialized) {
moduleConfigLog.warn('Already initialized, skipping');
return;
}
window._platformModulesInitialized = true;
moduleConfigLog.info('=== PLATFORM MODULES PAGE INITIALIZING ===');
moduleConfigLog.info('Platform code:', this.platformCode);
try {
await this.loadPlatform();
await this.loadModuleConfig();
moduleConfigLog.info('=== PLATFORM MODULES PAGE INITIALIZED ===');
} catch (error) {
moduleConfigLog.error('Failed to initialize modules page:', error);
this.error = 'Failed to load page data. Please refresh.';
}
},
async refresh() {
this.error = null;
this.successMessage = null;
await this.loadModuleConfig();
},
async loadPlatform() {
try {
this.platform = await apiClient.get(`/admin/platforms/${this.platformCode}`);
moduleConfigLog.info('Loaded platform:', this.platform?.name);
} catch (error) {
moduleConfigLog.error('Failed to load platform:', error);
throw error;
}
},
async loadModuleConfig() {
this.loading = true;
this.error = null;
try {
const platformId = this.platform?.id;
if (!platformId) {
throw new Error('Platform not loaded');
}
this.moduleConfig = await apiClient.get(`/admin/modules/platforms/${platformId}`);
moduleConfigLog.info('Loaded module config:', {
total: this.moduleConfig?.total,
enabled: this.moduleConfig?.enabled,
disabled: this.moduleConfig?.disabled
});
} catch (error) {
moduleConfigLog.error('Failed to load module config:', error);
this.error = error.message || 'Failed to load module configuration';
} finally {
this.loading = false;
}
},
async toggleModule(module) {
if (module.is_core) {
moduleConfigLog.warn('Cannot toggle core module:', module.code);
return;
}
this.saving = true;
this.error = null;
this.successMessage = null;
const action = module.is_enabled ? 'disable' : 'enable';
try {
const platformId = this.platform?.id;
const endpoint = `/admin/modules/platforms/${platformId}/${action}`;
const result = await apiClient.post(endpoint, {
module_code: module.code
});
moduleConfigLog.info(`${action}d module:`, module.code, result);
// Show success message
if (result.also_enabled?.length > 0) {
this.successMessage = `Module '${module.name}' enabled. Also enabled dependencies: ${result.also_enabled.join(', ')}`;
} else if (result.also_disabled?.length > 0) {
this.successMessage = `Module '${module.name}' disabled. Also disabled dependents: ${result.also_disabled.join(', ')}`;
} else {
this.successMessage = `Module '${module.name}' ${action}d successfully`;
}
// Reload module config to get updated state
await this.loadModuleConfig();
// Clear success message after delay
setTimeout(() => {
this.successMessage = null;
}, 5000);
} catch (error) {
moduleConfigLog.error(`Failed to ${action} module:`, error);
this.error = error.message || `Failed to ${action} module`;
} finally {
this.saving = false;
}
},
async enableAll() {
if (!confirm('This will enable all modules. Continue?')) {
return;
}
this.saving = true;
this.error = null;
this.successMessage = null;
try {
const platformId = this.platform?.id;
const result = await apiClient.post(`/admin/modules/platforms/${platformId}/enable-all`);
moduleConfigLog.info('Enabled all modules:', result);
this.successMessage = `All ${result.enabled_count} modules enabled`;
// Reload module config
await this.loadModuleConfig();
setTimeout(() => {
this.successMessage = null;
}, 5000);
} catch (error) {
moduleConfigLog.error('Failed to enable all modules:', error);
this.error = error.message || 'Failed to enable all modules';
} finally {
this.saving = false;
}
},
async disableOptional() {
if (!confirm('This will disable all optional modules, keeping only core modules. Continue?')) {
return;
}
this.saving = true;
this.error = null;
this.successMessage = null;
try {
const platformId = this.platform?.id;
const result = await apiClient.post(`/admin/modules/platforms/${platformId}/disable-optional`);
moduleConfigLog.info('Disabled optional modules:', result);
this.successMessage = `Optional modules disabled. Core modules kept: ${result.core_modules.join(', ')}`;
// Reload module config
await this.loadModuleConfig();
setTimeout(() => {
this.successMessage = null;
}, 5000);
} catch (error) {
moduleConfigLog.error('Failed to disable optional modules:', error);
this.error = error.message || 'Failed to disable optional modules';
} finally {
this.saving = false;
}
}
};
}