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.

etherpad.js 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /* global $, config, connection, dockToolbar, Moderator, Prezi,
  2. setLargeVideoVisible, ToolbarToggler, Util, VideoLayout */
  3. var Etherpad = (function (my) {
  4. var etherpadName = null;
  5. var etherpadIFrame = null;
  6. var domain = null;
  7. var options = "?showControls=true&showChat=false&showLineNumbers=true&useMonospaceFont=false";
  8. /**
  9. * Initializes the etherpad.
  10. */
  11. my.init = function (name) {
  12. if (config.etherpad_base && !etherpadName) {
  13. domain = config.etherpad_base;
  14. if (!name) {
  15. // In case we're the focus we generate the name.
  16. etherpadName = Math.random().toString(36).substring(7) +
  17. '_' + (new Date().getTime()).toString();
  18. shareEtherpad();
  19. }
  20. else
  21. etherpadName = name;
  22. enableEtherpadButton();
  23. }
  24. };
  25. /**
  26. * Opens/hides the Etherpad.
  27. */
  28. my.toggleEtherpad = function (isPresentation) {
  29. if (!etherpadIFrame)
  30. createIFrame();
  31. var largeVideo = null;
  32. if (Prezi.isPresentationVisible())
  33. largeVideo = $('#presentation>iframe');
  34. else
  35. largeVideo = $('#largeVideo');
  36. if ($('#etherpad>iframe').css('visibility') === 'hidden') {
  37. $('#activeSpeaker').css('visibility', 'hidden');
  38. largeVideo.fadeOut(300, function () {
  39. if (Prezi.isPresentationVisible()) {
  40. largeVideo.css({opacity: '0'});
  41. } else {
  42. VideoLayout.setLargeVideoVisible(false);
  43. }
  44. });
  45. $('#etherpad>iframe').fadeIn(300, function () {
  46. document.body.style.background = '#eeeeee';
  47. $('#etherpad>iframe').css({visibility: 'visible'});
  48. $('#etherpad').css({zIndex: 2});
  49. });
  50. }
  51. else if ($('#etherpad>iframe')) {
  52. $('#etherpad>iframe').fadeOut(300, function () {
  53. $('#etherpad>iframe').css({visibility: 'hidden'});
  54. $('#etherpad').css({zIndex: 0});
  55. document.body.style.background = 'black';
  56. });
  57. if (!isPresentation) {
  58. $('#largeVideo').fadeIn(300, function () {
  59. VideoLayout.setLargeVideoVisible(true);
  60. });
  61. }
  62. }
  63. resize();
  64. };
  65. my.isVisible = function() {
  66. var etherpadIframe = $('#etherpad>iframe');
  67. return etherpadIframe && etherpadIframe.is(':visible');
  68. };
  69. /**
  70. * Resizes the etherpad.
  71. */
  72. function resize() {
  73. if ($('#etherpad>iframe').length) {
  74. var remoteVideos = $('#remoteVideos');
  75. var availableHeight
  76. = window.innerHeight - remoteVideos.outerHeight();
  77. var availableWidth = Util.getAvailableVideoWidth();
  78. $('#etherpad>iframe').width(availableWidth);
  79. $('#etherpad>iframe').height(availableHeight);
  80. }
  81. }
  82. /**
  83. * Shares the Etherpad name with other participants.
  84. */
  85. function shareEtherpad() {
  86. connection.emuc.addEtherpadToPresence(etherpadName);
  87. connection.emuc.sendPresence();
  88. }
  89. /**
  90. * Creates the Etherpad button and adds it to the toolbar.
  91. */
  92. function enableEtherpadButton() {
  93. if (!$('#etherpadButton').is(":visible"))
  94. $('#etherpadButton').css({display: 'inline-block'});
  95. }
  96. /**
  97. * Creates the IFrame for the etherpad.
  98. */
  99. function createIFrame() {
  100. etherpadIFrame = document.createElement('iframe');
  101. etherpadIFrame.src = domain + etherpadName + options;
  102. etherpadIFrame.frameBorder = 0;
  103. etherpadIFrame.scrolling = "no";
  104. etherpadIFrame.width = $('#largeVideoContainer').width() || 640;
  105. etherpadIFrame.height = $('#largeVideoContainer').height() || 480;
  106. etherpadIFrame.setAttribute('style', 'visibility: hidden;');
  107. document.getElementById('etherpad').appendChild(etherpadIFrame);
  108. etherpadIFrame.onload = function() {
  109. document.domain = document.domain;
  110. bubbleIframeMouseMove(etherpadIFrame);
  111. setTimeout(function() {
  112. //the iframes inside of the etherpad are not yet loaded when the etherpad iframe is loaded
  113. var outer = etherpadIFrame.contentDocument.getElementsByName("ace_outer")[0];
  114. bubbleIframeMouseMove(outer);
  115. var inner = outer.contentDocument.getElementsByName("ace_inner")[0];
  116. bubbleIframeMouseMove(inner);
  117. }, 2000);
  118. };
  119. }
  120. function bubbleIframeMouseMove(iframe){
  121. var existingOnMouseMove = iframe.contentWindow.onmousemove;
  122. iframe.contentWindow.onmousemove = function(e){
  123. if(existingOnMouseMove) existingOnMouseMove(e);
  124. var evt = document.createEvent("MouseEvents");
  125. var boundingClientRect = iframe.getBoundingClientRect();
  126. evt.initMouseEvent(
  127. "mousemove",
  128. true, // bubbles
  129. false, // not cancelable
  130. window,
  131. e.detail,
  132. e.screenX,
  133. e.screenY,
  134. e.clientX + boundingClientRect.left,
  135. e.clientY + boundingClientRect.top,
  136. e.ctrlKey,
  137. e.altKey,
  138. e.shiftKey,
  139. e.metaKey,
  140. e.button,
  141. null // no related element
  142. );
  143. iframe.dispatchEvent(evt);
  144. };
  145. }
  146. /**
  147. * On Etherpad added to muc.
  148. */
  149. $(document).bind('etherpadadded.muc', function (event, jid, etherpadName) {
  150. console.log("Etherpad added", etherpadName);
  151. if (config.etherpad_base && !Moderator.isModerator()) {
  152. Etherpad.init(etherpadName);
  153. }
  154. });
  155. /**
  156. * On focus changed event.
  157. */
  158. // FIXME: there is no such event as 'focusechanged.muc'
  159. $(document).bind('focusechanged.muc', function (event, focus) {
  160. console.log("Focus changed");
  161. if (config.etherpad_base)
  162. shareEtherpad();
  163. });
  164. /**
  165. * On video selected event.
  166. */
  167. $(document).bind('video.selected', function (event, isPresentation) {
  168. if (!config.etherpad_base)
  169. return;
  170. if (etherpadIFrame && etherpadIFrame.style.visibility !== 'hidden')
  171. Etherpad.toggleEtherpad(isPresentation);
  172. });
  173. /**
  174. * Resizes the etherpad, when the window is resized.
  175. */
  176. $(window).resize(function () {
  177. resize();
  178. });
  179. return my;
  180. }(Etherpad || {}));