main.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. (function() {
  2. 'use strict';
  3. var initialized = false;
  4. var i18n;
  5. // i18n key prefix for MUC ("muc.") or 1:1 chat ("chat.")
  6. var key_prefix;
  7. var display_data = null;
  8. function show_clients(client_array) {
  9. var list = document.getElementById('client_list');
  10. for (var id = 0; id < client_array.length; id++) {
  11. var item = document.createElement('li');
  12. item.innerHTML = client_array[id];
  13. list.appendChild(item);
  14. }
  15. }
  16. function load_clients(url) {
  17. var request = new XMLHttpRequest();
  18. request.open('GET', url);
  19. request.onreadystatechange = function () {
  20. if (request.readyState === 4) {
  21. if (request.status === 200 || (isLocalFileRequest(url) && request.responseText.length > 0)) {
  22. show_clients(JSON.parse(request.responseText));
  23. }
  24. }
  25. };
  26. request.send(null);
  27. }
  28. function load_hash() {
  29. var muc = false;
  30. key_prefix = "chat.";
  31. var jid = window.location.search || window.location.hash;
  32. jid = decodeURIComponent(jid.substring(jid.indexOf('#') + 1, jid.length));
  33. try {
  34. base_decoded = window.atob(jid);
  35. if (base_decoded.search('@') >= 0)
  36. jid = base_decoded;
  37. } catch (err) {
  38. // ignore error, JID wasn't base64 encoded
  39. }
  40. if (jid.search("\\?join") >= 0) {
  41. muc = true;
  42. key_prefix = "muc.";
  43. }
  44. // TODO: proper error checking / display / Creation of invitations
  45. if (jid.search("@") <= 0) return {jid:jid, jid_encoded:jid, name: jid};
  46. var name = jid.split("@")[0];
  47. name = name.charAt(0).toUpperCase() + name.slice(1);
  48. var jid_parts = jid.split("?");
  49. jid_parts[0] = encodeURIComponent(jid_parts[0]) // URL-encode the JID only
  50. var jid_encoded = jid_parts.join("?");
  51. return {jid: jid, jid_encoded: jid_encoded, name: name};
  52. }
  53. function translate_ui() {
  54. // translation
  55. document.title = i18n.text(key_prefix + 'title', display_data);
  56. // MUC/chat specific
  57. ['heading', 'button'].forEach(function(id) {
  58. document.getElementById(id).innerHTML = i18n.text(key_prefix + id, display_data);
  59. });
  60. // and agnostic
  61. ['clients', 'recommend', 'checkfulllist', 'xmppis'].forEach(function(id) {
  62. document.getElementById(id).innerHTML = i18n.text(id, display_data);
  63. });
  64. }
  65. function rehash() {
  66. display_data = load_hash();
  67. document.getElementById('button').href = "xmpp:" + display_data.jid_encoded;
  68. document.getElementById('url_in').value = "xmpp:" + display_data.jid;
  69. translate_ui();
  70. }
  71. function createQR() {
  72. display_data = load_hash();
  73. new QRCode(document.getElementById("qrcode"), "xmpp:" + display_data.jid);
  74. }
  75. function load_done() {
  76. if (initialized) return;
  77. initialized = true;
  78. // load i18n and perform translation
  79. i18n = new I18nText({path: 'lang'});
  80. i18n.once(I18nText.event.LOCALE_CHANGE, function (data) {
  81. rehash();
  82. });
  83. i18n.setLocale('en');
  84. // functionality
  85. if (navigator.userAgent.indexOf("Android") >= 0) {
  86. load_clients("clients_Android.json")
  87. }
  88. else if (navigator.userAgent.indexOf("Linux") >= 0) {
  89. load_clients("clients_Linux.json");
  90. createQR();
  91. }
  92. else if (navigator.userAgent.indexOf("iPhone") >= 0) {
  93. load_clients("clients_iOS.json")
  94. }
  95. else {
  96. load_clients("clients_Linux.json");
  97. createQR();
  98. }
  99. window.addEventListener("hashchange", rehash, false);
  100. document.getElementById("url_in").addEventListener("focus", function(event) {
  101. event.target.select();
  102. });
  103. }
  104. // Wait for the DOM to be ready
  105. document.addEventListener('DOMContentLoaded', load_done, false);
  106. document.onreadystatechange = function() {
  107. if (document.readyState === 'interactive') {
  108. load_done();
  109. }
  110. };
  111. })();