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 updateChatConversation(from, displayName, message, stamp) {
  264. return Chat.updateChatConversation(from, displayName, message, stamp);
  265. }
  266. function initEtherpad(name) {
  267. Etherpad.init(name);
  268. }
  269. function onLocalRoleChanged(jid, info, pres, isModerator) {
  270. console.info("My role changed, new role: " + info.role);
  271. onModeratorStatusChanged(isModerator);
  272. VideoLayout.showModeratorIndicator();
  273. SettingsMenu.onRoleChanged();
  274. if (isModerator) {
  275. Authentication.closeAuthenticationWindow();
  276. messageHandler.notify(null, "notify.me",
  277. 'connected', "notify.moderator");
  278. Toolbar.checkAutoRecord();
  279. }
  280. }
  281. function onModeratorStatusChanged(isModerator) {
  282. Toolbar.showSipCallButton(isModerator);
  283. Toolbar.showRecordingButton(
  284. isModerator); //&&
  285. // FIXME:
  286. // Recording visible if
  287. // there are at least 2(+ 1 focus) participants
  288. //Object.keys(connection.emuc.members).length >= 3);
  289. }
  290. UI.notifyPasswordRequired = function (callback) {
  291. // password is required
  292. Toolbar.lockLockButton();
  293. var message = '<h2 data-i18n="dialog.passwordRequired">';
  294. message += APP.translation.translateString(
  295. "dialog.passwordRequired");
  296. message += '</h2>' +
  297. '<input name="lockKey" type="text" data-i18n=' +
  298. '"[placeholder]dialog.password" placeholder="' +
  299. APP.translation.translateString("dialog.password") +
  300. '" autofocus>';
  301. messageHandler.openTwoButtonDialog(null, null, null, message,
  302. true,
  303. "dialog.Ok",
  304. function (e, v, m, f) {},
  305. null,
  306. function (e, v, m, f) {
  307. if (v) {
  308. var lockKey = f.lockKey;
  309. if (lockKey) {
  310. Toolbar.setSharedKey(lockKey);
  311. callback(lockKey);
  312. }
  313. }
  314. },
  315. ':input:first'
  316. );
  317. };
  318. /**
  319. * The dialpad button is shown iff there is at least one member that supports
  320. * DTMF (e.g. jigasi).
  321. */
  322. function onDtmfSupportChanged(dtmfSupport) {
  323. //TODO: enable when the UI is ready
  324. //Toolbar.showDialPadButton(dtmfSupport);
  325. }
  326. UI.addUser = function (jid, id, displayName) {
  327. messageHandler.notify(
  328. displayName,'notify.somebody', 'connected', 'notify.connected'
  329. );
  330. if (!config.startAudioMuted ||
  331. config.startAudioMuted > APP.conference.membersCount)
  332. UIUtil.playSoundNotification('userJoined');
  333. // Configure avatar
  334. UI.setUserAvatar(jid, id);
  335. // Add Peer's container
  336. VideoLayout.ensurePeerContainerExists(jid);
  337. };
  338. UI.removeUser = function (jid) {
  339. console.log('left.muc', jid);
  340. var displayName = $('#participant_' + Strophe.getResourceFromJid(jid) +
  341. '>.displayname').html();
  342. messageHandler.notify(displayName,'notify.somebody',
  343. 'disconnected',
  344. 'notify.disconnected');
  345. if (!config.startAudioMuted ||
  346. config.startAudioMuted > APP.conference.membersCount) {
  347. UIUtil.playSoundNotification('userLeft');
  348. }
  349. ContactList.removeContact(jid);
  350. VideoLayout.participantLeft(jid);
  351. };
  352. function onMucPresenceStatus(jid, info) {
  353. VideoLayout.setPresenceStatus(Strophe.getResourceFromJid(jid), info.status);
  354. }
  355. function onPeerVideoTypeChanged(resourceJid, newVideoType) {
  356. VideoLayout.onVideoTypeChanged(resourceJid, newVideoType);
  357. }
  358. function onMucRoleChanged(role, displayName) {
  359. VideoLayout.showModeratorIndicator();
  360. if (role === 'moderator') {
  361. var messageKey, messageOptions = {};
  362. if (!displayName) {
  363. messageKey = "notify.grantedToUnknown";
  364. }
  365. else {
  366. messageKey = "notify.grantedTo";
  367. messageOptions = {to: displayName};
  368. }
  369. messageHandler.notify(
  370. displayName,'notify.somebody',
  371. 'connected', messageKey,
  372. messageOptions);
  373. }
  374. }
  375. UI.notifyAuthRequired = function (intervalCallback) {
  376. Authentication.openAuthenticationDialog(
  377. roomName, intervalCallback, function () {
  378. Toolbar.authenticateClicked();
  379. }
  380. );
  381. };
  382. UI.toggleSmileys = function () {
  383. Chat.toggleSmileys();
  384. };
  385. UI.getSettings = function () {
  386. return Settings.getSettings();
  387. };
  388. UI.toggleFilmStrip = function () {
  389. return BottomToolbar.toggleFilmStrip();
  390. };
  391. UI.toggleChat = function () {
  392. return BottomToolbar.toggleChat();
  393. };
  394. UI.toggleContactList = function () {
  395. return BottomToolbar.toggleContactList();
  396. };
  397. UI.inputDisplayNameHandler = function (value) {
  398. VideoLayout.inputDisplayNameHandler(value);
  399. };
  400. /**
  401. * Return the type of the remote video.
  402. * @param jid the jid for the remote video
  403. * @returns the video type video or screen.
  404. */
  405. UI.getRemoteVideoType = function (jid) {
  406. return VideoLayout.getRemoteVideoType(jid);
  407. };
  408. UI.getRoomNode = function () {
  409. if (roomNode)
  410. return roomNode;
  411. var path = window.location.pathname;
  412. // determinde the room node from the url
  413. // TODO: just the roomnode or the whole bare jid?
  414. if (config.getroomnode && typeof config.getroomnode === 'function') {
  415. // custom function might be responsible for doing the pushstate
  416. roomNode = config.getroomnode(path);
  417. } else {
  418. /* fall back to default strategy
  419. * this is making assumptions about how the URL->room mapping happens.
  420. * It currently assumes deployment at root, with a rewrite like the
  421. * following one (for nginx):
  422. location ~ ^/([a-zA-Z0-9]+)$ {
  423. rewrite ^/(.*)$ / break;
  424. }
  425. */
  426. if (path.length > 1) {
  427. roomNode = path.substr(1).toLowerCase();
  428. } else {
  429. var word = RoomnameGenerator.generateRoomWithoutSeparator();
  430. roomNode = word.toLowerCase();
  431. window.history.pushState('VideoChat',
  432. 'Room: ' + word, window.location.pathname + word);
  433. }
  434. }
  435. return roomNode;
  436. };
  437. UI.generateRoomName = function () {
  438. if (roomName)
  439. return roomName;
  440. var roomNode = UI.getRoomNode();
  441. roomName = roomNode + '@' + config.hosts.muc;
  442. return roomName;
  443. };
  444. UI.connectionIndicatorShowMore = function(jid) {
  445. return VideoLayout.showMore(jid);
  446. };
  447. UI.showLoginPopup = function(callback) {
  448. console.log('password is required');
  449. var message = '<h2 data-i18n="dialog.passwordRequired">';
  450. message += APP.translation.translateString(
  451. "dialog.passwordRequired");
  452. message += '</h2>' +
  453. '<input name="username" type="text" ' +
  454. 'placeholder="user@domain.net" autofocus>' +
  455. '<input name="password" ' +
  456. 'type="password" data-i18n="[placeholder]dialog.userPassword"' +
  457. ' placeholder="user password">';
  458. UI.messageHandler.openTwoButtonDialog(null, null, null, message,
  459. true,
  460. "dialog.Ok",
  461. function (e, v, m, f) {
  462. if (v) {
  463. if (f.username && f.password) {
  464. callback(f.username, f.password);
  465. }
  466. }
  467. },
  468. null, null, ':input:first'
  469. );
  470. };
  471. UI.closeAuthenticationDialog = function () {
  472. Authentication.closeAuthenticationDialog();
  473. Authentication.stopInterval();
  474. };
  475. UI.askForNickname = function () {
  476. return window.prompt('Your nickname (optional)');
  477. };
  478. UI.getRoomName = function () {
  479. return roomName;
  480. };
  481. /**
  482. * Sets muted audio state for the local participant.
  483. */
  484. UI.setAudioMuted = function (mute) {
  485. VideoLayout.showLocalAudioIndicator(mute);
  486. UIUtil.buttonClick("#toolbar_button_mute", "icon-microphone icon-mic-disabled");
  487. };
  488. UI.setVideoMuted = function (muted) {
  489. $('#toolbar_button_camera').toggleClass("icon-camera-disabled", muted);
  490. };
  491. UI.addListener = function (type, listener) {
  492. eventEmitter.on(type, listener);
  493. };
  494. UI.clickOnVideo = function (videoNumber) {
  495. var remoteVideos = $(".videocontainer:not(#mixedstream)");
  496. if (remoteVideos.length > videoNumber) {
  497. remoteVideos[videoNumber].click();
  498. }
  499. };
  500. //Used by torture
  501. UI.showToolbar = function () {
  502. return ToolbarToggler.showToolbar();
  503. };
  504. //Used by torture
  505. UI.dockToolbar = function (isDock) {
  506. return ToolbarToggler.dockToolbar(isDock);
  507. };
  508. UI.setUserAvatar = function (id, email) {
  509. // update avatar
  510. Avatar.setUserAvatar(id, email);
  511. var thumbUrl = Avatar.getThumbUrl(id);
  512. var contactListUrl = Avatar.getContactListUrl(id);
  513. VideoLayout.changeUserAvatar(id, thumbUrl);
  514. ContactList.changeUserAvatar(id, contactListUrl);
  515. if (APP.conference.isLocalId(id)) {
  516. SettingsMenu.changeAvatar(thumbUrl);
  517. }
  518. };
  519. UI.notifyConnectionFailed = function (stropheErrorMsg) {
  520. var title = APP.translation.generateTranslationHTML(
  521. "dialog.error");
  522. var message;
  523. if (stropheErrorMsg) {
  524. message = APP.translation.generateTranslationHTML(
  525. "dialog.connectErrorWithMsg", {msg: stropheErrorMsg});
  526. } else {
  527. message = APP.translation.generateTranslationHTML(
  528. "dialog.connectError");
  529. }
  530. messageHandler.openDialog(
  531. title, message, true, {}, function (e, v, m, f) { return false; }
  532. );
  533. };
  534. UI.notifyFirefoxExtensionRequired = function (url) {
  535. messageHandler.openMessageDialog(
  536. "dialog.extensionRequired",
  537. null,
  538. null,
  539. APP.translation.generateTranslationHTML(
  540. "dialog.firefoxExtensionPrompt", {url: url}
  541. )
  542. );
  543. };
  544. UI.notifyInitiallyMuted = function () {
  545. messageHandler.notify(
  546. null, "notify.mutedTitle", "connected", "notify.muted", null, {timeOut: 120000}
  547. );
  548. };
  549. UI.markDominantSpiker = function (id) {
  550. VideoLayout.onDominantSpeakerChanged(id);
  551. };
  552. UI.handleLastNEndpoints = function (ids) {
  553. VideoLayout.onLastNEndpointsChanged(ids, []);
  554. };
  555. UI.setAudioLevel = function (targetJid, lvl) {
  556. AudioLevels.updateAudioLevel(
  557. targetJid, lvl, VideoLayout.getLargeVideoResource()
  558. );
  559. };
  560. UI.updateDesktopSharingButtons = function () {
  561. Toolbar.changeDesktopSharingButtonState();
  562. };
  563. UI.hideStats = function () {
  564. VideoLayout.hideStats();
  565. };
  566. UI.updateLocalStats = function (percent, stats) {
  567. VideoLayout.updateLocalConnectionStats(percent, stats);
  568. };
  569. UI.updateRemoteStats = function (jid, percent, stats) {
  570. VideoLayout.updateConnectionStats(jid, percent, stats);
  571. };
  572. UI.showAuthenticateButton = function (show) {
  573. Toolbar.showAuthenticateButton(show);
  574. };
  575. UI.markVideoInterrupted = function (interrupted) {
  576. if (interrupted) {
  577. VideoLayout.onVideoInterrupted();
  578. } else {
  579. VideoLayout.onVideoRestored();
  580. }
  581. };
  582. module.exports = UI;