Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

external_api.js 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /**
  2. * Implements API class that embeds Jitsi Meet in external applications.
  3. */
  4. var JitsiMeetExternalAPI = (function()
  5. {
  6. /**
  7. * The minimum width for the Jitsi Meet frame
  8. * @type {number}
  9. */
  10. var MIN_WIDTH = 790;
  11. /**
  12. * The minimum height for the Jitsi Meet frame
  13. * @type {number}
  14. */
  15. var MIN_HEIGHT = 300;
  16. /**
  17. * Constructs new API instance. Creates iframe element that loads
  18. * Jitsi Meet.
  19. * @param domain the domain name of the server that hosts the conference
  20. * @param room_name the name of the room to join
  21. * @param width width of the iframe
  22. * @param height height of the iframe
  23. * @param parent_node the node that will contain the iframe
  24. * @constructor
  25. */
  26. function JitsiMeetExternalAPI(domain, room_name, width, height, parent_node)
  27. {
  28. this.parentNode = null;
  29. if(parent_node)
  30. {
  31. this.parentNode = parent_node;
  32. }
  33. else
  34. {
  35. var scriptTag = document.scripts[document.scripts.length - 1];
  36. this.parentNode = scriptTag.parentNode;
  37. }
  38. this.iframeHolder =
  39. this.parentNode.appendChild(document.createElement("div"));
  40. this.iframeHolder.id = "jitsiConference" + JitsiMeetExternalAPI.id;
  41. if(width < MIN_WIDTH)
  42. width = MIN_WIDTH;
  43. if(height < MIN_HEIGHT)
  44. height = MIN_HEIGHT;
  45. this.iframeHolder.style.width = width + "px";
  46. this.iframeHolder.style.height = height + "px";
  47. this.frameName = "jitsiConferenceFrame" + JitsiMeetExternalAPI.id;
  48. this.url = "https://" + domain + "/";
  49. if(room_name)
  50. this.url += room_name;
  51. this.url += "#external";
  52. JitsiMeetExternalAPI.id++;
  53. this.frame = document.createElement("iframe");
  54. this.frame.src = this.url;
  55. this.frame.name = this.frameName;
  56. this.frame.width = "100%";
  57. this.frame.height = "100%";
  58. this.frame = this.iframeHolder.appendChild(this.frame);
  59. this.frameLoaded = false;
  60. this.initialCommands = [];
  61. this.initListeners();
  62. }
  63. /**
  64. * Last id of api object
  65. * @type {number}
  66. */
  67. JitsiMeetExternalAPI.id = 0;
  68. /**
  69. * Sends the passed object to Jitsi Meet
  70. * @param object the object to be sent
  71. */
  72. JitsiMeetExternalAPI.prototype.sendMessage = function(object)
  73. {
  74. if(this.frameLoaded)
  75. {
  76. this.frame.contentWindow.postMessage(
  77. JSON.stringify(object), this.frame.src);
  78. }
  79. else
  80. {
  81. this.initialCommands.push(object);
  82. }
  83. };
  84. /**
  85. * Executes command. The available commands are:
  86. * displayName - sets the display name of the local participant to the value
  87. * passed in the arguments array.
  88. * muteAudio - mutes / unmutes audio with no arguments
  89. * muteVideo - mutes / unmutes video with no arguments
  90. * filmStrip - hides / shows the film strip with no arguments
  91. * If the command doesn't require any arguments the parameter should be set
  92. * to empty array or it may be omitted.
  93. * @param name the name of the command
  94. * @param arguments array of arguments
  95. */
  96. JitsiMeetExternalAPI.prototype.executeCommand = function(name,
  97. argumentsList)
  98. {
  99. var argumentsArray = argumentsList;
  100. if(!argumentsArray)
  101. argumentsArray = [];
  102. var object = {};
  103. object[name] = argumentsArray;
  104. this.sendMessage(object);
  105. };
  106. /**
  107. * Executes commands. The available commands are:
  108. * displayName - sets the display name of the local participant to the value
  109. * passed in the arguments array.
  110. * muteAudio - mutes / unmutes audio with no arguments
  111. * muteVideo - mutes / unmutes video with no arguments
  112. * filmStrip - hides / shows the film strip with no arguments
  113. * @param object the object with commands to be executed. The keys of the
  114. * object are the commands that will be executed and the values are the
  115. * arguments for the command.
  116. */
  117. JitsiMeetExternalAPI.prototype.executeCommands = function (object) {
  118. this.sendMessage(object);
  119. };
  120. /**
  121. * Processes message events sent from Jitsi Meet
  122. * @param event the event
  123. */
  124. JitsiMeetExternalAPI.prototype.processMessage = function(event)
  125. {
  126. var message;
  127. try {
  128. message = JSON.parse(event.data);
  129. } catch (e) {}
  130. if(message.loaded)
  131. {
  132. this.onFrameLoaded();
  133. }
  134. };
  135. /**
  136. * That method is called when the Jitsi Meet is loaded. Executes saved
  137. * commands that are send before the frame was loaded.
  138. */
  139. JitsiMeetExternalAPI.prototype.onFrameLoaded = function () {
  140. this.frameLoaded = true;
  141. for (var i = 0; i < this.initialCommands.length; i++)
  142. {
  143. this.sendMessage(this.initialCommands[i]);
  144. }
  145. this.initialCommands = null;
  146. };
  147. /**
  148. * Setups the listener for message events from Jitsi Meet.
  149. */
  150. JitsiMeetExternalAPI.prototype.initListeners = function () {
  151. var self = this;
  152. this.eventListener = function (event) {
  153. self.processMessage(event);
  154. };
  155. if (window.addEventListener)
  156. {
  157. window.addEventListener('message',
  158. this.eventListener, false);
  159. }
  160. else
  161. {
  162. window.attachEvent('onmessage', this.eventListener);
  163. }
  164. };
  165. /**
  166. * Removes the listeners and removes the Jitsi Meet frame.
  167. */
  168. JitsiMeetExternalAPI.prototype.dispose = function () {
  169. if (window.removeEventListener)
  170. {
  171. window.removeEventListener('message',
  172. this.eventListener, false);
  173. }
  174. else
  175. {
  176. window.detachEvent('onmessage',
  177. this.eventListener);
  178. }
  179. this.iframeHolder.parentNode.removeChild(this.iframeHolder);
  180. };
  181. return JitsiMeetExternalAPI;
  182. })();