2014-10-22 09:03:07 +00:00
|
|
|
/**
|
|
|
|
* Implements API class that embeds Jitsi Meet in external applications.
|
|
|
|
*/
|
|
|
|
var JitsiMeetExternalAPI = (function()
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* The minimum width for the Jitsi Meet frame
|
|
|
|
* @type {number}
|
|
|
|
*/
|
|
|
|
var MIN_WIDTH = 790;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The minimum height for the Jitsi Meet frame
|
|
|
|
* @type {number}
|
|
|
|
*/
|
|
|
|
var MIN_HEIGHT = 300;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructs new API instance. Creates iframe element that loads
|
|
|
|
* Jitsi Meet.
|
|
|
|
* @param domain the domain name of the server that hosts the conference
|
|
|
|
* @param room_name the name of the room to join
|
|
|
|
* @param width width of the iframe
|
|
|
|
* @param height height of the iframe
|
|
|
|
* @param parent_node the node that will contain the iframe
|
2015-08-07 00:01:21 +00:00
|
|
|
* @param filmStripOnly if the value is true only the small videos will be
|
|
|
|
* visible.
|
2016-06-07 19:08:02 +00:00
|
|
|
* @param noSsl if the value is true https won't be used
|
2014-10-22 09:03:07 +00:00
|
|
|
* @constructor
|
|
|
|
*/
|
2015-08-07 00:01:21 +00:00
|
|
|
function JitsiMeetExternalAPI(domain, room_name, width, height, parentNode,
|
2016-06-07 19:08:02 +00:00
|
|
|
configOverwrite, interfaceConfigOverwrite, noSsl) {
|
2015-09-11 03:05:53 +00:00
|
|
|
if (!width || width < MIN_WIDTH)
|
2015-07-22 18:34:44 +00:00
|
|
|
width = MIN_WIDTH;
|
2015-09-11 03:05:53 +00:00
|
|
|
if (!height || height < MIN_HEIGHT)
|
2015-07-22 18:34:44 +00:00
|
|
|
height = MIN_HEIGHT;
|
|
|
|
|
2014-10-22 09:03:07 +00:00
|
|
|
this.parentNode = null;
|
2015-07-28 18:22:11 +00:00
|
|
|
if (parentNode) {
|
|
|
|
this.parentNode = parentNode;
|
2015-07-22 18:34:44 +00:00
|
|
|
} else {
|
2014-10-22 09:03:07 +00:00
|
|
|
var scriptTag = document.scripts[document.scripts.length - 1];
|
|
|
|
this.parentNode = scriptTag.parentNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.iframeHolder =
|
|
|
|
this.parentNode.appendChild(document.createElement("div"));
|
|
|
|
this.iframeHolder.id = "jitsiConference" + JitsiMeetExternalAPI.id;
|
2015-08-17 18:46:26 +00:00
|
|
|
if(width)
|
|
|
|
this.iframeHolder.style.width = width + "px";
|
|
|
|
if(height)
|
|
|
|
this.iframeHolder.style.height = height + "px";
|
2014-10-22 09:03:07 +00:00
|
|
|
this.frameName = "jitsiConferenceFrame" + JitsiMeetExternalAPI.id;
|
2016-06-07 19:08:02 +00:00
|
|
|
this.url = (noSsl) ? "http" : "https" +"://" + domain + "/";
|
2014-10-22 09:03:07 +00:00
|
|
|
if(room_name)
|
|
|
|
this.url += room_name;
|
2015-07-22 18:26:07 +00:00
|
|
|
this.url += "#external=true";
|
2015-08-20 19:35:16 +00:00
|
|
|
|
|
|
|
var key;
|
|
|
|
if (configOverwrite) {
|
|
|
|
for (key in configOverwrite) {
|
|
|
|
if (!configOverwrite.hasOwnProperty(key) ||
|
|
|
|
typeof key !== 'string')
|
|
|
|
continue;
|
|
|
|
this.url += "&config." + key + "=" + configOverwrite[key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (interfaceConfigOverwrite) {
|
|
|
|
for (key in interfaceConfigOverwrite) {
|
|
|
|
if (!interfaceConfigOverwrite.hasOwnProperty(key) ||
|
|
|
|
typeof key !== 'string')
|
|
|
|
continue;
|
2015-09-11 02:42:15 +00:00
|
|
|
this.url += "&interfaceConfig." + key + "=" +
|
|
|
|
interfaceConfigOverwrite[key];
|
2015-08-20 19:35:16 +00:00
|
|
|
}
|
|
|
|
}
|
2015-08-07 00:01:21 +00:00
|
|
|
|
2014-10-22 09:03:07 +00:00
|
|
|
JitsiMeetExternalAPI.id++;
|
|
|
|
|
|
|
|
this.frame = document.createElement("iframe");
|
|
|
|
this.frame.src = this.url;
|
|
|
|
this.frame.name = this.frameName;
|
2014-10-27 13:24:09 +00:00
|
|
|
this.frame.id = this.frameName;
|
2014-10-22 09:03:07 +00:00
|
|
|
this.frame.width = "100%";
|
|
|
|
this.frame.height = "100%";
|
2014-10-27 13:24:09 +00:00
|
|
|
this.frame.setAttribute("allowFullScreen","true");
|
2014-10-22 09:03:07 +00:00
|
|
|
this.frame = this.iframeHolder.appendChild(this.frame);
|
|
|
|
|
2014-10-27 13:24:09 +00:00
|
|
|
|
2014-10-22 09:03:07 +00:00
|
|
|
this.frameLoaded = false;
|
|
|
|
this.initialCommands = [];
|
2014-10-27 13:24:09 +00:00
|
|
|
this.eventHandlers = {};
|
2014-10-22 09:03:07 +00:00
|
|
|
this.initListeners();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Last id of api object
|
|
|
|
* @type {number}
|
|
|
|
*/
|
|
|
|
JitsiMeetExternalAPI.id = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends the passed object to Jitsi Meet
|
|
|
|
* @param object the object to be sent
|
|
|
|
*/
|
2015-07-28 18:22:11 +00:00
|
|
|
JitsiMeetExternalAPI.prototype.sendMessage = function(object) {
|
|
|
|
if (this.frameLoaded) {
|
2014-10-22 09:03:07 +00:00
|
|
|
this.frame.contentWindow.postMessage(
|
|
|
|
JSON.stringify(object), this.frame.src);
|
|
|
|
}
|
2015-07-28 18:22:11 +00:00
|
|
|
else {
|
2014-10-22 09:03:07 +00:00
|
|
|
this.initialCommands.push(object);
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Executes command. The available commands are:
|
|
|
|
* displayName - sets the display name of the local participant to the value
|
|
|
|
* passed in the arguments array.
|
2015-07-28 18:43:05 +00:00
|
|
|
* toggleAudio - mutes / unmutes audio with no arguments
|
|
|
|
* toggleVideo - mutes / unmutes video with no arguments
|
2014-10-22 09:03:07 +00:00
|
|
|
* filmStrip - hides / shows the film strip with no arguments
|
|
|
|
* If the command doesn't require any arguments the parameter should be set
|
|
|
|
* to empty array or it may be omitted.
|
|
|
|
* @param name the name of the command
|
|
|
|
* @param arguments array of arguments
|
|
|
|
*/
|
|
|
|
JitsiMeetExternalAPI.prototype.executeCommand = function(name,
|
2015-07-28 18:22:11 +00:00
|
|
|
argumentsList) {
|
2014-10-22 09:03:07 +00:00
|
|
|
var argumentsArray = argumentsList;
|
2015-07-28 18:22:11 +00:00
|
|
|
if (!argumentsArray)
|
2014-10-22 09:03:07 +00:00
|
|
|
argumentsArray = [];
|
2014-10-27 13:24:09 +00:00
|
|
|
var object = {type: "command", action: "execute"};
|
2014-10-22 09:03:07 +00:00
|
|
|
object[name] = argumentsArray;
|
|
|
|
this.sendMessage(object);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Executes commands. The available commands are:
|
|
|
|
* displayName - sets the display name of the local participant to the value
|
|
|
|
* passed in the arguments array.
|
2015-07-28 18:43:05 +00:00
|
|
|
* toggleAudio - mutes / unmutes audio with no arguments
|
|
|
|
* toggleVideo - mutes / unmutes video with no arguments
|
2014-10-22 09:03:07 +00:00
|
|
|
* filmStrip - hides / shows the film strip with no arguments
|
|
|
|
* @param object the object with commands to be executed. The keys of the
|
|
|
|
* object are the commands that will be executed and the values are the
|
|
|
|
* arguments for the command.
|
|
|
|
*/
|
|
|
|
JitsiMeetExternalAPI.prototype.executeCommands = function (object) {
|
2014-10-27 13:24:09 +00:00
|
|
|
object.type = "command";
|
|
|
|
object.action = "execute";
|
2014-10-22 09:03:07 +00:00
|
|
|
this.sendMessage(object);
|
|
|
|
};
|
|
|
|
|
2014-10-27 13:24:09 +00:00
|
|
|
/**
|
2015-07-28 18:22:11 +00:00
|
|
|
* Adds event listeners to Meet Jitsi. The object key should be the name of
|
|
|
|
* the event and value - the listener.
|
2014-10-27 13:24:09 +00:00
|
|
|
* Currently we support the following
|
|
|
|
* events:
|
2014-11-08 14:41:16 +00:00
|
|
|
* incomingMessage - receives event notifications about incoming
|
2014-10-27 13:24:09 +00:00
|
|
|
* messages. The listener will receive object with the following structure:
|
|
|
|
* {{
|
|
|
|
* "from": from,//JID of the user that sent the message
|
|
|
|
* "nick": nick,//the nickname of the user that sent the message
|
|
|
|
* "message": txt//the text of the message
|
|
|
|
* }}
|
|
|
|
* outgoingMessage - receives event notifications about outgoing
|
|
|
|
* messages. The listener will receive object with the following structure:
|
|
|
|
* {{
|
|
|
|
* "message": txt//the text of the message
|
|
|
|
* }}
|
|
|
|
* displayNameChanged - receives event notifications about display name
|
|
|
|
* change. The listener will receive object with the following structure:
|
|
|
|
* {{
|
|
|
|
* jid: jid,//the JID of the participant that changed his display name
|
|
|
|
* displayname: displayName //the new display name
|
|
|
|
* }}
|
|
|
|
* participantJoined - receives event notifications about new participant.
|
|
|
|
* The listener will receive object with the following structure:
|
|
|
|
* {{
|
|
|
|
* jid: jid //the jid of the participant
|
|
|
|
* }}
|
2015-09-11 02:42:15 +00:00
|
|
|
* participantLeft - receives event notifications about the participant that
|
|
|
|
* left the room.
|
2014-10-27 13:24:09 +00:00
|
|
|
* The listener will receive object with the following structure:
|
|
|
|
* {{
|
|
|
|
* jid: jid //the jid of the participant
|
|
|
|
* }}
|
|
|
|
* @param object
|
|
|
|
*/
|
|
|
|
JitsiMeetExternalAPI.prototype.addEventListeners
|
2015-07-28 18:22:11 +00:00
|
|
|
= function (object) {
|
2014-10-27 13:24:09 +00:00
|
|
|
|
|
|
|
var message = {type: "event", action: "add", events: []};
|
|
|
|
for(var i in object)
|
|
|
|
{
|
|
|
|
message.events.push(i);
|
|
|
|
this.eventHandlers[i] = object[i];
|
|
|
|
}
|
|
|
|
this.sendMessage(message);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds event listeners to Meet Jitsi. Currently we support the following
|
|
|
|
* events:
|
2014-11-08 14:41:16 +00:00
|
|
|
* incomingMessage - receives event notifications about incoming
|
2014-10-27 13:24:09 +00:00
|
|
|
* messages. The listener will receive object with the following structure:
|
|
|
|
* {{
|
|
|
|
* "from": from,//JID of the user that sent the message
|
|
|
|
* "nick": nick,//the nickname of the user that sent the message
|
|
|
|
* "message": txt//the text of the message
|
|
|
|
* }}
|
|
|
|
* outgoingMessage - receives event notifications about outgoing
|
|
|
|
* messages. The listener will receive object with the following structure:
|
|
|
|
* {{
|
|
|
|
* "message": txt//the text of the message
|
|
|
|
* }}
|
|
|
|
* displayNameChanged - receives event notifications about display name
|
|
|
|
* change. The listener will receive object with the following structure:
|
|
|
|
* {{
|
|
|
|
* jid: jid,//the JID of the participant that changed his display name
|
|
|
|
* displayname: displayName //the new display name
|
|
|
|
* }}
|
|
|
|
* participantJoined - receives event notifications about new participant.
|
|
|
|
* The listener will receive object with the following structure:
|
|
|
|
* {{
|
|
|
|
* jid: jid //the jid of the participant
|
|
|
|
* }}
|
2015-09-11 02:42:15 +00:00
|
|
|
* participantLeft - receives event notifications about participant the that
|
|
|
|
* left the room.
|
2014-10-27 13:24:09 +00:00
|
|
|
* The listener will receive object with the following structure:
|
|
|
|
* {{
|
|
|
|
* jid: jid //the jid of the participant
|
|
|
|
* }}
|
|
|
|
* @param event the name of the event
|
|
|
|
* @param listener the listener
|
|
|
|
*/
|
|
|
|
JitsiMeetExternalAPI.prototype.addEventListener
|
2015-07-28 18:22:11 +00:00
|
|
|
= function (event, listener) {
|
2014-10-27 13:24:09 +00:00
|
|
|
|
|
|
|
var message = {type: "event", action: "add", events: [event]};
|
|
|
|
this.eventHandlers[event] = listener;
|
|
|
|
this.sendMessage(message);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes event listener.
|
|
|
|
* @param event the name of the event.
|
|
|
|
*/
|
|
|
|
JitsiMeetExternalAPI.prototype.removeEventListener
|
2015-07-28 18:22:11 +00:00
|
|
|
= function (event) {
|
2014-10-27 13:24:09 +00:00
|
|
|
if(!this.eventHandlers[event])
|
|
|
|
{
|
|
|
|
console.error("The event " + event + " is not registered.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var message = {type: "event", action: "remove", events: [event]};
|
|
|
|
delete this.eventHandlers[event];
|
|
|
|
this.sendMessage(message);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes event listeners.
|
|
|
|
* @param events array with the names of the events.
|
|
|
|
*/
|
|
|
|
JitsiMeetExternalAPI.prototype.removeEventListeners
|
2015-07-28 18:22:11 +00:00
|
|
|
= function (events) {
|
2014-10-27 13:24:09 +00:00
|
|
|
var eventsArray = [];
|
|
|
|
for(var i = 0; i < events.length; i++)
|
|
|
|
{
|
|
|
|
var event = events[i];
|
|
|
|
if(!this.eventHandlers[event])
|
|
|
|
{
|
|
|
|
console.error("The event " + event + " is not registered.");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
delete this.eventHandlers[event];
|
|
|
|
eventsArray.push(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(eventsArray.length > 0)
|
|
|
|
{
|
|
|
|
this.sendMessage(
|
|
|
|
{type: "event", action: "remove", events: eventsArray});
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2014-10-22 09:03:07 +00:00
|
|
|
/**
|
|
|
|
* Processes message events sent from Jitsi Meet
|
|
|
|
* @param event the event
|
|
|
|
*/
|
2015-07-28 18:22:11 +00:00
|
|
|
JitsiMeetExternalAPI.prototype.processMessage = function(event) {
|
2014-10-22 09:03:07 +00:00
|
|
|
var message;
|
|
|
|
try {
|
|
|
|
message = JSON.parse(event.data);
|
|
|
|
} catch (e) {}
|
2014-10-27 13:24:09 +00:00
|
|
|
|
|
|
|
if(!message.type) {
|
|
|
|
console.error("Message without type is received.");
|
|
|
|
return;
|
|
|
|
}
|
2015-07-28 18:22:11 +00:00
|
|
|
switch (message.type) {
|
2014-10-27 13:24:09 +00:00
|
|
|
case "system":
|
2015-07-28 18:22:11 +00:00
|
|
|
if(message.loaded) {
|
2014-10-27 13:24:09 +00:00
|
|
|
this.onFrameLoaded();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "event":
|
|
|
|
if(message.action != "result" ||
|
2015-07-28 18:22:11 +00:00
|
|
|
!message.event || !this.eventHandlers[message.event]) {
|
2014-10-27 13:24:09 +00:00
|
|
|
console.warn("The received event cannot be parsed.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.eventHandlers[message.event](message.result);
|
|
|
|
break;
|
|
|
|
default :
|
|
|
|
console.error("Unknown message type.");
|
|
|
|
return;
|
2014-10-22 09:03:07 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* That method is called when the Jitsi Meet is loaded. Executes saved
|
|
|
|
* commands that are send before the frame was loaded.
|
|
|
|
*/
|
|
|
|
JitsiMeetExternalAPI.prototype.onFrameLoaded = function () {
|
|
|
|
this.frameLoaded = true;
|
2015-07-28 18:22:11 +00:00
|
|
|
for (var i = 0; i < this.initialCommands.length; i++) {
|
2014-10-22 09:03:07 +00:00
|
|
|
this.sendMessage(this.initialCommands[i]);
|
|
|
|
}
|
|
|
|
this.initialCommands = null;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Setups the listener for message events from Jitsi Meet.
|
|
|
|
*/
|
|
|
|
JitsiMeetExternalAPI.prototype.initListeners = function () {
|
|
|
|
var self = this;
|
|
|
|
this.eventListener = function (event) {
|
|
|
|
self.processMessage(event);
|
|
|
|
};
|
2015-07-28 18:22:11 +00:00
|
|
|
if (window.addEventListener) {
|
2014-10-22 09:03:07 +00:00
|
|
|
window.addEventListener('message',
|
|
|
|
this.eventListener, false);
|
|
|
|
}
|
2015-07-28 18:22:11 +00:00
|
|
|
else {
|
2014-10-22 09:03:07 +00:00
|
|
|
window.attachEvent('onmessage', this.eventListener);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes the listeners and removes the Jitsi Meet frame.
|
|
|
|
*/
|
|
|
|
JitsiMeetExternalAPI.prototype.dispose = function () {
|
2015-07-28 18:22:11 +00:00
|
|
|
if (window.removeEventListener) {
|
2014-10-22 09:03:07 +00:00
|
|
|
window.removeEventListener('message',
|
|
|
|
this.eventListener, false);
|
|
|
|
}
|
2015-07-28 18:22:11 +00:00
|
|
|
else {
|
2014-10-22 09:03:07 +00:00
|
|
|
window.detachEvent('onmessage',
|
|
|
|
this.eventListener);
|
|
|
|
}
|
2014-10-27 13:24:09 +00:00
|
|
|
var frame = document.getElementById(this.frameName);
|
|
|
|
if(frame)
|
|
|
|
frame.src = 'about:blank';
|
|
|
|
var self = this;
|
|
|
|
window.setTimeout(function () {
|
|
|
|
self.iframeHolder.removeChild(self.frame);
|
|
|
|
self.iframeHolder.parentNode.removeChild(self.iframeHolder);
|
|
|
|
}, 10);
|
2014-10-22 09:03:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return JitsiMeetExternalAPI;
|
|
|
|
|
2015-09-11 03:05:53 +00:00
|
|
|
})();
|