textcircle.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. Meteor.subscribe("documents");
  8. Meteor.subscribe("editingUsers");
  9. Template.editor.helpers({
  10. // Return the id of the first document you can find.
  11. docid: function () {
  12. setupCurrentDocument();
  13. return Session.get("docid");
  14. },
  15. // Configure the CodeMirror editor.
  16. config: function () {
  17. return function (editor) {
  18. editor.setOption("lineNumbers", true);
  19. editor.setOption("theme", "cobalt");
  20. editor.setOption("mode", "html");
  21. // Set a callback that gets triggered whenever the user
  22. // makes a change in the code editing window.
  23. editor.on("change", function (cmEditor /* , info */) {
  24. let $preview = $("#viewer_iframe");
  25. // Send the current code over to the iframe for rendering.
  26. $preview.contents().find("html").html(cmEditor.getValue());
  27. Meteor.call("addEditingUser");
  28. });
  29. };
  30. }
  31. });
  32. Template.editingUsers.helpers({
  33. // Retrieve a set of users that are editing this document.
  34. users: function () {
  35. let doc = Documents.findOne();
  36. if (!doc) {
  37. // Give up.
  38. return null;
  39. }
  40. let eUsers = EditingUsers.findOne({ docId: doc._id });
  41. if (!eUsers) {
  42. // Give up.
  43. return null;
  44. }
  45. let users = new Array();
  46. let i = 0;
  47. for (let userId in eUsers.users) {
  48. users[i] = fixObjectsKeys(eUsers.users[userId]);
  49. users[i]._id = userId;
  50. i++;
  51. }
  52. return users;
  53. }
  54. });
  55. Template.navbar.helpers({
  56. documents: function () {
  57. return Documents.find();
  58. }
  59. });
  60. Template.docMeta.helpers({
  61. document: function () {
  62. return Documents.findOne({ _id: Session.get("docid") });
  63. },
  64. canEdit: function () {
  65. let doc = Documents.findOne({ _id: Session.get("docid") });
  66. return doc && doc.owner === Meteor.userId();
  67. },
  68. isPrivate: function () {
  69. let doc = Documents.findOne({ _id: Session.get("docid") });
  70. return !!(doc.isPrivate);
  71. }
  72. });
  73. Template.editableText.helpers({
  74. userCanEdit: function (doc, collection) {
  75. Meteor._debug("userCanEdit", doc, collection);
  76. // Can edit if the document is owned by me.
  77. doc = Documents.findOne({
  78. _id: Session.get("docid"),
  79. owner: Meteor.userId()
  80. });
  81. return !!doc;
  82. }
  83. });
  84. Template.navbar.events({
  85. "click .js-add-doc": function (event) {
  86. event.preventDefault();
  87. // User not available.
  88. if (!Meteor.user()) {
  89. alert("You need to login first.");
  90. }
  91. else {
  92. Meteor.call("addDoc", function (err, res) {
  93. if (!err) {
  94. console.log("addDoc res", res);
  95. Session.set("docid", res);
  96. }
  97. });
  98. }
  99. },
  100. "click .js-load-doc": function (event) {
  101. // This contains a complete document.
  102. Session.set("docid", this._id);
  103. }
  104. });
  105. Template.docMeta.events({
  106. "click .js-tog-private": function (event) {
  107. const docid = Session.get("docid");
  108. if (docid) {
  109. const doc = { _id: docid, isPrivate: event.target.checked };
  110. Meteor.call("updateDocPrivacy", doc);
  111. }
  112. }
  113. });
  114. } // end isClient...
  115. if (Meteor.isServer) {
  116. Meteor.publish("documents", function () {
  117. Meteor._debug("Publishing documents");
  118. return Documents.find({
  119. $or: [
  120. { isPrivate: false },
  121. { owner: this.userId }
  122. ]
  123. });
  124. });
  125. Meteor.publish("editingUsers", function () {
  126. Meteor._debug("Publishing all editingUsers");
  127. return EditingUsers.find();
  128. });
  129. Meteor.startup(function () {
  130. // Insert a document if there isn't one already.
  131. if (!Documents.findOne()) {
  132. Documents.insert({ title: "my new document" });
  133. }
  134. });
  135. }
  136. // Methods that provide write access to the data.
  137. Meteor.methods({
  138. addDoc: function () {
  139. Meteor._debug("addDoc, this", this);
  140. // Not logged-in.
  141. if (!this.userId) {
  142. return;
  143. }
  144. else {
  145. let doc = {
  146. owner: this.userId,
  147. createdOn: new Date(),
  148. title: "my new doc"
  149. };
  150. const docid = Documents.insert(doc);
  151. return docid;
  152. }
  153. },
  154. updateDocPrivacy: function (doc) {
  155. Meteor._debug("updatedocPrivacy", this.userId, doc)
  156. if (!this.userId) {
  157. return;
  158. }
  159. let res = Documents.update({ _id: doc._id, owner: this.userId }, { $set: { isPrivate: doc.isPrivate } });
  160. Meteor._debug("update", res);
  161. },
  162. addEditingUser: function () {
  163. let doc = Documents.findOne();
  164. if (!doc) {
  165. // No doc: give up.
  166. return;
  167. }
  168. if (!this.userId) {
  169. // No logged-in user: give up.
  170. return;
  171. }
  172. // Now I have a doc and possibly a user.
  173. let user = Meteor.user().profile;
  174. let eUsers = EditingUsers.findOne({ docId: doc._id });
  175. // No editing users have been stored yet.
  176. if (!eUsers) {
  177. eUsers = {
  178. docId: doc._id,
  179. users: {}
  180. };
  181. }
  182. user.lastEdit = new Date();
  183. eUsers.users[this.userId] = user;
  184. // Upsert: insert or update if filter matches.
  185. EditingUsers.upsert({ _id: eUsers._id }, eUsers);
  186. }
  187. });
  188. function setupCurrentDocument() {
  189. let doc;
  190. // No docid set yet.
  191. if (!Session.get("docid")) {
  192. doc = Documents.findOne();
  193. if (doc) {
  194. Session.set("docid", doc._id);
  195. }
  196. }
  197. }
  198. // This renames object keys by removing hyphens to make them compatible
  199. // with spacebars.
  200. function fixObjectsKeys(obj) {
  201. let newObj = {};
  202. for (let key in obj) {
  203. if (obj.hasOwnProperty(key)) {
  204. const key2 = key.replace("-", "");
  205. newObj[key2] = obj[key];
  206. }
  207. }
  208. return newObj;
  209. }