Pārlūkot izejas kodu

Merge pull request #1460 from jitsi/iframe_api_commands

IFrame API improvements
j8
Saúl Ibarra Corretgé 8 gadus atpakaļ
vecāks
revīzija
19f46ed4f0
3 mainītis faili ar 348 papildinājumiem un 329 dzēšanām
  1. 6
    2
      doc/api.md
  2. 3
    0
      modules/API/external/.eslintrc.js
  3. 339
    327
      modules/API/external/external_api.js

+ 6
- 2
doc/api.md Parādīt failu

123
 ```javascript
123
 ```javascript
124
 api.executeCommands(commands)
124
 api.executeCommands(commands)
125
 ```
125
 ```
126
-The `commands` parameter is an object with the names of the commands as keys and the arguments for the commands asvalues:
126
+The `commands` parameter is an object with the names of the commands as keys and the arguments for the commands as values:
127
 ```javascript
127
 ```javascript
128
 api.executeCommands({displayName: ['nickname'], toggleAudio: []});
128
 api.executeCommands({displayName: ['nickname'], toggleAudio: []});
129
 ```
129
 ```
130
 
130
 
131
 You can add event listeners to the embedded Jitsi Meet using the `addEventListener` method.
131
 You can add event listeners to the embedded Jitsi Meet using the `addEventListener` method.
132
+**NOTE: This method still exists but it is deprecated. JitsiMeetExternalAPI class extends [EventEmitter]. Use [EventEmitter] methods (`addListener` or `on`).**
132
 ```javascript
133
 ```javascript
133
 api.addEventListener(event, listener)
134
 api.addEventListener(event, listener)
134
 ```
135
 ```
198
 You can also add multiple event listeners by using `addEventListeners`.
199
 You can also add multiple event listeners by using `addEventListeners`.
199
 This method requires one argument of type Object. The object argument must
200
 This method requires one argument of type Object. The object argument must
200
 have the names of the events as keys and the listeners of the events as values.
201
 have the names of the events as keys and the listeners of the events as values.
202
+**NOTE: This method still exists but it is deprecated. JitsiMeetExternalAPI class extends [EventEmitter]. Use [EventEmitter] methods.**
201
 
203
 
202
 ```javascript
204
 ```javascript
203
 function incomingMessageListener(object)
205
 function incomingMessageListener(object)
216
 ```
218
 ```
217
 
219
 
218
 If you want to remove a listener you can use `removeEventListener` method with argument the name of the event.
220
 If you want to remove a listener you can use `removeEventListener` method with argument the name of the event.
219
-
221
+**NOTE: This method still exists but it is deprecated. JitsiMeetExternalAPI class extends [EventEmitter]. Use [EventEmitter] methods( `removeListener`).**
220
 ```javascript
222
 ```javascript
221
 api.removeEventListener("incomingMessage");
223
 api.removeEventListener("incomingMessage");
222
 ```
224
 ```
223
 
225
 
224
 If you want to remove more than one event you can use `removeEventListeners` method with an Array with the names of the events as an argument.
226
 If you want to remove more than one event you can use `removeEventListeners` method with an Array with the names of the events as an argument.
227
+**NOTE: This method still exists but it is deprecated. JitsiMeetExternalAPI class extends [EventEmitter]. Use [EventEmitter] methods.**
225
 ```javascript
228
 ```javascript
226
 api.removeEventListeners(["incomingMessage", "outgoingMessageListener"]);
229
 api.removeEventListeners(["incomingMessage", "outgoingMessageListener"]);
227
 ```
230
 ```
240
 
243
 
241
 [config.js]: https://github.com/jitsi/jitsi-meet/blob/master/config.js
244
 [config.js]: https://github.com/jitsi/jitsi-meet/blob/master/config.js
242
 [interface_config.js]: https://github.com/jitsi/jitsi-meet/blob/master/interface_config.js
245
 [interface_config.js]: https://github.com/jitsi/jitsi-meet/blob/master/interface_config.js
246
+[EventEmitter]: https://nodejs.org/api/events.html

+ 3
- 0
modules/API/external/.eslintrc.js Parādīt failu

1
+module.exports = {
2
+    'extends': '../../../react/.eslintrc.js'
3
+};

+ 339
- 327
modules/API/external/external_api.js Parādīt failu

1
-const logger = require("jitsi-meet-logger").getLogger(__filename);
1
+import EventEmitter from 'events';
2
+import postisInit from 'postis';
2
 
