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.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /* global $, APP, interfaceConfig */
  2. import { setDocumentEditingState } from '../../../react/features/etherpad';
  3. import { getToolboxHeight } from '../../../react/features/toolbox';
  4. import VideoLayout from '../videolayout/VideoLayout';
  5. import LargeContainer from '../videolayout/LargeContainer';
  6. import UIEvents from '../../../service/UI/UIEvents';
  7. import Filmstrip from '../videolayout/Filmstrip';
  8. /**
  9. * Etherpad options.
  10. */
  11. const options = $.param({
  12. showControls: true,
  13. showChat: false,
  14. showLineNumbers: true,
  15. useMonospaceFont: false
  16. });
  17. /**
  18. *
  19. */
  20. function bubbleIframeMouseMove(iframe) {
  21. const existingOnMouseMove = iframe.contentWindow.onmousemove;
  22. iframe.contentWindow.onmousemove = function(e) {
  23. if (existingOnMouseMove) {
  24. existingOnMouseMove(e);
  25. }
  26. const evt = document.createEvent('MouseEvents');
  27. const boundingClientRect = iframe.getBoundingClientRect();
  28. evt.initMouseEvent(
  29. 'mousemove',
  30. true, // bubbles
  31. false, // not cancelable
  32. window,
  33. e.detail,
  34. e.screenX,
  35. e.screenY,
  36. e.clientX + boundingClientRect.left,
  37. e.clientY + boundingClientRect.top,
  38. e.ctrlKey,
  39. e.altKey,
  40. e.shiftKey,
  41. e.metaKey,
  42. e.button,
  43. null // no related element
  44. );
  45. iframe.dispatchEvent(evt);
  46. };
  47. }
  48. /**
  49. * Default Etherpad frame width.
  50. */
  51. const DEFAULT_WIDTH = 640;
  52. /**
  53. * Default Etherpad frame height.
  54. */
  55. const DEFAULT_HEIGHT = 480;
  56. const ETHERPAD_CONTAINER_TYPE = 'etherpad';
  57. /**
  58. * Container for Etherpad iframe.
  59. */
  60. class Etherpad extends LargeContainer {
  61. /**
  62. * Creates new Etherpad object
  63. */
  64. constructor(domain, name) {
  65. super();
  66. const iframe = document.createElement('iframe');
  67. iframe.id = 'etherpadIFrame';
  68. iframe.src = `${domain + name}?${options}`;
  69. iframe.frameBorder = 0;
  70. iframe.scrolling = 'no';
  71. iframe.width = DEFAULT_WIDTH;
  72. iframe.height = DEFAULT_HEIGHT;
  73. iframe.setAttribute('style', 'visibility: hidden;');
  74. this.container.appendChild(iframe);
  75. iframe.onload = function() {
  76. // eslint-disable-next-line no-self-assign
  77. document.domain = document.domain;
  78. bubbleIframeMouseMove(iframe);
  79. setTimeout(() => {
  80. const doc = iframe.contentDocument;
  81. // the iframes inside of the etherpad are
  82. // not yet loaded when the etherpad iframe is loaded
  83. const outer = doc.getElementsByName('ace_outer')[0];
  84. bubbleIframeMouseMove(outer);
  85. const inner = doc.getElementsByName('ace_inner')[0];
  86. bubbleIframeMouseMove(inner);
  87. }, 2000);
  88. };
  89. this.iframe = iframe;
  90. }
  91. /**
  92. *
  93. */
  94. get isOpen() {
  95. return Boolean(this.iframe);
  96. }
  97. /**
  98. *
  99. */
  100. get container() {
  101. return document.getElementById('etherpad');
  102. }
  103. /**
  104. *
  105. */
  106. resize(containerWidth, containerHeight) {
  107. let height, width;
  108. if (interfaceConfig.VERTICAL_FILMSTRIP) {
  109. height = containerHeight - getToolboxHeight();
  110. width = containerWidth - Filmstrip.getFilmstripWidth();
  111. } else {
  112. height = containerHeight - Filmstrip.getFilmstripHeight();
  113. width = containerWidth;
  114. }
  115. $(this.iframe)
  116. .width(width)
  117. .height(height);
  118. }
  119. /**
  120. *
  121. */
  122. show() {
  123. const $iframe = $(this.iframe);
  124. const $container = $(this.container);
  125. const self = this;
  126. return new Promise(resolve => {
  127. $iframe.fadeIn(300, () => {
  128. self.bodyBackground = document.body.style.background;
  129. document.body.style.background = '#eeeeee';
  130. $iframe.css({ visibility: 'visible' });
  131. $container.css({ zIndex: 2 });
  132. APP.store.dispatch(setDocumentEditingState(true));
  133. resolve();
  134. });
  135. });
  136. }
  137. /**
  138. *
  139. */
  140. hide() {
  141. const $iframe = $(this.iframe);
  142. const $container = $(this.container);
  143. document.body.style.background = this.bodyBackground;
  144. return new Promise(resolve => {
  145. $iframe.fadeOut(300, () => {
  146. $iframe.css({ visibility: 'hidden' });
  147. $container.css({ zIndex: 0 });
  148. APP.store.dispatch(setDocumentEditingState(false));
  149. resolve();
  150. });
  151. });
  152. }
  153. /**
  154. * @return {boolean} do not switch on dominant speaker event if on stage.
  155. */
  156. stayOnStage() {
  157. return true;
  158. }
  159. }
  160. /**
  161. * Manager of the Etherpad frame.
  162. */
  163. export default class EtherpadManager {
  164. /**
  165. *
  166. */
  167. constructor(domain, name, eventEmitter) {
  168. if (!domain || !name) {
  169. throw new Error('missing domain or name');
  170. }
  171. this.domain = domain;
  172. this.name = name;
  173. this.eventEmitter = eventEmitter;
  174. this.etherpad = null;
  175. }
  176. /**
  177. *
  178. */
  179. get isOpen() {
  180. return Boolean(this.etherpad);
  181. }
  182. /**
  183. *
  184. */
  185. isVisible() {
  186. return VideoLayout.isLargeContainerTypeVisible(ETHERPAD_CONTAINER_TYPE);
  187. }
  188. /**
  189. * Create new Etherpad frame.
  190. */
  191. openEtherpad() {
  192. this.etherpad = new Etherpad(this.domain, this.name);
  193. VideoLayout.addLargeVideoContainer(
  194. ETHERPAD_CONTAINER_TYPE,
  195. this.etherpad
  196. );
  197. }
  198. /**
  199. * Toggle Etherpad frame visibility.
  200. * Open new Etherpad frame if there is no Etherpad frame yet.
  201. */
  202. toggleEtherpad() {
  203. if (!this.isOpen) {
  204. this.openEtherpad();
  205. }
  206. const isVisible = this.isVisible();
  207. VideoLayout.showLargeVideoContainer(
  208. ETHERPAD_CONTAINER_TYPE, !isVisible);
  209. this.eventEmitter
  210. .emit(UIEvents.TOGGLED_SHARED_DOCUMENT, !isVisible);
  211. APP.store.dispatch(setDocumentEditingState(!isVisible));
  212. }
  213. }