main.js 4.6 KB

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