textcircle.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // This collection stores all the documents.
  2. // Note: not Documents, but this.Documents, because of the editing package (?)
  3. this.Documents = new Mongo.Collection("documents");
  4. // This collection stores sets of users that are editing documents.
  5. EditingUsers = new Mongo.Collection("editingUsers");
  6. if (Meteor.isClient) {
  7. Template.editor.helpers({
  8. // Return the id of the first document you can find.
  9. docid: function () {
  10. const doc = Documents.findOne();
  11. return doc ? doc._id : undefined;
  12. },
  13. // Configure the CodeMirror editor.
  14. config: function () {
  15. return function (editor) {
  16. editor.setOption("lineNumbers", true);
  17. editor.setOption("theme", "cobalt");
  18. editor.setOption("mode", "html");
  19. // Set a callback that gets triggered whenever the user
  20. // makes a change in the code editing window.
  21. editor.on("change", function (cmEditor /* , info */) {
  22. let $preview = $("#viewer_iframe");
  23. // Send the current code over to the iframe for rendering.
  24. $preview.contents().find("html").html(cmEditor.getValue());
  25. Meteor.call("addEditingUser");
  26. });
  27. };
  28. }
  29. });
  30. Template.editingUsers.helpers({
  31. // Retrieve a set of users that are editing this document.
  32. users: function () {
  33. let doc = Documents.findOne();
  34. if (!doc) {
  35. // Give up.
  36. return null;
  37. }
  38. let eUsers = EditingUsers.findOne({ docId: doc._id });
  39. if (!eUsers) {
  40. // Give up.
  41. return null;
  42. }
  43. let users = new Array();
  44. let i = 0;
  45. for (let userId in eUsers.users) {
  46. users[i] = fixObjectsKeys(eUsers.users[userId]);
  47. users[i]._id = userId;
  48. i++;
  49. }
  50. return users;
  51. }
  52. });
  53. Template.navbar.events({
  54. "click .js-add-doc": function (event) {
  55. event.preventDefault();
  56. // User not available.
  57. if (!Meteor.user()) {
  58. alert("You need to login first.");
  59. }
  60. else {
  61. Meteor.call("addDoc");
  62. }
  63. }
  64. });
  65. } // end isClient...
  66. if (Meteor.isServer) {
  67. Meteor.startup(function () {
  68. // Insert a document if there isn't one already.
  69. if (!Documents.findOne()) {
  70. Documents.insert({ title: "my new document" });
  71. }
  72. });
  73. }
  74. // Methods that provide write access to the data.
  75. Meteor.methods({
  76. addDoc: function () {
  77. Meteor._debug("addDoc, this", this);
  78. // Not logged-in.
  79. if (!this.userId) {
  80. return;
  81. }
  82. else {
  83. let doc = {
  84. owner: this.userId,
  85. createdOn: new Date(),
  86. title: "my new doc"
  87. };
  88. Documents.insert(doc);
  89. }
  90. },
  91. addEditingUser: function () {
  92. let doc = Documents.findOne();
  93. if (!doc) {
  94. // No doc: give up.
  95. return;
  96. }
  97. if (!this.userId) {
  98. // No logged-in user: give up.
  99. return;
  100. }
  101. // Now I have a doc and possibly a user.
  102. let user = Meteor.user().profile;
  103. let eUsers = EditingUsers.findOne({ docId: doc._id });
  104. // No editing users have been stored yet.
  105. if (!eUsers) {
  106. eUsers = {
  107. docId: doc._id,
  108. users: {}
  109. };
  110. }
  111. user.lastEdit = new Date();
  112. eUsers.users[this.userId] = user;
  113. // Upsert: insert or update if filter matches.
  114. EditingUsers.upsert({ _id: eUsers._id }, eUsers);
  115. }
  116. });
  117. // This renames object keys by removing hyphens to make them compatible
  118. // with spacebars.
  119. function fixObjectsKeys(obj) {
  120. let newObj = {};
  121. for (let key in obj) {
  122. if (obj.hasOwnProperty(key)) {
  123. const key2 = key.replace("-", "");
  124. newObj[key2] = obj[key];
  125. }
  126. }
  127. return newObj;
  128. }