|
@@ -1,6 +1,7 @@
|
|
// This collection stores all the documents.
|
|
// This collection stores all the documents.
|
|
// Note: not Documents, but this.Documents, because of the editing package (?)
|
|
// Note: not Documents, but this.Documents, because of the editing package (?)
|
|
this.Documents = new Mongo.Collection("documents");
|
|
this.Documents = new Mongo.Collection("documents");
|
|
|
|
+// This collection stores sets of users that are editing documents.
|
|
EditingUsers = new Mongo.Collection("editingUsers");
|
|
EditingUsers = new Mongo.Collection("editingUsers");
|
|
|
|
|
|
if (Meteor.isClient) {
|
|
if (Meteor.isClient) {
|
|
@@ -15,16 +16,42 @@ if (Meteor.isClient) {
|
|
return function (editor) {
|
|
return function (editor) {
|
|
editor.setOption("lineNumbers", true);
|
|
editor.setOption("lineNumbers", true);
|
|
editor.setOption("mode", "html");
|
|
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");
|
|
let $preview = $("#viewer_iframe");
|
|
|
|
+ // Send the current code over to the iframe for rendering.
|
|
$preview.contents().find("html").html(cmEditor.getValue());
|
|
$preview.contents().find("html").html(cmEditor.getValue());
|
|
Meteor.call("addEditingUser");
|
|
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) {
|
|
if (Meteor.isServer) {
|
|
Meteor.startup(function () {
|
|
Meteor.startup(function () {
|
|
@@ -35,10 +62,10 @@ if (Meteor.isServer) {
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+// Methods that provide write access to the data.
|
|
Meteor.methods({
|
|
Meteor.methods({
|
|
addEditingUser: function () {
|
|
addEditingUser: function () {
|
|
- var doc, user, eUsers;
|
|
|
|
- doc = Documents.findOne();
|
|
|
|
|
|
+ let doc = Documents.findOne();
|
|
if (!doc) {
|
|
if (!doc) {
|
|
// No doc: give up.
|
|
// No doc: give up.
|
|
return;
|
|
return;
|
|
@@ -48,8 +75,8 @@ Meteor.methods({
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
// Now I have a doc and possibly a user.
|
|
// 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.
|
|
// No editing users have been stored yet.
|
|
if (!eUsers) {
|
|
if (!eUsers) {
|
|
eUsers = {
|
|
eUsers = {
|
|
@@ -59,6 +86,20 @@ Meteor.methods({
|
|
}
|
|
}
|
|
user.lastEdit = new Date();
|
|
user.lastEdit = new Date();
|
|
eUsers.users[this.userId] = user;
|
|
eUsers.users[this.userId] = user;
|
|
|
|
+ // Upsert: insert or update if filter matches.
|
|
EditingUsers.upsert({ _id: eUsers._id }, eUsers);
|
|
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;
|
|
|
|
+}
|