From 5048f4a915625ada6c98b9e574edaa9455e9442d Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Wed, 20 Aug 2014 16:45:59 +0100 Subject: Added final jsfiddle: an example app demonstrating most of the c2s api. --- jsfiddles/example_app/demo.css | 43 ++++++ jsfiddles/example_app/demo.html | 56 ++++++++ jsfiddles/example_app/demo.js | 303 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 402 insertions(+) create mode 100644 jsfiddles/example_app/demo.css create mode 100644 jsfiddles/example_app/demo.html create mode 100644 jsfiddles/example_app/demo.js (limited to 'jsfiddles/example_app') diff --git a/jsfiddles/example_app/demo.css b/jsfiddles/example_app/demo.css new file mode 100644 index 0000000000..4c1e157cc8 --- /dev/null +++ b/jsfiddles/example_app/demo.css @@ -0,0 +1,43 @@ +.roomListDashboard, .roomContents, .sendMessageForm { + visibility: hidden; +} + +.roomList { + background-color: #909090; +} + +.messageWrapper { + background-color: #EEEEEE; + height: 400px; + overflow: scroll; +} + +.membersWrapper { + background-color: #EEEEEE; + height: 200px; + width: 50%; + overflow: scroll; +} + +.textEntry { + width: 100% +} + +p { + font-family: monospace; +} + +table +{ + border-spacing:5px; +} + +th,td +{ + padding:5px; +} + +.roomList tr:not(:first-child):hover { + background-color: orange; + cursor: pointer; +} diff --git a/jsfiddles/example_app/demo.html b/jsfiddles/example_app/demo.html new file mode 100644 index 0000000000..0af946f6bc --- /dev/null +++ b/jsfiddles/example_app/demo.html @@ -0,0 +1,56 @@ +
+

Matrix example application: Requires a local home server running at http://localhost:8080

+
+

No account? Register:

+ + + +
+
+

Got an account? Login:

+ + + +
+
+ +
+
+ + +
+ + + + + + + + +
RoomMy stateLatest message
+
+ +
+

Select a room

+
+ + + +
+
+
+ + +
+
+ +
+

Member list:

+
+ + + +
+
+
+ diff --git a/jsfiddles/example_app/demo.js b/jsfiddles/example_app/demo.js new file mode 100644 index 0000000000..295597f0f7 --- /dev/null +++ b/jsfiddles/example_app/demo.js @@ -0,0 +1,303 @@ +var accountInfo = {}; + +var eventStreamInfo = { + from: "END" +}; + +var roomInfo = []; +var memberInfo = []; +var viewingRoomId; + +// ************** Event Streaming ************** +var longpollEventStream = function() { + var url = "http://localhost:8080/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; + var updatedMemberList = false; + var i=0; + var j=0; + for (i=0; i 0) { + data.room_alias_name = roomAlias; + } + $.ajax({ + url: "http://localhost:8080/matrix/client/api/v1/rooms?access_token="+accountInfo.access_token, + type: "POST", + contentType: "application/json; charset=utf-8", + data: JSON.stringify(data), + dataType: "json", + success: function(response) { + $("#roomAlias").val(""); + response.membership = "join"; // you are automatically joined into every room you make. + response.latest_message = ""; + + roomInfo.push(response); + setRooms(roomInfo); + }, + error: function(err) { + alert(JSON.stringify($.parseJSON(err.responseText))); + } + }); +}); + +// ************** Getting current state ************** +var getCurrentRoomList = function() { + var url = "http://localhost:8080/matrix/client/api/v1/im/sync?access_token=" + accountInfo.access_token + "&from=END&to=START&limit=1"; + $.getJSON(url, function(data) { + for (var i=0; i=0; --i) { + addMessage(data.chunk[i]); + } + }); +}; + +var getMemberList = function(roomId) { + $("#members").empty(); + memberInfo = []; + var url = "http://localhost:8080/matrix/client/api/v1/rooms/" + roomId + "/members/list?access_token=" + accountInfo.access_token; + $.getJSON(url, function(data) { + for (var i=0; i"+roomList[i].room_id+"" + + ""+roomList[i].membership+"" + + ""+roomList[i].latest_message+"" + + ""; + rows += row; + } + + $("#rooms").append(rows); + + $('#rooms').find("tr").click(function(){ + var roomId = $(this).find('td:eq(0)').text(); + var membership = $(this).find('td:eq(1)').text(); + if (membership !== "join") { + console.log("Joining room " + roomId); + var url = "http://localhost:8080/matrix/client/api/v1/rooms/$roomid/members/$user/state?access_token=$token"; + url = url.replace("$token", accountInfo.access_token); + url = url.replace("$roomid", encodeURIComponent(roomId)); + url = url.replace("$user", encodeURIComponent(accountInfo.user_id)); + $.ajax({ + url: url, + type: "PUT", + contentType: "application/json; charset=utf-8", + data: JSON.stringify({membership: "join"}), + dataType: "json", + success: function(data) { + loadRoomContent(roomId); + getCurrentRoomList(); + }, + error: function(err) { + alert(JSON.stringify($.parseJSON(err.responseText))); + } + }); + } + else { + loadRoomContent(roomId); + } + }); +}; + +var addMessage = function(data) { + var row = "" + + ""+data.user_id+"" + + ""+data.content.body+"" + + ""; + $("#messages").append(row); +}; + +var addMember = function(data) { + var row = "" + + ""+data.target_user_id+"" + + ""+data.content.membership+"" + + ""; + $("#members").append(row); +}; + -- cgit 1.5.1