diff options
author | Mark Haines <mark.haines@matrix.org> | 2015-01-15 16:51:52 +0000 |
---|---|---|
committer | Mark Haines <mark.haines@matrix.org> | 2015-01-22 11:15:03 +0000 |
commit | 8907e143c1a3378c7dced4048b165e38bb2b17fa (patch) | |
tree | bf1a2b2cea306c6b94513be44b35bac88b93fed4 /jsfiddles/event_stream | |
parent | Move scripts into scripts (diff) | |
download | synapse-8907e143c1a3378c7dced4048b165e38bb2b17fa.tar.xz |
Remove jsfiddles
Diffstat (limited to 'jsfiddles/event_stream')
-rw-r--r-- | jsfiddles/event_stream/demo.css | 17 | ||||
-rw-r--r-- | jsfiddles/event_stream/demo.html | 23 | ||||
-rw-r--r-- | jsfiddles/event_stream/demo.js | 145 |
3 files changed, 0 insertions, 185 deletions
diff --git a/jsfiddles/event_stream/demo.css b/jsfiddles/event_stream/demo.css deleted file mode 100644 index 48a55f372d..0000000000 --- a/jsfiddles/event_stream/demo.css +++ /dev/null @@ -1,17 +0,0 @@ -.loggedin { - visibility: hidden; -} - -p { - font-family: monospace; -} - -table -{ - border-spacing:5px; -} - -th,td -{ - padding:5px; -} diff --git a/jsfiddles/event_stream/demo.html b/jsfiddles/event_stream/demo.html deleted file mode 100644 index 7657780d28..0000000000 --- a/jsfiddles/event_stream/demo.html +++ /dev/null @@ -1,23 +0,0 @@ -<div> - <p>This event stream demo requires a home server to be running on http://localhost:8008</p> -</div> -<form class="loginForm"> - <input type="text" id="userLogin" placeholder="Username"></input> - <input type="password" id="passwordLogin" placeholder="Password"></input> - <input type="button" class="login" value="Login"></input> -</form> -<div class="loggedin"> - <form class="sendMessageForm"> - <input type="button" class="sendMessage" value="Send random message"></input> - </form> - <p id="streamErrorText"></p> - <table id="rooms"> - <tbody> - <tr> - <th>Room ID</th> - <th>Latest message</th> - </tr> - </tbody> - </table> -</div> - diff --git a/jsfiddles/event_stream/demo.js b/jsfiddles/event_stream/demo.js deleted file mode 100644 index acba8391fa..0000000000 --- a/jsfiddles/event_stream/demo.js +++ /dev/null @@ -1,145 +0,0 @@ -var accountInfo = {}; - -var eventStreamInfo = { - from: "END" -}; - -var roomInfo = []; - -var longpollEventStream = function() { - var url = "http://localhost:8008/_matrix/client/api/v1/events?access_token=$token&from=$from"; - url = url.replace("$token", accountInfo.access_token); - url = url.replace("$from", eventStreamInfo.from); - - $.getJSON(url, function(data) { - eventStreamInfo.from = data.end; - - var hasNewLatestMessage = false; - for (var i=0; i<data.chunk.length; ++i) { - if (data.chunk[i].type === "m.room.message") { - for (var j=0; j<roomInfo.length; ++j) { - if (roomInfo[j].room_id === data.chunk[i].room_id) { - roomInfo[j].latest_message = data.chunk[i].content.body; - hasNewLatestMessage = true; - } - } - } - } - - if (hasNewLatestMessage) { - setRooms(roomInfo); - } - $("#streamErrorText").text(""); - longpollEventStream(); - }).fail(function(err) { - $("#streamErrorText").text("Event stream error: "+JSON.stringify($.parseJSON(err.responseText))); - setTimeout(longpollEventStream, 5000); - }); -}; - -var showLoggedIn = function(data) { - accountInfo = data; - longpollEventStream(); - getCurrentRoomList(); - $(".loggedin").css({visibility: "visible"}); -}; - -$('.login').live('click', function() { - var user = $("#userLogin").val(); - var password = $("#passwordLogin").val(); - $.ajax({ - url: "http://localhost:8008/_matrix/client/api/v1/login", - type: "POST", - contentType: "application/json; charset=utf-8", - data: JSON.stringify({ user: user, password: password, type: "m.login.password" }), - dataType: "json", - success: function(data) { - $("#rooms").find("tr:gt(0)").remove(); - showLoggedIn(data); - }, - error: function(err) { - var errMsg = "To try this, you need a home server running!"; - var errJson = $.parseJSON(err.responseText); - if (errJson) { - errMsg = JSON.stringify(errJson); - } - alert(errMsg); - } - }); -}); - -var getCurrentRoomList = function() { - $("#roomId").val(""); - var url = "http://localhost:8008/_matrix/client/api/v1/initialSync?access_token=" + accountInfo.access_token + "&limit=1"; - $.getJSON(url, function(data) { - var rooms = data.rooms; - for (var i=0; i<rooms.length; ++i) { - if ("messages" in rooms[i]) { - rooms[i].latest_message = rooms[i].messages.chunk[0].content.body; - } - } - roomInfo = rooms; - setRooms(roomInfo); - }).fail(function(err) { - alert(JSON.stringify($.parseJSON(err.responseText))); - }); -}; - -$('.sendMessage').live('click', function() { - if (roomInfo.length === 0) { - alert("There is no room to send a message to!"); - return; - } - - var index = Math.floor(Math.random() * roomInfo.length); - - sendMessage(roomInfo[index].room_id); -}); - -var sendMessage = function(roomId) { - var body = "jsfiddle message @" + $.now(); - - if (roomId.length === 0) { - return; - } - - var url = "http://localhost:8008/_matrix/client/api/v1/rooms/$roomid/send/m.room.message?access_token=$token"; - url = url.replace("$token", accountInfo.access_token); - url = url.replace("$roomid", encodeURIComponent(roomId)); - - var data = { - msgtype: "m.text", - body: body - }; - - $.ajax({ - url: url, - type: "POST", - contentType: "application/json; charset=utf-8", - data: JSON.stringify(data), - dataType: "json", - success: function(data) { - $("#messageBody").val(""); - }, - error: function(err) { - alert(JSON.stringify($.parseJSON(err.responseText))); - } - }); -}; - -var setRooms = function(roomList) { - // wipe existing entries - $("#rooms").find("tr:gt(0)").remove(); - - var rows = ""; - for (var i=0; i<roomList.length; ++i) { - row = "<tr>" + - "<td>"+roomList[i].room_id+"</td>" + - "<td>"+roomList[i].latest_message+"</td>" + - "</tr>"; - rows += row; - } - - $("#rooms").append(rows); -}; - |