main.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. // code that is only sent to the client
  2. // main.js is loaded last:
  3. // http://docs.meteor.com/#/full/structuringyourapp
  4. // subscribe to read data
  5. Meteor.subscribe("documents");
  6. Meteor.subscribe("editingUsers");
  7. Meteor.subscribe("comments");
  8. Router.configure({
  9. layoutTemplate: "ApplicationLayout"
  10. });
  11. Router.route("/", function () {
  12. Meteor._debug("Routing /");
  13. this.render("navbar", { to: "header" });
  14. this.render("docList", { to: "main" });
  15. });
  16. Router.route("/documents/:_id", function () {
  17. Meteor._debug("Routing /documents/" + this.params._id);
  18. Session.set("docid", this.params._id);
  19. this.render("navbar", { to: "header" });
  20. this.render("docItem", { to: "main" });
  21. });
  22. Template.editor.helpers({
  23. // Return the id of the first document you can find.
  24. docid: function () {
  25. setupCurrentDocument();
  26. return Session.get("docid");
  27. },
  28. // Configure the CodeMirror editor.
  29. config: function () {
  30. return function (editor) {
  31. editor.setOption("lineNumbers", true);
  32. editor.setOption("theme", "cobalt");
  33. editor.setOption("mode", "html");
  34. // Set a callback that gets triggered whenever the user
  35. // makes a change in the code editing window.
  36. editor.on("change", function (cmEditor /* , info */) {
  37. let $preview = $("#viewer_iframe");
  38. // Send the current code over to the iframe for rendering.
  39. $preview.contents().find("html").html(cmEditor.getValue());
  40. Meteor.call("addEditingUser", Session.get("docid"));
  41. });
  42. };
  43. }
  44. });
  45. Template.editingUsers.helpers({
  46. // Retrieve a list of users that are editing this document.
  47. users: function () {
  48. let doc = Documents.findOne({ _id: Session.get("docid") });
  49. if (!doc) {
  50. // Give up.
  51. return null;
  52. }
  53. let eUsers = EditingUsers.findOne({ docId: doc._id });
  54. if (!eUsers) {
  55. // Give up.
  56. return null;
  57. }
  58. let users = new Array();
  59. let i = 0;
  60. for (let userId in eUsers.users) {
  61. users[i] = fixObjectsKeys(eUsers.users[userId]);
  62. users[i]._id = userId;
  63. i++;
  64. }
  65. return users;
  66. }
  67. });
  68. Template.navbar.helpers({
  69. // Retrieve a list of documents.
  70. documents: function () {
  71. return Documents.find();
  72. }
  73. });
  74. Template.docList.helpers({
  75. // Retrieve a list of documents.
  76. documents: function () {
  77. return Documents.find();
  78. }
  79. });
  80. Template.insertCommentForm.helpers({
  81. docid: function () {
  82. return Session.get("docid");
  83. }
  84. });
  85. Template.commentList.helpers({
  86. comments: function () {
  87. return Comments.find({ docid: Session.get("docid") });
  88. }
  89. });
  90. Template.docMeta.helpers({
  91. // Find current document.
  92. document: function () {
  93. return Documents.findOne({ _id: Session.get("docid") });
  94. },
  95. // Test if a user is allowed to edit current doc.
  96. canEdit: function () {
  97. let doc = Documents.findOne({ _id: Session.get("docid") });
  98. return doc && doc.owner === Meteor.userId();
  99. },
  100. isPrivate: function () {
  101. let doc = Documents.findOne({ _id: Session.get("docid") });
  102. return !!(doc.isPrivate);
  103. }
  104. });
  105. Template.editableText.helpers({
  106. // Test if a user is allowed to edit current doc.
  107. userCanEdit: function (doc, collection) {
  108. Meteor._debug("userCanEdit", doc, collection);
  109. // Can edit if the document is owned by me.
  110. doc = Documents.findOne({
  111. _id: Session.get("docid"),
  112. owner: Meteor.userId()
  113. });
  114. return !!doc;
  115. }
  116. });
  117. Template.navbar.events({
  118. // Add doc button.
  119. "click .js-add-doc": function (event) {
  120. event.preventDefault();
  121. // User not available.
  122. if (!Meteor.user()) {
  123. alert("You need to login first!");
  124. }
  125. else {
  126. // They are logged in... lets insert a doc.
  127. Meteor.call("addDoc", function (err, res) {
  128. if (!err) {
  129. console.log("addDoc res", res);
  130. Session.set("docid", res);
  131. }
  132. });
  133. }
  134. },
  135. // Load doc button.
  136. "click .js-load-doc": function (event) {
  137. // This contains a complete document.
  138. Session.set("docid", this._id);
  139. }
  140. });
  141. Template.docMeta.events({
  142. // Change document privacy.
  143. "click .js-tog-private": function (event) {
  144. // They are logged in... lets insert a doc.
  145. const docid = Session.get("docid");
  146. if (docid) {
  147. const doc = { _id: docid, isPrivate: event.target.checked };
  148. Meteor.call("updateDocPrivacy", doc);
  149. }
  150. }
  151. });
  152. // Helper to make sure a doc is available.
  153. function setupCurrentDocument() {
  154. let doc;
  155. // No docid set yet.
  156. if (!Session.get("docid")) {
  157. doc = Documents.findOne();
  158. if (doc) {
  159. Session.set("docid", doc._id);
  160. }
  161. }
  162. }
  163. // This renames object keys by removing hyphens to make them compatible
  164. // with spacebars.
  165. function fixObjectsKeys(obj) {
  166. let newObj = {};
  167. for (let key in obj) {
  168. if (obj.hasOwnProperty(key)) {
  169. const key2 = key.replace("-", "");
  170. newObj[key2] = obj[key];
  171. }
  172. }
  173. return newObj;
  174. }