|  | @@ -1,6 +1,7 @@
 | 
	
		
			
				|  |  |  // This collection stores all the documents.
 | 
	
		
			
				|  |  |  // Note: not Documents, but this.Documents, because of the editing package (?)
 | 
	
		
			
				|  |  |  this.Documents = new Mongo.Collection("documents");
 | 
	
		
			
				|  |  | +// This collection stores sets of users that are editing documents.
 | 
	
		
			
				|  |  |  EditingUsers = new Mongo.Collection("editingUsers");
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  if (Meteor.isClient) {
 | 
	
	
		
			
				|  | @@ -15,16 +16,42 @@ if (Meteor.isClient) {
 | 
	
		
			
				|  |  |        return function (editor) {
 | 
	
		
			
				|  |  |          editor.setOption("lineNumbers", true);
 | 
	
		
			
				|  |  |          editor.setOption("mode", "html");
 | 
	
		
			
				|  |  | -        editor.on("change", function (cmEditor, info) {
 | 
	
		
			
				|  |  | +        // Set a callback that gets triggered whenever the user
 | 
	
		
			
				|  |  | +        // makes a change in the code editing window.
 | 
	
		
			
				|  |  | +        editor.on("change", function (cmEditor /* , info */) {
 | 
	
		
			
				|  |  |            let $preview = $("#viewer_iframe");
 | 
	
		
			
				|  |  | +          // Send the current code over to the iframe for rendering.
 | 
	
		
			
				|  |  |            $preview.contents().find("html").html(cmEditor.getValue());
 | 
	
		
			
				|  |  |            Meteor.call("addEditingUser");
 | 
	
		
			
				|  |  |          });
 | 
	
		
			
				|  |  |        };
 | 
	
		
			
				|  |  | -    },
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  |    });
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -}
 | 
	
		
			
				|  |  | +  Template.editingUsers.helpers({
 | 
	
		
			
				|  |  | +    // Retrieve a set of users that are editing this document.
 | 
	
		
			
				|  |  | +    users: function () {
 | 
	
		
			
				|  |  | +      let doc = Documents.findOne();
 | 
	
		
			
				|  |  | +      if (!doc) {
 | 
	
		
			
				|  |  | +        // Give up.
 | 
	
		
			
				|  |  | +        return null;
 | 
	
		
			
				|  |  | +      }
 | 
	
		
			
				|  |  | +      let eUsers = EditingUsers.findOne({ docId: doc._id });
 | 
	
		
			
				|  |  | +      if (!eUsers) {
 | 
	
		
			
				|  |  | +        // Give up.
 | 
	
		
			
				|  |  | +        return null;
 | 
	
		
			
				|  |  | +      }
 | 
	
		
			
				|  |  | +      let users = new Array();
 | 
	
		
			
				|  |  | +      let i = 0;
 | 
	
		
			
				|  |  | +      for (let userId in eUsers.users) {
 | 
	
		
			
				|  |  | +        users[i] = fixObjectsKeys(eUsers.users[userId]);
 | 
	
		
			
				|  |  | +        users[i]._id = userId;
 | 
	
		
			
				|  |  | +        i++;
 | 
	
		
			
				|  |  | +      }
 | 
	
		
			
				|  |  | +      return users;
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +  });
 | 
	
		
			
				|  |  | +} // end isClient...
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  if (Meteor.isServer) {
 | 
	
		
			
				|  |  |    Meteor.startup(function () {
 | 
	
	
		
			
				|  | @@ -35,10 +62,10 @@ if (Meteor.isServer) {
 | 
	
		
			
				|  |  |    });
 | 
	
		
			
				|  |  |  }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +// Methods that provide write access to the data.
 | 
	
		
			
				|  |  |  Meteor.methods({
 | 
	
		
			
				|  |  |    addEditingUser: function () {
 | 
	
		
			
				|  |  | -    var doc, user, eUsers;
 | 
	
		
			
				|  |  | -    doc = Documents.findOne();
 | 
	
		
			
				|  |  | +    let doc = Documents.findOne();
 | 
	
		
			
				|  |  |      if (!doc) {
 | 
	
		
			
				|  |  |        // No doc: give up.
 | 
	
		
			
				|  |  |        return;
 | 
	
	
		
			
				|  | @@ -48,8 +75,8 @@ Meteor.methods({
 | 
	
		
			
				|  |  |        return;
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |      // Now I have a doc and possibly a user.
 | 
	
		
			
				|  |  | -    user = Meteor.user().profile;
 | 
	
		
			
				|  |  | -    eUsers = EditingUsers.findOne({ docId: doc._id });
 | 
	
		
			
				|  |  | +    let user = Meteor.user().profile;
 | 
	
		
			
				|  |  | +    let eUsers = EditingUsers.findOne({ docId: doc._id });
 | 
	
		
			
				|  |  |      // No editing users have been stored yet.
 | 
	
		
			
				|  |  |      if (!eUsers) {
 | 
	
		
			
				|  |  |        eUsers = {
 | 
	
	
		
			
				|  | @@ -59,6 +86,20 @@ Meteor.methods({
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |      user.lastEdit = new Date();
 | 
	
		
			
				|  |  |      eUsers.users[this.userId] = user;
 | 
	
		
			
				|  |  | +    // Upsert: insert or update if filter matches.
 | 
	
		
			
				|  |  |      EditingUsers.upsert({ _id: eUsers._id }, eUsers);
 | 
	
		
			
				|  |  |    }
 | 
	
		
			
				|  |  |  });
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +// This renames object keys by removing hyphens to make them compatible
 | 
	
		
			
				|  |  | +// with spacebars.
 | 
	
		
			
				|  |  | +function fixObjectsKeys(obj) {
 | 
	
		
			
				|  |  | +  let newObj = {};
 | 
	
		
			
				|  |  | +  for (let key in obj) {
 | 
	
		
			
				|  |  | +    if (obj.hasOwnProperty(key)) {
 | 
	
		
			
				|  |  | +      const key2 = key.replace("-", "");
 | 
	
		
			
				|  |  | +      newObj[key2] = obj[key];
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +  }
 | 
	
		
			
				|  |  | +  return newObj;
 | 
	
		
			
				|  |  | +}
 |