web-mojo
Version:
WEB-MOJO - A lightweight JavaScript framework for building data-driven web applications
510 lines (415 loc) ⢠11.5 kB
Markdown
-2.0](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
[](https://github.com/yourusername/web-mojo)
**MOJO** is a modern, lightweight JavaScript framework for building data-driven web applications. Built with a **core + extensions** architecture, MOJO provides maximum flexibility while maintaining clean boundaries and optimal performance.
šļø **Core + Extensions Architecture** - Clean separation with plugin system
š¦ **Subpath Exports** - Import exactly what you need
ā” **Lazy Loading** - Reduced bundle sizes with dynamic imports
š **Plugin System** - Extensions enhance core without dependencies
šÆ **Tree Shaking** - Optimized builds with modern bundlers
```bash
npm install web-mojo
```
```javascript
// Core framework
import { WebApp, Page, View } from 'web-mojo';
// Create a simple page
class HomePage extends Page {
getTemplate() {
return '<h1>Welcome to MOJO!</h1>';
}
}
// Initialize app
const app = new WebApp({
name: 'My App',
container: '#app'
});
app.registerPage('home', HomePage);
app.start();
```
MOJO 2.1.0 uses a **core + extensions** architecture:
The stable runtime and building blocks:
- **WebApp** & **PortalApp** - Application containers
- **View** & **Page** - Component system
- **Model** & **Collection** - Data layer
- **Router** - URL routing
- **Dialog** - Modal system
- **Essential services** - File upload, events, utilities
Feature-rich packages that extend core functionality:
Complete authentication system with JWT tokens:
```javascript
import { AuthApp, LoginPage } from 'web-mojo/auth';
const app = new AuthApp({
api: { baseURL: 'https://api.example.com' },
loginRedirect: '/dashboard'
});
```
Image and PDF viewers with editing capabilities:
```javascript
import 'web-mojo/lightbox'; // Auto-registers plugins
// Core can now use lightbox features
import { Dialog } from 'web-mojo';
// Dialog automatically gets image cropping when lightbox is loaded
```
Interactive charts built on Chart.js:
```javascript
import { PieChart, SeriesChart } from 'web-mojo/charts';
const chart = new PieChart({
data: salesData,
container: '#chart'
});
```
Documentation portal system:
```javascript
import { DocItApp } from 'web-mojo/docit';
const docs = new DocItApp({
books: ['user-guide', 'api-docs']
});
```
Beautiful loading animations:
```html
<script src="web-mojo/loader"></script>
<script>
// Your app initialization
// Call hideInitialLoader() when ready
</script>
```
```javascript
import { PortalApp, Page } from 'web-mojo';
import 'web-mojo/lightbox'; // Enable image features
const app = new PortalApp({
name: 'Admin Portal',
sidebar: {
menus: [{
items: [
{ text: 'Dashboard', route: 'dashboard', icon: 'bi-speedometer2' },
{ text: 'Users', route: 'users', icon: 'bi-people' }
]
}]
}
});
class DashboardPage extends Page {
constructor(options = {}) {
super({
title: 'Dashboard',
template: `
<div class="row">
<div class="col-md-6">
<div class="card">
<div class="card-body">
<h5>Welcome to MOJO</h5>
<p>{{message}}</p>
<button class="btn btn-primary" data-action="show-dialog">
Open Dialog
</button>
</div>
</div>
</div>
</div>
`,
...options
});
}
getTemplateData() {
return {
message: 'Your application is ready!'
};
}
async onActionShowDialog() {
const { Dialog } = await import('web-mojo');
Dialog.showInfo('Hello from MOJO!');
}
}
app.registerPage('dashboard', DashboardPage);
app.start();
```
```javascript
import { AuthApp } from 'web-mojo/auth';
import { WebApp } from 'web-mojo';
// Auth portal
const authApp = new AuthApp({
api: { baseURL: 'https://api.example.com' },
ui: {
title: 'Acme Corp',
logoUrl: '/assets/logo.png'
}
});
// Main app (after authentication)
const mainApp = new WebApp({
name: 'Acme Portal',
api: {
baseURL: 'https://api.example.com',
token: localStorage.getItem('auth_token')
}
});
// Handle successful login
authApp.events.on('auth:login', (user) => {
window.location.href = '/dashboard';
});
```
```javascript
import { Model, Collection, View } from 'web-mojo';
// Define models
class User extends Model {
static endpoint = '/api/users';
}
class UserList extends Collection {
model = User;
endpoint = '/api/users';
}
// Create views
class UserTableView extends View {
constructor(options = {}) {
super({
template: `
<table class="table">
<thead>
<tr><th>Name</th><th>Email</th><th>Actions</th></tr>
</thead>
<tbody>
{{
<tr>
<td>{{name}}</td>
<td>{{email}}</td>
<td>
<button class="btn btn-sm btn-primary"
data-action="edit-user"
data-user-id="{{id}}">Edit</button>
</td>
</tr>
{{/users}}
</tbody>
</table>
`,
...options
});
this.collection = new UserList();
}
async onMount() {
await this.collection.fetch();
this.render();
}
getTemplateData() {
return {
users: this.collection.toJSON()
};
}
async onActionEditUser(action, event, element) {
const userId = element.dataset.userId;
const user = this.collection.get(userId);
const { Dialog } = await import('web-mojo');
Dialog.showModelForm(user, {
title: 'Edit User',
fields: ['name', 'email']
});
}
}
```
```
web-mojo/
āāā src/
ā āāā index.js
ā āāā auth.js
ā āāā lightbox.js
ā āāā charts.js
ā āāā docit.js
ā āāā loader.js
ā āāā core/
ā ā āāā View.js
ā ā āāā Page.js
ā ā āāā WebApp.js
ā ā āāā PortalApp.js
ā ā āāā models/
ā ā āāā views/
ā ā āāā services/
ā ā āāā utils/
ā āāā extensions/
ā āāā auth/
ā āāā lightbox/
ā āāā charts/
ā āāā docit/
āāā examples/
āāā dist/
```
```bash
npm install
npm run build:lib
npm run dev
npm run dev:watch
```
When developing the framework itself:
```javascript
// Use aliases for clean imports
import View from '@core/View.js';
import AuthApp from '@ext/auth/AuthApp.js';
import { PieChart } from '@ext/charts/PieChart.js';
```
Main application container with routing and page management.
```javascript
const app = new WebApp({
name: 'My App', // App name
container: '#app', // DOM container
debug: true, // Debug mode
api: { // API configuration
baseURL: 'https://api.example.com',
token: 'jwt-token'
}
});
// Register pages
app.registerPage('home', HomePage);
app.registerPage('users', UserListPage);
// Navigation
await app.navigate('users');
await app.navigate('user/123');
// Start app
await app.start();
```
Extended WebApp with built-in sidebar and top navigation.
```javascript
const app = new PortalApp({
// All WebApp options plus:
sidebar: {
menus: [{
name: 'main',
items: [
{ text: 'Home', route: 'home', icon: 'bi-house' },
{
text: 'Admin',
icon: 'bi-gear',
children: [
{ text: 'Users', route: 'users' },
{ text: 'Settings', route: 'settings' }
]
}
]
}]
},
topbar: {
brand: 'My Portal',
rightItems: [
{ icon: 'bi-bell', action: 'notifications' }
]
}
});
```
```javascript
class MyView extends View {
constructor(options = {}) {
super({
className: 'my-view',
template: `
<div>
<h3>{{title}}</h3>
<button data-action="click-me">Click Me</button>
<div data-region="content"></div>
</div>
`,
...options
});
}
// Lifecycle hooks
async onMount() { /* Called when mounted to DOM */ }
async onUnmount() { /* Called when removed */ }
// Template data
getTemplateData() {
return { title: 'My View' };
}
// Event handlers
async onActionClickMe(action, event, element) {
this.showRegion('content', new AnotherView());
}
// Custom events
onCustomEvent(data) { /* Handle custom events */ }
}
```
For modern build tools:
```javascript
// vite.config.js
export default {
optimizeDeps: {
exclude: ['web-mojo']
},
ssr: {
noExternal: ['web-mojo']
}
}
```
```javascript
// Bundle all CSS automatically
import 'web-mojo'; // Includes core CSS
// Or import specific stylesheets
import 'web-mojo/css/core';
import 'web-mojo/css/portal';
import 'web-mojo/css/auth';
```
```javascript
// Old (2.0.x)
import WebApp from '/src/core/WebApp.js';
import AuthApp from '/src/auth/AuthApp.js';
// New (2.1.0)
import { WebApp } from 'web-mojo';
import { AuthApp } from 'web-mojo/auth';
```
```html
<!-- Old -->
<link href="/src/css/core.css" rel="stylesheet" />
<!-- New -->
<link href="/dist/core.css" rel="stylesheet" />
```
1. Fork the repository
2. Create your feature branch (`git checkout -b feature/amazing-feature`)
3. Make your changes following our coding standards
4. Add tests for new functionality
5. Commit your changes (`git commit -m 'Add amazing feature'`)
6. Push to the branch (`git push origin feature/amazing-feature`)
7. Open a Pull Request
### Development Setup
```bash
git clone https://github.com/yourusername/web-mojo.git
cd web-mojo
npm install
npm run dev
```
## š License
This project is licensed under the Apache License 2.0 - see the [LICENSE](LICENSE) file for details.
## š Support
- š **Documentation**: [Full docs and examples](./docs/)
- š **Issues**: [GitHub Issues](https://github.com/yourusername/web-mojo/issues)
- š¬ **Discussions**: [GitHub Discussions](https://github.com/yourusername/web-mojo/discussions)
---
Built with ā¤ļø by the MOJO Framework Team
[![License: Apache