You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

api_connector.js 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /**
  2. * Implements API class that communicates with external api class
  3. * and provides interface to access Jitsi Meet features by external
  4. * applications that embed Jitsi Meet
  5. */
  6. var APIConnector = (function () {
  7. function APIConnector() { }
  8. /**
  9. * List of the available commands.
  10. * @type {{
  11. * displayName: inputDisplayNameHandler,
  12. * muteAudio: toggleAudio,
  13. * muteVideo: toggleVideo,
  14. * filmStrip: toggleFilmStrip
  15. * }}
  16. */
  17. var commands =
  18. {
  19. displayName: VideoLayout.inputDisplayNameHandler,
  20. muteAudio: toggleAudio,
  21. muteVideo: toggleVideo,
  22. toggleFilmStrip: BottomToolbar.toggleFilmStrip,
  23. toggleChat: BottomToolbar.toggleChat,
  24. toggleContactList: BottomToolbar.toggleContactList
  25. };
  26. /**
  27. * Maps the supported events and their status
  28. * (true it the event is enabled and false if it is disabled)
  29. * @type {{
  30. * incomingMessage: boolean,
  31. * outgoingMessage: boolean,
  32. * displayNameChange: boolean,
  33. * participantJoined: boolean,
  34. * participantLeft: boolean
  35. * }}
  36. */
  37. var events =
  38. {
  39. incomingMessage: false,
  40. outgoingMessage:false,
  41. displayNameChange: false,
  42. participantJoined: false,
  43. participantLeft: false
  44. };
  45. /**
  46. * Check whether the API should be enabled or not.
  47. * @returns {boolean}
  48. */
  49. APIConnector.isEnabled = function () {
  50. var hash = location.hash;
  51. if(hash && hash.indexOf("external") > -1 && window.postMessage)
  52. return true;
  53. return false;
  54. };
  55. /**
  56. * Initializes the APIConnector. Setups message event listeners that will
  57. * receive information from external applications that embed Jitsi Meet.
  58. * It also sends a message to the external application that APIConnector
  59. * is initialized.
  60. */
  61. APIConnector.init = function () {
  62. if (window.addEventListener)
  63. {
  64. window.addEventListener('message',
  65. APIConnector.processMessage, false);
  66. }
  67. else
  68. {
  69. window.attachEvent('onmessage', APIConnector.processMessage);
  70. }
  71. APIConnector.sendMessage({type: "system", loaded: true});
  72. };
  73. /**
  74. * Sends message to the external application.
  75. * @param object
  76. */
  77. APIConnector.sendMessage = function (object) {
  78. window.parent.postMessage(JSON.stringify(object), "*");
  79. };
  80. /**
  81. * Processes a message event from the external application
  82. * @param event the message event
  83. */
  84. APIConnector.processMessage = function(event)
  85. {
  86. var message;
  87. try {
  88. message = JSON.parse(event.data);
  89. } catch (e) {}
  90. if(!message.type)
  91. return;
  92. switch (message.type)
  93. {
  94. case "command":
  95. APIConnector.processCommand(message);
  96. break;
  97. case "event":
  98. APIConnector.processEvent(message);
  99. break;
  100. default:
  101. console.error("Unknown type of the message");
  102. return;
  103. }
  104. };
  105. /**
  106. * Processes commands from external applicaiton.
  107. * @param message the object with the command
  108. */
  109. APIConnector.processCommand = function (message)
  110. {
  111. if(message.action != "execute")
  112. {
  113. console.error("Unknown action of the message");
  114. return;
  115. }
  116. for(var key in message)
  117. {
  118. if(commands[key])
  119. commands[key].apply(null, message[key]);
  120. }
  121. };
  122. /**
  123. * Processes events objects from external applications
  124. * @param event the event
  125. */
  126. APIConnector.processEvent = function (event) {
  127. if(!event.action)
  128. {
  129. console.error("Event with no action is received.");
  130. return;
  131. }
  132. switch(event.action)
  133. {
  134. case "add":
  135. for(var i = 0; i < event.events.length; i++)
  136. {
  137. events[event.events[i]] = true;
  138. }
  139. break;
  140. case "remove":
  141. for(var i = 0; i < event.events.length; i++)
  142. {
  143. events[event.events[i]] = false;
  144. }
  145. break;
  146. default:
  147. console.error("Unknown action for event.");
  148. }
  149. };
  150. /**
  151. * Checks whether the event is enabled ot not.
  152. * @param name the name of the event.
  153. * @returns {*}
  154. */
  155. APIConnector.isEventEnabled = function (name) {
  156. return events[name];
  157. };
  158. /**
  159. * Sends event object to the external application that has been subscribed
  160. * for that event.
  161. * @param name the name event
  162. * @param object data associated with the event
  163. */
  164. APIConnector.triggerEvent = function (name, object) {
  165. APIConnector.sendMessage({
  166. type: "event", action: "result", event: name, result: object});
  167. };
  168. /**
  169. * Removes the listeners.
  170. */
  171. APIConnector.dispose = function () {
  172. if(window.removeEventListener)
  173. {
  174. window.removeEventListener("message",
  175. APIConnector.processMessage, false);
  176. }
  177. else
  178. {
  179. window.detachEvent('onmessage', APIConnector.processMessage);
  180. }
  181. };
  182. return APIConnector;
  183. })();