verto.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. 'use strict';
  2. var cur_call = null;
  3. var confMan = null;
  4. var verto;
  5. var ringing = false;
  6. var autocall = false;
  7. var chatting_with = false;
  8. $( ".selector" ).pagecontainer({ "theme": "a" });
  9. function display(msg) {
  10. $("#calltitle").html(msg);
  11. }
  12. function clearConfMan() {
  13. if (confMan) {
  14. confMan.destroy();
  15. confMan = null;
  16. }
  17. $("#conf").hide();
  18. $("#message").hide();
  19. chatting_with = null;
  20. }
  21. function goto_dialog(where) {
  22. $( ":mobile-pagecontainer" ).pagecontainer( "change", "#dialog-" + where, { role: "dialog" } );
  23. }
  24. function goto_page(where, force) {
  25. $( ":mobile-pagecontainer" ).pagecontainer( "change", "#page-" + where);
  26. }
  27. var first_login = false;
  28. var online_visible = false;
  29. function online(on) {
  30. if (on) {
  31. $("#online").show();
  32. $("#offline").hide();
  33. first_login = true;
  34. } else {
  35. $("#online").hide();
  36. $("#offline").show();
  37. }
  38. online_visible = on;
  39. }
  40. function setupChat() {
  41. $("#chatwin").html("");
  42. $("#chatsend").click(function() {
  43. if (!cur_call && chatting_with) {
  44. return;
  45. }
  46. cur_call.message({to: chatting_with,
  47. body: $("#chatmsg").val(),
  48. from_msg_name: cur_call.params.caller_id_name,
  49. from_msg_number: cur_call.params.caller_id_number
  50. });
  51. $("#chatmsg").val("");
  52. });
  53. $("#chatmsg").keyup(function (event) {
  54. if (event.keyCode == 13 && !event.shiftKey) {
  55. $( "#chatsend" ).trigger( "click" );
  56. }
  57. });
  58. }
  59. function check_vid() {
  60. var use_vid = $("#use_vid").is(':checked');
  61. return use_vid;
  62. }
  63. function messageTextToJQ(body) {
  64. // Builds a jQuery collection from body text, linkifies http/https links, imageifies http/https links to images, and doesn't allow script injection
  65. var match, $link, img_url, $body_parts = $(), rx = /(https?:\/\/[^ \n\r]+|\n\r|\n|\r)/;
  66. while ((match = rx.exec(body)) !== null) {
  67. if (match.index !== 0) {
  68. $body_parts = $body_parts.add(document.createTextNode(body.substr(0, match.index)));
  69. }
  70. if (match[0].match(/^(\n|\r|\n\r)$/)) {
  71. // Make a BR from a newline
  72. $body_parts = $body_parts.add($('<br />'));
  73. body = body.substr(match.index + match[0].length);
  74. } else {
  75. // Make a link (or image)
  76. $link = $('<a target="_blank" />').attr('href', match[0]);
  77. if (match[0].search(/\.(gif|jpe?g|png)/) > -1) {
  78. // Make an image
  79. img_url = match[0];
  80. // Handle dropbox links
  81. if (img_url.indexOf('dropbox.com') !== -1) {
  82. if (img_url.indexOf('?dl=1') === -1 && img_url.indexOf('?dl=0') === -1) {
  83. img_url += '?dl=1';
  84. } else if (img_url.indexOf('?dl=0') !== -1) {
  85. img_url = img_url.replace(/dl=0$/, 'dl=1');
  86. }
  87. }
  88. $link.append($('<img border="0" class="chatimg" />').attr('src', img_url));
  89. } else {
  90. // Make a link
  91. $link.text(match[0]);
  92. }
  93. body = body.substr(match.index + match[0].length);
  94. $body_parts = $body_parts.add($link);
  95. }
  96. }
  97. if (body) {
  98. $body_parts = $body_parts.add(document.createTextNode(body));
  99. }
  100. return $body_parts;
  101. } // END function messageTextToJQ
  102. var callbacks = {
  103. onMessage: function(verto, dialog, msg, data) {
  104. switch (msg) {
  105. case $.verto.enum.message.pvtEvent:
  106. // console.error("pvtEvent", data.pvtData);
  107. if (data.pvtData) {
  108. switch (data.pvtData.action) {
  109. case "conference-liveArray-part":
  110. clearConfMan();
  111. break;
  112. case "conference-liveArray-join":
  113. clearConfMan();
  114. confMan = new $.verto.confMan(verto, {
  115. tableID: "#conf_list",
  116. statusID: "#conf_count",
  117. mainModID: "#conf_mod",
  118. displayID: "#conf_display",
  119. dialog: dialog,
  120. hasVid: check_vid(),
  121. laData: data.pvtData
  122. });
  123. $("#conf").show();
  124. $("#chatwin").html("");
  125. $("#message").show();
  126. chatting_with = data.pvtData.chatID;
  127. break;
  128. }
  129. }
  130. break;
  131. case $.verto.enum.message.info:
  132. var body = data.body;
  133. /*
  134. // This section has been replaced with messageTextToJQ function
  135. if (body.match(/\.gif|\.jpg|\.jpeg|\.png/)) {
  136. var mod = "";
  137. if (body.match(/dropbox.com/)) {
  138. mod = "?dl=1";
  139. }
  140. body = body.replace(/(http[s]{0,1}:\/\/\S+)/g, "<a target='_blank' href='$1'>$1<br><img border='0' class='chatimg' src='$1'" + mod + "><\/a>");
  141. } else {
  142. body = body.replace(/(http[s]{0,1}:\/\/\S+)/g, "<a target='_blank' href='$1'>$1<\/a>");
  143. }
  144. if (body.slice(-1) !== "\n") {
  145. body += "\n";
  146. }
  147. body = body.replace(/(?:\r\n|\r|\n)/g, '<br />');
  148. var from = data.from_msg_name || data.from;
  149. $("#chatwin").append("<span class=chatuid>" + from + ":</span><br>" + body);
  150. $('#chatwin').animate({"scrollTop": $('#chatwin')[0].scrollHeight}, "fast");
  151. */
  152. var from = data.from_msg_name || data.from;
  153. $('#chatwin')
  154. .append($('<span class="chatuid" />').text(from + ':'))
  155. .append($('<br />'))
  156. .append(messageTextToJQ(body))
  157. .append($('<br />'));
  158. $('#chatwin').animate({"scrollTop": $('#chatwin')[0].scrollHeight}, "fast");
  159. break;
  160. case $.verto.enum.message.display:
  161. var party = dialog.params.remote_caller_id_name + "<" + dialog.params.remote_caller_id_number + ">";
  162. display("Talking to: " + dialog.cidString());
  163. break;
  164. default:
  165. break;
  166. }
  167. },
  168. onDialogState: function(d) {
  169. cur_call = d;
  170. if (d.state == $.verto.enum.state.ringing) {
  171. ringing = true;
  172. } else {
  173. ringing = false;
  174. }
  175. switch (d.state) {
  176. case $.verto.enum.state.ringing:
  177. display("Call From: " + d.cidString());
  178. $("#ansbtn").click(function() {
  179. cur_call.answer({
  180. useStereo: $("#use_stereo").is(':checked'),
  181. callee_id_name: $("#name").val(),
  182. callee_id_number: $("#cid").val(),
  183. });
  184. $('#dialog-incoming-call').dialog('close');
  185. });
  186. $("#declinebtn").click(function() {
  187. cur_call.hangup();
  188. $('#dialog-incoming-call').dialog('close');
  189. });
  190. goto_dialog("incoming-call");
  191. $("#dialog-incoming-call-txt").text("Incoming call from: " + d.cidString());
  192. if (d.params.wantVideo) {
  193. $("#vansbtn").click(function() {
  194. $("#use_vid").prop("checked", true);
  195. cur_call.answer({
  196. useVideo: true,
  197. useStereo: $("#use_stereo").is(':checked')
  198. });
  199. });
  200. // the buttons in this jquery mobile wont hide .. gotta wrap them in a div as a workaround
  201. $("#vansdiv").show();
  202. } else {
  203. $("#vansdiv").hide();
  204. }
  205. break;
  206. case $.verto.enum.state.trying:
  207. display("Calling: " + d.cidString());
  208. goto_page("incall");
  209. break;
  210. case $.verto.enum.state.early:
  211. case $.verto.enum.state.active:
  212. display("Talking to: " + d.cidString());
  213. goto_page("incall");
  214. break;
  215. case $.verto.enum.state.hangup:
  216. $("#main_info").html("Call ended with cause: " + d.cause);
  217. goto_page("main");
  218. case $.verto.enum.state.destroy:
  219. $("#hangup_cause").html("");
  220. clearConfMan();
  221. cur_call = null;
  222. break;
  223. case $.verto.enum.state.held:
  224. break;
  225. default:
  226. display("");
  227. break;
  228. }
  229. },
  230. onWSLogin: function(v, success) {
  231. display("");
  232. cur_call = null;
  233. ringing = false;
  234. if (success) {
  235. online(true);
  236. /*
  237. verto.subscribe("presence", {
  238. handler: function(v, e) {
  239. console.error("PRESENCE:", e);
  240. }
  241. });
  242. */
  243. if (!window.location.hash) {
  244. goto_page("main");
  245. }
  246. if (autocall) {
  247. autocall = false;
  248. docall();
  249. }
  250. } else {
  251. goto_page("main");
  252. goto_dialog("login-error");
  253. }
  254. },
  255. onWSClose: function(v, success) {
  256. display("");
  257. online(false);
  258. var today = new Date();
  259. $("#errordisplay").html("Connection Error.<br>Last Attempt: " + today);
  260. goto_page("main");
  261. },
  262. onEvent: function(v, e) {
  263. console.debug("GOT EVENT", e);
  264. },
  265. };
  266. $("#hold").click(function(e) {
  267. cur_call.toggleHold();
  268. goto_dialog("hold");
  269. });
  270. $("#cancelxferbtn").click(function(e) {
  271. $("#xferto").val("");
  272. $("#xferdiv").hide();
  273. });
  274. $(".startxferbtn").click(function(e) {
  275. if ($('#xferdiv').is(':visible')) {
  276. var xfer = $("#xferto").val();
  277. if (xfer) {
  278. cur_call.transfer(xfer);
  279. }
  280. $("#xferto").val("");
  281. $("#xferdiv").hide();
  282. } else {
  283. $("#xferdiv").show();
  284. }
  285. });
  286. $("#clearbtn").click(function(e) {
  287. $("#ext").val("");
  288. });
  289. $(".dialbtn").click(function(e) {
  290. $("#ext").val($("#ext").val() + e.currentTarget.textContent);
  291. });
  292. $(".dtmf").click(function(e) {
  293. if ($('#xferdiv').is(':visible')) {
  294. $("#xferto").val($("#xferto").val() + e.currentTarget.textContent);
  295. } else {
  296. cur_call.dtmf(e.currentTarget.textContent);
  297. }
  298. });
  299. $("#hupbtn").click(function() {
  300. verto.hangup();
  301. cur_call = null;
  302. });
  303. $("#webcam").click(function() {
  304. check_vid();
  305. });
  306. function docall() {
  307. $('#ext').trigger('change');
  308. if (cur_call) {
  309. return;
  310. }
  311. $("#main_info").html("Trying");
  312. cur_call = verto.newCall({
  313. destination_number: $("#ext").val(),
  314. caller_id_name: $("#name").val(),
  315. caller_id_number: $("#cid").val(),
  316. useVideo: check_vid(),
  317. useStereo: $("#use_stereo").is(':checked')
  318. });
  319. }
  320. $("#callbtn").click(function() {
  321. docall();
  322. });
  323. function pop(id, cname, dft) {
  324. var tmp = $.cookie(cname) || dft;
  325. $.cookie(cname, tmp, {
  326. expires: 365
  327. });
  328. $(id).val(tmp).change(function() {
  329. $.cookie(cname, $(id).val(), {
  330. expires: 365
  331. });
  332. });
  333. }
  334. function init() {
  335. cur_call = null;
  336. if (!autocall) {
  337. pop("#ext", "verto_demo_ext", "3500");
  338. }
  339. pop("#name", "verto_demo_name", "FreeSWITCH User");
  340. pop("#cid", "verto_demo_cid", "1008");
  341. pop("#textto", "verto_demo_textto", "1000");
  342. pop("#login", "verto_demo_login", "1008");
  343. pop("#passwd", "verto_demo_passwd", "1234");
  344. pop("#hostName", "verto_demo_hostname", window.location.hostname);
  345. pop("#wsURL", "verto_demo_wsurl", "wss://" + window.location.hostname + ":8082");
  346. var tmp = $.cookie("verto_demo_vid_checked") || "false";
  347. $.cookie("verto_demo_vid_checked", tmp, {
  348. expires: 365
  349. });
  350. $("#use_vid").prop("checked", tmp === "true").change(function(e) {
  351. tmp = $("#use_vid").is(':checked');
  352. $.cookie("verto_demo_vid_checked", tmp ? "true" : "false", {
  353. expires: 365
  354. });
  355. });
  356. tmp = $.cookie("verto_demo_stereo_checked") || "false";
  357. $.cookie("verto_demo_stereo_checked", tmp, {
  358. expires: 365
  359. });
  360. $("#use_stereo").prop("checked", tmp === "true").change(function(e) {
  361. tmp = $("#use_stereo").is(':checked');
  362. $.cookie("verto_demo_stereo_checked", tmp ? "true" : "false", {
  363. expires: 365
  364. });
  365. });
  366. tmp = $.cookie("verto_demo_stun_checked") || "true";
  367. $.cookie("verto_demo_stun_checked", tmp, {
  368. expires: 365
  369. });
  370. $("#use_stun").prop("checked", tmp === "true").change(function(e) {
  371. tmp = $("#use_stun").is(':checked');
  372. $.cookie("verto_demo_stun_checked", tmp ? "true" : "false", {
  373. expires: 365
  374. });
  375. if (verto) {
  376. verto.iceServers(tmp);
  377. }
  378. });
  379. verto = new $.verto({
  380. login: $("#login").val() + "@" + $("#hostName").val(),
  381. passwd: $("#passwd").val(),
  382. socketUrl: $("#wsURL").val(),
  383. tag: "webcam",
  384. ringFile: "sounds/bell_ring2.wav",
  385. videoParams: {
  386. "minWidth": "1280",
  387. "minHeight": "720",
  388. "minFrameRate": 30
  389. },
  390. audioParams: {
  391. googAutoGainControl: false,
  392. googNoiseSuppression: false,
  393. googHighpassFilter: false
  394. },
  395. iceServers: $("#use_stun").is(':checked')
  396. },callbacks);
  397. $("#login").change(function(e) {
  398. $("#cid").val(e.currentTarget.value);
  399. $.cookie("verto_demo_cid", e.currentTarget.value, {
  400. expires: 365
  401. });
  402. });
  403. $("#vtxtbtn").click(function() {
  404. verto.message({
  405. to: $("#textto").val(),
  406. body: $("#textmsg").val()
  407. });
  408. $("#textmsg").val("");
  409. });
  410. $("#logoutbtn").click(function() {
  411. verto.logout();
  412. online(false);
  413. });
  414. $("#loginbtn").click(function() {
  415. online(false);
  416. verto.loginData({
  417. login: $("#login").val() + "@" + $("#hostName").val(),
  418. passwd: $("#passwd").val()
  419. });
  420. verto.login();
  421. goto_page("main");
  422. });
  423. $("#xferdiv").hide();
  424. $("#webcam").hide();
  425. online(false);
  426. setupChat();
  427. $("#ext").keyup(function (event) {
  428. if (event.keyCode == 13) {
  429. $( "#callbtn" ).trigger( "click" );
  430. }
  431. });
  432. $(document).keypress(function(event) {
  433. if (!(cur_call && event.target.id == "page-incall")) return;
  434. var key = String.fromCharCode(event.keyCode);
  435. var i = parseInt(key);
  436. if (key === "#" || key === "*" || key === "0" || (i > 0 && i <= 9)) {
  437. cur_call.dtmf(key);
  438. }
  439. });
  440. if (window.location.hostname !== "webrtc.freeswitch.org") {
  441. $("#directory").hide();
  442. }
  443. }
  444. $(document).ready(function() {
  445. var hash = window.location.hash.substring(1);
  446. var a = [];
  447. if (hash && hash.indexOf("page-") == -1) {
  448. window.location.hash = "";
  449. $("#ext").val(hash);
  450. autocall = true;
  451. }
  452. if (hash && (a = hash.split("&"))) {
  453. window.location.hash = a[0];
  454. }
  455. init();
  456. });
  457. var lastTo = 0;
  458. $(document).bind("pagecontainerchange", function(e, data) {
  459. if (lastTo) {
  460. clearTimeout(lastTo);
  461. }
  462. switch (window.location.hash) {
  463. case "#page-incall":
  464. lastTo = setTimeout(function() {
  465. if (!cur_call) {
  466. goto_page("main");
  467. }
  468. }, 1000);
  469. break;
  470. case "#page-main":
  471. if (cur_call) {
  472. goto_page("incall");
  473. }
  474. break;
  475. case "#page-login":
  476. lastTo = setTimeout(function() {
  477. if (online_visible) {
  478. goto_page("main");
  479. }
  480. },
  481. 1000);
  482. break;
  483. }
  484. });