textcircle.js 4.1 KB

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