// code that is only sent to the client // main.js is loaded last: // http://docs.meteor.com/#/full/structuringyourapp // subscribe to read data Meteor.subscribe("documents"); Meteor.subscribe("editingUsers"); Router.configure({ layoutTemplate: "ApplicationLayout" }); Router.route("/", function () { Meteor._debug("Routing /"); this.render("navbar", { to: "header" }); this.render("docList", { to: "main" }); }); Router.route("/documents/:_id", function () { Meteor._debug("Routing /documents/" + this.params._id); Session.set("docid", this.params._id); this.render("navbar", { to: "header" }); this.render("docItem", { to: "main" }); }); Template.editor.helpers({ // Return the id of the first document you can find. docid: function () { setupCurrentDocument(); return Session.get("docid"); }, // Configure the CodeMirror editor. config: function () { return function (editor) { editor.setOption("lineNumbers", true); editor.setOption("theme", "cobalt"); editor.setOption("mode", "html"); // Set a callback that gets triggered whenever the user // makes a change in the code editing window. editor.on("change", function (cmEditor /* , info */) { let $preview = $("#viewer_iframe"); // Send the current code over to the iframe for rendering. $preview.contents().find("html").html(cmEditor.getValue()); Meteor.call("addEditingUser", Session.get("docid")); }); }; } }); Template.editingUsers.helpers({ // Retrieve a list of users that are editing this document. users: function () { let doc = Documents.findOne({ _id: Session.get("docid") }); if (!doc) { // Give up. return null; } let eUsers = EditingUsers.findOne({ docId: doc._id }); if (!eUsers) { // Give up. return null; } let users = new Array(); let i = 0; for (let userId in eUsers.users) { users[i] = fixObjectsKeys(eUsers.users[userId]); users[i]._id = userId; i++; } return users; } }); Template.navbar.helpers({ // Retrieve a list of documents. documents: function () { return Documents.find(); } }); Template.docList.helpers({ // Retrieve a list of documents. documents: function () { return Documents.find(); } }); Template.docMeta.helpers({ // Find current document. document: function () { return Documents.findOne({ _id: Session.get("docid") }); }, // Test if a user is allowed to edit current doc. canEdit: function () { let doc = Documents.findOne({ _id: Session.get("docid") }); return doc && doc.owner === Meteor.userId(); }, isPrivate: function () { let doc = Documents.findOne({ _id: Session.get("docid") }); return !!(doc.isPrivate); } }); Template.editableText.helpers({ // Test if a user is allowed to edit current doc. userCanEdit: function (doc, collection) { Meteor._debug("userCanEdit", doc, collection); // Can edit if the document is owned by me. doc = Documents.findOne({ _id: Session.get("docid"), owner: Meteor.userId() }); return !!doc; } }); Template.navbar.events({ // Add doc button. "click .js-add-doc": function (event) { event.preventDefault(); // User not available. if (!Meteor.user()) { alert("You need to login first!"); } else { // They are logged in... lets insert a doc. Meteor.call("addDoc", function (err, res) { if (!err) { console.log("addDoc res", res); Session.set("docid", res); } }); } }, // Load doc button. "click .js-load-doc": function (event) { // This contains a complete document. Session.set("docid", this._id); } }); Template.docMeta.events({ // Change document privacy. "click .js-tog-private": function (event) { // They are logged in... lets insert a doc. const docid = Session.get("docid"); if (docid) { const doc = { _id: docid, isPrivate: event.target.checked }; Meteor.call("updateDocPrivacy", doc); } } }); // Helper to make sure a doc is available. function setupCurrentDocument() { let doc; // No docid set yet. if (!Session.get("docid")) { doc = Documents.findOne(); if (doc) { Session.set("docid", doc._id); } } } // This renames object keys by removing hyphens to make them compatible // with spacebars. function fixObjectsKeys(obj) { let newObj = {}; for (let key in obj) { if (obj.hasOwnProperty(key)) { const key2 = key.replace("-", ""); newObj[key2] = obj[key]; } } return newObj; }