main.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. 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", Session.get("docid"));
  26. });
  27. };
  28. }
  29. });
  30. Template.editingUsers.helpers({
  31. // Retrieve a list of users that are editing this document.
  32. users: function () {
  33. let doc = Documents.findOne({ _id: Session.get("docid") });
  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. // Retrieve a list of documents.
  55. documents: function () {
  56. return Documents.find();
  57. }
  58. });
  59. Template.docMeta.helpers({
  60. // Find current document.
  61. document: function () {
  62. return Documents.findOne({ _id: Session.get("docid") });
  63. },
  64. // Test if a user is allowed to edit current doc.
  65. canEdit: function () {
  66. let doc = Documents.findOne({ _id: Session.get("docid") });
  67. return doc && doc.owner === Meteor.userId();
  68. },
  69. isPrivate: function () {
  70. let doc = Documents.findOne({ _id: Session.get("docid") });
  71. return !!(doc.isPrivate);
  72. }
  73. });
  74. Template.editableText.helpers({
  75. // Test if a user is allowed to edit current doc.
  76. userCanEdit: function (doc, collection) {
  77. Meteor._debug("userCanEdit", doc, collection);
  78. // Can edit if the document is owned by me.
  79. doc = Documents.findOne({
  80. _id: Session.get("docid"),
  81. owner: Meteor.userId()
  82. });
  83. return !!doc;
  84. }
  85. });
  86. Template.navbar.events({
  87. // Add doc button.
  88. "click .js-add-doc": function (event) {
  89. event.preventDefault();
  90. // User not available.
  91. if (!Meteor.user()) {
  92. alert("You need to login first!");
  93. }
  94. else {
  95. // They are logged in... lets insert a doc.
  96. Meteor.call("addDoc", function (err, res) {
  97. if (!err) {
  98. console.log("addDoc res", res);
  99. Session.set("docid", res);
  100. }
  101. });
  102. }
  103. },
  104. // Load doc button.
  105. "click .js-load-doc": function (event) {
  106. // This contains a complete document.
  107. Session.set("docid", this._id);
  108. }
  109. });
  110. Template.docMeta.events({
  111. // Change document privacy.
  112. "click .js-tog-private": function (event) {
  113. // They are logged in... lets insert a doc.
  114. const docid = Session.get("docid");
  115. if (docid) {
  116. const doc = { _id: docid, isPrivate: event.target.checked };
  117. Meteor.call("updateDocPrivacy", doc);
  118. }
  119. }
  120. });
  121. // Helper to make sure a doc is available.
  122. function setupCurrentDocument() {
  123. let doc;
  124. // No docid set yet.
  125. if (!Session.get("docid")) {
  126. doc = Documents.findOne();
  127. if (doc) {
  128. Session.set("docid", doc._id);
  129. }
  130. }
  131. }
  132. // This renames object keys by removing hyphens to make them compatible
  133. // with spacebars.
  134. function fixObjectsKeys(obj) {
  135. let newObj = {};
  136. for (let key in obj) {
  137. if (obj.hasOwnProperty(key)) {
  138. const key2 = key.replace("-", "");
  139. newObj[key2] = obj[key];
  140. }
  141. }
  142. return newObj;
  143. }