3
 
3
-/**
4
- * Implements API class that embeds Jitsi Meet in external applications.
5
- */
6
-
7
-var postisInit = require("postis");
4
+const logger = require('jitsi-meet-logger').getLogger(__filename);
8
 
5
 
9
 /**
6
 /**
10
- * The minimum width for the Jitsi Meet frame
11
- * @type {number}
7
+ * Maps the names of the commands expected by the API with the name of the
8
+ * commands expected by jitsi-meet
12
  */
9
  */
13
-var MIN_WIDTH = 790;
10
+const commands = {
11
+    avatarUrl: 'avatar-url',
12
+    displayName: 'display-name',
13
+    email: 'email',
14
+    hangup: 'video-hangup',
15
+    toggleAudio: 'toggle-audio',
16
+    toggleChat: 'toggle-chat',
17
+    toggleContactList: 'toggle-contact-list',
18
+    toggleFilmStrip: 'toggle-film-strip',
19
+    toggleShareScreen: 'toggle-share-screen',
20
+    toggleVideo: 'toggle-video'
21
+};
14
 
22
 
15
 /**
23
 /**
16
- * The minimum height for the Jitsi Meet frame
17
- * @type {number}
24
+ * Maps the names of the events expected by the API with the name of the
25
+ * events expected by jitsi-meet
18
  */
26
  */
19
-var MIN_HEIGHT = 300;
27
+const events = {
28
+    displayNameChange: 'display-name-change',
29
+    incomingMessage: 'incoming-message',
30
+    outgoingMessage: 'outgoing-message',
31
+    participantJoined: 'participant-joined',
32
+    participantLeft: 'participant-left',
33
+    readyToClose: 'video-ready-to-close',
34
+    videoConferenceJoined: 'video-conference-joined',
35
+    videoConferenceLeft: 'video-conference-left'
36
+};
20
 
37
 
21
 /**
38
 /**
22
  * Last id of api object
39
  * Last id of api object
23
  * @type {number}
40
  * @type {number}
24
  */
41
  */
25
-var id = 0;
26
-
27
-/**
28
- * Maps the names of the commands expected by the API with the name of the
29
- * commands expected by jitsi-meet
30
- */
31
-var commands = {
32
-    "displayName": "display-name",
33
-    "toggleAudio": "toggle-audio",
34
-    "toggleVideo": "toggle-video",
35
-    "toggleFilmStrip": "toggle-film-strip",
36
-    "toggleChat": "toggle-chat",
37
-    "toggleContactList": "toggle-contact-list",
38
-    "toggleShareScreen": "toggle-share-screen",
39
-    "hangup": "video-hangup",
40
-    "email": "email",
41
-    "avatarUrl": "avatar-url"
42
-};
42
+let id = 0;
43
 
43
 
44
 /**
44
 /**
45
- * Maps the names of the events expected by the API with the name of the
46
- * events expected by jitsi-meet
45
+ * The minimum height for the Jitsi Meet frame
46
+ * @type {number}
47
  */
47
  */
48
-var events = {
49
-    "incomingMessage": "incoming-message",
50
-    "outgoingMessage": "outgoing-message",
51
-    "displayNameChange": "display-name-change",
52
-    "participantJoined": "participant-joined",
53
-    "participantLeft": "participant-left",
54
-    "videoConferenceJoined": "video-conference-joined",
55
-    "videoConferenceLeft": "video-conference-left",
56
-    "readyToClose": "video-ready-to-close"
57
-};
48
+const MIN_HEIGHT = 300;
58
 
49
 
59
 /**
50
 /**
60
- * Sends the passed object to Jitsi Meet
61
- * @param postis {Postis object} the postis instance that is going to be used
62
- * to send the message
63
- * @param object the object to be sent
64
- * - method {sting}
65
- * - params {object}
51
+ * The minimum width for the Jitsi Meet frame
52
+ * @type {number}
66
  */
53
  */
67
-function sendMessage(postis, object) {
68
-    postis.send(object);
69
-}
54
+const MIN_WIDTH = 790;
70
 
55
 
