main.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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.docMeta.helpers({
  80. // Find current document.
  81. document: function () {
  82. return Documents.findOne({ _id: Session.get("docid") });
  83. },
  84. // Test if a user is allowed to edit current doc.
  85. canEdit: function () {
  86. let doc = Documents.findOne({ _id: Session.get("docid") });
  87. return doc && doc.owner === Meteor.userId();
  88. },
  89. isPrivate: function () {
  90. let doc = Documents.findOne({ _id: Session.get("docid") });
  91. return !!(doc.isPrivate);
  92. }
  93. });
  94. Template.editableText.helpers({
  95. // Test if a user is allowed to edit current doc.
  96. userCanEdit: function (doc, collection) {
  97. Meteor._debug("userCanEdit", doc, collection);
  98. // Can edit if the document is owned by me.
  99. doc = Documents.findOne({
  100. _id: Session.get("docid"),
  101. owner: Meteor.userId()
  102. });
  103. return !!doc;
  104. }
  105. });
  106. Template.navbar.events({
  107. // Add doc button.
  108. "click .js-add-doc": function (event) {
  109. event.preventDefault();
  110. // User not available.
  111. if (!Meteor.user()) {
  112. alert("You need to login first!");
  113. }
  114. else {
  115. // They are logged in... lets insert a doc.
  116. Meteor.call("addDoc", function (err, res) {
  117. if (!err) {
  118. console.log("addDoc res", res);
  119. Session.set("docid", res);
  120. }
  121. });
  122. }
  123. },
  124. // Load doc button.
  125. "click .js-load-doc": function (event) {
  126. // This contains a complete document.
  127. Session.set("docid", this._id);
  128. }
  129. });
  130. Template.docMeta.events({
  131. // Change document privacy.
  132. "click .js-tog-private": function (event) {
  133. // They are logged in... lets insert a doc.
  134. const docid = Session.get("docid");
  135. if (docid) {
  136. const doc = { _id: docid, isPrivate: event.target.checked };
  137. Meteor.call("updateDocPrivacy", doc);
  138. }
  139. }
  140. });
  141. // Helper to make sure a doc is available.
  142. function setupCurrentDocument() {
  143. let doc;
  144. // No docid set yet.
  145. if (!Session.get("docid")) {
  146. doc = Documents.findOne();
  147. if (doc) {
  148. Session.set("docid", doc._id);
  149. }
  150. }
  151. }
  152. // This renames object keys by removing hyphens to make them compatible
  153. // with spacebars.
  154. function fixObjectsKeys(obj) {
  155. let newObj = {};
  156. for (let key in obj) {
  157. if (obj.hasOwnProperty(key)) {
  158. const key2 = key.replace("-", "");
  159. newObj[key2] = obj[key];
  160. }
  161. }
  162. return newObj;
  163. }