-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(components): implement component/plugin system and move controls…
… there
- Loading branch information
1 parent
637b320
commit 03cd276
Showing
7 changed files
with
71 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { Virchual } from './../virchual'; | ||
import { Event } from './../utils/event'; | ||
|
||
export type ComponentDependencies = { | ||
virchual: Virchual; | ||
eventBus: Event; | ||
}; | ||
|
||
export interface ComponentConstructor { | ||
new (dependencies: ComponentDependencies): Component; | ||
} | ||
|
||
export interface Component { | ||
mount(): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { Component, ComponentDependencies } from './../component'; | ||
import { identity } from '../../types'; | ||
import { stop } from '../../utils/event'; | ||
|
||
export class Controls implements Component { | ||
private buttons: HTMLButtonElement[]; | ||
private onClickBound: () => identity; | ||
|
||
constructor(private dependencies: ComponentDependencies) { | ||
this.buttons = [].slice.call(dependencies.virchual.container.querySelectorAll('.virchual__control')); | ||
|
||
dependencies.eventBus.on('destroy', () => { | ||
console.log('controls component destroy'); | ||
}); | ||
|
||
this.onClickBound = this.onClick.bind(this); | ||
} | ||
|
||
mount(): void { | ||
this.buttons.forEach(button => this.dependencies.eventBus.on('click', this.onClickBound, button)); | ||
} | ||
|
||
private onClick(event: MouseEvent) { | ||
stop(event); | ||
|
||
const button: HTMLButtonElement = (event.target as Element).closest('button') as HTMLButtonElement; | ||
const control = button.dataset.controls as 'prev' | 'next'; | ||
|
||
if (control === 'prev') { | ||
this.dependencies.virchual.prev(); | ||
|
||
return; | ||
} | ||
|
||
this.dependencies.virchual.next(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from './component'; | ||
|
||
export { Controls } from './controls/controls'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
import { Controls } from './components/controls/controls'; | ||
import { Virchual } from './virchual'; | ||
|
||
export { Virchual }; | ||
export { Virchual, Controls }; | ||
|
||
// For CommonJS default export support | ||
module.exports = Virchual; | ||
module.exports = { Virchual, Controls }; | ||
module.exports.default = Virchual; | ||
module.exports.__esModule = true; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters