about summary refs log tree commit diff
path: root/scripts/main.js
diff options
context:
space:
mode:
authorAlex Jordan <alex@strugee.net>2017-01-16 14:05:40 -0500
committerAlex Jordan <alex@strugee.net>2017-01-16 14:05:40 -0500
commita120c8c6305c1eeffa57444baec7ff3e5575af9d (patch)
tree0b7304ec89a59f35daf502f8df2f2e293cea2c54 /scripts/main.js
parentd4551bb4fa008351d901fe6c5453735a1d2edecf (diff)
downloadphp-xmpp-invitation-a120c8c6305c1eeffa57444baec7ff3e5575af9d.tar.gz
Move the JS into a separate file
Diffstat (limited to 'scripts/main.js')
-rw-r--r--scripts/main.js85
1 files changed, 85 insertions, 0 deletions
diff --git a/scripts/main.js b/scripts/main.js
new file mode 100644
index 0000000..c743e77
--- /dev/null
+++ b/scripts/main.js
@@ -0,0 +1,85 @@
+// load i18n and perform translation
+var i18n = new I18nText({path: 'lang'})
+i18n.once(I18nText.EVT_LOCALE_CHANGE, function (data) {
+	translate_ui();
+})
+i18n.setLocale('en');
+
+// i18n key prefix for MUC ("muc.") or 1:1 chat ("chat.")
+var key_prefix;
+var display_data = null;
+
+function show_clients(client_array) {
+	var list = document.getElementById('client_list');
+	for (var id = 0; id < client_array.length; id++) {
+		var item = document.createElement('li');
+		item.innerHTML = client_array[id];
+		list.appendChild(item);
+	}
+}
+
+function load_clients() {
+	url = "clients.json";
+	var request = new XMLHttpRequest()
+	request.open('GET', url)
+	request.onreadystatechange = function () {
+		if (request.readyState === 4) {
+			if (request.status == 200 || (isLocalFileRequest(url) && request.responseText.length > 0)) {
+				show_clients(JSON.parse(request.responseText))
+			}
+		}
+	}
+	request.send(null)
+}
+
+function load_hash() {
+	var muc = false;
+	key_prefix = "chat.";
+	var jid = window.location.search || window.location.hash;
+	jid = decodeURIComponent(jid.substring(jid.indexOf('#') + 1, jid.length));
+	try {
+		base_decoded = window.atob(jid);
+		if (base_decoded.search('@') >= 0)
+			jid = base_decoded;
+	} catch (err) {
+		// ignore error, JID wasn't base64 encoded
+	}
+	if (jid.search("\\?join") >= 0) {
+		muc = true;
+		key_prefix = "muc.";
+	}
+
+	// TODO: proper error checking / display / Creation of invitations
+	if (jid.search("@") <= 0) return {jid:"", name: "Somebody"};
+
+	var name = jid.split("@")[0];
+	name = name.charAt(0).toUpperCase() + name.slice(1);
+	return {jid: jid, name: name};
+}
+
+function translate_ui() {
+	// translation
+	document.title = i18n.text(key_prefix + 'title',  display_data);
+	// MUC/chat specific
+	['heading', 'intro', 'button'].forEach(function(id) {
+		document.getElementById(id).innerHTML = i18n.text(key_prefix + id, display_data);
+	});
+	// and agnostic
+	['clients', 'recommend', 'checkfulllist', 'xmppis'].forEach(function(id) {
+		document.getElementById(id).innerHTML = i18n.text(id, display_data);
+	});
+}
+
+function rehash() {
+	display_data = load_hash();
+	document.getElementById('button').href = "xmpp:" + display_data.jid;
+	document.getElementById('url_in').value = "xmpp:" + display_data.jid;
+	translate_ui();
+}
+
+function load_done() {
+	// functionality
+	load_clients();
+	rehash();
+	window.addEventListener("hashchange", rehash, false);
+}