From a77aa2fc911d93837a98d92abc0890d7e9686258 Mon Sep 17 00:00:00 2001 From: Maxim Kadushkin Date: Tue, 1 Sep 2020 11:42:25 +0300 Subject: [PATCH] [common] added common notifications controller --- .../mobile/lib/controller/Collaboration.jsx | 24 ++++++++++-------- apps/common/mobile/utils/notifications.js | 25 +++++++++++++++++++ .../mobile/src/components/app.jsx | 11 +++++--- 3 files changed, 46 insertions(+), 14 deletions(-) create mode 100644 apps/common/mobile/utils/notifications.js diff --git a/apps/common/mobile/lib/controller/Collaboration.jsx b/apps/common/mobile/lib/controller/Collaboration.jsx index 59f310d380..14ac7c170e 100644 --- a/apps/common/mobile/lib/controller/Collaboration.jsx +++ b/apps/common/mobile/lib/controller/Collaboration.jsx @@ -1,5 +1,6 @@ import React, { useState } from 'react' import { resetUsers } from '../store/actions/actions.js' +import Notifications from '../../utils/notifications.js' const Collaboration = () => { const onChangeEditUsers = (users) => { @@ -7,18 +8,21 @@ const Collaboration = () => { store.dispatch(resetUsers(Object.values(users))); }; + Common.Notifications.on('engineCreated', api => { + // this.api = api; + api.asc_registerCallback('asc_onAuthParticipantsChanged', onChangeEditUsers); + api.asc_registerCallback('asc_onParticipantsChanged', onChangeEditUsers); + // this.api.asc_registerCallback('asc_onAddComment', _.bind(this.onApiAddComment, this)); + // this.api.asc_registerCallback('asc_onAddComments', _.bind(this.onApiAddComments, this)); + // this.api.asc_registerCallback('asc_onChangeCommentData', _.bind(this.onApiChangeCommentData, this)); + // this.api.asc_registerCallback('asc_onRemoveComment', _.bind(this.onApiRemoveComment, this)); + // this.api.asc_registerCallback('asc_onRemoveComments', _.bind(this.onApiRemoveComments, this)); + // this.api.asc_registerCallback('asc_onShowComment', _.bind(this.apiShowComments, this)); + // this.api.asc_registerCallback('asc_onHideComment', _.bind(this.apiHideComments, this)); + }); + return { setApi(api) { - this.api = api; - this.api.asc_registerCallback('asc_onAuthParticipantsChanged', onChangeEditUsers); - this.api.asc_registerCallback('asc_onParticipantsChanged', onChangeEditUsers); - // this.api.asc_registerCallback('asc_onAddComment', _.bind(this.onApiAddComment, this)); - // this.api.asc_registerCallback('asc_onAddComments', _.bind(this.onApiAddComments, this)); - // this.api.asc_registerCallback('asc_onChangeCommentData', _.bind(this.onApiChangeCommentData, this)); - // this.api.asc_registerCallback('asc_onRemoveComment', _.bind(this.onApiRemoveComment, this)); - // this.api.asc_registerCallback('asc_onRemoveComments', _.bind(this.onApiRemoveComments, this)); - // this.api.asc_registerCallback('asc_onShowComment', _.bind(this.apiShowComments, this)); - // this.api.asc_registerCallback('asc_onHideComment', _.bind(this.apiHideComments, this)); } } }; diff --git a/apps/common/mobile/utils/notifications.js b/apps/common/mobile/utils/notifications.js new file mode 100644 index 0000000000..f43e7080c7 --- /dev/null +++ b/apps/common/mobile/utils/notifications.js @@ -0,0 +1,25 @@ + +export default class Notifications { + constructor() { + this._events = {}; + } + + on(event, callback) { + !this._events[event] && (this._events[event] = []); + this._events[event].push(callback); + } + + off(event, callback) { + if ( this._events[event] && this._events[event].includes(callback) ) { + this._events[event].splice(this._events[event].indexOf(callback), 1); + } + } + + trigger(event/*, args*/) { + if ( this._events[event] ) { + this._events[event].forEach(callback => { + callback.apply(this, Array.prototype.slice.call(arguments, 1)); + }); + } + } +} diff --git a/apps/documenteditor/mobile/src/components/app.jsx b/apps/documenteditor/mobile/src/components/app.jsx index cea2bab651..29c0d0f783 100644 --- a/apps/documenteditor/mobile/src/components/app.jsx +++ b/apps/documenteditor/mobile/src/components/app.jsx @@ -28,6 +28,7 @@ import routes from '../js/routes'; import '../../../../common/Gateway.js'; import '../../../../common/main/lib/util/utils.js'; import { CollaborationController } from '../../../../common/mobile/lib/controller/Collaboration.jsx'; +import Notifications from '../../../../common/mobile/utils/notifications.js' export default class extends React.Component { constructor() { @@ -48,6 +49,11 @@ export default class extends React.Component { username: '', password: '', } + + Common.Notifications = new Notifications(); + + Common.Controllers = {}; + Common.Controllers.Collaboration = new CollaborationController(); } render() { return ( @@ -244,10 +250,7 @@ export default class extends React.Component { Common.Gateway.on('opendocument', loadDocument); Common.Gateway.appReady(); - Common.Controllers = {}; - Common.Controllers.Collaboration = new CollaborationController(); - Common.Controllers.Collaboration.setApi(this.api, ""); - + Common.Notifications.trigger('engineCreated', this.api); }, error => { console.log('promise failed ' + error); });