From e9d8c3b53a3d43096f4a8334de20e47b682111d3 Mon Sep 17 00:00:00 2001 From: John McLear Date: Tue, 31 Mar 2015 20:26:55 +0100 Subject: [PATCH] expose method for getting a full user list on the client including historical data --- src/static/js/ace2_inner.js | 59 +++++++++++++++++++++++++++++------ src/static/js/pad.js | 4 +++ src/static/js/pad_userlist.js | 24 ++++++++++++++ 3 files changed, 77 insertions(+), 10 deletions(-) diff --git a/src/static/js/ace2_inner.js b/src/static/js/ace2_inner.js index 919fa4c78..3c6c7cb86 100644 --- a/src/static/js/ace2_inner.js +++ b/src/static/js/ace2_inner.js @@ -3731,32 +3731,71 @@ function Ace2Inner(){ var apool = rep.apool; // TODO: support selection ranges - // TODO: Support multiple authors // TODO: Still work when authorship colors have been cleared // TODO: i18n // TODO: There appears to be a race condition or so. var author = null; if (alineAttrs) { + var authors = []; + var authorNames = []; var opIter = Changeset.opIterator(alineAttrs); - if (opIter.hasNext()) { + + while (opIter.hasNext()){ var op = opIter.next(); authorId = Changeset.opAttributeValue(op, 'author', apool); + + // Only push unique authors and ones with values + if(authors.indexOf(authorId) === -1 && authorId !== ""){ + authors.push(authorId); + } + } + } - if(authorId.length === 0){ - author = "not available"; - }else{ - var authorObj = parent.parent.clientVars.collab_client_vars.historicalAuthorData[authorId]; - author = authorObj.name; - if(author === "") author = "not available"; + + // No author information is available IE on a new pad. + if(authors.length === 0){ + var authorString = "No author information is available"; } + else{ + // Known authors info, both current and historical + var padAuthors = parent.parent.pad.userList(); + var authorObj = {}; + authors.forEach(function(authorId){ + padAuthors.forEach(function(padAuthor){ + // If the person doing the lookup is the author.. + if(padAuthor.userId === authorId){ + if(parent.parent.clientVars.userId === authorId){ + authorObj = { + name: "Me" + } + }else{ + authorObj = padAuthor; + } + } + }); + if(!authorObj){ + author = "Unknown"; + return; + } + author = authorObj.name; + if(!author) author = "Unknown"; + authorNames.push(author); + }) + } + if(authors.length === 1){ + var authorString = "The author of this line is " + authorNames; + } + if(authors.length > 1){ + var authorString = "The authors of this line are " + authorNames.join(" & "); + } parent.parent.$.gritter.add({ // (string | mandatory) the heading of the notification - title: 'Authors', + title: 'Line Authors', // (string | mandatory) the text inside the notification - text: 'The author of this line is ' + author, + text: authorString, // (bool | optional) if you want it to fade out on its own or just sit there sticky: false, // (int | optional) the time you want it to be alive for before fading out diff --git a/src/static/js/pad.js b/src/static/js/pad.js index 0d896a607..5ac9d03d4 100644 --- a/src/static/js/pad.js +++ b/src/static/js/pad.js @@ -433,6 +433,10 @@ var pad = { { return pad.myUserInfo.name; }, + userList: function() + { + return paduserlist.users(); + }, sendClientReady: function(isReconnect, messageType) { messageType = typeof messageType !== 'undefined' ? messageType : 'CLIENT_READY'; diff --git a/src/static/js/pad_userlist.js b/src/static/js/pad_userlist.js index d306256a2..22dab40a9 100644 --- a/src/static/js/pad_userlist.js +++ b/src/static/js/pad_userlist.js @@ -508,6 +508,30 @@ var paduserlist = (function() }); // }, + users: function(){ + // Returns an object of users who have been on this pad + // Firstly we have to get live data.. + var userList = otherUsersInfo; + // Now we need to add ourselves.. + userList.push(myUserInfo); + // Now we add historical authors + var historical = clientVars.collab_client_vars.historicalAuthorData; + for (var key in historical){ + var userId = historical[key].userId; + // Check we don't already have this author in our array + var exists = false; + + userList.forEach(function(user){ + if(user.userId === userId) exists = true; + }); + + if(exists === false){ + userList.push(historical[key]); + } + + } + return userList; + }, setMyUserInfo: function(info) { //translate the colorId