|
| 1 | +import * as React from 'react'; |
| 2 | +import * as ReactDOM from 'react-dom'; |
| 3 | +import { connect, IStatusBarItem } from 'mo'; |
| 4 | +import { Controller } from 'mo/react/controller'; |
| 5 | +import { notificationService, statusBarService } from 'mo/services'; |
| 6 | +import { singleton } from 'tsyringe'; |
| 7 | +import { Notification } from 'mo/workbench/statusBar/notification'; |
| 8 | +import { NotificationPanel } from 'mo/workbench/statusBar/notification/notificationPanel'; |
| 9 | + |
| 10 | +import { IActionBarItem } from 'mo/components/actionBar'; |
| 11 | +import { |
| 12 | + INotificationItem, |
| 13 | + NOTIFICATION_CLEAR_ALL, |
| 14 | + NOTIFICATION_HIDE, |
| 15 | +} from 'mo/model/notification'; |
| 16 | +import { select } from 'mo/common/dom'; |
| 17 | +import { ID_APP } from 'mo/common/id'; |
| 18 | + |
| 19 | +export interface INotificationController { |
| 20 | + onCloseNotification(item: INotificationItem): void; |
| 21 | + onClick?: (e: React.MouseEvent, item: IStatusBarItem) => void; |
| 22 | + onActionBarClick?( |
| 23 | + event: React.MouseEvent<Element, MouseEvent>, |
| 24 | + item: IActionBarItem<any> |
| 25 | + ): void; |
| 26 | +} |
| 27 | + |
| 28 | +@singleton() |
| 29 | +export class NotificationController |
| 30 | + extends Controller |
| 31 | + implements INotificationController { |
| 32 | + constructor() { |
| 33 | + super(); |
| 34 | + this.init(); |
| 35 | + } |
| 36 | + |
| 37 | + public onCloseNotification(item: INotificationItem<any>): void { |
| 38 | + if (typeof item.id === 'number') { |
| 39 | + notificationService.removeNotification(item.id); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + private _notificationPanel: HTMLDivElement | undefined = undefined; |
| 44 | + |
| 45 | + private showHideNotifications() { |
| 46 | + if (!this._notificationPanel) { |
| 47 | + this.renderNotificationPanel(); |
| 48 | + } |
| 49 | + notificationService.showHideNotifications(); |
| 50 | + } |
| 51 | + |
| 52 | + public onClick = (e: React.MouseEvent, item: IStatusBarItem) => { |
| 53 | + this.showHideNotifications(); |
| 54 | + }; |
| 55 | + |
| 56 | + public onActionBarClick = ( |
| 57 | + event: React.MouseEvent<Element, MouseEvent>, |
| 58 | + item: IActionBarItem<any> |
| 59 | + ) => { |
| 60 | + const action = item.id; |
| 61 | + if (action === NOTIFICATION_CLEAR_ALL.id) { |
| 62 | + notificationService.showHideNotifications(); |
| 63 | + } else if (action === NOTIFICATION_HIDE.id) { |
| 64 | + this.showHideNotifications(); |
| 65 | + } |
| 66 | + }; |
| 67 | + |
| 68 | + private init() { |
| 69 | + const notificationItem = notificationService.getState(); |
| 70 | + const NotificationView = connect(notificationService, Notification); |
| 71 | + notificationService.setState({ |
| 72 | + ...notificationItem, |
| 73 | + render: () => <NotificationView onClick={this.onClick} />, |
| 74 | + }); |
| 75 | + statusBarService.appendRightItem(notificationItem); |
| 76 | + } |
| 77 | + |
| 78 | + public renderNotificationPanel() { |
| 79 | + const NotificationPanelView = connect( |
| 80 | + notificationService, |
| 81 | + NotificationPanel |
| 82 | + ); |
| 83 | + const root = select('#' + ID_APP); |
| 84 | + const container = document.createElement('div'); |
| 85 | + root?.appendChild(container); |
| 86 | + ReactDOM.render( |
| 87 | + <NotificationPanelView |
| 88 | + onActionBarClick={this.onActionBarClick} |
| 89 | + onCloseNotification={this.onCloseNotification} |
| 90 | + />, |
| 91 | + container |
| 92 | + ); |
| 93 | + this._notificationPanel = container; |
| 94 | + } |
| 95 | +} |
0 commit comments