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.

UI.js 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  1. /* global Strophe, APP, $, config, interfaceConfig, toastr */
  2. /* jshint -W101 */
  3. var UI = {};
  4. var VideoLayout = require("./videolayout/VideoLayout.js");
  5. var AudioLevels = require("./audio_levels/AudioLevels.js");
  6. var Prezi = require("./prezi/Prezi.js");
  7. var Etherpad = require("./etherpad/Etherpad.js");
  8. var Chat = require("./side_pannels/chat/Chat.js");
  9. var Toolbar = require("./toolbars/Toolbar");
  10. var ToolbarToggler = require("./toolbars/ToolbarToggler");
  11. var BottomToolbar = require("./toolbars/BottomToolbar");
  12. var ContactList = require("./side_pannels/contactlist/ContactList");
  13. var Avatar = require("./avatar/Avatar");
  14. var EventEmitter = require("events");
  15. var SettingsMenu = require("./side_pannels/settings/SettingsMenu");
  16. var Settings = require("./../settings/Settings");
  17. var PanelToggler = require("./side_pannels/SidePanelToggler");
  18. var RoomnameGenerator = require("../util/RoomnameGenerator");
  19. UI.messageHandler = require("./util/MessageHandler");
  20. var messageHandler = UI.messageHandler;
  21. var Authentication = require("./authentication/Authentication");
  22. var UIUtil = require("./util/UIUtil");
  23. var JitsiPopover = require("./util/JitsiPopover");
  24. var CQEvents = require("../../service/connectionquality/CQEvents");
  25. var DesktopSharingEventTypes
  26. = require("../../service/desktopsharing/DesktopSharingEventTypes");
  27. var StatisticsEvents = require("../../service/statistics/Events");
  28. var UIEvents = require("../../service/UI/UIEvents");
  29. var Feedback = require("./Feedback");
  30. var eventEmitter = new EventEmitter();
  31. UI.eventEmitter = eventEmitter;
  32. var roomNode = null;
  33. var roomName = null;
  34. function promptDisplayName() {
  35. var message = '<h2 data-i18n="dialog.displayNameRequired">';
  36. message += APP.translation.translateString(
  37. "dialog.displayNameRequired");
  38. message += '</h2>' +
  39. '<input name="displayName" type="text" data-i18n=' +
  40. '"[placeholder]defaultNickname" placeholder="' +
  41. APP.translation.translateString(
  42. "defaultNickname", {name: "Jane Pink"}) +
  43. '" autofocus>';
  44. var buttonTxt
  45. = APP.translation.generateTranslationHTML("dialog.Ok");
  46. var buttons = [];
  47. buttons.push({title: buttonTxt, value: "ok"});
  48. messageHandler.openDialog(null, message,
  49. true,
  50. buttons,
  51. function (e, v, m, f) {
  52. if (v == "ok") {
  53. var displayName = f.displayName;
  54. if (displayName) {
  55. UI.inputDisplayNameHandler(displayName);
  56. return true;
  57. }
  58. }
  59. e.preventDefault();
  60. },
  61. function () {
  62. var form = $.prompt.getPrompt();
  63. var input = form.find("input[name='displayName']");
  64. input.focus();
  65. var button = form.find("button");
  66. button.attr("disabled", "disabled");
  67. input.keyup(function () {
  68. if(!input.val())
  69. button.attr("disabled", "disabled");
  70. else
  71. button.removeAttr("disabled");
  72. });
  73. }
  74. );
  75. }
  76. function setupPrezi() {
  77. $("#reloadPresentationLink").click(function() {
  78. Prezi.reloadPresentation();
  79. });
  80. }
  81. function setupChat() {
  82. Chat.init(eventEmitter);
  83. $("#toggle_smileys").click(function() {
  84. Chat.toggleSmileys();
  85. });
  86. }
  87. function setupToolbars() {
  88. Toolbar.init(eventEmitter);
  89. Toolbar.setupButtonsFromConfig();
  90. BottomToolbar.init(eventEmitter);
  91. }
  92. UI.notifyGracefulShudown = function () {
  93. messageHandler.openMessageDialog(
  94. 'dialog.serviceUnavailable',
  95. 'dialog.gracefulShutdown'
  96. );
  97. };
  98. UI.notifyReservationError = function (code, msg) {
  99. var title = APP.translation.generateTranslationHTML(
  100. "dialog.reservationError");
  101. var message = APP.translation.generateTranslationHTML(
  102. "dialog.reservationErrorMsg", {code: code, msg: msg});
  103. messageHandler.openDialog(
  104. title,
  105. message,
  106. true, {},
  107. function (event, value, message, formVals) {
  108. return false;
  109. }
  110. );
  111. };
  112. UI.notifyKicked = function () {
  113. messageHandler.openMessageDialog("dialog.sessTerminated", "dialog.kickMessage");
  114. };
  115. UI.notifyBridgeDown = function () {
  116. messageHandler.showError("dialog.error", "dialog.bridgeUnavailable");
  117. };
  118. UI.changeDisplayName = function (id, displayName) {
  119. ContactList.onDisplayNameChange(id, displayName);
  120. SettingsMenu.onDisplayNameChange(id, displayName);
  121. VideoLayout.onDisplayNameChanged(id, displayName);
  122. };
  123. UI.initConference = function () {
  124. var id = APP.conference.localId;
  125. Toolbar.updateRoomUrl(window.location.href);
  126. var meHTML = APP.translation.generateTranslationHTML("me");
  127. var settings = Settings.getSettings();
  128. $("#localNick").html(settings.email || settings.uid + " (" + meHTML + ")");
  129. // Make sure we configure our avatar id, before creating avatar for us
  130. UI.setUserAvatar(id, settings.email || settings.uid);
  131. // Add myself to the contact list.
  132. ContactList.addContact(id);
  133. // Once we've joined the muc show the toolbar
  134. ToolbarToggler.showToolbar();
  135. var displayName = config.displayJids ? id : settings.displayName;
  136. if (displayName) {
  137. UI.changeDisplayName('localVideoContainer', displayName);
  138. }
  139. VideoLayout.mucJoined();
  140. Toolbar.checkAutoEnableDesktopSharing();
  141. };
  142. function registerListeners() {
  143. UI.addListener(UIEvents.LARGEVIDEO_INIT, function () {
  144. AudioLevels.init();
  145. });
  146. UI.addListener(UIEvents.FILM_STRIP_TOGGLED, function (isToggled) {
  147. VideoLayout.onFilmStripToggled(isToggled);
  148. });
  149. UI.addListener(UIEvents.EMAIL_CHANGED, function (email) {
  150. UI.setUserAvatar(APP.conference.localId, email);
  151. });
  152. }
  153. function bindEvents() {
  154. function onResize() {
  155. Chat.resizeChat();
  156. VideoLayout.resizeLargeVideoContainer();
  157. }
  158. // Resize and reposition videos in full screen mode.
  159. $(document).on(
  160. 'webkitfullscreenchange mozfullscreenchange fullscreenchange', onResize
  161. );
  162. $(window).resize(onResize);
  163. }
  164. UI.start = function () {
  165. document.title = interfaceConfig.APP_NAME;
  166. var setupWelcomePage = null;
  167. if(config.enableWelcomePage && window.location.pathname == "/" &&
  168. (!window.localStorage.welcomePageDisabled ||
  169. window.localStorage.welcomePageDisabled == "false")) {
  170. $("#videoconference_page").hide();
  171. if (!setupWelcomePage)
  172. setupWelcomePage = require("./welcome_page/WelcomePage");
  173. setupWelcomePage();
  174. return;
  175. }
  176. $("#welcome_page").hide();
  177. // Set the defaults for prompt dialogs.
  178. $.prompt.setDefaults({persistent: false});
  179. registerListeners();
  180. VideoLayout.init(eventEmitter);
  181. bindEvents();
  182. setupPrezi();
  183. if (!interfaceConfig.filmStripOnly) {
  184. $("#videospace").mousemove(function () {
  185. return ToolbarToggler.showToolbar();
  186. });
  187. setupToolbars();
  188. setupChat();
  189. // Display notice message at the top of the toolbar
  190. if (config.noticeMessage) {
  191. $('#noticeText').text(config.noticeMessage);
  192. $('#notice').css({display: 'block'});
  193. }
  194. $("#downloadlog").click(function (event) {
  195. // dump(event.target);
  196. // FIXME integrate logs
  197. });
  198. Feedback.init();
  199. } else {
  200. $("#header").css("display", "none");
  201. $("#bottomToolbar").css("display", "none");
  202. $("#downloadlog").css("display", "none");
  203. $("#remoteVideos").css("padding", "0px 0px 18px 0px");
  204. $("#remoteVideos").css("right", "0px");
  205. messageHandler.disableNotifications();
  206. $('body').popover("disable");
  207. JitsiPopover.enabled = false;
  208. }
  209. document.title = interfaceConfig.APP_NAME;
  210. if(config.requireDisplayName) {
  211. if (APP.settings.getDisplayName()) {
  212. promptDisplayName();
  213. }
  214. }
  215. if (!interfaceConfig.filmStripOnly) {
  216. toastr.options = {
  217. "closeButton": true,
  218. "debug": false,
  219. "positionClass": "notification-bottom-right",
  220. "onclick": null,
  221. "showDuration": "300",
  222. "hideDuration": "1000",
  223. "timeOut": "2000",
  224. "extendedTimeOut": "1000",
  225. "showEasing": "swing",
  226. "hideEasing": "linear",
  227. "showMethod": "fadeIn",
  228. "hideMethod": "fadeOut",
  229. "reposition": function () {
  230. if (PanelToggler.isVisible()) {
  231. $("#toast-container").addClass("notification-bottom-right-center");
  232. } else {
  233. $("#toast-container").removeClass("notification-bottom-right-center");
  234. }
  235. },
  236. "newestOnTop": false
  237. };
  238. SettingsMenu.init();
  239. }
  240. };
  241. UI.addLocalStream = function (stream, isMuted) {
  242. switch (stream.type) {
  243. case 'audio':
  244. VideoLayout.changeLocalAudio(stream, isMuted);
  245. break;
  246. case 'video':
  247. VideoLayout.changeLocalVideo(stream, isMuted);
  248. break;
  249. default:
  250. console.error("Unknown stream type: " + stream.type);
  251. break;
  252. }
  253. };
  254. UI.addRemoteStream = function (stream) {
  255. VideoLayout.onRemoteStreamAdded(stream);
  256. };
  257. function chatAddError(errorMessage, originalText) {
  258. return Chat.chatAddError(errorMessage, originalText);
  259. }
  260. function chatSetSubject(text) {
  261. return Chat.chatSetSubject(text);
  262. }
  263. function initEtherpad(name) {
  264. Etherpad.init(name);
  265. }
  266. function onLocalRoleChanged(jid, info, pres, isModerator) {
  267. console.info("My role changed, new role: " + info.role);
  268. onModeratorStatusChanged(isModerator);
  269. VideoLayout.showModeratorIndicator();
  270. SettingsMenu.onRoleChanged();
  271. if (isModerator) {
  272. Authentication.closeAuthenticationWindow();
  273. messageHandler.notify(null, "notify.me",
  274. 'connected', "notify.moderator");
  275. Toolbar.checkAutoRecord();
  276. }
  277. }
  278. function onModeratorStatusChanged(isModerator) {
  279. Toolbar.showSipCallButton(isModerator);
  280. Toolbar.showRecordingButton(
  281. isModerator); //&&
  282. // FIXME:
  283. // Recording visible if
  284. // there are at least 2(+ 1 focus) participants
  285. //Object.keys(connection.emuc.members).length >= 3);
  286. }
  287. UI.notifyPasswordRequired = function (callback) {
  288. // password is required
  289. Toolbar.lockLockButton();
  290. var message = '<h2 data-i18n="dialog.passwordRequired">';
  291. message += APP.translation.translateString(
  292. "dialog.passwordRequired");
  293. message += '</h2>' +
  294. '<input name="lockKey" type="text" data-i18n=' +
  295. '"[placeholder]dialog.password" placeholder="' +
  296. APP.translation.translateString("dialog.password") +
  297. '" autofocus>';
  298. messageHandler.openTwoButtonDialog(null, null, null, message,
  299. true,
  300. "dialog.Ok",
  301. function (e, v, m, f) {},
  302. null,
  303. function (e, v, m, f) {
  304. if (v) {
  305. var lockKey = f.lockKey;
  306. if (lockKey) {
  307. Toolbar.setSharedKey(lockKey);
  308. callback(lockKey);
  309. }
  310. }
  311. },
  312. ':input:first'
  313. );
  314. };
  315. /**
  316. * The dialpad button is shown iff there is at least one member that supports
  317. * DTMF (e.g. jigasi).
  318. */
  319. function onDtmfSupportChanged(dtmfSupport) {
  320. //TODO: enable when the UI is ready
  321. //Toolbar.showDialPadButton(dtmfSupport);
  322. }
  323. UI.addUser = function (jid, id, displayName) {
  324. messageHandler.notify(
  325. displayName,'notify.somebody', 'connected', 'notify.connected'
  326. );
  327. if (!config.startAudioMuted ||
  328. config.startAudioMuted > APP.conference.membersCount)
  329. UIUtil.playSoundNotification('userJoined');
  330. // Configure avatar
  331. UI.setUserAvatar(jid, id);
  332. // Add Peer's container
  333. VideoLayout.ensurePeerContainerExists(jid);
  334. };
  335. UI.removeUser = function (jid) {
  336. console.log('left.muc', jid);
  337. var displayName = $('#participant_' + Strophe.getResourceFromJid(jid) +
  338. '>.displayname').html();
  339. messageHandler.notify(displayName,'notify.somebody',
  340. 'disconnected',
  341. 'notify.disconnected');
  342. if (!config.startAudioMuted ||
  343. config.startAudioMuted > APP.conference.membersCount) {
  344. UIUtil.playSoundNotification('userLeft');
  345. }
  346. ContactList.removeContact(jid);
  347. VideoLayout.participantLeft(jid);
  348. };
  349. function onMucPresenceStatus(jid, info) {
  350. VideoLayout.setPresenceStatus(Strophe.getResourceFromJid(jid), info.status);
  351. }
  352. function onPeerVideoTypeChanged(resourceJid, newVideoType) {
  353. VideoLayout.onVideoTypeChanged(resourceJid, newVideoType);
  354. }
  355. function onMucRoleChanged(role, displayName) {
  356. VideoLayout.showModeratorIndicator();
  357. if (role === 'moderator') {
  358. var messageKey, messageOptions = {};
  359. if (!displayName) {
  360. messageKey = "notify.grantedToUnknown";
  361. }
  362. else {
  363. messageKey = "notify.grantedTo";
  364. messageOptions = {to: displayName};
  365. }
  366. messageHandler.notify(
  367. displayName,'notify.somebody',
  368. 'connected', messageKey,
  369. messageOptions);
  370. }
  371. }
  372. UI.notifyAuthRequired = function (intervalCallback) {
  373. Authentication.openAuthenticationDialog(
  374. roomName, intervalCallback, function () {
  375. Toolbar.authenticateClicked();
  376. }
  377. );
  378. };
  379. UI.toggleSmileys = function () {
  380. Chat.toggleSmileys();
  381. };
  382. UI.getSettings = function () {
  383. return Settings.getSettings();
  384. };
  385. UI.toggleFilmStrip = function () {
  386. return BottomToolbar.toggleFilmStrip();
  387. };
  388. UI.toggleChat = function () {
  389. return BottomToolbar.toggleChat();
  390. };
  391. UI.toggleContactList = function () {
  392. return BottomToolbar.toggleContactList();
  393. };
  394. UI.inputDisplayNameHandler = function (value) {
  395. VideoLayout.inputDisplayNameHandler(value);
  396. };
  397. /**
  398. * Return the type of the remote video.
  399. * @param jid the jid for the remote video
  400. * @returns the video type video or screen.
  401. */
  402. UI.getRemoteVideoType = function (jid) {
  403. return VideoLayout.getRemoteVideoType(jid);
  404. };
  405. UI.getRoomNode = function () {
  406. if (roomNode)
  407. return roomNode;
  408. var path = window.location.pathname;
  409. // determinde the room node from the url
  410. // TODO: just the roomnode or the whole bare jid?
  411. if (config.getroomnode && typeof config.getroomnode === 'function') {
  412. // custom function might be responsible for doing the pushstate
  413. roomNode = config.getroomnode(path);
  414. } else {
  415. /* fall back to default strategy
  416. * this is making assumptions about how the URL->room mapping happens.
  417. * It currently assumes deployment at root, with a rewrite like the
  418. * following one (for nginx):
  419. location ~ ^/([a-zA-Z0-9]+)$ {
  420. rewrite ^/(.*)$ / break;
  421. }
  422. */
  423. if (path.length > 1) {
  424. roomNode = path.substr(1).toLowerCase();
  425. } else {
  426. var word = RoomnameGenerator.generateRoomWithoutSeparator();
  427. roomNode = word.toLowerCase();
  428. window.history.pushState('VideoChat',
  429. 'Room: ' + word, window.location.pathname + word);
  430. }
  431. }
  432. return roomNode;
  433. };
  434. UI.generateRoomName = function () {
  435. if (roomName)
  436. return roomName;
  437. var roomNode = UI.getRoomNode();
  438. roomName = roomNode + '@' + config.hosts.muc;
  439. return roomName;
  440. };
  441. UI.connectionIndicatorShowMore = function(jid) {
  442. return VideoLayout.showMore(jid);
  443. };
  444. UI.showLoginPopup = function(callback) {
  445. console.log('password is required');
  446. var message = '<h2 data-i18n="dialog.passwordRequired">';
  447. message += APP.translation.translateString(
  448. "dialog.passwordRequired");
  449. message += '</h2>' +
  450. '<input name="username" type="text" ' +
  451. 'placeholder="user@domain.net" autofocus>' +
  452. '<input name="password" ' +
  453. 'type="password" data-i18n="[placeholder]dialog.userPassword"' +
  454. ' placeholder="user password">';
  455. UI.messageHandler.openTwoButtonDialog(null, null, null, message,
  456. true,
  457. "dialog.Ok",
  458. function (e, v, m, f) {
  459. if (v) {
  460. if (f.username && f.password) {
  461. callback(f.username, f.password);
  462. }
  463. }
  464. },
  465. null, null, ':input:first'
  466. );
  467. };
  468. UI.closeAuthenticationDialog = function () {
  469. Authentication.closeAuthenticationDialog();
  470. Authentication.stopInterval();
  471. };
  472. UI.askForNickname = function () {
  473. return window.prompt('Your nickname (optional)');
  474. };
  475. UI.getRoomName = function () {
  476. return roomName;
  477. };
  478. /**
  479. * Sets muted audio state for the local participant.
  480. */
  481. UI.setAudioMuted = function (mute) {
  482. VideoLayout.showLocalAudioIndicator(mute);
  483. UIUtil.buttonClick("#toolbar_button_mute", "icon-microphone icon-mic-disabled");
  484. };
  485. UI.setVideoMuted = function (muted) {
  486. $('#toolbar_button_camera').toggleClass("icon-camera-disabled", muted);
  487. };
  488. UI.addListener = function (type, listener) {
  489. eventEmitter.on(type, listener);
  490. };
  491. UI.clickOnVideo = function (videoNumber) {
  492. var remoteVideos = $(".videocontainer:not(#mixedstream)");
  493. if (remoteVideos.length > videoNumber) {
  494. remoteVideos[videoNumber].click();
  495. }
  496. };
  497. //Used by torture
  498. UI.showToolbar = function () {
  499. return ToolbarToggler.showToolbar();
  500. };
  501. //Used by torture
  502. UI.dockToolbar = function (isDock) {
  503. return ToolbarToggler.dockToolbar(isDock);
  504. };
  505. UI.setUserAvatar = function (id, email) {
  506. // update avatar
  507. Avatar.setUserAvatar(id, email);
  508. var thumbUrl = Avatar.getThumbUrl(id);
  509. var contactListUrl = Avatar.getContactListUrl(id);
  510. VideoLayout.changeUserAvatar(id, thumbUrl);
  511. ContactList.changeUserAvatar(id, contactListUrl);
  512. if (APP.conference.isLocalId(id)) {
  513. SettingsMenu.changeAvatar(thumbUrl);
  514. }
  515. };
  516. UI.notifyConnectionFailed = function (stropheErrorMsg) {
  517. var title = APP.translation.generateTranslationHTML(
  518. "dialog.error");
  519. var message;
  520. if (stropheErrorMsg) {
  521. message = APP.translation.generateTranslationHTML(
  522. "dialog.connectErrorWithMsg", {msg: stropheErrorMsg});
  523. } else {
  524. message = APP.translation.generateTranslationHTML(
  525. "dialog.connectError");
  526. }
  527. messageHandler.openDialog(
  528. title, message, true, {}, function (e, v, m, f) { return false; }
  529. );
  530. };
  531. UI.notifyFirefoxExtensionRequired = function (url) {
  532. messageHandler.openMessageDialog(
  533. "dialog.extensionRequired",
  534. null,
  535. null,
  536. APP.translation.generateTranslationHTML(
  537. "dialog.firefoxExtensionPrompt", {url: url}
  538. )
  539. );
  540. };
  541. UI.notifyInitiallyMuted = function () {
  542. messageHandler.notify(
  543. null, "notify.mutedTitle", "connected", "notify.muted", null, {timeOut: 120000}
  544. );
  545. };
  546. UI.markDominantSpiker = function (id) {
  547. VideoLayout.onDominantSpeakerChanged(id);
  548. };
  549. UI.handleLastNEndpoints = function (ids) {
  550. VideoLayout.onLastNEndpointsChanged(ids, []);
  551. };
  552. UI.setAudioLevel = function (targetJid, lvl) {
  553. AudioLevels.updateAudioLevel(
  554. targetJid, lvl, VideoLayout.getLargeVideoResource()
  555. );
  556. };
  557. UI.updateDesktopSharingButtons = function () {
  558. Toolbar.changeDesktopSharingButtonState();
  559. };
  560. UI.hideStats = function () {
  561. VideoLayout.hideStats();
  562. };
  563. UI.updateLocalStats = function (percent, stats) {
  564. VideoLayout.updateLocalConnectionStats(percent, stats);
  565. };
  566. UI.updateRemoteStats = function (jid, percent, stats) {
  567. VideoLayout.updateConnectionStats(jid, percent, stats);
  568. };
  569. UI.showAuthenticateButton = function (show) {
  570. Toolbar.showAuthenticateButton(show);
  571. };
  572. UI.markVideoInterrupted = function (interrupted) {
  573. if (interrupted) {
  574. VideoLayout.onVideoInterrupted();
  575. } else {
  576. VideoLayout.onVideoRestored();
  577. }
  578. };
  579. UI.addMessage = function (from, displayName, message, stamp) {
  580. Chat.updateChatConversation(from, displayName, message, stamp);
  581. };
  582. module.exports = UI;