71
 /**
56
 /**
72
  * Adds given number to the numberOfParticipants property of given APIInstance.
57
  * Adds given number to the numberOfParticipants property of given APIInstance.
73
- * @param {JitsiMeetExternalAPI} APIInstance the instance of the
74
- * JitsiMeetExternalAPI
75
- * @param {int} number - the number of participants to be added to
58
+ *
59
+ * @param {JitsiMeetExternalAPI} APIInstance - The instance of the API.
60
+ * @param {int} number - The number of participants to be added to
76
  * numberOfParticipants property (this parameter can be negative number if the
61
  * numberOfParticipants property (this parameter can be negative number if the
77
  * numberOfParticipants should be decreased).
62
  * numberOfParticipants should be decreased).
63
+ * @returns {void}
78
  */
64
  */
79
 function changeParticipantNumber(APIInstance, number) {
65
 function changeParticipantNumber(APIInstance, number) {
80
     APIInstance.numberOfParticipants += number;
66
     APIInstance.numberOfParticipants += number;
84
  * Generates array with URL params based on the passed config object that will
70
  * Generates array with URL params based on the passed config object that will
85
  * be used for the Jitsi Meet URL generation.
71
  * be used for the Jitsi Meet URL generation.
86
  *
72
  *
87
- * @param config {object} the config object.
88
- * @returns {Array<string>} the array with URL param strings.
73
+ * @param {Object} config - The config object.
74
+ * @returns {Array<string>} The array with URL param strings.
89
  */
75
  */
90
-function configToURLParamsArray(config) {
76
+function configToURLParamsArray(config = {}) {
91
     const params = [];
77
     const params = [];
92
 
78
 
93
-    for (const key in config) {
79
+    for (const key in config) { // eslint-disable-line guard-for-in
94
         try {
80
         try {
95
-            params.push(key + '='
96
-                + encodeURIComponent(JSON.stringify(config[key])));
81
+            params.push(`${key}=${
82
+                encodeURIComponent(JSON.stringify(config[key]))}`);
97
         } catch (e) {
83
         } catch (e) {
98
             console.warn(`Error encoding ${key}: ${e}`);
84
             console.warn(`Error encoding ${key}: ${e}`);
99
         }
85
         }
100
     }
86
     }
87
+
101
     return params;
88
     return params;
102
 }
89
 }
103
 
90
 
104
 /**
91
 /**
105
- * Constructs new API instance. Creates iframe element that loads
106
- * Jitsi Meet.
107
- * @param domain the domain name of the server that hosts the conference
108
- * @param room_name the name of the room to join
109
- * @param width width of the iframe
110
- * @param height height of the iframe
111
- * @param parent_node the node that will contain the iframe
112
- * @param configOverwrite object containing configuration options defined in
113
- * config.js to be overridden.
114
- * @param interfaceConfigOverwrite object containing configuration options
115
- * defined in interface_config.js to be overridden.
116
- * @param noSsl if the value is true https won't be used
117
- * @param {string} [jwt] the JWT token if needed by jitsi-meet for
92
+ * Generates the URL for the iframe.
93
+ *
94
+ * @param {string} domain - The domain name of the server that hosts the
95
+ * conference.
96
+ * @param {string} [options] - Another optional parameters.
97
+ * @param {Object} [options.configOverwrite] - Object containing configuration
98
+ * options defined in config.js to be overridden.
99
+ * @param {Object} [options.interfaceConfigOverwrite] - Object containing
100
+ * configuration options defined in interface_config.js to be overridden.
101
+ * @param {string} [options.jwt] - The JWT token if needed by jitsi-meet for
118
  * authentication.
102
  * authentication.
119
- * @constructor
103
+ * @param {boolean} [options.noSsl] - If the value is true https won't be used.
104
+ * @param {string} [options.roomName] - The name of the room to join.
105
+ * @returns {string} The URL.
120
  */
106
  */
121
-function JitsiMeetExternalAPI(domain, room_name, width, height, parentNode,
122
-    configOverwrite, interfaceConfigOverwrite, noSsl, jwt) {
123
-    if (!width || width < MIN_WIDTH)
124
-        width = MIN_WIDTH;
125
-    if (!height || height < MIN_HEIGHT)
126
-        height = MIN_HEIGHT;
127
-
128
-    this.parentNode = null;
129
-    if (parentNode) {
130
-        this.parentNode = parentNode;
131
-    } else {
132
-        var scriptTag = document.scripts[document.scripts.length - 1];
133
-        this.parentNode = scriptTag.parentNode;
134
-    }
107
+function generateURL(domain, options = {}) {
108
+    const {
109
+        configOverwrite,
110
+        interfaceConfigOverwrite,
111
+        jwt,
112
+        noSSL,
113
+        roomName
114
+    } = options;
135
 
115
 
136
-    this.iframeHolder =
137
-        this.parentNode.appendChild(document.createElement("div"));
138
-    this.iframeHolder.id = "jitsiConference" + id;
139
-    if(width)
140
-        this.iframeHolder.style.width = width + "px";
141
-    if(height)
142
-        this.iframeHolder.style.height = height + "px";
143
-    this.frameName = "jitsiConferenceFrame" + id;
144
-    this.url = (noSsl) ? "http" : "https" +"://" + domain + "/";
145
-    if(room_name)
146
-        this.url += room_name;
116
+    let url = `${noSSL ? 'http' : 'https'}://${domain}/${roomName || ''}`;
147
 
117
 
148
     if (jwt) {
118
     if (jwt) {
149
-        this.url += '?jwt=' + jwt;
119
+        url += `?jwt=${jwt}`;
150
     }
120
     }
151
 
121
 
152
-    this.url += "#jitsi_meet_external_api_id=" + id;
122
+    url += `#jitsi_meet_external_api_id=${id}`;
153
 
123
 
154
     const configURLParams = configToURLParamsArray(configOverwrite);
124
     const configURLParams = configToURLParamsArray(configOverwrite);
125
+
155
     if (configURLParams.length) {
126
     if (configURLParams.length) {
156
-        this.url += '&config.' + configURLParams.join('&config.');
127
+        url += `&config.${configURLParams.join('&config.')}`;
157
     }
128
     }
158
 
129
 
159
     const interfaceConfigURLParams
130
     const interfaceConfigURLParams
160
         = configToURLParamsArray(interfaceConfigOverwrite);
131
         = configToURLParamsArray(interfaceConfigOverwrite);
132
+
161
     if (interfaceConfigURLParams.length) {
133
     if (interfaceConfigURLParams.length) {
162
-        this.url += '&interfaceConfig.'
163
-            + interfaceConfigURLParams.join('&interfaceConfig.');
134
+        url += `&interfaceConfig.${
135
+            interfaceConfigURLParams.join('&interfaceConfig.')}`;
164
     }
136
     }
165
 
137
 
166
-    this.frame = document.createElement("iframe");
167
-    this.frame.src = this.url;
168
-    this.frame.name = this.frameName;
169
-    this.frame.id = this.frameName;
170
-    this.frame.width = "100%";
171
-    this.frame.height = "100%";
172
-    this.frame.setAttribute("allowFullScreen","true");
173
-    this.frame = this.iframeHolder.appendChild(this.frame);
174
-    this.postis = postisInit({
175
-        window: this.frame.contentWindow,
176
-        scope: "jitsi_meet_external_api_" + id
177
-    });
178
-
179
-    this.eventHandlers = {};
180
-
181
-    // Map<{string} event_name, {boolean} postis_listener_added>
182
-    this.postisListeners = {};
183
-
184
-    this.numberOfParticipants = 1;
185
-    this._setupListeners();
186
-
187
-    id++;
138
+    return url;
188
 }
139
 }
189
 
140
 
190
 /**
141
 /**
191
- * Executes command. The available commands are:
192
- * displayName - sets the display name of the local participant to the value
193
- * passed in the arguments array.
194
- * toggleAudio - mutes / unmutes audio with no arguments
195
- * toggleVideo - mutes / unmutes video with no arguments
196
- * filmStrip - hides / shows the film strip with no arguments
197
- * If the command doesn't require any arguments the parameter should be set
198
- * to empty array or it may be omitted.
199
- * @param name the name of the command
200
- * @param arguments array of arguments
142
+ * The IFrame API interface class.
201
  */
143
  */
202
-JitsiMeetExternalAPI.prototype.executeCommand
203
-= function(name, ...argumentsList) {
204
-    if(!(name in commands)) {
205
-        logger.error("Not supported command name.");
206
-        return;
144
+class JitsiMeetExternalAPI extends EventEmitter {
145
+    /**
146
+     * Constructs new API instance. Creates iframe and loads Jitsi Meet in it.
147
+     *
148
+     * @param {string} domain - The domain name of the server that hosts the
149
+     * conference.
150
+     * @param {string} [roomName] - The name of the room to join.
151
+     * @param {number} [width] - Width of the iframe.
152
+     * @param {number} [height] - Height of the iframe.
153
+     * @param {DOMElement} [parentNode] - The node that will contain the
154
+     * iframe.
155
+     * @param {Object} [configOverwrite] - Object containing configuration
156
+     * options defined in config.js to be overridden.
157
+     * @param {Object} [interfaceConfigOverwrite] - Object containing
158
+     * configuration options defined in interface_config.js to be overridden.
159
+     * @param {boolean} [noSSL] - If the value is true https won't be used.
160
+     * @param {string} [jwt] - The JWT token if needed by jitsi-meet for
161
+     * authentication.
162
+     */
163
+    constructor(domain, // eslint-disable-line max-params
164
+        roomName = '',
165
+        width = MIN_WIDTH,
166
+        height = MIN_HEIGHT,
167
+        parentNode = document.body,
168
+        configOverwrite = {},
169
+        interfaceConfigOverwrite = {},
170
+        noSSL = false,
171
+        jwt = undefined) {
172
+        super();
173
+        this.parentNode = parentNode;
174
+        this.url = generateURL(domain, {
175
+            configOverwrite,
176
+            interfaceConfigOverwrite,
177
+            jwt,
178
+            noSSL,
179
+            roomName
180
+        });
181
+        this._createIFrame(Math.max(height, MIN_HEIGHT),
182
+            Math.max(width, MIN_WIDTH));
183
+        this.postis = postisInit({
184
+            scope: `jitsi_meet_external_api_${id}`,
185
+            window: this.frame.contentWindow
186
+        });
187
+        this.numberOfParticipants = 1;
188
+        this._setupListeners();
189
+        id++;
207
     }
190
     }
208
-    sendMessage(this.postis, {method: commands[name], params: argumentsList});
209
-};
210
 
191
 
211
-/**
212
- * Executes commands. The available commands are:
213
- * displayName - sets the display name of the local participant to the value
214
- * passed in the arguments array.
215
- * toggleAudio - mutes / unmutes audio. no arguments
216
- * toggleVideo - mutes / unmutes video. no arguments
217
- * filmStrip - hides / shows the film strip. no arguments
218
- * toggleChat - hides / shows chat. no arguments.
219
- * toggleContactList - hides / shows contact list. no arguments.
220
- * toggleShareScreen - starts / stops screen sharing. no arguments.
221
- * @param object the object with commands to be executed. The keys of the
222
- * object are the commands that will be executed and the values are the
223
- * arguments for the command.
224
- */
225
-JitsiMeetExternalAPI.prototype.executeCommands = function(object) {
226
-    for(var key in object)
227
-        this.executeCommand(key, object[key]);
228
-};
192
+    /**
193
+     * Creates the iframe element.
194
+     *
195
+     * @param {number} height - The height of the iframe.
196
+     * @param {number} width - The with of the iframe.
197
+     * @returns {void}
198
+     *
199
+     * @private
200
+     */
201
+    _createIFrame(height, width) {
202
+        this.iframeHolder
203
+            = this.parentNode.appendChild(document.createElement('div'));
204
+        this.iframeHolder.id = `jitsiConference${id}`;
205
+        this.iframeHolder.style.width = `${width}px`;
206
+        this.iframeHolder.style.height = `${height}px`;
207
+
208
+        this.frameName = `jitsiConferenceFrame${id}`;
209
+
210
+        this.frame = document.createElement('iframe');
211
+        this.frame.src = this.url;
212
+        this.frame.name = this.frameName;
213
+        this.frame.id = this.frameName;
214
+        this.frame.width = '100%';
215
+        this.frame.height = '100%';
216
+        this.frame.setAttribute('allowFullScreen', 'true');
217
+        this.frame = this.iframeHolder.appendChild(this.frame);
218
+    }
229
 
219
 
230
-/**
231
- * Adds event listeners to Meet Jitsi. The object key should be the name of
232
- * the event and value - the listener.
233
- * Currently we support the following
234
- * events:
235
- * incomingMessage - receives event notifications about incoming
236
- * messages. The listener will receive object with the following structure:
237
- * {{
238
- *  "from": from,//JID of the user that sent the message
239
- *  "nick": nick,//the nickname of the user that sent the message
240
- *  "message": txt//the text of the message
241
- * }}
242
- * outgoingMessage - receives event notifications about outgoing
243
- * messages. The listener will receive object with the following structure:
244
- * {{
245
- *  "message": txt//the text of the message
246
- * }}
247
- * displayNameChanged - receives event notifications about display name
248
- * change. The listener will receive object with the following structure:
249
- * {{
250
- * jid: jid,//the JID of the participant that changed his display name
251
- * displayname: displayName //the new display name
252
- * }}
253
- * participantJoined - receives event notifications about new participant.
254
- * The listener will receive object with the following structure:
255
- * {{
256
- * jid: jid //the jid of the participant
257
- * }}
258
- * participantLeft - receives event notifications about the participant that
259
- * left the room.
260
- * The listener will receive object with the following structure:
261
- * {{
262
- * jid: jid //the jid of the participant
263
- * }}
264
- * video-conference-joined - receives event notifications about the local user
265
- * has successfully joined the video conference.
266
- * The listener will receive object with the following structure:
267
- * {{
268
- * roomName: room //the room name of the conference
269
- * }}
270
- * video-conference-left - receives event notifications about the local user
271
- * has left the video conference.
272
- * The listener will receive object with the following structure:
273
- * {{
274
- * roomName: room //the room name of the conference
275
- * }}
276
- * readyToClose - all hangup operations are completed and Jitsi Meet is ready
277
- * to be disposed.
278
- * @param object
279
- */
280
-JitsiMeetExternalAPI.prototype.addEventListeners = function(object) {
281
-    for(var i in object)
282
-        this.addEventListener(i, object[i]);
283
-};
220
+    /**
221
+     * Setups listeners that are used internally for JitsiMeetExternalAPI.
222
+     *
223
+     * @returns {void}
224
+     *
225
+     * @private
226
+     */
227
+    _setupListeners() {
228
+        this.postis.listen('participant-joined',
229
+            changeParticipantNumber.bind(null, this, 1));
230
+        this.postis.listen('participant-left',
231
+            changeParticipantNumber.bind(null, this, -1));
232
+
233
+        for (const eventName in events) { // eslint-disable-line guard-for-in
234
+            const postisMethod = events[eventName];
235
+
236
+            this.postis.listen(postisMethod,
237
+                (...args) => this.emit(eventName, ...args));
238
+        }
239
+    }
284
 
240
 
285
-/**
286
- * Adds event listeners to Meet Jitsi. Currently we support the following
287
- * events:
288
- * incomingMessage - receives event notifications about incoming
289
- * messages. The listener will receive object with the following structure:
290
- * {{
291
- *  "from": from,//JID of the user that sent the message
292
- *  "nick": nick,//the nickname of the user that sent the message
293
- *  "message": txt//the text of the message
294
- * }}
295
- * outgoingMessage - receives event notifications about outgoing
296
- * messages. The listener will receive object with the following structure:
297
- * {{
298
- *  "message": txt//the text of the message
299
- * }}
300
- * displayNameChanged - receives event notifications about display name
301
- * change. The listener will receive object with the following structure:
302
- * {{
303
- * jid: jid,//the JID of the participant that changed his display name
304
- * displayname: displayName //the new display name
305
- * }}
306
- * participantJoined - receives event notifications about new participant.
307
- * The listener will receive object with the following structure:
308
- * {{
309
- * jid: jid //the jid of the participant
310
- * }}
311
- * participantLeft - receives event notifications about participant the that
312
- * left the room.
313
- * The listener will receive object with the following structure:
314
- * {{
315
- * jid: jid //the jid of the participant
316
- * }}
317
- * video-conference-joined - receives event notifications fired when the local
318
- * user has joined the video conference.
319
- * The listener will receive object with the following structure:
320
- * {{
321
- * roomName: room //the room name of the conference
322
- * }}
323
- * video-conference-left - receives event notifications fired when the local
324
- * user has joined the video conference.
325
- * The listener will receive object with the following structure:
326
- * {{
327
- * roomName: room //the room name of the conference
328
- * }}
329
- * @param event the name of the event
330
- * @param listener the listener
331
- */
332
-JitsiMeetExternalAPI.prototype.addEventListener = function(event, listener) {
333
-    if(!(event in events)) {
334
-        logger.error("Not supported event name.");
335
-        return;
241
+    /**
242
+     * Adds event listener to Meet Jitsi.
243
+     *
244
+     * @param {string} event - The name of the event.
245
+     * @param {Function} listener - The listener.
246
+     * @returns {void}
247
+     *
248
+     * @deprecated
249
+     * NOTE: This method is not removed for backward comatability purposes.
250
+     */
251
+    addEventListener(event, listener) {
252
+        this.on(event, listener);
336
     }
253
     }
337
-    // We cannot remove listeners from postis that's why we are handling the
338
-    // callback that way.
339
-    if(!this.postisListeners[event]) {
340
-        this.postis.listen(events[event], function(data) {
341
-            if((event in this.eventHandlers) &&
342
-                typeof this.eventHandlers[event] === "function")
343
-                this.eventHandlers[event].call(null, data);
344
-        }.bind(this));
345
-        this.postisListeners[event] = true;
254
+
255
+    /**
256
+     * Adds event listeners to Meet Jitsi.
257
+     *
258
+     * @param {Object} listeners - The object key should be the name of
259
+     * the event and value - the listener.
260
+     * Currently we support the following
261
+     * events:
262
+     * incomingMessage - receives event notifications about incoming
263
+     * messages. The listener will receive object with the following structure:
264
+     * {{
265
+     *  'from': from,//JID of the user that sent the message
266
+     *  'nick': nick,//the nickname of the user that sent the message
267
+     *  'message': txt//the text of the message
268
+     * }}
269
+     * outgoingMessage - receives event notifications about outgoing
270
+     * messages. The listener will receive object with the following structure:
271
+     * {{
272
+     *  'message': txt//the text of the message
273
+     * }}
274
+     * displayNameChanged - receives event notifications about display name
275
+     * change. The listener will receive object with the following structure:
276
+     * {{
277
+     * jid: jid,//the JID of the participant that changed his display name
278
+     * displayname: displayName //the new display name
279
+     * }}
280
+     * participantJoined - receives event notifications about new participant.
281
+     * The listener will receive object with the following structure:
282
+     * {{
283
+     * jid: jid //the jid of the participant
284
+     * }}
285
+     * participantLeft - receives event notifications about the participant that
286
+     * left the room.
287
+     * The listener will receive object with the following structure:
288
+     * {{
289
+     * jid: jid //the jid of the participant
290
+     * }}
291
+     * video-conference-joined - receives event notifications about the local
292
+     * user has successfully joined the video conference.
293
+     * The listener will receive object with the following structure:
294
+     * {{
295
+     * roomName: room //the room name of the conference
296
+     * }}
297
+     * video-conference-left - receives event notifications about the local user
298
+     * has left the video conference.
299
+     * The listener will receive object with the following structure:
300
+     * {{
301
+     * roomName: room //the room name of the conference
302
+     * }}
303
+     * readyToClose - all hangup operations are completed and Jitsi Meet is
304
+     * ready to be disposed.
305
+     * @returns {void}
306
+     *
307
+     * @deprecated
308
+     * NOTE: This method is not removed for backward comatability purposes.
309
+     */
310
+    addEventListeners(listeners) {
311
+        for (const event in listeners) { // eslint-disable-line guard-for-in
312
+            this.addEventListener(event, listeners[event]);
313
+        }
346
     }
314
     }
347
-    this.eventHandlers[event] = listener;
348
-};
349
 
315
 
350
-/**
351
- * Removes event listener.
352
- * @param event the name of the event.
353
- */
354
-JitsiMeetExternalAPI.prototype.removeEventListener = function(event) {
355
-    if(!(event in this.eventHandlers))
356
-    {
357
-        logger.error("The event " + event + " is not registered.");
358
-        return;
316
+    /**
317
+     * Removes the listeners and removes the Jitsi Meet frame.
318
+     *
319
+     * @returns {void}
320
+     */
321
+    dispose() {
322
+        const frame = document.getElementById(this.frameName);
323
+
324
+        this.postis.destroy();
325
+        if (frame) {
326
+            frame.src = 'about:blank';
327
+        }
328
+        window.setTimeout(() => {
329
+            this.iframeHolder.removeChild(this.frame);
330
+            this.iframeHolder.parentNode.removeChild(this.iframeHolder);
331
+        }, 10);
359
     }
332
     }
360
-    delete this.eventHandlers[event];
361
-};
362
 
333
 
363
-/**
364
- * Removes event listeners.
365
- * @param events array with the names of the events.
366
- */
367
-JitsiMeetExternalAPI.prototype.removeEventListeners = function(events) {
368
-    for(var i = 0; i < events.length; i++)
369
-        this.removeEventListener(events[i]);
370
-};
334
+    /**
335
+     * Executes command. The available commands are:
336
+     * displayName - sets the display name of the local participant to the value
337
+     * passed in the arguments array.
338
+     * toggleAudio - mutes / unmutes audio with no arguments.
339
+     * toggleVideo - mutes / unmutes video with no arguments.
340
+     * filmStrip - hides / shows the film strip with no arguments.
341
+     * If the command doesn't require any arguments the parameter should be set
342
+     * to empty array or it may be omitted.
343
+     *
344
+     * @param {string} name - The name of the command.
345
+     * @returns {void}
346
+     */
347
+    executeCommand(name, ...args) {
348
+        if (!(name in commands)) {
349
+            logger.error('Not supported command name.');
350
+
351
+            return;
352
+        }
353
+        this.postis.send({
354
+            method: commands[name],
355
+            params: args
356
+        });
357
+    }
371
 
358
 
372
-/**
373
- * Returns the number of participants in the conference.
374
- * NOTE: the local participant is included.
375
- * @returns {int} the number of participants in the conference.
376
- */
377
-JitsiMeetExternalAPI.prototype.getNumberOfParticipants = function() {
378
-    return this.numberOfParticipants;
379
-};
359
+    /**
360
+     * Executes commands. The available commands are:
361
+     * displayName - sets the display name of the local participant to the value
362
+     * passed in the arguments array.
363
+     * toggleAudio - mutes / unmutes audio. no arguments
364
+     * toggleVideo - mutes / unmutes video. no arguments
365
+     * filmStrip - hides / shows the film strip. no arguments
366
+     * toggleChat - hides / shows chat. no arguments.
367
+     * toggleContactList - hides / shows contact list. no arguments.
368
+     * toggleShareScreen - starts / stops screen sharing. no arguments.
369
+     *
370
+     * @param {Object} commandList - The object with commands to be executed.
371
+     * The keys of the object are the commands that will be executed and the
372
+     * values are the arguments for the command.
373
+     * @returns {void}
374
+     */
375
+    executeCommands(commandList) {
376
+        for (const key in commandList) { // eslint-disable-line guard-for-in
377
+            this.executeCommand(key, commandList[key]);
378
+        }
379
+    }
380
 
380
 
381
-/**
382
- * Setups listeners that are used internally for JitsiMeetExternalAPI.
383
- */
384
-JitsiMeetExternalAPI.prototype._setupListeners = function() {
385
-    this.postis.listen("participant-joined",
386
-        changeParticipantNumber.bind(null, this, 1));
387
-    this.postis.listen("participant-left",
388
-        changeParticipantNumber.bind(null, this, -1));
389
-};
381
+    /**
382
+     * Returns the number of participants in the conference. The local
383
+     * participant is included.
384
+     *
385
+     * @returns {int} The number of participants in the conference.
386
+     */
387
+    getNumberOfParticipants() {
388
+        return this.numberOfParticipants;
389
+    }
390
 
390
 
391
-/**
392
- * Removes the listeners and removes the Jitsi Meet frame.
393
- */
394
-JitsiMeetExternalAPI.prototype.dispose = function() {
395
-    this.postis.destroy();
396
-    var frame = document.getElementById(this.frameName);
397
-    if(frame)
398
-        frame.src = 'about:blank';
399
-    var self = this;
400
-    window.setTimeout(function () {
401
-        self.iframeHolder.removeChild(self.frame);
402
-        self.iframeHolder.parentNode.removeChild(self.iframeHolder);
403
-    }, 10);
404
-};
391
+    /**
392
+     * Removes event listener.
393
+     *
394
+     * @param {string} event - The name of the event.
395
+     * @returns {void}
396
+     *
397
+     * @deprecated
398
+     * NOTE: This method is not removed for backward comatability purposes.
399
+     */
400
+    removeEventListener(event) {
401
+        this.removeListeners(event);
402
+    }
403
+
404
+    /**
405
+     * Removes event listeners.
406
+     *
407
+     * @param {Array<string>} eventList - Array with the names of the events.
408
+     * @returns {void}
409
+     *
410
+     * @deprecated
411
+     * NOTE: This method is not removed for backward comatability purposes.
412
+     */
413
+    removeEventListeners(eventList) {
414
+        eventList.forEach(event => this.removeEventListener(event));
415
+    }
416
+}
405
 
417
 
406
 module.exports = JitsiMeetExternalAPI;
418
 module.exports = JitsiMeetExternalAPI;

Notiek ielāde…
Atcelt
Saglabāt