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