textcircle.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. setupCurrentDocument();
  11. return Session.get("docid");
  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", function (err, res) {
  62. if (!err) {
  63. console.log("addDoc res", res);
  64. Session.set("docid", res);
  65. }
  66. });
  67. }
  68. }
  69. });
  70. } // end isClient...
  71. if (Meteor.isServer) {
  72. Meteor.startup(function () {
  73. // Insert a document if there isn't one already.
  74. if (!Documents.findOne()) {
  75. Documents.insert({ title: "my new document" });
  76. }
  77. });
  78. }
  79. // Methods that provide write access to the data.
  80. Meteor.methods({
  81. addDoc: function () {
  82. Meteor._debug("addDoc, this", this);
  83. // Not logged-in.
  84. if (!this.userId) {
  85. return;
  86. }
  87. else {
  88. let doc = {
  89. owner: this.userId,
  90. createdOn: new Date(),
  91. title: "my new doc"
  92. };
  93. const docid = Documents.insert(doc);
  94. return docid;
  95. }
  96. },
  97. addEditingUser: function () {
  98. let doc = Documents.findOne();
  99. if (!doc) {
  100. // No doc: give up.
  101. return;
  102. }
  103. if (!this.userId) {
  104. // No logged-in user: give up.
  105. return;
  106. }
  107. // Now I have a doc and possibly a user.
  108. let user = Meteor.user().profile;
  109. let eUsers = EditingUsers.findOne({ docId: doc._id });
  110. // No editing users have been stored yet.
  111. if (!eUsers) {
  112. eUsers = {
  113. docId: doc._id,
  114. users: {}
  115. };
  116. }
  117. user.lastEdit = new Date();
  118. eUsers.users[this.userId] = user;
  119. // Upsert: insert or update if filter matches.
  120. EditingUsers.upsert({ _id: eUsers._id }, eUsers);
  121. }
  122. });
  123. function setupCurrentDocument() {
  124. let doc;
  125. // No docid set yet.
  126. if (!Session.get("docid")) {
  127. doc = Documents.findOne();
  128. if (doc) {
  129. Session.set("docid", doc._id);
  130. }
  131. }
  132. }
  133. // This renames object keys by removing hyphens to make them compatible
  134. // with spacebars.
  135. function fixObjectsKeys(obj) {
  136. let newObj = {};
  137. for (let key in obj) {
  138. if (obj.hasOwnProperty(key)) {
  139. const key2 = key.replace("-", "");
  140. newObj[key2] = obj[key];
  141. }
  142. }
  143. return newObj;
  144. }