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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728
  1. /* jshint -W117 */
  2. /* application specific logic */
  3. var connection = null;
  4. var focus = null;
  5. var RTC;
  6. var RTCPeerConnection = null;
  7. var nickname = null;
  8. var sharedKey = '';
  9. var roomUrl = null;
  10. var ssrc2jid = {};
  11. /* window.onbeforeunload = closePageWarning; */
  12. function init() {
  13. RTC = setupRTC();
  14. if (RTC === null) {
  15. window.location.href = 'webrtcrequired.html';
  16. return;
  17. } else if (RTC.browser != 'chrome') {
  18. window.location.href = 'chromeonly.html';
  19. return;
  20. }
  21. RTCPeerconnection = TraceablePeerConnection;
  22. connection = new Strophe.Connection(document.getElementById('boshURL').value || config.bosh || '/http-bind');
  23. if (connection.disco) {
  24. // for chrome, add multistream cap
  25. }
  26. connection.jingle.pc_constraints = RTC.pc_constraints;
  27. var jid = document.getElementById('jid').value || config.hosts.domain || window.location.hostname;
  28. connection.connect(jid, document.getElementById('password').value, function (status) {
  29. if (status == Strophe.Status.CONNECTED) {
  30. console.log('connected');
  31. if (RTC.browser == 'firefox') {
  32. getUserMediaWithConstraints(['audio']);
  33. } else {
  34. getUserMediaWithConstraints(['audio', 'video'], '360');
  35. }
  36. document.getElementById('connect').disabled = true;
  37. } else {
  38. console.log('status', status);
  39. }
  40. });
  41. }
  42. function doJoin() {
  43. var roomnode = null;
  44. var path = window.location.pathname;
  45. var roomjid;
  46. // determinde the room node from the url
  47. // TODO: just the roomnode or the whole bare jid?
  48. if (config.getroomnode && typeof config.getroomnode === 'function') {
  49. // custom function might be responsible for doing the pushstate
  50. roomnode = config.getroomnode(path);
  51. } else {
  52. /* fall back to default strategy
  53. * this is making assumptions about how the URL->room mapping happens.
  54. * It currently assumes deployment at root, with a rewrite like the
  55. * following one (for nginx):
  56. location ~ ^/([a-zA-Z0-9]+)$ {
  57. rewrite ^/(.*)$ / break;
  58. }
  59. */
  60. if (path.length > 1) {
  61. roomnode = path.substr(1).toLowerCase();
  62. } else {
  63. roomnode = Math.random().toString(36).substr(2, 20);
  64. window.history.pushState('VideoChat', 'Room: ' + roomnode, window.location.pathname + roomnode);
  65. }
  66. }
  67. roomjid = roomnode + '@' + config.hosts.muc;
  68. if (config.useNicks) {
  69. var nick = window.prompt('Your nickname (optional)');
  70. if (nick) {
  71. roomjid += '/' + nick;
  72. } else {
  73. roomjid += '/' + Strophe.getNodeFromJid(connection.jid);
  74. }
  75. } else {
  76. roomjid += '/' + Strophe.getNodeFromJid(connection.jid).substr(0,8);
  77. }
  78. connection.emuc.doJoin(roomjid);
  79. }
  80. $(document).bind('mediaready.jingle', function (event, stream) {
  81. connection.jingle.localStream = stream;
  82. RTC.attachMediaStream($('#localVideo'), stream);
  83. document.getElementById('localVideo').muted = true;
  84. document.getElementById('localVideo').autoplay = true;
  85. document.getElementById('localVideo').volume = 0;
  86. updateLargeVideo(document.getElementById('localVideo').src, true);
  87. $('#localVideo').click(function () {
  88. updateLargeVideo($(this).attr('src'), true);
  89. });
  90. doJoin();
  91. });
  92. $(document).bind('mediafailure.jingle', function () {
  93. // FIXME
  94. });
  95. $(document).bind('remotestreamadded.jingle', function (event, data, sid) {
  96. function waitForRemoteVideo(selector, sid) {
  97. var sess = connection.jingle.sessions[sid];
  98. videoTracks = data.stream.getVideoTracks();
  99. if (videoTracks.length === 0 || selector[0].currentTime > 0) {
  100. RTC.attachMediaStream(selector, data.stream); // FIXME: why do i have to do this for FF?
  101. $(document).trigger('callactive.jingle', [selector, sid]);
  102. console.log('waitForremotevideo', sess.peerconnection.iceConnectionState, sess.peerconnection.signalingState);
  103. } else {
  104. setTimeout(function () { waitForRemoteVideo(selector, sid); }, 100);
  105. }
  106. }
  107. var sess = connection.jingle.sessions[sid];
  108. // look up an associated JID for a stream id
  109. if (data.stream.id.indexOf('mixedmslabel') == -1) {
  110. var ssrclines = SDPUtil.find_lines(sess.peerconnection.remoteDescription.sdp, 'a=ssrc');
  111. ssrclines = ssrclines.filter(function (line) {
  112. return line.indexOf('mslabel:' + data.stream.label) != -1;
  113. });
  114. if (ssrclines.length) {
  115. thessrc = ssrclines[0].substring(7).split(' ')[0];
  116. // ok to overwrite the one from focus? might save work in colibri.js
  117. console.log('associated jid', ssrc2jid[thessrc], data.peerjid);
  118. if (ssrc2jid[thessrc]) {
  119. data.peerjid = ssrc2jid[thessrc];
  120. }
  121. }
  122. }
  123. var container;
  124. var remotes = document.getElementById('remoteVideos');
  125. if (data.peerjid) {
  126. container = document.getElementById('participant_' + Strophe.getResourceFromJid(data.peerjid));
  127. if (!container) {
  128. console.warn('no container for', data.peerjid);
  129. // create for now...
  130. // FIXME: should be removed
  131. container = document.createElement('span');
  132. container.id = 'participant_' + Strophe.getResourceFromJid(data.peerjid);
  133. container.className = 'videocontainer';
  134. remotes.appendChild(container);
  135. } else {
  136. //console.log('found container for', data.peerjid);
  137. }
  138. } else {
  139. if (data.stream.id != 'mixedmslabel') {
  140. console.warn('can not associate stream', data.stream.id, 'with a participant');
  141. }
  142. // FIXME: for the mixed ms we dont need a video -- currently
  143. container = document.createElement('span');
  144. container.className = 'videocontainer';
  145. remotes.appendChild(container);
  146. }
  147. var vid = document.createElement('video');
  148. var id = 'remoteVideo_' + sid + '_' + data.stream.id;
  149. vid.id = id;
  150. vid.autoplay = true;
  151. vid.oncontextmenu = function () { return false; };
  152. container.appendChild(vid);
  153. // TODO: make mixedstream display:none via css?
  154. if (id.indexOf('mixedmslabel') != -1) {
  155. container.id = 'mixedstream';
  156. $(container).hide();
  157. }
  158. var sel = $('#' + id);
  159. sel.hide();
  160. RTC.attachMediaStream(sel, data.stream);
  161. waitForRemoteVideo(sel, sid);
  162. data.stream.onended = function () {
  163. console.log('stream ended', this.id);
  164. var src = $('#' + id).attr('src');
  165. if (src === $('#largeVideo').attr('src')) {
  166. // this is currently displayed as large
  167. // pick the last visible video in the row
  168. // if nobody else is left, this picks the local video
  169. var pick = $('#remoteVideos>span[id!="mixedstream"]:visible:last>video').get(0);
  170. // mute if localvideo
  171. document.getElementById('largeVideo').volume = pick.volume;
  172. document.getElementById('largeVideo').src = pick.src;
  173. }
  174. $('#' + id).parent().remove();
  175. resizeThumbnails();
  176. };
  177. sel.click(
  178. function () {
  179. updateLargeVideo($(this).attr('src'), false);
  180. }
  181. );
  182. });
  183. $(document).bind('callincoming.jingle', function (event, sid) {
  184. var sess = connection.jingle.sessions[sid];
  185. // TODO: check affiliation and/or role
  186. console.log('emuc data for', sess.peerjid, connection.emuc.members[sess.peerjid]);
  187. sess.sendAnswer();
  188. sess.accept();
  189. });
  190. $(document).bind('callactive.jingle', function (event, videoelem, sid) {
  191. if (videoelem.attr('id').indexOf('mixedmslabel') == -1) {
  192. // ignore mixedmslabela0 and v0
  193. videoelem.show();
  194. resizeThumbnails();
  195. document.getElementById('largeVideo').volume = 1;
  196. $('#largeVideo').attr('src', videoelem.attr('src'));
  197. }
  198. });
  199. $(document).bind('callterminated.jingle', function (event, sid, reason) {
  200. // FIXME
  201. });
  202. $(document).bind('setLocalDescription.jingle', function (event, sid) {
  203. // put our ssrcs into presence so other clients can identify our stream
  204. var sess = connection.jingle.sessions[sid];
  205. var newssrcs = {};
  206. var localSDP = new SDP(sess.peerconnection.localDescription.sdp);
  207. localSDP.media.forEach(function (media) {
  208. var type = SDPUtil.parse_mline(media.split('\r\n')[0]).media;
  209. var ssrc = SDPUtil.find_line(media, 'a=ssrc:').substring(7).split(' ')[0];
  210. // assumes a single local ssrc
  211. newssrcs[type] = ssrc;
  212. });
  213. console.log('new ssrcs', newssrcs);
  214. // just blast off presence for everything -- TODO: optimize
  215. var pres = $pres({to: connection.emuc.myroomjid });
  216. pres.c('x', {xmlns: 'http://jabber.org/protocol/muc'}).up();
  217. pres.c('media', {xmlns: 'http://estos.de/ns/mjs'});
  218. Object.keys(newssrcs).forEach(function (mtype) {
  219. pres.c('source', {type: mtype, ssrc: newssrcs[mtype]}).up();
  220. });
  221. pres.up();
  222. connection.send(pres);
  223. });
  224. $(document).bind('joined.muc', function (event, jid, info) {
  225. updateRoomUrl(window.location.href);
  226. document.getElementById('localNick').appendChild(
  227. document.createTextNode(Strophe.getResourceFromJid(jid) + ' (you)')
  228. );
  229. // Once we've joined the muc show the toolbar
  230. showToolbar();
  231. if (Object.keys(connection.emuc.members).length < 1) {
  232. focus = new ColibriFocus(connection, config.hosts.bridge);
  233. }
  234. });
  235. $(document).bind('entered.muc', function (event, jid, info, pres) {
  236. console.log('entered', jid, info);
  237. console.log(focus);
  238. var container = document.createElement('span');
  239. container.id = 'participant_' + Strophe.getResourceFromJid(jid);
  240. container.className = 'videocontainer';
  241. var remotes = document.getElementById('remoteVideos');
  242. remotes.appendChild(container);
  243. var nickfield = document.createElement('span');
  244. nickfield.appendChild(document.createTextNode(Strophe.getResourceFromJid(jid)));
  245. container.appendChild(nickfield);
  246. resizeThumbnails();
  247. if (focus !== null) {
  248. // FIXME: this should prepare the video
  249. if (focus.confid === null) {
  250. console.log('make new conference with', jid);
  251. focus.makeConference(Object.keys(connection.emuc.members));
  252. } else {
  253. console.log('invite', jid, 'into conference');
  254. focus.addNewParticipant(jid);
  255. }
  256. }
  257. else if (sharedKey) {
  258. updateLockButton();
  259. }
  260. $(pres).find('>media[xmlns="http://estos.de/ns/mjs"]>source').each(function (idx, ssrc) {
  261. //console.log(jid, 'assoc ssrc', ssrc.getAttribute('type'), ssrc.getAttribute('ssrc'));
  262. ssrc2jid[ssrc.getAttribute('ssrc')] = jid;
  263. });
  264. });
  265. $(document).bind('left.muc', function (event, jid) {
  266. console.log('left', jid);
  267. connection.jingle.terminateByJid(jid);
  268. var container = document.getElementById('participant_' + Strophe.getResourceFromJid(jid));
  269. if (container) {
  270. // hide here, wait for video to close before removing
  271. $(container).hide();
  272. resizeThumbnails();
  273. }
  274. if (Object.keys(connection.emuc.members).length === 0) {
  275. console.log('everyone left');
  276. if (focus !== null) {
  277. // FIXME: closing the connection is a hack to avoid some
  278. // problemswith reinit
  279. if (focus.peerconnection !== null) {
  280. focus.peerconnection.close();
  281. }
  282. focus = new ColibriFocus(connection, config.hosts.bridge);
  283. }
  284. }
  285. });
  286. $(document).bind('presence.muc', function (event, jid, info, pres) {
  287. $(pres).find('>media[xmlns="http://estos.de/ns/mjs"]>source').each(function (idx, ssrc) {
  288. //console.log(jid, 'assoc ssrc', ssrc.getAttribute('type'), ssrc.getAttribute('ssrc'));
  289. ssrc2jid[ssrc.getAttribute('ssrc')] = jid;
  290. });
  291. });
  292. $(document).bind('passwordrequired.muc', function (event, jid) {
  293. console.log('on password required', jid);
  294. $.prompt('<h2>Password required</h2>' +
  295. '<input id="lockKey" type="text" placeholder="shared key" autofocus>',
  296. {
  297. persistent: true,
  298. buttons: { "Ok": true , "Cancel": false},
  299. defaultButton: 1,
  300. loaded: function(event) {
  301. document.getElementById('lockKey').focus();
  302. },
  303. submit: function(e,v,m,f){
  304. if(v)
  305. {
  306. var lockKey = document.getElementById('lockKey');
  307. if (lockKey.value != null)
  308. {
  309. setSharedKey(lockKey);
  310. connection.emuc.doJoin(jid, lockKey.value);
  311. }
  312. }
  313. }
  314. });
  315. });
  316. /**
  317. * Updates the large video with the given new video source.
  318. */
  319. function updateLargeVideo(newSrc, localVideo) {
  320. console.log('hover in', newSrc);
  321. if ($('#largeVideo').attr('src') != newSrc) {
  322. if (!localVideo)
  323. document.getElementById('largeVideo').volume = 1;
  324. else
  325. document.getElementById('largeVideo').volume = 0;
  326. $('#largeVideo').fadeOut(300, function () {
  327. $(this).attr('src', newSrc);
  328. var videoTransform = document.getElementById('largeVideo').style.webkitTransform;
  329. if (localVideo && videoTransform != 'scaleX(-1)') {
  330. document.getElementById('largeVideo').style.webkitTransform = "scaleX(-1)";
  331. }
  332. else if (!localVideo && videoTransform == 'scaleX(-1)') {
  333. document.getElementById('largeVideo').style.webkitTransform = "none";
  334. }
  335. $(this).fadeIn(300);
  336. });
  337. }
  338. }
  339. function toggleVideo() {
  340. if (!(connection && connection.jingle.localStream)) return;
  341. for (var idx = 0; idx < connection.jingle.localStream.getVideoTracks().length; idx++) {
  342. connection.jingle.localStream.getVideoTracks()[idx].enabled = !connection.jingle.localStream.getVideoTracks()[idx].enabled;
  343. }
  344. }
  345. function toggleAudio() {
  346. if (!(connection && connection.jingle.localStream)) return;
  347. for (var idx = 0; idx < connection.jingle.localStream.getAudioTracks().length; idx++) {
  348. connection.jingle.localStream.getAudioTracks()[idx].enabled = !connection.jingle.localStream.getAudioTracks()[idx].enabled;
  349. }
  350. }
  351. function resizeLarge() {
  352. var availableHeight = window.innerHeight;
  353. var chatspaceWidth = $('#chatspace').width();
  354. var numvids = $('#remoteVideos>video:visible').length;
  355. if (numvids < 5)
  356. availableHeight -= 100; // min thumbnail height for up to 4 videos
  357. else
  358. availableHeight -= 50; // min thumbnail height for more than 5 videos
  359. availableHeight -= 79; // padding + link ontop
  360. var availableWidth = window.innerWidth - chatspaceWidth;
  361. var aspectRatio = 16.0 / 9.0;
  362. if (availableHeight < availableWidth / aspectRatio) {
  363. availableWidth = Math.floor(availableHeight * aspectRatio);
  364. }
  365. if (availableWidth < 0 || availableHeight < 0) return;
  366. $('#largeVideo').parent().width(availableWidth);
  367. $('#largeVideo').parent().height(availableWidth / aspectRatio);
  368. resizeThumbnails();
  369. }
  370. function resizeThumbnails() {
  371. // Calculate the available height, which is the inner window height minus 39px for the header
  372. // minus 4px for the delimiter lines on the top and bottom of the large video,
  373. // minus the 36px space inside the remoteVideos container used for highlighting shadow.
  374. var availableHeight = window.innerHeight - $('#largeVideo').height() - 79;
  375. var numvids = $('#remoteVideos>span:visible').length;
  376. // Remove the 1px borders arround videos.
  377. var availableWinWidth = $('#remoteVideos').width() - 2 * numvids;
  378. var availableWidth = availableWinWidth / numvids;
  379. var aspectRatio = 16.0 / 9.0;
  380. var maxHeight = Math.min(160, availableHeight);
  381. availableHeight = Math.min(maxHeight, availableWidth / aspectRatio);
  382. if (availableHeight < availableWidth / aspectRatio) {
  383. availableWidth = Math.floor(availableHeight * aspectRatio);
  384. }
  385. // size videos so that while keeping AR and max height, we have a nice fit
  386. $('#remoteVideos').height(availableHeight+26); // add the 2*18px-padding-top border used for highlighting shadow.
  387. $('#remoteVideos>span').width(availableWidth);
  388. $('#remoteVideos>span').height(availableHeight);
  389. }
  390. $(document).ready(function () {
  391. $('#nickinput').keydown(function(event) {
  392. if (event.keyCode == 13) {
  393. event.preventDefault();
  394. var val = this.value;
  395. this.value = '';
  396. if (!nickname) {
  397. nickname = val;
  398. $('#nickname').css({visibility:"hidden"});
  399. $('#chatconversation').css({visibility:'visible'});
  400. $('#usermsg').css({visibility:'visible'});
  401. $('#usermsg').focus();
  402. return;
  403. }
  404. }
  405. });
  406. $('#usermsg').keydown(function(event) {
  407. if (event.keyCode == 13) {
  408. event.preventDefault();
  409. var message = this.value;
  410. $('#usermsg').val('').trigger('autosize.resize');
  411. this.focus();
  412. connection.emuc.sendMessage(message, nickname);
  413. }
  414. });
  415. $('#usermsg').autosize();
  416. resizeLarge();
  417. $(window).resize(function () {
  418. resizeLarge();
  419. });
  420. if (!$('#settings').is(':visible')) {
  421. console.log('init');
  422. init();
  423. } else {
  424. loginInfo.onsubmit = function (e) {
  425. if (e.preventDefault) e.preventDefault();
  426. $('#settings').hide();
  427. init();
  428. };
  429. }
  430. });
  431. $(window).bind('beforeunload', function () {
  432. if (connection && connection.connected) {
  433. // ensure signout
  434. $.ajax({
  435. type: 'POST',
  436. url: config.bosh,
  437. async: false,
  438. cache: false,
  439. contentType: 'application/xml',
  440. data: "<body rid='" + (connection.rid || connection._proto.rid) + "' xmlns='http://jabber.org/protocol/httpbind' sid='" + (connection.sid || connection._proto.sid) + "' type='terminate'><presence xmlns='jabber:client' type='unavailable'/></body>",
  441. success: function (data) {
  442. console.log('signed out');
  443. console.log(data);
  444. },
  445. error: function (XMLHttpRequest, textStatus, errorThrown) {
  446. console.log('signout error', textStatus + ' (' + errorThrown + ')');
  447. }
  448. });
  449. }
  450. });
  451. function dump(elem, filename) {
  452. console.log("ELEMENT", elem);
  453. elem = elem.parentNode;
  454. elem.download = filename || 'xmpplog.json';
  455. elem.href = 'data:application/json;charset=utf-8,\n';
  456. var data = {};
  457. data.time = new Date();
  458. data.url = window.location.href;
  459. data.ua = navigator.userAgent;
  460. if (connection.logger) {
  461. data.xmpp = connection.logger.log;
  462. }
  463. if (connection.jingle) {
  464. Object.keys(connection.jingle.sessions).forEach(function (sid) {
  465. var session = connection.jingle.sessions[sid];
  466. if (session.peerconnection && session.peerconnection.updateLog) {
  467. // FIXME: should probably be a .dump call
  468. data["jingle_" + session.sid] = session.peerconnection.updateLog;
  469. }
  470. });
  471. }
  472. elem.href += encodeURIComponent(JSON.stringify(data, null, ' '));
  473. return false;
  474. }
  475. /*
  476. * Appends the given message to the chat conversation.
  477. */
  478. // <a onclick="dump(event.target);">my download button</a>
  479. function dump(elem, filename){
  480. elem = elem.parentNode;
  481. elem.download = filename || 'xmpplog.json';
  482. elem.href = 'data:application/json;charset=utf-8,\n';
  483. var data = {};
  484. data.time = new Date();
  485. data.url = window.location.href;
  486. data.ua = navigator.userAgent;
  487. if (connection.logger) {
  488. data.xmpp = connection.logger.log;
  489. }
  490. if (connection.jingle) {
  491. Object.keys(connection.jingle.sessions).forEach(function (sid) {
  492. var session = connection.jingle.sessions[sid];
  493. if (session.peerconnection && session.peerconnection.updateLog) {
  494. // FIXME: should probably be a .dump call
  495. data["jingle_" + session.sid] = session.peerconnection.updateLog;
  496. }
  497. });
  498. }
  499. elem.href += encodeURIComponent(JSON.stringify(data, null, ' '));
  500. return false;
  501. }
  502. /*
  503. * Appends the given message to the chat conversation.
  504. */
  505. function updateChatConversation(nick, message)
  506. {
  507. var divClassName = '';
  508. if (nickname == nick)
  509. divClassName = "localuser";
  510. else
  511. divClassName = "remoteuser";
  512. $('#chatconversation').append('<div class="' + divClassName + '"><b>' + nick + ': </b>' + message + '</div>');
  513. $('#chatconversation').animate({ scrollTop: $('#chatconversation')[0].scrollHeight}, 1000);
  514. }
  515. /*
  516. * Changes the style class of the element given by id.
  517. */
  518. function buttonClick(id, classname) {
  519. $(id).toggleClass(classname); // add the class to the clicked element
  520. }
  521. /*
  522. * Opens the lock room dialog.
  523. */
  524. function openLockDialog() {
  525. // Only the focus is able to set a shared key.
  526. if (focus == null) {
  527. if (sharedKey)
  528. $.prompt("This conversation is currently protected by a shared secret key.",
  529. {
  530. title: "Secrect key",
  531. persistent: false
  532. });
  533. else
  534. $.prompt("This conversation isn't currently protected by a secret key. Only the owner of the conference could set a shared key.",
  535. {
  536. title: "Secrect key",
  537. persistent: false
  538. });
  539. }
  540. else {
  541. if (sharedKey)
  542. $.prompt("Are you sure you would like to remove your secret key?",
  543. {
  544. title: "Remove secrect key",
  545. persistent: false,
  546. buttons: { "Remove": true, "Cancel": false},
  547. defaultButton: 1,
  548. submit: function(e,v,m,f){
  549. if(v)
  550. {
  551. setSharedKey('');
  552. lockRoom(false);
  553. }
  554. }
  555. });
  556. else
  557. $.prompt('<h2>Set a secrect key to lock your room</h2>' +
  558. '<input id="lockKey" type="text" placeholder="your shared key" autofocus>',
  559. {
  560. persistent: false,
  561. buttons: { "Save": true , "Cancel": false},
  562. defaultButton: 1,
  563. loaded: function(event) {
  564. document.getElementById('lockKey').focus();
  565. },
  566. submit: function(e,v,m,f){
  567. if(v)
  568. {
  569. var lockKey = document.getElementById('lockKey');
  570. if (lockKey.value)
  571. {
  572. setSharedKey(lockKey.value);
  573. lockRoom(true);
  574. }
  575. }
  576. }
  577. });
  578. }
  579. }
  580. /*
  581. * Opens the invite link dialog.
  582. */
  583. function openLinkDialog() {
  584. $.prompt('<input id="inviteLinkRef" type="text" value="' + roomUrl + '" onclick="this.select();">',
  585. {
  586. title: "Share this link with everyone you want to invite",
  587. persistent: false,
  588. buttons: { "Cancel": false},
  589. loaded: function(event) {
  590. document.getElementById('inviteLinkRef').select();
  591. }
  592. });
  593. }
  594. /*
  595. * Locks / unlocks the room.
  596. */
  597. function lockRoom(lock) {
  598. console.log("LOCK", sharedKey);
  599. if (lock)
  600. connection.emuc.lockRoom(sharedKey);
  601. else
  602. connection.emuc.lockRoom('');
  603. updateLockButton();
  604. }
  605. /*
  606. * Sets the shared key.
  607. */
  608. function setSharedKey(sKey) {
  609. sharedKey = sKey;
  610. }
  611. /*
  612. * Updates the lock button state.
  613. */
  614. function updateLockButton() {
  615. buttonClick("#lockIcon", "fa fa-unlock fa-lg fa fa-lock fa-lg");
  616. }
  617. /*
  618. * Opens / closes the chat area.
  619. */
  620. function openChat() {
  621. var chatspace = $('#chatspace');
  622. var videospace = $('#videospace');
  623. var chatspaceWidth = chatspace.width();
  624. if (chatspace.css("opacity") == 1) {
  625. chatspace.animate({opacity: 0}, "fast");
  626. chatspace.animate({width: 0}, "slow");
  627. videospace.animate({right: 0, width:"100%"}, "slow");
  628. }
  629. else {
  630. chatspace.animate({width:"20%"}, "slow");
  631. chatspace.animate({opacity: 1}, "slow");
  632. videospace.animate({right:chatspaceWidth, width:"80%"}, "slow");
  633. }
  634. // Request the focus in the nickname field or the chat input field.
  635. if ($('#nickinput').is(':visible'))
  636. $('#nickinput').focus();
  637. else
  638. $('#usermsg').focus();
  639. }
  640. /*
  641. * Shows the call main toolbar.
  642. */
  643. function showToolbar() {
  644. $('#toolbar').css({visibility:"visible"});
  645. }
  646. /*
  647. * Updates the room invite url.
  648. */
  649. function updateRoomUrl(newRoomUrl) {
  650. roomUrl = newRoomUrl;
  651. }
  652. /*
  653. * Warning to the user that the conference window is about to be closed.
  654. */
  655. function closePageWarning() {
  656. if (focus != null)
  657. return "You are the owner of this conference call and you are about to end it.";
  658. else
  659. return "You are about to leave this conversation.";
  660. }