diff --git a/ajax.js b/ajax.js
index a927b43..8f83977 100644
--- a/ajax.js
+++ b/ajax.js
@@ -14,15 +14,6 @@ class InvalidJsonError extends AjaxError {}
const MIME_JSON = "application/json";
-/**
- * Wrap a function so that it receives `this` as first argument.
- */
-function thisToArg(f) {
- return function(...rest) {
- return f(this, ...rest);
- }
-}
-
function xhrInitForPromise(resolve, reject, url, method, headers) {
const t_resolve = thisToArg(resolve),
t_reject = thisToArg(reject);
diff --git a/controller/controller.js b/controller/controller.js
new file mode 100644
index 0000000..c41977e
--- /dev/null
+++ b/controller/controller.js
@@ -0,0 +1,142 @@
+"use strict";
+
+function createClient(endpoint) {
+ const api = new mm_client.MattermostApi(normalizedEndpoint(endpoint));
+ return new mm_client.MattermostClient(api);
+}
+
+function buttonDisable(element, text) {
+ if (!element.dataset.originalText) {
+ element.dataset.originalText = element.innerText;
+ }
+ element.innerText = text;
+ element.disabled = true;
+}
+function buttonEnable(element) {
+ element.innerText = element.dataset.originalText;
+ element.disabled = false;
+}
+
+function logIn() {
+ const client = createClient(byId("login_server").value);
+
+ buttonDisable(byId("login_button"), "Logging in...");
+
+ client.logIn(byId("login_login_id").value, byId("login_password").value)
+ .then(json => {
+ buttonEnable(byId("login_button"));
+ byId("login_message").innerText = "";
+ byId("channel_list").innerText = `Logged in as ${json.username}`;
+ window.location = "#";
+ populateServerSelectionList();
+ populateChannelList();
+ })
+ .catch(error => {
+ console.error(error);
+ buttonEnable(byId("login_button"));
+ byId("login_message").innerText = `${error}`;
+ });
+}
+
+function logOut(endpoint, button) {
+ const client = createClient(endpoint);
+
+ buttonDisable(button, "Logging out...");
+
+ client.logOut()
+ .then(response => {
+ console.info("Succesfully logged out");
+ populateServerSelectionList();
+ populateChannelList();
+ })
+ .catch(error => {
+ console.error(error);
+ const span = document.createElement("span");
+ span.innerText = `Failed to log out: ${error.message}`;
+ button.parentElement.appendChild(span);
+ buttonEnable(button);
+ });
+}
+
+function channelNameElements(team, channel) {
+ let icon = "";
+ let teamName = team.name;
+ let channelName = channel.name;
+ let title = "";
+
+ switch (channel.type) {
+ case "O": // Public channel
+ title = `${channel.name} in team ${team.name} (public)`;
+ break;
+ case "P": // Private channel
+ icon = "🔒";
+ title = `${channel.name} in team ${team.name} (private)`;
+ break;
+ case "D": // Direct message
+ return undefined;
+ teamName = "";
+ channelName = `👤 ...`;
+ title = `Direct message`;
+ break;
+ case "G": // Group chat
+ teamName = "";
+ channelName = `👥 ${channel.display_name}`;
+ title = `Group chat with ${channel.display_name}`;
+ break;
+ default: // Unsupported
+ icon = channel.type;
+ title = `${channel.name} in team ${team.name} (type ${channel.type})`;
+ break;
+ }
+
+ let elements = [];
+
+ if (icon) {
+ elements.push(`${icon} `);
+ }
+
+ if (teamName) {
+ const teamElement = document.createElement("span");
+ teamElement.className = "team-name";
+ teamElement.innerText = teamName;
+ elements.push(teamElement);
+
+ const separatorElement = document.createElement("span");
+ separatorElement.className = "separator";
+ separatorElement.innerText = "/";
+ elements.push(separatorElement);
+ }
+
+ const channelElement = document.createElement("span");
+ channelElement.className = "channel-name";
+ channelElement.innerText = channelName;
+ elements.push(channelElement);
+
+ return [title, elements];
+}
+
+function switchToChannel(client, team, channel) {
+ for (let el of byId("channel_list").childNodes) {
+ if (el.dataset["id"] == channel.id) {
+ el.className = "active";
+ } else {
+ el.className = "";
+ }
+ }
+
+ byId("channel_contents").innerText = "Loading…";
+ client.channelPosts(channel.id)
+ .then(response => {
+ console.info(`Got channel contents of ${channel.id} (${channel.name})`);
+ populateChannelContents(response);
+ })
+ .catch(error => {
+ console.error(error);
+ byId("channel_contents").innerText = `Failed to get channel contents:\n${error.message}`;
+ });
+
+ const [title, elements] = channelNameElements(team, channel);
+ byId("channel_header").innerHTML = "";
+ byId("channel_header").append(...elements);
+ byId("compose").setAttribute("placeholder", `Write to ${byId("channel_header").textContent}`);
+}
diff --git a/index.html b/index.html
index 68c27f6..035ee19 100644
--- a/index.html
+++ b/index.html
@@ -63,6 +63,11 @@
+
+
+
+
+